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
-25
View File
@@ -1,25 +0,0 @@
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);
}
+8 -6
View File
@@ -1,7 +1,6 @@
use crate::app::{
View,
keybindings::{Group, binding_for_view},
};
use std::collections::HashSet;
use crate::app::keybindings::{Group, KeyBinding};
use ratatui::{
buffer::Buffer,
layout::{Alignment, Constraint, Layout, Rect},
@@ -15,8 +14,11 @@ pub struct KeybindingsWidget {
}
impl KeybindingsWidget {
pub fn new(view: View) -> Self {
let (used_groups, keybindings) = binding_for_view(view);
pub fn new(keybindings: Vec<Option<&'static KeyBinding>>) -> Self {
let used_groups: HashSet<Group> = keybindings
.iter()
.filter_map(|b| b.map(|binding| binding.group))
.collect();
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
for (i, group) in Group::iter().enumerate() {
-85
View File
@@ -1,85 +0,0 @@
use crate::app::{App, View, widgets::KeybindingsWidget};
use clap::ValueEnum;
use ratatui::{
buffer::Buffer,
layout::{Alignment, Constraint, Layout, Rect},
style::Stylize,
text::Line,
widgets::{Block, Borders, Paragraph, Widget},
};
fn view_options() -> Vec<(usize, String)> {
View::value_variants()
.iter()
.enumerate()
.filter_map(|(i, v)| {
v.to_possible_value().map(|possible_value| {
let name = possible_value
.get_name()
.replace('-', " ")
.to_uppercase()
.to_string();
(i, name)
})
})
.filter(|(_, v)| v != "MAIN MENU")
.collect()
}
pub fn main_menu_widget(app: &App, area: Rect, buf: &mut Buffer) {
let vertical_layout: Layout = Layout::vertical([Constraint::Fill(1), Constraint::Length(4)]);
let [main_menu_area, keybindings_area] = vertical_layout.areas(area);
Block::new()
.borders(Borders::LEFT | Borders::TOP | Borders::RIGHT)
.render(main_menu_area, buf);
let main_menu_areas: Vec<Rect> = main_menu_area.layout_vec(&Layout::vertical([
Constraint::Percentage(50),
Constraint::Percentage(50),
]));
{
let title_area: Rect = main_menu_areas[0].centered_vertically(Constraint::Percentage(50));
let title_text: String = vec![
r" __ __ _ _____ _ ",
r"/ / /\ \ \__ _ _ __ (_)_ __ /__ \_ _ _ __ _ __ ___| |___ ",
r"\ \/ \/ / _` | '__| | | '_ \ / /\/ | | | '_ \| '_ \ / _ \ / __|",
r" \ /\ / (_| | | | | | | | / / | |_| | | | | | | | __/ \__ \",
r" \/ \/ \__,_|_| |_|_| |_| \/ \__,_|_| |_|_| |_|\___|_|___/",
]
.join("\n");
Paragraph::new(title_text)
.alignment(Alignment::Center)
.yellow()
.block(Block::new().gray().borders(Borders::LEFT | Borders::RIGHT))
.render(title_area, buf);
}
{
let options_area: Rect = main_menu_areas[1];
let lines: Vec<Line<'_>> = view_options()
.into_iter()
.map(|(i, view)| {
let styled = if app.game_states.main_menu_state.selected_view == i {
Line::from(format!("> {}", view)).yellow()
} else {
Line::from(view).white()
};
styled
})
.collect();
Paragraph::new(lines)
.alignment(Alignment::Center)
.green()
.block(Block::new().gray().borders(Borders::LEFT | Borders::RIGHT))
.render(options_area, buf);
}
KeybindingsWidget::new(View::MainMenu).render(keybindings_area, buf);
}
-6
View File
@@ -1,9 +1,3 @@
pub mod default;
pub mod keybindings;
pub mod main_menu;
pub mod settings;
pub use default::default_view_widget;
pub use keybindings::KeybindingsWidget;
pub use main_menu::main_menu_widget;
pub use settings::settings_widget;
-41
View File
@@ -1,41 +0,0 @@
use crate::app::{App, widgets::KeybindingsWidget};
use ratatui::{
buffer::Buffer,
layout::{Alignment, Constraint, Layout, Rect},
style::Stylize,
widgets::{Block, Borders, Padding, Paragraph, Widget},
};
pub fn settings_widget(app: &App, area: Rect, buf: &mut Buffer) {
let vertical_layout: Layout = Layout::vertical([
Constraint::Length(10),
Constraint::Fill(1),
Constraint::Length(4),
]);
let [title_area, main_area, keybindings_area] = vertical_layout.areas(area);
Paragraph::new("War in tunnels")
.alignment(Alignment::Left)
.yellow()
.block(
Block::new()
.gray()
.borders(Borders::LEFT | Borders::TOP | Borders::RIGHT)
.padding(Padding::new(1, 1, 1, 1)),
)
.render(title_area, buf);
Paragraph::new("Settings")
.alignment(Alignment::Center)
.yellow()
.block(
Block::new()
.gray()
.borders(Borders::LEFT | Borders::RIGHT)
.padding(Padding::new(1, 1, 1, 1)),
)
.render(main_area, buf);
KeybindingsWidget::new(app.view).render(keybindings_area, buf);
}