generated from GarandPLG/rust-flake-template
Refactor keybindings widget and reorder Group enum
- Introduce `KeybindingsWidget::new(view)` constructor and remove the standalone `keybindings_widget` function. - Update all imports and call sites to use `KeybindingsWidget` directly. - Reorder `Group` enum to place `Quit` after `Select` and adjust its iterator accordingly.
This commit is contained in:
@@ -14,14 +14,14 @@ pub enum Action {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Group {
|
||||
Quit,
|
||||
Movement,
|
||||
Select,
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub fn iter() -> impl Iterator<Item = Group> {
|
||||
[Group::Quit, Group::Movement, Group::Select]
|
||||
[Group::Movement, Group::Select, Group::Quit]
|
||||
.iter()
|
||||
.copied()
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
use crate::app::{
|
||||
App, View,
|
||||
widgets::{keybindings_widget, main_menu_widget},
|
||||
widgets::{KeybindingsWidget, main_menu_widget},
|
||||
};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -32,7 +32,7 @@ impl Widget for &App {
|
||||
)
|
||||
.render(main_area, buf);
|
||||
|
||||
keybindings_widget(self.view).render(keybindings_area, buf);
|
||||
KeybindingsWidget::new(self.view).render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,43 @@ pub struct KeybindingsWidget {
|
||||
grouped: Vec<Paragraph<'static>>,
|
||||
}
|
||||
|
||||
impl KeybindingsWidget {
|
||||
pub fn new(view: View) -> Self {
|
||||
let (used_groups, keybindings) = binding_for_view(view);
|
||||
|
||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
|
||||
for (i, group) in Group::iter().enumerate() {
|
||||
if !used_groups.contains(&group) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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()])
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut block: Block<'_> = Block::default().padding(Padding::new(1, 1, 0, 0));
|
||||
|
||||
if i != 0 {
|
||||
block = block.borders(Borders::LEFT);
|
||||
}
|
||||
|
||||
grouped_keybindings.push(
|
||||
Paragraph::new(grouped_lines)
|
||||
.alignment(Alignment::Center)
|
||||
.block(block),
|
||||
);
|
||||
}
|
||||
|
||||
Self {
|
||||
grouped: grouped_keybindings,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for KeybindingsWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
let count: u16 = self.grouped.len() as u16;
|
||||
@@ -41,36 +78,3 @@ impl Widget for KeybindingsWidget {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keybindings_widget(view: View) -> KeybindingsWidget {
|
||||
let (used_groups, keybindings) = binding_for_view(view);
|
||||
|
||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
|
||||
for (i, group) in Group::iter().enumerate() {
|
||||
if !used_groups.contains(&group) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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()]))
|
||||
.collect();
|
||||
|
||||
let mut block: Block<'_> = Block::default().padding(Padding::new(1, 1, 0, 0));
|
||||
|
||||
if i != 0 {
|
||||
block = block.borders(Borders::LEFT);
|
||||
}
|
||||
|
||||
grouped_keybindings.push(
|
||||
Paragraph::new(grouped_lines)
|
||||
.alignment(Alignment::Center)
|
||||
.block(block),
|
||||
);
|
||||
}
|
||||
|
||||
KeybindingsWidget {
|
||||
grouped: grouped_keybindings,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::app::{App, View, widgets::keybindings_widget};
|
||||
use crate::app::{App, View, widgets::KeybindingsWidget};
|
||||
use clap::ValueEnum;
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -81,5 +81,5 @@ pub fn main_menu_widget(app: &App, area: Rect, buf: &mut Buffer) {
|
||||
.render(options_area, buf);
|
||||
}
|
||||
|
||||
keybindings_widget(View::MainMenu).render(keybindings_area, buf);
|
||||
KeybindingsWidget::new(View::MainMenu).render(keybindings_area, buf);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod keybindings;
|
||||
pub mod main_menu;
|
||||
|
||||
pub use keybindings::keybindings_widget;
|
||||
pub use keybindings::KeybindingsWidget;
|
||||
pub use main_menu::main_menu_widget;
|
||||
|
||||
Reference in New Issue
Block a user