From a4925082ca9b8c43310ad5c8baf73b6154c643b7 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 7 Nov 2024 01:18:53 +0100 Subject: [PATCH] cmdsys: separate match_key_static --- crates/tek_core/src/command.rs | 25 +++++--- crates/tek_sequencer/src/sequencer_cmd.rs | 71 ++++++++++++++++------- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/crates/tek_core/src/command.rs b/crates/tek_core/src/command.rs index a71c9d0d..4883ed34 100644 --- a/crates/tek_core/src/command.rs +++ b/crates/tek_core/src/command.rs @@ -1,5 +1,9 @@ use crate::*; +pub trait Command: Sized { + fn run (&self, state: &mut T) -> Perhaps; +} + pub const fn key (code: KeyCode) -> KeyEvent { KeyEvent { code, @@ -21,13 +25,15 @@ pub const fn shift (key: KeyEvent) -> KeyEvent { KeyEvent { modifiers: key.modifiers.union(KeyModifiers::SHIFT), ..key } } -pub trait Command: Sized { - fn run (&self, state: &mut T) -> Perhaps; -} - pub trait HandleKey + 'static>: Sized { - const HANDLE_KEY_MAP: &'static [(KeyEvent, C)]; // FIXME: needs to be method - fn match_key (key: &KeyEvent) -> Option<&'static C> { + const HANDLE_KEY_MAP: &'static [(KeyEvent, C)] = &[]; // FIXME: needs to be method + #[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() { if key == binding { return Some(command); @@ -35,8 +41,11 @@ pub trait HandleKey + 'static>: Sized { } None } - fn handle_key (&mut self, key: &KeyEvent) -> Perhaps { - if let Some(command) = Self::match_key(key) { + #[inline] fn match_key (&self, key: &KeyEvent) -> Option<&'static C> { + Self::match_key_static(key) + } + #[inline] fn handle_key (&mut self, key: &KeyEvent) -> Perhaps { + if let Some(command) = self.match_key(key) { command.run(self) } else { Ok(None) diff --git a/crates/tek_sequencer/src/sequencer_cmd.rs b/crates/tek_sequencer/src/sequencer_cmd.rs index 9b7fc686..609240e6 100644 --- a/crates/tek_sequencer/src/sequencer_cmd.rs +++ b/crates/tek_sequencer/src/sequencer_cmd.rs @@ -155,7 +155,7 @@ impl Handle for PhrasePool { if let TuiEvent::Input(crossterm::event::Event::Key(key)) = from.event() { match self.mode { Some(PhrasePoolMode::Rename(..)) => { - if HandleKey::::match_key(key).is_some() { + if HandleKey::::match_key_static(key).is_some() { let _undo = HandleKey::::handle_key(self, key)?; return Ok(Some(true)) } else if let KeyEvent { code: KeyCode::Char(c), .. } = key { @@ -332,14 +332,26 @@ impl Command> for PhraseLengthCommand { focus.next() }, Self::Increment => match focus { - PhraseLengthFocus::Bar => { *length += 4 * PPQ }, - PhraseLengthFocus::Beat => { *length += PPQ }, - PhraseLengthFocus::Tick => { *length += 1 }, + PhraseLengthFocus::Bar => { + *length += 4 * PPQ + }, + PhraseLengthFocus::Beat => { + *length += PPQ + }, + PhraseLengthFocus::Tick => { + *length += 1 + }, }, Self::Decrement => match focus { - PhraseLengthFocus::Bar => { *length = length.saturating_sub(4 * PPQ) }, - PhraseLengthFocus::Beat => { *length = length.saturating_sub(PPQ) }, - PhraseLengthFocus::Tick => { *length = length.saturating_sub(1) }, + PhraseLengthFocus::Bar => { + *length = length.saturating_sub(4 * PPQ) + }, + PhraseLengthFocus::Beat => { + *length = length.saturating_sub(PPQ) + }, + PhraseLengthFocus::Tick => { + *length = length.saturating_sub(1) + }, }, Self::Cancel => { state.mode = None; @@ -366,26 +378,41 @@ impl Command> for PhraseLengthCommand { impl Command> for PhraseEditorCommand { fn run (&self, state: &mut PhraseEditor) -> Perhaps { match self { - Self::ToggleDirection => { state.mode = !state.mode; }, - Self::EnterEditMode => { state.entered = true; }, - Self::ExitEditMode => { state.entered = false; }, - - Self::TimeZoomOut => { state.time_zoom_out() }, - 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 { + Self::ToggleDirection => { + state.mode = !state.mode; + }, + Self::EnterEditMode => { + state.entered = true; + }, + Self::ExitEditMode => { + state.entered = false; + }, + Self::TimeZoomOut => { + state.time_zoom_out() + }, + 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.time_cursor_advance(); }, - Self::NoteSet => if state.entered { + Self::NoteSet => if state.entered { state.put(); }, - - Self::GoUp => match state.entered { + Self::GoUp => match state.entered { true => state.note_cursor_inc(), false => state.note_scroll_inc(), },