From 8b5931c321ec783563f961b80b17949fdfa89dee Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 14 Nov 2024 22:46:14 +0100 Subject: [PATCH] wip: refactor pt.31: 41 errors --- crates/tek_tui/src/tui_arranger.rs | 59 ++++++------- crates/tek_tui/src/tui_phrase.rs | 2 +- crates/tek_tui/src/tui_sequencer.rs | 126 +++++++++++++++------------- 3 files changed, 98 insertions(+), 89 deletions(-) diff --git a/crates/tek_tui/src/tui_arranger.rs b/crates/tek_tui/src/tui_arranger.rs index b2ef67b9..ab7e19aa 100644 --- a/crates/tek_tui/src/tui_arranger.rs +++ b/crates/tek_tui/src/tui_arranger.rs @@ -64,7 +64,7 @@ impl InputToCommand> for ArrangerAppCommand { key!(KeyCode::Char(' ')) => { Self::App(Transport(TransportViewCommand::Transport(TransportCommand::Play(None)))) }, - _ => match view.focused() { + _ => Self::App(match view.focused() { Content(ArrangerViewFocus::Transport) => Transport( TransportViewCommand::input_to_command(&view.sequencer.transport, input)? ), @@ -194,44 +194,17 @@ impl InputToCommand> for ArrangerAppCommand { } } } - } + }) }) } } impl Command> for ArrangerAppCommand { fn execute (self, state: &mut ArrangerApp) -> Perhaps { + use AppViewCommand::*; let undo = match self { - Self::Focus(cmd) => { - delegate(cmd, Self::Focus, state) - }, - Self::App(cmd) => match cmd { - ArrangerViewCommand::Phrases(cmd) => { - delegate(cmd, |x|Self::App(ArrangerViewCommand::Phrases(x)), &mut state.app) - }, - ArrangerViewCommand::Editor(cmd) => { - delegate(cmd, |x|Self::App(ArrangerViewCommand::Editor(x)), &mut state.app) - }, - ArrangerViewCommand::Transport(cmd) => { - delegate(cmd, |x|Self::App(ArrangerViewCommand::Transport(x)), &mut state.app) - }, - ArrangerViewCommand::Zoom(zoom) => { - todo!(); - }, - ArrangerViewCommand::Select(selected) => { - state.selected = selected; - Ok(None) - }, - ArrangerViewCommand::Edit(command) => { - return Ok(command.execute(&mut state.model)?.map(ArrangerViewCommand::Edit)) - }, - ArrangerViewCommand::EditPhrase(phrase) => { - app.sequencer.editor.phrase = phrase.clone(); - state.focus(ArrangerViewFocus::PhraseEditor); - state.focus_enter(); - Ok(None) - } - }, + Focus(cmd) => { delegate(cmd, Focus, state) }, + App(cmd) => { delegate(cmd, App, state) } _ => {todo!()} }?; state.show_phrase(); @@ -240,6 +213,28 @@ impl Command> for ArrangerAppCommand { } } +impl Command> for ArrangerViewCommand { + fn execute (self, state: &mut ArrangerApp) -> Perhaps { + use ArrangerViewCommand::*; + match self { + Phrases(cmd) => { delegate(cmd, Phrases, &mut state.app) }, + Editor(cmd) => { delegate(cmd, Editor, &mut state.app) }, + Transport(cmd) => { delegate(cmd, Transport, &mut state.app) }, + Zoom(zoom) => { todo!(); }, + Select(selected) => { state.selected = selected; Ok(None) }, + Edit(command) => { + return Ok(command.execute(&mut state.model)?.map(ArrangerViewCommand::Edit)) + }, + EditPhrase(phrase) => { + state.sequencer.editor.phrase = phrase.clone(); + state.focus(ArrangerViewFocus::PhraseEditor); + state.focus_enter(); + Ok(None) + } + } + } +} + /// Root view for standalone `tek_arranger` pub struct ArrangerView { pub model: ArrangerModel, diff --git a/crates/tek_tui/src/tui_phrase.rs b/crates/tek_tui/src/tui_phrase.rs index efa57cea..c746bb50 100644 --- a/crates/tek_tui/src/tui_phrase.rs +++ b/crates/tek_tui/src/tui_phrase.rs @@ -352,7 +352,7 @@ const NTH_OCTAVE: [&'static str; 11] = [ "-2", "-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", ]; -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Debug)] pub enum PhraseEditorCommand { // TODO: 1-9 seek markers that by default start every 8th of the phrase ToggleDirection, diff --git a/crates/tek_tui/src/tui_sequencer.rs b/crates/tek_tui/src/tui_sequencer.rs index 0f7f806c..4c6f5713 100644 --- a/crates/tek_tui/src/tui_sequencer.rs +++ b/crates/tek_tui/src/tui_sequencer.rs @@ -1,5 +1,22 @@ use crate::*; +impl TryFrom<&Arc>> for SequencerApp { + type Error = Box; + fn try_from (jack: &Arc>) -> Usually { + let clock = Arc::new(Clock::from(Instant::default())); + Ok(Self::new(SequencerModel { + phrases: vec![], + player: MIDIPlayer::new(jack, &clock, "preview")?, + transport: TransportModel { + metronome: false, + transport: jack.read().unwrap().transport(), + jack: jack.clone(), + clock: clock.clone() + }, + }.into(), None, None)) + } +} + pub type SequencerApp = AppView< E, SequencerView, @@ -7,13 +24,52 @@ pub type SequencerApp = AppView< SequencerStatusBar, >; -#[derive(Clone, PartialEq)] +impl Handle for SequencerApp { + fn handle (&mut self, i: &TuiInput) -> Perhaps { + SequencerAppCommand::execute_with_state(self, i) + } +} + +pub type SequencerAppCommand = AppViewCommand; + +#[derive(Clone, Debug, PartialEq)] pub enum SequencerViewCommand { Transport(TransportCommand), Phrases(PhrasePoolViewCommand), Editor(PhraseEditorCommand), } +impl InputToCommand> for SequencerAppCommand { + fn input_to_command (state: &SequencerApp, input: &TuiInput) -> Option { + use AppViewFocus::*; + use FocusCommand::*; + use SequencerViewCommand::*; + match input.event() { + key!(KeyCode::Tab) => Some(Self::Focus(Next)), + key!(Shift-KeyCode::Tab) => Some(Self::Focus(Prev)), + key!(KeyCode::BackTab) => Some(Self::Focus(Prev)), + key!(Shift-KeyCode::BackTab) => Some(Self::Focus(Prev)), + key!(KeyCode::Up) => Some(Self::Focus(Up)), + key!(KeyCode::Down) => Some(Self::Focus(Down)), + key!(KeyCode::Left) => Some(Self::Focus(Left)), + key!(KeyCode::Right) => Some(Self::Focus(Right)), + _ => Some(Self::App(match state.focused() { + Content(SequencerFocus::Transport) => + TransportCommand::input_to_command(&state.transport, input) + .map(Transport), + Content(SequencerFocus::PhrasePool) => + PhrasePoolViewCommand::input_to_command(&state.phrases, input) + .map(Phrases), + Content(SequencerFocus::PhraseEditor) => + PhraseEditorCommand::input_to_command(&state.editor, input) + .map(Editor), + _ => return None, + })) + } + } +} + + /// Root view for standalone `tek_sequencer`. pub struct SequencerView { pub model: SequencerModel, @@ -46,23 +102,6 @@ pub enum SequencerStatusBar { PhraseEditor, } -impl TryFrom<&Arc>> for SequencerApp { - type Error = Box; - fn try_from (jack: &Arc>) -> Usually { - let clock = Arc::new(Clock::from(Instant::default())); - Ok(Self::new(SequencerModel { - phrases: vec![], - player: MIDIPlayer::new(jack, &clock, "preview")?, - transport: TransportModel { - metronome: false, - transport: jack.read().unwrap().transport(), - jack: jack.clone(), - clock: clock.clone() - }, - }.into(), None, None)) - } -} - impl From for SequencerView { fn from (model: SequencerModel) -> Self { Self { @@ -88,7 +127,7 @@ impl Content for SequencerView { } } -impl Audio for SequencerView { +impl Audio for SequencerView { fn process (&mut self, client: &Client, scope: &ProcessScope) -> Control { self.model.process(client, scope) } @@ -112,48 +151,23 @@ impl Content for SequencerStatusBar { } } -impl Handle for SequencerApp { - fn handle (&mut self, i: &TuiInput) -> Perhaps { - SequencerViewCommand::execute_with_state(self, i) - } -} - -impl Command> for SequencerViewCommand { - fn execute (self, state: &mut SequencerApp) -> Perhaps { +impl Command> for SequencerAppCommand { + fn execute (self, state: &mut SequencerApp) -> Perhaps { + use AppViewCommand::*; match self { - Self::Focus(cmd) => delegate(cmd, Self::Focus, state), - Self::Phrases(cmd) => delegate(cmd, Self::Phrases, &mut state.phrases), - Self::Editor(cmd) => delegate(cmd, Self::Editor, &mut state.editor), - Self::Transport(cmd) => delegate(cmd, Self::Transport, &mut state.transport) + Focus(cmd) => delegate(cmd, Focus, state), + App(cmd) => delegate(cmd, App, state), } } } -impl InputToCommand> for SequencerViewCommand { - fn input_to_command (state: &SequencerApp, input: &TuiInput) -> Option { - use AppViewFocus::*; - use FocusCommand::*; - match input.event() { - key!(KeyCode::Tab) => Some(Self::Focus(Next)), - key!(Shift-KeyCode::Tab) => Some(Self::Focus(Prev)), - key!(KeyCode::BackTab) => Some(Self::Focus(Prev)), - key!(Shift-KeyCode::BackTab) => Some(Self::Focus(Prev)), - key!(KeyCode::Up) => Some(Self::Focus(Up)), - key!(KeyCode::Down) => Some(Self::Focus(Down)), - key!(KeyCode::Left) => Some(Self::Focus(Left)), - key!(KeyCode::Right) => Some(Self::Focus(Right)), - _ => match state.focused() { - Content(SequencerFocus::Transport) => - TransportViewCommand::input_to_command(&state.transport, input) - .map(Self::Transport), - Content(SequencerFocus::PhrasePool) => - PhrasePoolViewCommand::input_to_command(&state.phrases, input) - .map(Self::Phrases), - Content(SequencerFocus::PhraseEditor) => - PhraseEditorCommand::input_to_command(&state.editor, input) - .map(Self::Editor), - _ => None, - } +impl Command> for SequencerViewCommand { + fn execute (self, state: &mut SequencerApp) -> Perhaps { + use SequencerViewCommand::*; + match self { + Phrases(cmd) => delegate(cmd, Phrases, &mut state.phrases), + Editor(cmd) => delegate(cmd, Editor, &mut state.editor), + Transport(cmd) => delegate(cmd, Transport, &mut state.transport) } } }