command system; first for transport

This commit is contained in:
🪞👃🪞 2024-11-06 23:32:10 +01:00
parent 2e96b10fad
commit 524e283075
4 changed files with 230 additions and 46 deletions

View file

@ -0,0 +1,44 @@
use crate::*;
pub const fn key (code: KeyCode) -> KeyEvent {
KeyEvent {
code,
modifiers: KeyModifiers::NONE,
kind: crossterm::event::KeyEventKind::Press,
state: crossterm::event::KeyEventState::NONE
}
}
pub const fn ctrl (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::CONTROL), ..key }
}
pub const fn alt (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::ALT), ..key }
}
pub const fn shift (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::SHIFT), ..key }
}
pub trait Command<T>: Sized {
fn run (&self, state: &mut T) -> Perhaps<Self>;
}
pub trait HandleKey<C: Command<Self> + 'static>: Sized {
const HANDLE_KEY_MAP: &'static [(KeyEvent, C)];
fn handle_key (&mut self, key: &KeyEvent) -> Perhaps<C> {
let mut run_command: Option<&'static C> = None;
for (binding, command) in Self::HANDLE_KEY_MAP.iter() {
if key == binding {
run_command = Some(command);
break
}
}
if let Some(command) = run_command {
command.run(self)
} else {
Ok(None)
}
}
}

View file

@ -38,6 +38,7 @@ use std::fmt::{Debug, Display};
submod! {
audio
color
command
edn
engine
focus