Refactor view handling and keybinding API

Move the `View` enum to a dedicated module and implement `Widget` for
`&App` there.
Rename `default_view_keybindings` to `default_keybindings` and adjust
its
signature. Redesign `KeybindingsWidget` to accept an explicit list of
bindings instead of a `View`. Update imports, function signatures, and
rendering
logic across the codebase to reflect these changes.
This commit is contained in:
2026-03-13 20:10:49 +01:00
parent d563bbc966
commit 61e9dedfe8
15 changed files with 109 additions and 115 deletions
-50
View File
@@ -1,6 +1,4 @@
use crate::app::View;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
@@ -115,51 +113,3 @@ pub fn event_to_action(event: &KeyEvent) -> Option<Action> {
.find(|b| b.code == event.code && b.kind == event.kind && b.modifiers == event.modifiers)
.map(|b| b.action)
}
fn used_groups_in_view(keybindings: &Vec<Option<&'static KeyBinding>>) -> HashSet<Group> {
keybindings
.iter()
.filter_map(|b| b.map(|binding| binding.group))
.collect()
}
pub fn binding_for_view(view: View) -> (HashSet<Group>, Vec<Option<&'static KeyBinding>>) {
let main_menu_keybindings: Vec<Option<&'static KeyBinding>> = vec![
binding_for(Action::Up),
binding_for(Action::Down),
binding_for(Action::Space),
binding_for(Action::Quit),
binding_for(Action::Quit2),
];
let settings_keybinding: Vec<Option<&'static KeyBinding>> = vec![
binding_for(Action::Up),
binding_for(Action::Down),
binding_for(Action::Space),
binding_for(Action::Enter),
binding_for(Action::Quit),
binding_for(Action::Quit2),
binding_for(Action::Esc),
];
let default_keybindings: Vec<Option<&'static KeyBinding>> = vec![
binding_for(Action::Quit),
binding_for(Action::Quit2),
binding_for(Action::Esc),
];
match view {
View::MainMenu => (
used_groups_in_view(&main_menu_keybindings),
main_menu_keybindings,
),
View::Settings => (
used_groups_in_view(&settings_keybinding),
settings_keybinding,
),
_ => (
used_groups_in_view(&default_keybindings),
default_keybindings,
),
}
}