Extract keybinding handling to separate module

This commit is contained in:
2026-03-12 16:37:52 +01:00
parent ef271c0d87
commit 8b7c2c55ce
3 changed files with 28 additions and 19 deletions
+2 -19
View File
@@ -1,8 +1,5 @@
use crate::{
app::{
GameStates,
keybindings::{Action, event_to_action, main_menu_keybindings},
},
app::{GameStates, handle_keybindings},
cli::Cli,
};
use clap::ValueEnum;
@@ -103,21 +100,7 @@ impl App {
}
fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<()> {
match self.view {
View::MainMenu => main_menu_keybindings(self, &key_event),
_ => {
if let Some(action) = event_to_action(&key_event) {
match action {
Action::Quit => self.exit = true,
Action::Quit2 => self.exit = true,
Action::Esc => self.view = View::MainMenu,
_ => (),
}
}
}
}
Ok(())
handle_keybindings(self, key_event)
}
}
+24
View File
@@ -0,0 +1,24 @@
use crate::app::{
App, View,
keybindings::{Action, event_to_action, main_menu_keybindings},
};
use ratatui::crossterm::event::KeyEvent;
use std::io::Result;
pub fn handle_keybindings(app: &mut App, key_event: KeyEvent) -> Result<()> {
match app.view {
View::MainMenu => main_menu_keybindings(app, &key_event),
_ => {
if let Some(action) = event_to_action(&key_event) {
match action {
Action::Quit => app.exit = true,
Action::Quit2 => app.exit = true,
Action::Esc => app.view = View::MainMenu,
_ => (),
}
}
}
}
Ok(())
}
+2
View File
@@ -1,5 +1,6 @@
pub mod app;
pub mod game_states;
pub mod keybind;
pub mod keybindings;
pub mod widget;
pub mod widgets;
@@ -8,3 +9,4 @@ pub use app::{App, Event, GameMode, PerkDecks, View, handle_input_events};
pub use game_states::{
GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState,
};
pub use keybind::handle_keybindings;