use crate::core::*; /// Trait for things that handle input events. pub trait Handle { /// Handle an input event. /// Returns Ok(true) if the device handled the event. /// This is the mechanism which allows nesting of components;. fn handle (&mut self, _e: &AppEvent) -> Usually { Ok(false) } } /// Implement the `Handle` trait. #[macro_export] macro_rules! handle { ($T:ty) => { impl Handle for $T {} }; ($T:ty |$self:ident, $e:ident|$block:tt) => { impl Handle for $T { fn handle (&mut $self, $e: &AppEvent) -> Usually { $block } } }; ($T:ty = $handle:path) => { impl Handle for $T { fn handle (&mut self, e: &AppEvent) -> Usually { $handle(self, e) } } } } #[derive(Debug)] pub enum AppEvent { /// Terminal input Input(::crossterm::event::Event), /// Update values but not the whole form. Update, /// Update the whole form. Redraw, /// Device gains focus Focus, /// Device loses focus Blur, /// JACK notification Jack(JackEvent) } pub type KeyHandler = &'static dyn Fn(&mut T)->Usually; pub type KeyBinding = ( KeyCode, KeyModifiers, &'static str, &'static str, KeyHandler ); pub type KeyMap = [KeyBinding]; pub fn handle_keymap ( state: &mut T, event: &AppEvent, keymap: &KeyMap, ) -> Usually { match event { AppEvent::Input(Event::Key(event)) => { for (code, modifiers, _, _, command) in keymap.iter() { if *code == event.code && modifiers.bits() == event.modifiers.bits() { return command(state) } } }, _ => {} }; Ok(false) } /// Define a key binding. #[macro_export] macro_rules! key { ($k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr) => { (KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f) }; } /// Define a keymap. #[macro_export] macro_rules! keymap { ($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* $(,)? }) => { &[ $((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),* ] as &'static [KeyBinding<$T>] } }