generated from GarandPLG/rust-flake-template
Extract default view keybindings and widget
Move generic view rendering and keybinding logic into a new default module and replace inline implementations with calls to the new helpers.
This commit is contained in:
+2
-11
@@ -1,6 +1,6 @@
|
|||||||
use crate::app::{
|
use crate::app::{
|
||||||
App, View,
|
App, View,
|
||||||
keybindings::{Action, event_to_action, main_menu_keybindings},
|
keybindings::{default_view_keybindings, main_menu_keybindings},
|
||||||
};
|
};
|
||||||
use ratatui::crossterm::event::KeyEvent;
|
use ratatui::crossterm::event::KeyEvent;
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
@@ -8,16 +8,7 @@ use std::io::Result;
|
|||||||
pub fn handle_keybindings(app: &mut App, key_event: KeyEvent) -> Result<()> {
|
pub fn handle_keybindings(app: &mut App, key_event: KeyEvent) -> Result<()> {
|
||||||
match app.view {
|
match app.view {
|
||||||
View::MainMenu => main_menu_keybindings(app, &key_event),
|
View::MainMenu => main_menu_keybindings(app, &key_event),
|
||||||
_ => {
|
_ => default_view_keybindings(app, &key_event),
|
||||||
if let Some(action) = event_to_action(&key_event) {
|
|
||||||
match action {
|
|
||||||
Action::Quit => app.exit = true,
|
|
||||||
Action::Quit2 => app.exit = true,
|
|
||||||
Action::Esc => app.view = View::MainMenu,
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use crate::app::{
|
||||||
|
App, View,
|
||||||
|
keybindings::{Action, event_to_action},
|
||||||
|
};
|
||||||
|
use ratatui::crossterm::event::KeyEvent;
|
||||||
|
|
||||||
|
pub fn default_view_keybindings(app: &mut App, key_event: &KeyEvent) {
|
||||||
|
if let Some(action) = event_to_action(&key_event) {
|
||||||
|
match action {
|
||||||
|
Action::Quit => app.exit = true,
|
||||||
|
Action::Quit2 => app.exit = true,
|
||||||
|
Action::Esc => app.view = View::MainMenu,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
|
pub mod default;
|
||||||
pub mod keybindings;
|
pub mod keybindings;
|
||||||
pub mod main_menu;
|
pub mod main_menu;
|
||||||
|
|
||||||
|
pub use default::default_view_keybindings;
|
||||||
pub use keybindings::{
|
pub use keybindings::{
|
||||||
Action, Group, KEYBINDINGS, KeyBinding, binding_for, binding_for_view, event_to_action,
|
Action, Group, KEYBINDINGS, KeyBinding, binding_for, binding_for_view, event_to_action,
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-25
@@ -1,13 +1,8 @@
|
|||||||
use crate::app::{
|
use crate::app::{
|
||||||
App, View,
|
App, View,
|
||||||
widgets::{KeybindingsWidget, main_menu_widget},
|
widgets::{default_view_widget, main_menu_widget},
|
||||||
};
|
|
||||||
use ratatui::{
|
|
||||||
buffer::Buffer,
|
|
||||||
layout::{Alignment, Constraint, Layout, Rect},
|
|
||||||
style::Stylize,
|
|
||||||
widgets::{Block, Borders, Paragraph, Widget},
|
|
||||||
};
|
};
|
||||||
|
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
|
||||||
|
|
||||||
impl Widget for &App {
|
impl Widget for &App {
|
||||||
fn render(self, area: Rect, buf: &mut Buffer)
|
fn render(self, area: Rect, buf: &mut Buffer)
|
||||||
@@ -16,24 +11,7 @@ impl Widget for &App {
|
|||||||
{
|
{
|
||||||
match self.view {
|
match self.view {
|
||||||
View::MainMenu => main_menu_widget(self, area, buf),
|
View::MainMenu => main_menu_widget(self, area, buf),
|
||||||
_ => {
|
_ => default_view_widget(self, area, buf),
|
||||||
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(self.view).render(keybindings_area, buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
|
pub mod default;
|
||||||
pub mod keybindings;
|
pub mod keybindings;
|
||||||
pub mod main_menu;
|
pub mod main_menu;
|
||||||
|
|
||||||
|
pub use default::default_view_widget;
|
||||||
pub use keybindings::KeybindingsWidget;
|
pub use keybindings::KeybindingsWidget;
|
||||||
pub use main_menu::main_menu_widget;
|
pub use main_menu::main_menu_widget;
|
||||||
|
|||||||
Reference in New Issue
Block a user