less cloning

This commit is contained in:
Abdulmujeeb Raji 2025-02-21 08:27:42 +00:00
parent 82a45eab08
commit 996cb404cd
Signed by: midnadimple
GPG key ID: EB02C582F8C3962B
2 changed files with 34 additions and 32 deletions

View file

@ -49,8 +49,8 @@ impl Mode {
}
match self {
Mode::MainMenu(ref menu_list) => menu_list.handle_key(self.clone(), key),
_ => return self,
Mode::MainMenu(menu_list) => menu_list.handle_key(key),
_ => Mode::Exit,
}
}
}
@ -86,8 +86,8 @@ impl Widget for &mut Mode {
match self {
Mode::MainMenu(menu_list) => {
menu_list.render(list_outer_area, buf);
menu::render_profile(profile_area, buf);
menu_list.render(list_outer_area, buf);
}
_ => {},
}

View file

@ -14,6 +14,21 @@ use ratatui::prelude::*;
use crate::game;
const GAME_HEADER_STYLE: Style = Style::new()
.fg(ZINC.c100)
.bg(BLUE.c600)
.add_modifier(Modifier::BOLD);
const PROFILE_HEADER_STYLE: Style = Style::new()
.fg(ZINC.c100)
.bg(VIOLET.c600)
.add_modifier(Modifier::BOLD);
const HEADER_STYLE: Style = Style::new().fg(ROSE.c500).add_modifier(Modifier::BOLD);
const SELECTED_STYLE: Style = Style::new().bg(ZINC.c700).add_modifier(Modifier::BOLD);
const FOOTER_LEFT_STYLE: Style = Style::new().fg(PURPLE.c400).add_modifier(Modifier::BOLD);
const FOOTER_RIGHT_STYLE: Style = Style::new().fg(LIME.c400);
pub fn render_header(area: Rect, buf: &mut Buffer) {
Block::new()
.title(Line::raw(" txtris ").centered().style(HEADER_STYLE))
@ -72,21 +87,6 @@ pub struct OptionList {
state: ListState,
}
const GAME_HEADER_STYLE: Style = Style::new()
.fg(ZINC.c100)
.bg(BLUE.c600)
.add_modifier(Modifier::BOLD);
const PROFILE_HEADER_STYLE: Style = Style::new()
.fg(ZINC.c100)
.bg(VIOLET.c600)
.add_modifier(Modifier::BOLD);
const HEADER_STYLE: Style = Style::new().fg(ROSE.c500).add_modifier(Modifier::BOLD);
const SELECTED_STYLE: Style = Style::new().bg(ZINC.c700).add_modifier(Modifier::BOLD);
const FOOTER_LEFT_STYLE: Style = Style::new().fg(PURPLE.c400).add_modifier(Modifier::BOLD);
const FOOTER_RIGHT_STYLE: Style = Style::new().fg(LIME.c400);
impl FromIterator<&'static str> for OptionList {
fn from_iter<I: IntoIterator<Item = &'static str>>(iter: I) -> Self {
let items = iter.into_iter().collect();
@ -96,32 +96,34 @@ impl FromIterator<&'static str> for OptionList {
}
impl OptionList {
pub fn handle_key(&self, current_mode: game::Mode, key: KeyEvent) -> game::Mode {
pub fn handle_key(mut self, key: KeyEvent) -> game::Mode {
match key.code {
KeyCode::Char('q') | KeyCode::Esc => game::Mode::Exit,
KeyCode::Char('j') | KeyCode::Down => self.select_next(),
KeyCode::Char('k') | KeyCode::Up => self.select_previous(),
KeyCode::Char('j') | KeyCode::Down => {
self.select_next();
game::Mode::MainMenu(self)
}
KeyCode::Char('k') | KeyCode::Up => {
self.select_previous();
game::Mode::MainMenu(self)
}
KeyCode::Char('c') => {
if key.modifiers.contains(event::KeyModifiers::CONTROL) {
game::Mode::Exit
} else {
current_mode
game::Mode::MainMenu(self)
}
}
_ => current_mode
_ => game::Mode::MainMenu(self),
}
}
fn select_next(&self) -> game::Mode {
let mut menu_list = self.clone();
menu_list.state.select_next();
game::Mode::MainMenu(menu_list)
fn select_next(&mut self) {
self.state.select_next();
}
fn select_previous(&self) -> game::Mode {
let mut menu_list = self.clone();
menu_list.state.select_previous();
game::Mode::MainMenu(menu_list)
fn select_previous(&mut self) {
self.state.select_previous();
}
pub fn render(&mut self, area: Rect, buf: &mut Buffer) {