diff --git a/crates/tek/src/tui/phrase_editor.rs b/crates/tek/src/tui/phrase_editor.rs index 2667236d..01492997 100644 --- a/crates/tek/src/tui/phrase_editor.rs +++ b/crates/tek/src/tui/phrase_editor.rs @@ -27,19 +27,10 @@ pub enum PhraseCommand { SetTimeZoom(usize), SetTimeLock(bool), Show(Option>>), - ToggleDirection, -} - -impl InputToCommand for PhraseCommand { - fn input_to_command (state: &PhraseEditorModel, from: &TuiInput) -> Option { - return state.to_editor_command(from) - } } +event_map_input_to_command!(Tui: PhraseEditorModel: PhraseCommand: PhraseEditorModel::KEYS); impl PhraseEditorModel { - fn phrase_length (&self) -> usize { - self.phrase().as_ref().map(|p|p.read().unwrap().length).unwrap_or(1) - } const KEYS: [(TuiEvent, &'static dyn Fn(&Self)->PhraseCommand);31] = [ (kexp!(Ctrl-Alt-Up), &|s: &Self|SetNoteScroll(s.note_point() + 3)), (kexp!(Ctrl-Alt-Down), &|s: &Self|SetNoteScroll(s.note_point().saturating_sub(3))), @@ -72,12 +63,11 @@ impl PhraseEditorModel { (kexp!(Char('.')), &|s: &Self|SetNoteLength(Note::next(s.note_len()))), (kexp!(Char('<')), &|s: &Self|SetNoteLength(Note::prev(s.note_len()))), // TODO: 3plet (kexp!(Char('>')), &|s: &Self|SetNoteLength(Note::next(s.note_len()))), - //key_pat!(Char('`')) -> ToggleDirection, //// TODO: key_pat!(Char('/')) => // toggle 3plet //// TODO: key_pat!(Char('?')) => // toggle dotted ]; - fn to_editor_command (&self, from: &TuiInput) -> Option { - event_map!(Self::KEYS).handle(self, from.event()) + fn phrase_length (&self) -> usize { + self.phrase().as_ref().map(|p|p.read().unwrap().length).unwrap_or(1) } } diff --git a/crates/tek/src/tui/tui_input.rs b/crates/tek/src/tui/tui_input.rs index 5cce5e33..f7d24a10 100644 --- a/crates/tek/src/tui/tui_input.rs +++ b/crates/tek/src/tui/tui_input.rs @@ -84,6 +84,14 @@ impl<'a, const N: usize, E: PartialEq, T, U> EventMap<'a, N, E, T, U> { }; } +#[macro_export] macro_rules! event_map_input_to_command { + ($Engine:ty: $Model:ty: $Command:ty: $EventMap:expr) => { + input_to_command!($Command: <$Engine>|state: $Model, input|{ + event_map!($EventMap).handle(state, input.event())? + }); + } +} + #[macro_export] macro_rules! kexp { (Ctrl-Alt-$code:ident) => { key_event_expr!($code, KeyModifiers::from_bits(0b0000_0110).unwrap()) }; (Ctrl-$code:ident) => { key_event_expr!($code, KeyModifiers::CONTROL) };