diff options
author | Shav Kinderlehrer <[email protected]> | 2024-03-23 13:08:14 -0400 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2024-03-23 13:08:14 -0400 |
commit | 0e29fa02995273bfd803aea48773cbe52a7366ed (patch) | |
tree | efd8302cfc433c076010d94849fda224d36167d4 /src/components/global_keys.rs | |
parent | 0c5e8ab544823fbb4936c536ee1d8a66298f7e51 (diff) | |
download | molehole-0e29fa02995273bfd803aea48773cbe52a7366ed.tar.gz molehole-0e29fa02995273bfd803aea48773cbe52a7366ed.zip |
Rework actions and events + start statusbar
Diffstat (limited to 'src/components/global_keys.rs')
-rw-r--r-- | src/components/global_keys.rs | 75 |
1 files changed, 32 insertions, 43 deletions
diff --git a/src/components/global_keys.rs b/src/components/global_keys.rs index d0a5800..39ec1b7 100644 --- a/src/components/global_keys.rs +++ b/src/components/global_keys.rs @@ -24,60 +24,49 @@ pub struct GlobalKeys { impl Component for GlobalKeys { fn init(&mut self) -> eyre::Result<()> { - self.key_commands.append(&mut vec![KeyCommand { - key_code: "?".to_string(), - description: "Toggle help menu".to_string(), - action: None, - }]); - 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, ) -> eyre::Result<Option<AppAction>> { if key.kind == KeyEventKind::Press { let key_event = serialize_key_event(key); - let eat_input = match key_event.as_str() { - "?" => { - self.should_show = !self.should_show; - self.scroll = 0; - true - } - "g" => { - self.scroll = 0; - true - } - "G" => { - self.scroll = self.key_commands.len() - 1; - true - } - "down" | "j" => { - if self.scroll < self.key_commands.len() - 1 { - self.scroll += 1; - } - true - } - "up" | "k" => { - if self.scroll > 0 { - self.scroll -= 1; - } - true - } - _ => false, - }; - self.scroll_state = self.scroll_state.position(self.scroll); - if eat_input && self.should_show { - return Ok(None); - } - for key_command in &mut self.key_commands { if key_command.key_code == key_event { - return Ok(key_command.action.clone()); + return Ok(Some(key_command.action.clone())); } } } @@ -108,7 +97,8 @@ impl Component for GlobalKeys { Title::from("Keyboard shortcuts").alignment(Alignment::Center), ) .borders(Borders::ALL) - .border_type(BorderType::Thick); + .border_type(BorderType::Thick) + .style(Style::default().bg(Color::DarkGray)); let mut lines: Vec<Line> = vec![]; for key_command in &mut self.key_commands { @@ -124,8 +114,7 @@ impl Component for GlobalKeys { let commands = Paragraph::new(lines) .block(block) .wrap(Wrap { trim: true }) - .scroll((u16::try_from(self.scroll)?, 0)) - .style(Style::default().bg(Color::DarkGray).fg(Color::White)); + .scroll((u16::try_from(self.scroll)?, 0)); if self.should_show { frame.render_widget(Clear, center); |