mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
cmdsys: separate match_key_static
This commit is contained in:
parent
1f375219db
commit
a4925082ca
2 changed files with 66 additions and 30 deletions
|
|
@ -1,5 +1,9 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
|
pub trait Command<T>: Sized {
|
||||||
|
fn run (&self, state: &mut T) -> Perhaps<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn key (code: KeyCode) -> KeyEvent {
|
pub const fn key (code: KeyCode) -> KeyEvent {
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code,
|
code,
|
||||||
|
|
@ -21,13 +25,15 @@ pub const fn shift (key: KeyEvent) -> KeyEvent {
|
||||||
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::SHIFT), ..key }
|
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 {
|
pub trait HandleKey<C: Command<Self> + 'static>: Sized {
|
||||||
const HANDLE_KEY_MAP: &'static [(KeyEvent, C)]; // FIXME: needs to be method
|
const HANDLE_KEY_MAP: &'static [(KeyEvent, C)] = &[]; // FIXME: needs to be method
|
||||||
fn match_key (key: &KeyEvent) -> Option<&'static C> {
|
#[inline] fn match_input (from: &TuiInput) -> Option<&'static C> {
|
||||||
|
if let TuiEvent::Input(crossterm::event::Event::Key(key)) = from.event() {
|
||||||
|
return Self::match_key_static(&key)
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
#[inline] fn match_key_static (key: &KeyEvent) -> Option<&'static C> {
|
||||||
for (binding, command) in Self::HANDLE_KEY_MAP.iter() {
|
for (binding, command) in Self::HANDLE_KEY_MAP.iter() {
|
||||||
if key == binding {
|
if key == binding {
|
||||||
return Some(command);
|
return Some(command);
|
||||||
|
|
@ -35,8 +41,11 @@ pub trait HandleKey<C: Command<Self> + 'static>: Sized {
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
fn handle_key (&mut self, key: &KeyEvent) -> Perhaps<C> {
|
#[inline] fn match_key (&self, key: &KeyEvent) -> Option<&'static C> {
|
||||||
if let Some(command) = Self::match_key(key) {
|
Self::match_key_static(key)
|
||||||
|
}
|
||||||
|
#[inline] fn handle_key (&mut self, key: &KeyEvent) -> Perhaps<C> {
|
||||||
|
if let Some(command) = self.match_key(key) {
|
||||||
command.run(self)
|
command.run(self)
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
|
||||||
|
|
@ -155,7 +155,7 @@ impl Handle<Tui> for PhrasePool<Tui> {
|
||||||
if let TuiEvent::Input(crossterm::event::Event::Key(key)) = from.event() {
|
if let TuiEvent::Input(crossterm::event::Event::Key(key)) = from.event() {
|
||||||
match self.mode {
|
match self.mode {
|
||||||
Some(PhrasePoolMode::Rename(..)) => {
|
Some(PhrasePoolMode::Rename(..)) => {
|
||||||
if HandleKey::<PhraseNameCommand>::match_key(key).is_some() {
|
if HandleKey::<PhraseNameCommand>::match_key_static(key).is_some() {
|
||||||
let _undo = HandleKey::<PhraseNameCommand>::handle_key(self, key)?;
|
let _undo = HandleKey::<PhraseNameCommand>::handle_key(self, key)?;
|
||||||
return Ok(Some(true))
|
return Ok(Some(true))
|
||||||
} else if let KeyEvent { code: KeyCode::Char(c), .. } = key {
|
} else if let KeyEvent { code: KeyCode::Char(c), .. } = key {
|
||||||
|
|
@ -332,14 +332,26 @@ impl<E: Engine> Command<PhrasePool<E>> for PhraseLengthCommand {
|
||||||
focus.next()
|
focus.next()
|
||||||
},
|
},
|
||||||
Self::Increment => match focus {
|
Self::Increment => match focus {
|
||||||
PhraseLengthFocus::Bar => { *length += 4 * PPQ },
|
PhraseLengthFocus::Bar => {
|
||||||
PhraseLengthFocus::Beat => { *length += PPQ },
|
*length += 4 * PPQ
|
||||||
PhraseLengthFocus::Tick => { *length += 1 },
|
},
|
||||||
|
PhraseLengthFocus::Beat => {
|
||||||
|
*length += PPQ
|
||||||
|
},
|
||||||
|
PhraseLengthFocus::Tick => {
|
||||||
|
*length += 1
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Self::Decrement => match focus {
|
Self::Decrement => match focus {
|
||||||
PhraseLengthFocus::Bar => { *length = length.saturating_sub(4 * PPQ) },
|
PhraseLengthFocus::Bar => {
|
||||||
PhraseLengthFocus::Beat => { *length = length.saturating_sub(PPQ) },
|
*length = length.saturating_sub(4 * PPQ)
|
||||||
PhraseLengthFocus::Tick => { *length = length.saturating_sub(1) },
|
},
|
||||||
|
PhraseLengthFocus::Beat => {
|
||||||
|
*length = length.saturating_sub(PPQ)
|
||||||
|
},
|
||||||
|
PhraseLengthFocus::Tick => {
|
||||||
|
*length = length.saturating_sub(1)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Self::Cancel => {
|
Self::Cancel => {
|
||||||
state.mode = None;
|
state.mode = None;
|
||||||
|
|
@ -366,26 +378,41 @@ impl<E: Engine> Command<PhrasePool<E>> for PhraseLengthCommand {
|
||||||
impl<E: Engine> Command<PhraseEditor<E>> for PhraseEditorCommand {
|
impl<E: Engine> Command<PhraseEditor<E>> for PhraseEditorCommand {
|
||||||
fn run (&self, state: &mut PhraseEditor<E>) -> Perhaps<Self> {
|
fn run (&self, state: &mut PhraseEditor<E>) -> Perhaps<Self> {
|
||||||
match self {
|
match self {
|
||||||
Self::ToggleDirection => { state.mode = !state.mode; },
|
Self::ToggleDirection => {
|
||||||
Self::EnterEditMode => { state.entered = true; },
|
state.mode = !state.mode;
|
||||||
Self::ExitEditMode => { state.entered = false; },
|
},
|
||||||
|
Self::EnterEditMode => {
|
||||||
Self::TimeZoomOut => { state.time_zoom_out() },
|
state.entered = true;
|
||||||
Self::TimeZoomIn => { state.time_zoom_in() },
|
},
|
||||||
|
Self::ExitEditMode => {
|
||||||
Self::NoteLengthDecrement => { state.note_length_dec() },
|
state.entered = false;
|
||||||
Self::NoteLengthIncrement => { state.note_length_inc() },
|
},
|
||||||
Self::NotePageUp => { state.note_page_up() },
|
Self::TimeZoomOut => {
|
||||||
Self::NotePageDown => { state.note_page_down() },
|
state.time_zoom_out()
|
||||||
Self::NoteAppend => if state.entered {
|
},
|
||||||
|
Self::TimeZoomIn => {
|
||||||
|
state.time_zoom_in()
|
||||||
|
},
|
||||||
|
Self::NoteLengthDecrement => {
|
||||||
|
state.note_length_dec()
|
||||||
|
},
|
||||||
|
Self::NoteLengthIncrement => {
|
||||||
|
state.note_length_inc()
|
||||||
|
},
|
||||||
|
Self::NotePageUp => {
|
||||||
|
state.note_page_up()
|
||||||
|
},
|
||||||
|
Self::NotePageDown => {
|
||||||
|
state.note_page_down()
|
||||||
|
},
|
||||||
|
Self::NoteAppend => if state.entered {
|
||||||
state.put();
|
state.put();
|
||||||
state.time_cursor_advance();
|
state.time_cursor_advance();
|
||||||
},
|
},
|
||||||
Self::NoteSet => if state.entered {
|
Self::NoteSet => if state.entered {
|
||||||
state.put();
|
state.put();
|
||||||
},
|
},
|
||||||
|
Self::GoUp => match state.entered {
|
||||||
Self::GoUp => match state.entered {
|
|
||||||
true => state.note_cursor_inc(),
|
true => state.note_cursor_inc(),
|
||||||
false => state.note_scroll_inc(),
|
false => state.note_scroll_inc(),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue