mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
88 lines
2.3 KiB
Rust
88 lines
2.3 KiB
Rust
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<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:tt) => {
|
|
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(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>]
|
|
}
|
|
}
|