From 7226a09bb49f5c83015498de1ad4455b02c91e85 Mon Sep 17 00:00:00 2001 From: GarandPLG Date: Thu, 12 Mar 2026 23:06:55 +0100 Subject: [PATCH] Extract default view keybindings and widget Move generic view rendering and keybinding logic into a new default module and replace inline implementations with calls to the new helpers. --- src/app/keybind.rs | 13 ++----------- src/app/keybindings/default.rs | 16 ++++++++++++++++ src/app/keybindings/mod.rs | 2 ++ src/app/widget.rs | 28 +++------------------------- src/app/widgets/default.rs | 25 +++++++++++++++++++++++++ src/app/widgets/mod.rs | 2 ++ 6 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 src/app/keybindings/default.rs create mode 100644 src/app/widgets/default.rs diff --git a/src/app/keybind.rs b/src/app/keybind.rs index 91b21e6..8a189f0 100644 --- a/src/app/keybind.rs +++ b/src/app/keybind.rs @@ -1,6 +1,6 @@ use crate::app::{ App, View, - keybindings::{Action, event_to_action, main_menu_keybindings}, + keybindings::{default_view_keybindings, main_menu_keybindings}, }; use ratatui::crossterm::event::KeyEvent; use std::io::Result; @@ -8,16 +8,7 @@ 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, - _ => (), - } - } - } + _ => default_view_keybindings(app, &key_event), } Ok(()) diff --git a/src/app/keybindings/default.rs b/src/app/keybindings/default.rs new file mode 100644 index 0000000..bd39769 --- /dev/null +++ b/src/app/keybindings/default.rs @@ -0,0 +1,16 @@ +use crate::app::{ + App, View, + keybindings::{Action, event_to_action}, +}; +use ratatui::crossterm::event::KeyEvent; + +pub fn default_view_keybindings(app: &mut App, key_event: &KeyEvent) { + 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, + _ => (), + } + } +} diff --git a/src/app/keybindings/mod.rs b/src/app/keybindings/mod.rs index 473b04c..54d6e2e 100644 --- a/src/app/keybindings/mod.rs +++ b/src/app/keybindings/mod.rs @@ -1,6 +1,8 @@ +pub mod default; pub mod keybindings; pub mod main_menu; +pub use default::default_view_keybindings; pub use keybindings::{ Action, Group, KEYBINDINGS, KeyBinding, binding_for, binding_for_view, event_to_action, }; diff --git a/src/app/widget.rs b/src/app/widget.rs index dfb5e7a..08c90a0 100644 --- a/src/app/widget.rs +++ b/src/app/widget.rs @@ -1,13 +1,8 @@ use crate::app::{ App, View, - widgets::{KeybindingsWidget, main_menu_widget}, -}; -use ratatui::{ - buffer::Buffer, - layout::{Alignment, Constraint, Layout, Rect}, - style::Stylize, - widgets::{Block, Borders, Paragraph, Widget}, + widgets::{default_view_widget, main_menu_widget}, }; +use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; impl Widget for &App { fn render(self, area: Rect, buf: &mut Buffer) @@ -16,24 +11,7 @@ impl Widget for &App { { match self.view { View::MainMenu => main_menu_widget(self, area, buf), - _ => { - let vertical_layout: Layout = - Layout::vertical([Constraint::Fill(1), Constraint::Length(4)]); - - let [main_area, keybindings_area] = vertical_layout.areas(area); - - Paragraph::new("Work in progress") - .alignment(Alignment::Center) - .yellow() - .block( - Block::new() - .gray() - .borders(Borders::LEFT | Borders::TOP | Borders::RIGHT), - ) - .render(main_area, buf); - - KeybindingsWidget::new(self.view).render(keybindings_area, buf); - } + _ => default_view_widget(self, area, buf), } } } diff --git a/src/app/widgets/default.rs b/src/app/widgets/default.rs new file mode 100644 index 0000000..f52c076 --- /dev/null +++ b/src/app/widgets/default.rs @@ -0,0 +1,25 @@ +use crate::app::{App, widgets::KeybindingsWidget}; +use ratatui::{ + buffer::Buffer, + layout::{Alignment, Constraint, Layout, Rect}, + style::Stylize, + widgets::{Block, Borders, Paragraph, Widget}, +}; + +pub fn default_view_widget(app: &App, area: Rect, buf: &mut Buffer) { + let vertical_layout: Layout = Layout::vertical([Constraint::Fill(1), Constraint::Length(4)]); + + let [main_area, keybindings_area] = vertical_layout.areas(area); + + Paragraph::new("Work in progress") + .alignment(Alignment::Center) + .yellow() + .block( + Block::new() + .gray() + .borders(Borders::LEFT | Borders::TOP | Borders::RIGHT), + ) + .render(main_area, buf); + + KeybindingsWidget::new(app.view).render(keybindings_area, buf); +} diff --git a/src/app/widgets/mod.rs b/src/app/widgets/mod.rs index ef2f046..3285731 100644 --- a/src/app/widgets/mod.rs +++ b/src/app/widgets/mod.rs @@ -1,5 +1,7 @@ +pub mod default; pub mod keybindings; pub mod main_menu; +pub use default::default_view_widget; pub use keybindings::KeybindingsWidget; pub use main_menu::main_menu_widget;