cmdsys: HandleKey -> MatchInput

This commit is contained in:
🪞👃🪞 2024-11-08 19:10:24 +01:00
parent 1aaad23691
commit 2b163e9e27
7 changed files with 389 additions and 348 deletions

View file

@ -1,7 +1,7 @@
use crate::*;
#[derive(Clone, PartialEq)]
enum SequencerCommand {
pub enum SequencerCommand {
FocusNext,
FocusPrev,
FocusUp,
@ -66,156 +66,24 @@ pub enum PhraseEditorCommand {
GoLeft,
GoRight,
}
impl HandleKey<SequencerCommand> for Sequencer<Tui> {
fn match_input (&self, from: &TuiInput) -> Option<SequencerCommand> {
match from.event() {
key!(KeyCode::Tab) => Some(SequencerCommand::FocusNext),
key!(Shift-KeyCode::Tab) => Some(SequencerCommand::FocusPrev),
key!(KeyCode::BackTab) => Some(SequencerCommand::FocusPrev),
key!(Shift-KeyCode::BackTab) => Some(SequencerCommand::FocusPrev),
key!(KeyCode::Up) => Some(SequencerCommand::FocusUp),
key!(KeyCode::Down) => Some(SequencerCommand::FocusDown),
key!(KeyCode::Left) => Some(SequencerCommand::FocusLeft),
key!(KeyCode::Right) => Some(SequencerCommand::FocusRight),
key!(KeyCode::Char(' ')) => Some(SequencerCommand::Transport(
TransportCommand::TogglePlay)), // FIXME go through transport
_ => match self.focused() {
SequencerFocus::Transport => self.transport.as_ref()
.map(|t|t.read().unwrap().match_input(from).map(SequencerCommand::Transport))
.flatten(),
SequencerFocus::PhrasePool => self.phrases.read().unwrap()
.match_input(from)
.map(SequencerCommand::Phrases),
SequencerFocus::PhraseEditor => self.editor
.match_input(from)
.map(SequencerCommand::Editor),
}
}
}
}
impl HandleKey<PhrasePoolCommand> for PhrasePool<Tui> {
fn match_input (&self, from: &TuiInput) -> Option<PhrasePoolCommand> {
match from.event() {
key!(KeyCode::Up) => Some(PhrasePoolCommand::Prev),
key!(KeyCode::Down) => Some(PhrasePoolCommand::Next),
key!(KeyCode::Char(',')) => Some(PhrasePoolCommand::MoveUp),
key!(KeyCode::Char('.')) => Some(PhrasePoolCommand::MoveDown),
key!(KeyCode::Delete) => Some(PhrasePoolCommand::Delete),
key!(KeyCode::Char('a')) => Some(PhrasePoolCommand::Append),
key!(KeyCode::Char('i')) => Some(PhrasePoolCommand::Insert),
key!(KeyCode::Char('d')) => Some(PhrasePoolCommand::Duplicate),
key!(KeyCode::Char('c')) => Some(PhrasePoolCommand::RandomColor),
key!(KeyCode::Char('n')) => Some(PhrasePoolCommand::Rename(PhraseRenameCommand::Begin)),
key!(KeyCode::Char('t')) => Some(PhrasePoolCommand::Length(PhraseLengthCommand::Begin)),
_ => match self.mode {
Some(PhrasePoolMode::Rename(..)) => HandleKey::<PhraseRenameCommand>
::match_input(self, from).map(PhrasePoolCommand::Rename),
Some(PhrasePoolMode::Length(..)) => HandleKey::<PhraseLengthCommand>
::match_input(self, from).map(PhrasePoolCommand::Length),
_ => None
}
}
}
}
impl HandleKey<PhraseRenameCommand> for PhrasePool<Tui> {
fn match_input (&self, from: &TuiInput) -> Option<PhraseRenameCommand> {
match from.event() {
key!(KeyCode::Backspace) => Some(PhraseRenameCommand::Backspace),
key!(KeyCode::Enter) => Some(PhraseRenameCommand::Confirm),
key!(KeyCode::Esc) => Some(PhraseRenameCommand::Cancel),
key!(KeyCode::Char(c)) => Some(PhraseRenameCommand::Append(*c)),
_ => None
}
}
}
impl HandleKey<PhraseLengthCommand> for PhrasePool<Tui> {
fn match_input (&self, from: &TuiInput) -> Option<PhraseLengthCommand> {
match from.event() {
key!(KeyCode::Up) => Some(PhraseLengthCommand::Inc),
key!(KeyCode::Down) => Some(PhraseLengthCommand::Dec),
key!(KeyCode::Right) => Some(PhraseLengthCommand::Next),
key!(KeyCode::Left) => Some(PhraseLengthCommand::Prev),
key!(KeyCode::Enter) => Some(PhraseLengthCommand::Confirm),
key!(KeyCode::Esc) => Some(PhraseLengthCommand::Cancel),
_ => None
}
}
}
impl HandleKey<PhraseEditorCommand> for PhraseEditor<Tui> {
fn match_input (&self, from: &TuiInput) -> Option<PhraseEditorCommand> {
match from.event() {
key!(KeyCode::Char('`')) => Some(PhraseEditorCommand::ToggleDirection),
key!(KeyCode::Enter) => Some(PhraseEditorCommand::EnterEditMode),
key!(KeyCode::Esc) => Some(PhraseEditorCommand::ExitEditMode),
key!(KeyCode::Char('[')) => Some(PhraseEditorCommand::NoteLengthDec),
key!(KeyCode::Char(']')) => Some(PhraseEditorCommand::NoteLengthInc),
key!(KeyCode::Char('a')) => Some(PhraseEditorCommand::NoteAppend),
key!(KeyCode::Char('s')) => Some(PhraseEditorCommand::NoteSet),
key!(KeyCode::Char('-')) => Some(PhraseEditorCommand::TimeZoomOut),
key!(KeyCode::Char('_')) => Some(PhraseEditorCommand::TimeZoomOut),
key!(KeyCode::Char('=')) => Some(PhraseEditorCommand::TimeZoomIn),
key!(KeyCode::Char('+')) => Some(PhraseEditorCommand::TimeZoomIn),
key!(KeyCode::PageUp) => Some(PhraseEditorCommand::NotePageUp),
key!(KeyCode::PageDown) => Some(PhraseEditorCommand::NotePageDown),
key!(KeyCode::Up) => Some(PhraseEditorCommand::GoUp),
key!(KeyCode::Down) => Some(PhraseEditorCommand::GoDown),
key!(KeyCode::Left) => Some(PhraseEditorCommand::GoLeft),
key!(KeyCode::Right) => Some(PhraseEditorCommand::GoRight),
_ => None
}
}
}
/// Handle top-level events in standalone sequencer.
impl Handle<Tui> for Sequencer<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(command) = self.match_input(from) {
let _undo = command.run(self)?;
return Ok(Some(true))
}
Ok(None)
}
}
impl Handle<Tui> for PhrasePool<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(command) = HandleKey::<PhrasePoolCommand>::match_input(self, from) {
let _undo = command.run(self)?;
return Ok(Some(true))
}
Ok(None)
}
}
impl Handle<Tui> for PhraseEditor<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(command) = self.match_input(from) {
let _undo = command.run(self)?;
return Ok(Some(true))
}
Ok(None)
}
}
impl<E: Engine> Command<Sequencer<E>> for SequencerCommand {
fn run (&self, state: &mut Sequencer<E>) -> Perhaps<Self> {
use SequencerCommand::*;
match self {
Self::FocusNext => { state.focus_next(); },
Self::FocusPrev => { state.focus_prev(); },
Self::FocusUp => { state.focus_up(); },
Self::FocusDown => { state.focus_down(); },
Self::FocusLeft => { state.focus_left(); },
Self::FocusRight => { state.focus_right(); },
Self::Transport(command) => if let Some(ref transport) = state.transport {
return command
.run(&mut*transport.write().unwrap())
.map(|x|x.map(SequencerCommand::Transport))
FocusNext => { state.focus_next(); },
FocusPrev => { state.focus_prev(); },
FocusUp => { state.focus_up(); },
FocusDown => { state.focus_down(); },
FocusLeft => { state.focus_left(); },
FocusRight => { state.focus_right(); },
Transport(command) => if let Some(ref transport) = state.transport {
return command.run(&mut*transport.write().unwrap()).map(|x|x.map(Transport))
},
Self::Phrases(command) => {
return command
.run(&mut*state.phrases.write().unwrap())
.map(|x|x.map(SequencerCommand::Phrases))
Phrases(command) => {
return command.run(&mut*state.phrases.write().unwrap()).map(|x|x.map(Phrases))
},
Self::Editor(command) => {
return command
.run(&mut state.editor)
.map(|x|x.map(SequencerCommand::Editor))
Editor(command) => {
return command.run(&mut state.editor).map(|x|x.map(Editor))
},
}
Ok(None)