Refactor keybindings with groups and symbols

Introduce a `Group` enum to categorize actions and add `group` and
`symbol`
fields to `KeyBinding`. Extend `Action` with `Up`, `Down`, `Space`, and
`Esc`, updating all keybinding definitions accordingly.

Update `binding_for_view` to return the new actions per view and handle
`Esc` by returning to the main menu.

Rewrite the keybindings widget as a custom `Widget` that renders grouped
paragraphs with proper layout.

Adjust the main menu layout percentages to accommodate the new widget.
This commit is contained in:
2026-03-12 16:00:45 +01:00
parent f481f5dc87
commit 71b9d44a77
6 changed files with 126 additions and 43 deletions
+51 -9
View File
@@ -4,17 +4,39 @@ use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
Quit,
ScrollUp,
ScrollDown,
Up,
Down,
Space,
Esc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Group {
Quit,
Movement,
Select,
}
impl Group {
pub fn iter() -> impl Iterator<Item = Group> {
[Group::Quit, Group::Movement, Group::Select]
.iter()
.copied()
}
pub fn len() -> usize {
Self::iter().count()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyBinding {
pub action: Action,
pub code: KeyCode,
pub kind: KeyEventKind,
pub modifiers: KeyModifiers,
pub group: Group,
pub symbol: &'static str,
pub description: &'static str,
}
@@ -24,29 +46,46 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
code: KeyCode::Char('q'),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
group: Group::Quit,
symbol: "q",
description: "Quit",
},
KeyBinding {
action: Action::ScrollUp,
action: Action::Up,
code: KeyCode::Up,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
group: Group::Movement,
symbol: "",
description: "Up",
},
KeyBinding {
action: Action::ScrollDown,
action: Action::Down,
code: KeyCode::Down,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
group: Group::Movement,
symbol: "",
description: "Down",
},
KeyBinding {
action: Action::Select,
action: Action::Space,
code: KeyCode::Char(' '),
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
group: Group::Select,
symbol: "Space",
description: "Select",
},
KeyBinding {
action: Action::Esc,
code: KeyCode::Esc,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
group: Group::Movement,
symbol: "Esc",
description: "Go back",
},
];
pub fn binding_for(action: Action) -> Option<&'static KeyBinding> {
@@ -63,11 +102,14 @@ pub fn event_to_action(event: &KeyEvent) -> Option<Action> {
pub fn binding_for_view(view: View) -> Vec<&'static KeyBinding> {
match view {
View::MainMenu => vec![
binding_for(Action::ScrollUp).unwrap(),
binding_for(Action::ScrollDown).unwrap(),
binding_for(Action::Select).unwrap(),
binding_for(Action::Up).unwrap(),
binding_for(Action::Down).unwrap(),
binding_for(Action::Space).unwrap(),
binding_for(Action::Quit).unwrap(),
],
_ => vec![],
_ => vec![
binding_for(Action::Quit).unwrap(),
binding_for(Action::Esc).unwrap(),
],
}
}