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

@ -45,12 +45,12 @@ impl Mode {
fn handle_key(self, key: KeyEvent) -> Mode { fn handle_key(self, key: KeyEvent) -> Mode {
if key.kind != KeyEventKind::Press { if key.kind != KeyEventKind::Press {
return self return self
} }
match self { match self {
Mode::MainMenu(ref menu_list) => menu_list.handle_key(self.clone(), key), Mode::MainMenu(menu_list) => menu_list.handle_key(key),
_ => return self, _ => Mode::Exit,
} }
} }
} }
@ -86,8 +86,8 @@ impl Widget for &mut Mode {
match self { match self {
Mode::MainMenu(menu_list) => { Mode::MainMenu(menu_list) => {
menu_list.render(list_outer_area, buf);
menu::render_profile(profile_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; 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) { pub fn render_header(area: Rect, buf: &mut Buffer) {
Block::new() Block::new()
.title(Line::raw(" txtris ").centered().style(HEADER_STYLE)) .title(Line::raw(" txtris ").centered().style(HEADER_STYLE))
@ -72,21 +87,6 @@ pub struct OptionList {
state: ListState, 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 { impl FromIterator<&'static str> for OptionList {
fn from_iter<I: IntoIterator<Item = &'static str>>(iter: I) -> Self { fn from_iter<I: IntoIterator<Item = &'static str>>(iter: I) -> Self {
let items = iter.into_iter().collect(); let items = iter.into_iter().collect();
@ -96,32 +96,34 @@ impl FromIterator<&'static str> for OptionList {
} }
impl 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 { match key.code {
KeyCode::Char('q') | KeyCode::Esc => game::Mode::Exit, KeyCode::Char('q') | KeyCode::Esc => game::Mode::Exit,
KeyCode::Char('j') | KeyCode::Down => self.select_next(), KeyCode::Char('j') | KeyCode::Down => {
KeyCode::Char('k') | KeyCode::Up => self.select_previous(), self.select_next();
game::Mode::MainMenu(self)
}
KeyCode::Char('k') | KeyCode::Up => {
self.select_previous();
game::Mode::MainMenu(self)
}
KeyCode::Char('c') => { KeyCode::Char('c') => {
if key.modifiers.contains(event::KeyModifiers::CONTROL) { if key.modifiers.contains(event::KeyModifiers::CONTROL) {
game::Mode::Exit game::Mode::Exit
} else { } else {
current_mode game::Mode::MainMenu(self)
} }
} }
_ => current_mode _ => game::Mode::MainMenu(self),
} }
} }
fn select_next(&self) -> game::Mode { fn select_next(&mut self) {
let mut menu_list = self.clone(); self.state.select_next();
menu_list.state.select_next();
game::Mode::MainMenu(menu_list)
} }
fn select_previous(&self) -> game::Mode { fn select_previous(&mut self) {
let mut menu_list = self.clone(); self.state.select_previous();
menu_list.state.select_previous();
game::Mode::MainMenu(menu_list)
} }
pub fn render(&mut self, area: Rect, buf: &mut Buffer) { pub fn render(&mut self, area: Rect, buf: &mut Buffer) {