generated from GarandPLG/rust-flake-template
KeybindingsWidget now accepts actions
Views now pass a list of `Action` values to `KeybindingsWidget` instead of pre‑resolved `KeyBinding` references. The widget resolves the bindings internally using `binding_for`, simplifying import statements and view logic.
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
use crate::app::{
|
||||
keybindings::{Action, KeyBinding, binding_for},
|
||||
widgets::KeybindingsWidget,
|
||||
};
|
||||
use crate::app::{keybindings::Action, widgets::KeybindingsWidget};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
@@ -25,12 +22,8 @@ pub fn default_view(area: Rect, buf: &mut Buffer) {
|
||||
.render(main_area, buf);
|
||||
|
||||
{
|
||||
let keybindings: Vec<Option<&'static KeyBinding>> = vec![
|
||||
binding_for(Action::Quit),
|
||||
binding_for(Action::Quit2),
|
||||
binding_for(Action::Esc),
|
||||
];
|
||||
let actions: Vec<Action> = vec![Action::Quit, Action::Quit2, Action::Esc];
|
||||
|
||||
KeybindingsWidget::new(keybindings).render(keybindings_area, buf);
|
||||
KeybindingsWidget::new(actions).render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
use crate::app::{
|
||||
App, View,
|
||||
keybindings::{Action, KeyBinding, binding_for},
|
||||
widgets::KeybindingsWidget,
|
||||
};
|
||||
use crate::app::{App, View, keybindings::Action, widgets::KeybindingsWidget};
|
||||
use clap::ValueEnum;
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -87,14 +83,14 @@ pub fn main_menu_view(app: &App, area: Rect, buf: &mut Buffer) {
|
||||
}
|
||||
|
||||
{
|
||||
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),
|
||||
let actions: Vec<Action> = vec![
|
||||
Action::Up,
|
||||
Action::Down,
|
||||
Action::Space,
|
||||
Action::Quit,
|
||||
Action::Quit2,
|
||||
];
|
||||
|
||||
KeybindingsWidget::new(keybindings).render(keybindings_area, buf);
|
||||
KeybindingsWidget::new(actions).render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-14
@@ -1,8 +1,4 @@
|
||||
use crate::app::{
|
||||
App,
|
||||
keybindings::{Action, KeyBinding, binding_for},
|
||||
widgets::KeybindingsWidget,
|
||||
};
|
||||
use crate::app::{App, keybindings::Action, widgets::KeybindingsWidget};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
@@ -64,16 +60,16 @@ pub fn settings_view(app: &App, area: Rect, buf: &mut Buffer) {
|
||||
}
|
||||
|
||||
{
|
||||
let keybindings: Vec<Option<&'static KeyBinding>> = vec![
|
||||
binding_for(Action::Up),
|
||||
binding_for(Action::Down),
|
||||
binding_for(Action::Space),
|
||||
binding_for(Action::Enter),
|
||||
binding_for(Action::Quit),
|
||||
binding_for(Action::Quit2),
|
||||
binding_for(Action::Esc),
|
||||
let actions: Vec<Action> = vec![
|
||||
Action::Up,
|
||||
Action::Down,
|
||||
Action::Space,
|
||||
Action::Enter,
|
||||
Action::Quit,
|
||||
Action::Quit2,
|
||||
Action::Esc,
|
||||
];
|
||||
|
||||
KeybindingsWidget::new(keybindings).render(keybindings_area, buf);
|
||||
KeybindingsWidget::new(actions).render(keybindings_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::app::keybindings::{Group, KeyBinding};
|
||||
use crate::app::keybindings::{Action, Group, KeyBinding, binding_for};
|
||||
use clap::ValueEnum;
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
@@ -9,13 +7,17 @@ use ratatui::{
|
||||
text::Line,
|
||||
widgets::{Block, Borders, Padding, Paragraph, Widget},
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct KeybindingsWidget {
|
||||
grouped: Vec<Paragraph<'static>>,
|
||||
}
|
||||
|
||||
impl KeybindingsWidget {
|
||||
pub fn new(keybindings: Vec<Option<&'static KeyBinding>>) -> Self {
|
||||
pub fn new(actions: Vec<Action>) -> Self {
|
||||
let keybindings: Vec<Option<&'static KeyBinding>> =
|
||||
actions.iter().map(|a| binding_for(*a)).collect();
|
||||
|
||||
let used_groups: HashSet<Group> = keybindings
|
||||
.iter()
|
||||
.filter_map(|b| b.map(|binding| binding.group))
|
||||
|
||||
Reference in New Issue
Block a user