generated from GarandPLG/rust-flake-template
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:
@@ -0,0 +1,99 @@
|
||||
use crate::app::{
|
||||
App, View,
|
||||
keybindings::{Action, KeyBinding, binding_for},
|
||||
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_view(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);
|
||||
}
|
||||
|
||||
{
|
||||
let 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),
|
||||
];
|
||||
|
||||
KeybindingsWidget::new(keybindings).render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user