diff options
author | Tim Oram <dev@mitmaro.ca> | 2022-04-12 09:58:26 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2022-04-12 19:56:44 -0230 |
commit | 8c573df1632692cb2b5c620f5d08aac18a7907cf (patch) | |
tree | e9e913c9c9e107cd1581eea5780931a74aeebdb2 | |
parent | 8e9fc2f9bc4c8d8d9f7d73e2ee80e8106e30f186 (diff) |
Move help input handling out input module
-rw-r--r-- | src/core/src/components/help/mod.rs | 24 | ||||
-rw-r--r-- | src/core/src/components/help/tests.rs | 39 | ||||
-rw-r--r-- | src/core/src/modules/list/mod.rs | 16 | ||||
-rw-r--r-- | src/core/src/modules/show_commit/mod.rs | 22 | ||||
-rw-r--r-- | src/core/src/util.rs | 18 | ||||
-rw-r--r-- | src/input/src/event_handler.rs | 6 | ||||
-rw-r--r-- | src/input/src/input_options.rs | 8 |
7 files changed, 83 insertions, 50 deletions
diff --git a/src/core/src/components/help/mod.rs b/src/core/src/components/help/mod.rs index b4880d4..62549de 100644 --- a/src/core/src/components/help/mod.rs +++ b/src/core/src/components/help/mod.rs @@ -2,10 +2,12 @@ mod tests; use display::DisplayColor; -use input::{Event, InputOptions}; +use input::{Event, InputOptions, KeyBindings, MetaEvent}; use unicode_segmentation::UnicodeSegmentation; use view::{handle_view_data_scroll, LineSegment, ViewData, ViewLine, ViewSender}; +use crate::first; + // TODO Remove `union` call when bitflags/bitflags#180 is resolved const INPUT_OPTIONS: InputOptions = InputOptions::RESIZE.union(InputOptions::MOVEMENT); @@ -76,12 +78,26 @@ impl Help { self.active.then(|| &INPUT_OPTIONS) } + pub(crate) fn read_event(&self, event: Event, key_bindings: &KeyBindings) -> Option<Event> { + if self.is_active() { + match event { + Event::Key(_) => Some(Event::from(MetaEvent::Help)), + _ => None, + } + } + else { + (key_bindings.help.contains(&event)).then(|| Event::from(MetaEvent::Help)) + } + } + pub(crate) fn handle_event(&mut self, event: Event, view_sender: &ViewSender) { - if handle_view_data_scroll(event, view_sender).is_none() { - if let Event::Key(_) = event { + let mut event_handler = || { + if event == Event::from(MetaEvent::Help) { self.active = false; } - } + Some(()) + }; + first!(|| handle_view_data_scroll(event, view_sender), event_handler); } pub(crate) fn set_active(&mut self) { diff --git a/src/core/src/components/help/tests.rs b/src/core/src/components/help/tests.rs index d9d56a4..64a8fa6 100644 --- a/src/core/src/components/help/tests.rs +++ b/src/core/src/components/help/tests.rs @@ -1,9 +1,16 @@ -use input::MetaEvent; +use input::{testutil::create_test_keybindings, KeyModifiers, MetaEvent, MouseEvent, MouseEventKind}; use rstest::rstest; use view::{assert_rendered_output, testutil::with_view_sender}; use super::*; +fn handle_event(help: &mut Help, event: Event) { + let key_bindings = &create_test_keybindings(); + if let Some(evt) = help.read_event(event, key_bindings) { + with_view_sender(|context| help.handle_event(evt, &context.sender)); + } +} + #[test] fn empty() { let mut module = Help::new_from_keybindings(&[]); @@ -44,21 +51,23 @@ fn from_key_bindings() { #[case::scroll_up(Event::from(MetaEvent::ScrollUp))] #[case::scroll_jump_down(Event::from(MetaEvent::ScrollJumpDown))] #[case::scroll_jump_up(Event::from(MetaEvent::ScrollJumpUp))] -fn input_continue_active(#[case] event: Event) { - with_view_sender(|context| { - let mut module = Help::new_from_keybindings(&[]); - module.set_active(); - let _ = module.handle_event(event, &context.sender); - assert!(module.is_active()); - }); +#[case::mouse_event(Event::Mouse(MouseEvent { + kind: MouseEventKind::ScrollUp, + column: 0, + row: 0, + modifiers: KeyModifiers::empty(), +}))] +fn handle_standard_events(#[case] event: Event) { + let mut module = Help::new_from_keybindings(&[]); + module.set_active(); + handle_event(&mut module, event); + assert!(module.is_active()); } #[test] -fn input_other() { - with_view_sender(|context| { - let mut module = Help::new_from_keybindings(&[]); - module.set_active(); - let _ = module.handle_event(Event::from('a'), &context.sender); - assert!(!module.is_active()); - }); +fn handle_other_key_event() { + let mut module = Help::new_from_keybindings(&[]); + module.set_active(); + handle_event(&mut module, Event::from('a')); + assert!(!module.is_active()); } diff --git a/src/core/src/modules/list/mod.rs b/src/core/src/modules/list/mod.rs index c71665d..9874b17 100644 --- a/src/core/src/modules/list/mod.rs +++ b/src/core/src/modules/list/mod.rs @@ -20,9 +20,7 @@ use crate::{ }; // TODO Remove `union` call when bitflags/bitflags#180 is resolved -const INPUT_OPTIONS: InputOptions = InputOptions::UNDO_REDO - .union(InputOptions::RESIZE) - .union(InputOptions::HELP); +const INPUT_OPTIONS: InputOptions = InputOptions::UNDO_REDO.union(InputOptions::RESIZE); #[derive(Debug, PartialEq)] enum ListState { @@ -91,9 +89,11 @@ impl Module for List { } fn read_event(&self, event: Event, key_bindings: &KeyBindings) -> Event { - select!(default || event, || self.read_event_help(event), || { - Some(self.read_event_default(event, key_bindings)) - }) + select!( + default || self.read_event_default(event, key_bindings), + || self.normal_mode_help.read_event(event, key_bindings), + || self.visual_mode_help.read_event(event, key_bindings) + ) } } @@ -382,10 +382,6 @@ impl List { } } - fn read_event_help(&self, event: Event) -> Option<Event> { - (self.visual_mode_help.is_active() || self.normal_mode_help.is_active()).then(|| event) - } - #[allow(clippy::cognitive_complexity)] fn read_event_default(&self, event: Event, key_bindings: &KeyBindings) -> Event { match self.state { diff --git a/src/core/src/modules/show_commit/mod.rs b/src/core/src/modules/show_commit/mod.rs index 5a11de5..077279c 100644 --- a/src/core/src/modules/show_commit/mod.rs +++ b/src/core/src/modules/show_commit/mod.rs @@ -25,9 +25,7 @@ use crate::{ }; // TODO Remove `union` call when bitflags/bitflags#180 is resolved -const INPUT_OPTIONS: InputOptions = InputOptions::UNDO_REDO - .union(InputOptions::MOVEMENT) - .union(InputOptions::HELP); +const INPUT_OPTIONS: InputOptions = InputOptions::UNDO_REDO.union(InputOptions::MOVEMENT); pub(crate) struct ShowCommit { commit_diff_loader_options: CommitDiffLoaderOptions, @@ -116,16 +114,20 @@ impl Module for ShowCommit { } fn input_options(&self) -> &InputOptions { - select!(default || &INPUT_OPTIONS, || self.help.input_options(),) + select!(default || &INPUT_OPTIONS, || self.help.input_options()) } fn read_event(&self, event: Event, key_bindings: &KeyBindings) -> Event { - if key_bindings.show_diff.contains(&event) { - Event::from(MetaEvent::ShowDiff) - } - else { - event - } + select!( + default || { + key_bindings + .show_diff + .contains(&event) + .then(|| Event::from(MetaEvent::ShowDiff)) + .unwrap_or(event) + }, + || { self.help.read_event(event, key_bindings) } + ) } fn handle_event(&mut self, event: Event, view_sender: &ViewSender, _: &mut TodoFile) -> ProcessResult { diff --git a/src/core/src/util.rs b/src/core/src/util.rs index c9c7ab8..382c375 100644 --- a/src/core/src/util.rs +++ b/src/core/src/util.rs @@ -1,5 +1,13 @@ #[macro_export] macro_rules! select { + (default $default: expr, $first: expr) => { + if let Some(value) = $first() { + value + } + else { + $default() + } + }; (default $default: expr, $first: expr, $($arg:expr),*) => { if let Some(value) = $first() { value @@ -12,3 +20,13 @@ macro_rules! select { } }; } + +#[macro_export] +macro_rules! first { + ($first: expr, $($arg:expr),*) => { + if $first().is_some() { + } + $(else if $arg().is_some() { + })* + }; +} diff --git a/src/input/src/event_handler.rs b/src/input/src/event_handler.rs index 3fd4cf1..2c7d163 100644 --- a/src/input/src/event_handler.rs +++ b/src/input/src/event_handler.rs @@ -40,10 +40,6 @@ impl EventHandler { } } - if input_options.contains(InputOptions::HELP) && self.key_bindings.help.contains(&event) { - return Event::from(MetaEvent::Help); - } - if input_options.contains(InputOptions::UNDO_REDO) { if let Some(evt) = Self::handle_undo_redo(&self.key_bindings, event) { return evt; @@ -133,7 +129,6 @@ mod tests { }), true)] #[case::resize(Event::Resize(100, 100), false)] #[case::movement(Event::from(KeyCode::Up), false)] - #[case::help(Event::from('?'), false)] #[case::undo_redo(Event::Key(KeyEvent { code: KeyCode::Char('z'), modifiers: KeyModifiers::CONTROL, @@ -158,7 +153,6 @@ mod tests { }), true)] #[case::resize(Event::Resize(100, 100), true)] #[case::movement(Event::from(KeyCode::Up), true)] - #[case::help(Event::from('?'), true)] #[case::undo_redo(Event::Key(KeyEvent { code: KeyCode::Char('z'), modifiers: KeyModifiers::CONTROL, diff --git a/src/input/src/input_options.rs b/src/input/src/input_options.rs index c34c904..cd18634 100644 --- a/src/input/src/input_options.rs +++ b/src/input/src/input_options.rs @@ -3,13 +3,11 @@ use bitflags::bitflags; bitflags! { /// Represents options for parsing input events. pub struct InputOptions: u8 { - /// Enable help input handling - const HELP = 0b0000_0001; /// Enable movement input handling - const MOVEMENT = 0b0000_0010; + const MOVEMENT = 0b0000_0001; /// Enable terminal resize input handling - const RESIZE = 0b0000_0100; + const RESIZE = 0b0000_0010; /// Enable undo and redo input handling - const UNDO_REDO = 0b0000_1000; + const UNDO_REDO = 0b0000_0100; } } |