mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
108 lines
3.1 KiB
Rust
108 lines
3.1 KiB
Rust
use crate::*;
|
|
|
|
/// Spawn thread that listens for user input
|
|
pub fn input_thread (
|
|
exited: &Arc<AtomicBool>,
|
|
device: &Arc<RwLock<impl Handle + Send + Sync + 'static>>
|
|
) -> 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<bool> {
|
|
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<bool> {
|
|
$block
|
|
}
|
|
}
|
|
};
|
|
($T:ty = $handle:path) => {
|
|
impl Handle for $T {
|
|
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
|
$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<T> = &'static dyn Fn(&mut T)->Usually<bool>;
|
|
|
|
pub type KeyBinding<T> = (
|
|
KeyCode, KeyModifiers, &'static str, &'static str, KeyHandler<T>
|
|
);
|
|
|
|
pub type KeyMap<T> = [KeyBinding<T>];
|
|
|
|
pub fn handle_keymap <T> (
|
|
state: &mut T, event: &AppEvent, keymap: &KeyMap<T>,
|
|
) -> Usually<bool> {
|
|
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>]
|
|
}
|
|
}
|