use crate::*; /// Spawn thread that listens for user input pub fn input_thread ( exited: &Arc, device: &Arc> ) -> JoinHandle<()> { let poll = Duration::from_millis(100); let exited = exited.clone(); let device = device.clone(); spawn(move || loop { // Exit if flag is set if exited.fetch_and(true, Ordering::Relaxed) { break } // Listen for events and send them to the main thread if ::crossterm::event::poll(poll).is_ok() { let event = ::crossterm::event::read().unwrap(); if let Event::Key(KeyEvent { code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, .. }) = event { exited.store(true, Ordering::Relaxed); } else if let Err(e) = device.write().unwrap().handle(&AppEvent::Input(event)) { panic!("{e}") } } }) } /// 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:expr) => { 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(crossterm::event::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 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>] } }