generated from GarandPLG/rust-flake-template
Add Settings view, widget, and keybindings
Introduce Settings view handling in key events and widgets. Add `Enter` action with its keybinding. Refactor keybinding lookup to use `Option<&KeyBinding>` for missing entries. Update view option generation to safely filter possible values. Export `settings_keybindings` and `settings_widget` from their modules.
This commit is contained in:
@@ -26,9 +26,15 @@ impl KeybindingsWidget {
|
||||
|
||||
let grouped_lines: Vec<Line<'_>> = keybindings
|
||||
.iter()
|
||||
.filter(|b| b.group == group)
|
||||
.map(|b| {
|
||||
Line::from_iter([b.symbol.red().bold(), " - ".into(), b.description.into()])
|
||||
.filter(|b| b.as_ref().map_or(false, |kb| kb.group == group))
|
||||
.filter_map(|b| {
|
||||
b.as_ref().map(|kb| {
|
||||
Line::from_iter([
|
||||
kb.symbol.red().bold(),
|
||||
" - ".into(),
|
||||
kb.description.into(),
|
||||
])
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ fn view_options() -> Vec<(usize, String)> {
|
||||
View::value_variants()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, v)| {
|
||||
let name = v
|
||||
.to_possible_value()
|
||||
.unwrap()
|
||||
.get_name()
|
||||
.replace('-', " ")
|
||||
.to_uppercase()
|
||||
.to_string();
|
||||
(i, name)
|
||||
.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()
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user