diff options
author | Shav Kinderlehrer <[email protected]> | 2024-07-23 17:48:28 -0400 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2024-07-23 17:48:28 -0400 |
commit | dc0f2ce9ba97ebb47e05b80a511da6eb29818b63 (patch) | |
tree | dc83035069f5a015047be1ca3da6f65781eb4695 /src/components/global_keys.rs | |
parent | f638f4bd1e3a03bc2bdd5f9dcd57d4830fd3c553 (diff) | |
download | molehole-dc0f2ce9ba97ebb47e05b80a511da6eb29818b63.tar.gz molehole-dc0f2ce9ba97ebb47e05b80a511da6eb29818b63.zip |
Merge old-moleholencurses
Diffstat (limited to 'src/components/global_keys.rs')
-rw-r--r-- | src/components/global_keys.rs | 131 |
1 files changed, 0 insertions, 131 deletions
diff --git a/src/components/global_keys.rs b/src/components/global_keys.rs deleted file mode 100644 index dd903b1..0000000 --- a/src/components/global_keys.rs +++ /dev/null @@ -1,131 +0,0 @@ -use crossterm::event::{KeyEvent, KeyEventKind}; -use ratatui::prelude::{ - Alignment, Color, Constraint, Direction, Frame, Layout, Line, Margin, Rect, - Span, Style, Stylize, -}; -use ratatui::widgets::block::{Block, BorderType, Title}; -use ratatui::widgets::{ - Borders, Clear, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, - Wrap, -}; - -use crate::app_action::AppAction; -use crate::component::Component; -use crate::keys::key_commands::{serialize_key_event, KeyCommand}; - -#[derive(Default)] -pub struct GlobalKeys { - pub key_commands: Vec<KeyCommand>, - - pub should_show: bool, - pub scroll: usize, - pub scroll_state: ScrollbarState, -} - -impl Component for GlobalKeys { - fn init(&mut self) -> eyre::Result<()> { - self.scroll_state = - ScrollbarState::new(self.key_commands.len()).position(self.scroll); - - Ok(()) - } - - fn handle_action(&mut self, action: AppAction) { - match action { - AppAction::ScrollUp => { - if self.scroll > 0 { - self.scroll -= 1; - } - } - AppAction::ScrollDown => { - if self.scroll < self.key_commands.len() - 1 { - self.scroll += 1; - } - } - AppAction::ScrollTop => { - self.scroll = 0; - } - AppAction::ScrollBottom => { - self.scroll = self.key_commands.len() - 1; - } - AppAction::ShowHelpMenu => { - self.should_show = !self.should_show; - self.scroll = 0; - } - - _ => {} - } - self.scroll_state = self.scroll_state.position(self.scroll); - } - - fn handle_key_event(&mut self, key: KeyEvent) -> Option<AppAction> { - if key.kind == KeyEventKind::Press { - let key_event = serialize_key_event(key); - for key_command in &mut self.key_commands { - if key_command.key_code == key_event { - return Some(key_command.action.clone()); - } - } - } - - None - } - - fn render(&mut self, frame: &mut Frame, rect: Rect) -> eyre::Result<()> { - let vertical_center = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Percentage(50 / 2), - Constraint::Percentage(50), - Constraint::Percentage(50 / 2), - ]) - .split(rect); - let center = Layout::default() - .direction(Direction::Horizontal) - .constraints([ - Constraint::Percentage(50 / 2), - Constraint::Percentage(50), - Constraint::Percentage(50 / 2), - ]) - .split(vertical_center[1])[1]; - - let block = Block::default() - .title( - Title::from("Keyboard shortcuts").alignment(Alignment::Center), - ) - .borders(Borders::ALL) - .border_type(BorderType::Thick) - .style(Style::default().bg(Color::DarkGray)); - - let mut lines: Vec<Line> = vec![]; - for key_command in &mut self.key_commands { - let command = Span::from(key_command.key_code.clone()); - let description = - Span::from(key_command.description.clone()).italic(); - let spacer = Span::from(" "); - - let line = Line::from(vec![command, spacer, description]); - lines.push(line); - } - - let commands = Paragraph::new(lines) - .block(block) - .wrap(Wrap { trim: true }) - .scroll((u16::try_from(self.scroll)?, 0)); - - if self.should_show { - frame.render_widget(Clear, center); - frame.render_widget(commands, center); - frame.render_stateful_widget( - Scrollbar::new(ScrollbarOrientation::VerticalRight), - center.inner(&Margin { - vertical: 1, - horizontal: 0, - }), - &mut self.scroll_state, - ); - } - - Ok(()) - } -} |