mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 20:26:42 +01:00
command system; first for transport
This commit is contained in:
parent
2e96b10fad
commit
524e283075
4 changed files with 230 additions and 46 deletions
44
crates/tek_core/src/command.rs
Normal file
44
crates/tek_core/src/command.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@ use std::fmt::{Debug, Display};
|
|||
submod! {
|
||||
audio
|
||||
color
|
||||
command
|
||||
edn
|
||||
engine
|
||||
focus
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue