keymap macros

This commit is contained in:
🪞👃🪞 2024-06-18 04:37:58 +03:00
parent d39cce271f
commit a50e022ab6
8 changed files with 233 additions and 199 deletions

View file

@ -85,3 +85,40 @@ pub type BoxedNotificationHandler = Box<dyn Fn(AppEvent) + Send>;
pub type BoxedProcessHandler = Box<dyn FnMut(&Client, &ProcessScope)-> Control + Send>;
pub type Jack<N> = AsyncClient<N, ClosureProcessHandler<BoxedProcessHandler>>;
pub type KeyBinding<T> = (
KeyCode, KeyModifiers, &'static str, &'static str, &'static dyn Fn(&mut T)
);
#[macro_export] macro_rules! key {
($k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: ident) => {
(KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f)
};
}
#[macro_export] macro_rules! keymap {
($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: ident]),* }) => {
&[
$((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as &'static dyn Fn(&mut $T))),*
] as &'static [KeyBinding<$T>]
}
}
pub use crate::{key, keymap};
pub fn handle_keymap <T> (
commands: &[KeyBinding<T>], state: &mut T, event: &AppEvent
) -> Result<(), Box<dyn Error>> {
match event {
AppEvent::Input(Event::Key(event)) => {
for (code, modifiers, _, _, command) in commands.iter() {
if *code == event.code && modifiers.bits() == event.modifiers.bits() {
command(state);
break
}
}
},
_ => {}
};
Ok(())
}