diff --git a/crates/tek_sequencer/src/arranger.rs b/crates/tek_sequencer/src/arranger.rs index 57d5fa17..c56b30a6 100644 --- a/crates/tek_sequencer/src/arranger.rs +++ b/crates/tek_sequencer/src/arranger.rs @@ -1,7 +1,23 @@ use crate::*; -/// Represents the tracks and scenes of the composition. +/// Root level object for standalone `tek_arranger` pub struct Arranger { + /// Controls the JACK transport. + pub transport: Option>>>, + /// Contains all the sequencers. + pub arrangement: Arrangement, + /// This allows the sequencer view to be moved or hidden. + pub show_sequencer: Option, + /// Index of currently focused component + pub focus: usize, + /// Focus target that passes events down to sequencer + pub sequencer_proxy: SequencerProxy, + /// Slot for modal dialog displayed on top of app. + pub modal: Option>>, +} + +/// Represents the tracks and scenes of the composition. +pub struct Arrangement { /// Name of arranger pub name: Arc>, /// Collection of tracks. @@ -12,13 +28,11 @@ pub struct Arranger { pub selected: ArrangerFocus, /// Display mode of arranger pub mode: ArrangerViewMode, - /// Slot for modal dialog displayed on top of app. - pub modal: Option>>, /// Whether the arranger is currently focused pub focused: bool } -impl Arranger { +impl Arrangement { pub fn new (name: &str) -> Self { Self { name: Arc::new(RwLock::new(name.into())), @@ -26,7 +40,6 @@ impl Arranger { selected: ArrangerFocus::Clip(0, 0), scenes: vec![], tracks: vec![], - modal: None, focused: false } } @@ -345,7 +358,7 @@ impl ArrangerViewMode { /////////////////////////////////////////////////////////////////////////////////////////////////// pub struct VerticalArranger<'a, E: Engine>( - pub &'a Arranger, pub usize + pub &'a Arrangement, pub usize ); pub struct VerticalArrangerGrid<'a>( pub u16, pub &'a [(usize, usize)], pub &'a [(usize, usize)] @@ -357,7 +370,7 @@ pub struct VerticalArrangerCursor<'a>( /////////////////////////////////////////////////////////////////////////////////////////////////// pub struct HorizontalArranger<'a, E: Engine>( - pub &'a Arranger + pub &'a Arrangement ); /////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/crates/tek_sequencer/src/arranger_cli.rs b/crates/tek_sequencer/src/arranger_cli.rs index 169a5e6d..ecd2da44 100644 --- a/crates/tek_sequencer/src/arranger_cli.rs +++ b/crates/tek_sequencer/src/arranger_cli.rs @@ -1,6 +1,5 @@ include!("lib.rs"); use tek_core::clap::{self, Parser}; - pub fn main () -> Usually<()> { ArrangerCli::parse().run() } /// Parses CLI arguments to the `tek_arranger` invocation. @@ -27,14 +26,14 @@ impl ArrangerCli { let jack = JackClient::Inactive( Client::new("tek_arranger", ClientOptions::NO_START_SERVER)?.0 ); - let mut arranger = Arranger::new(""); + let mut arrangement = Arrangement::new(""); let jack_transport = jack.transport(); let mut transport = TransportToolbar::new(Some(jack_transport)); if let Some(name) = self.name.as_ref() { - *arranger.name.write().unwrap() = name.clone(); + *arrangement.name.write().unwrap() = name.clone(); } for _ in 0..self.tracks { - let track = arranger.track_add(None)?; + let track = arrangement.track_add(None)?; for _ in 0..self.scenes { track.phrases.push( Arc::new(RwLock::new(Phrase::new("", true, PPQ * 4, None))) @@ -42,7 +41,7 @@ impl ArrangerCli { } } for _ in 0..self.scenes { - let _scene = arranger.scene_add(None)?; + let _scene = arrangement.scene_add(None)?; //for i in 0..self.tracks { //scene.clips[i] = Some(i); //} @@ -57,137 +56,14 @@ impl ArrangerCli { } )? ); - Tui::run(Arc::new(RwLock::new(ArrangerStandalone { + Tui::run(Arc::new(RwLock::new(Arranger { transport: self.transport.then_some(transport), show_sequencer: Some(tek_core::Direction::Down), - arranger, + arrangement, focus: 0, - sequencer_proxy: SequencerProxy(Default::default(), false) + sequencer_proxy: SequencerProxy(Default::default(), false), + modal: None })))?; Ok(()) } } - -/// Root level object for standalone `tek_arranger` -struct ArrangerStandalone { - /// Controls the JACK transport. - transport: Option>>>, - /// Contains all the sequencers. - arranger: Arranger, - /// This allows the sequencer view to be moved or hidden. - show_sequencer: Option, - /// Index of currently focused component - focus: usize, - /// Focus target that passes events down to sequencer - sequencer_proxy: SequencerProxy, -} - -/// The standalone arranger consists of transport, clip grid, and sequencer. -impl Content for ArrangerStandalone { - type Engine = Tui; - fn content (&self) -> impl Widget { - Layers::new(move|add|{ - add(&Stack::down(move|add|{ - add(&self.transport)?; - if let (Some(direction), Some(sequencer)) = ( - self.show_sequencer, - self.arranger.sequencer(), - ) { - let arranger = &self.arranger as &dyn Widget; - let sequencer = sequencer as &dyn Widget; - add(&Split::new(direction, 20, arranger, sequencer.min_y(20))) - } else { - add(&self.arranger) - } - }))?; - if let Some(ref modal) = self.arranger.modal { - add(&Background(COLOR_BG1))?; - add(&Foreground(COLOR_BG2))?; - //add(modal as &dyn Widget)?; - } - Ok(()) - }) - } -} - -/// Handle top-level events in standalone arranger. -impl Handle for ArrangerStandalone { - fn handle (&mut self, from: &TuiInput) -> Perhaps { - let focus = self.focus; - let is_first_row = self.arranger.is_first_row(); - let is_last_row = self.arranger.is_last_row(); - let mut focused_handle = || { - if focus == 2 { - self.arranger.sequencer_mut().handle(from) - } else { - self.focused_mut().handle(from) - } - }; - match from.event() { - key!(KeyCode::Char(' ')) => { - if let Some(ref mut transport) = self.transport { - transport.write().unwrap().toggle_play()?; - } else { - return Ok(None) - } - }, - key!(KeyCode::Tab) => { - self.focus_next(); - }, - key!(KeyCode::BackTab) => { - self.focus_prev(); - }, - key!(KeyCode::Down) => { - if focus == 0 { - self.focus_next(); - } else if focus == 1 && is_last_row { - self.focus_next(); - } else { - return focused_handle() - } - }, - key!(KeyCode::Up) => { - if focus == 1 && is_first_row { - self.focus_prev(); - } else { - return focused_handle() - } - }, - _ => return focused_handle() - } - Ok(Some(true)) - } -} - -/// Focusable items in standalone arranger. -impl Focus<3, Tui> for ArrangerStandalone { - fn focus (&self) -> usize { - self.focus - } - fn focus_mut (&mut self) -> &mut usize { - &mut self.focus - } - fn focusable (&self) -> [&dyn Focusable;3] { - focusables!(self.transport, self.arranger, self.sequencer_proxy) - } - fn focusable_mut (&mut self) -> [&mut dyn Focusable;3] { - focusables_mut!(self.transport, self.arranger, self.sequencer_proxy) - } -} - -#[derive(Default)] -struct SequencerProxy(PhantomData, bool); - -impl Handle for SequencerProxy { - fn handle (&mut self, _: &TuiInput) -> Perhaps { unreachable!() } -} - -impl Content for SequencerProxy { - type Engine = Tui; - fn content (&self) -> impl Widget { "" } -} - -impl Focusable for SequencerProxy { - fn is_focused (&self) -> bool { self.1 } - fn set_focused (&mut self, focus: bool) { self.1 = focus } -} diff --git a/crates/tek_sequencer/src/arranger_tui.rs b/crates/tek_sequencer/src/arranger_tui.rs index 654e431b..315ab570 100644 --- a/crates/tek_sequencer/src/arranger_tui.rs +++ b/crates/tek_sequencer/src/arranger_tui.rs @@ -1,26 +1,34 @@ use crate::*; -impl Arranger { - pub fn rename_selected (&mut self) { - self.modal = Some(Box::new(ArrangerRenameModal::new( - self.selected, - &match self.selected { - ArrangerFocus::Mix => self.name.clone(), - ArrangerFocus::Track(t) => self.tracks[t].name.clone(), - ArrangerFocus::Scene(s) => self.scenes[s].name.clone(), - ArrangerFocus::Clip(t, s) => self.tracks[t].phrases[s].read().unwrap().name.clone(), +/// The standalone arranger consists of transport, clip grid, and sequencer. +impl Content for Arranger { + type Engine = Tui; + fn content (&self) -> impl Widget { + Layers::new(move|add|{ + add(&Stack::down(move|add|{ + add(&self.transport)?; + if let (Some(direction), Some(sequencer)) = ( + self.show_sequencer, + self.arrangement.sequencer(), + ) { + let arrangement = &self.arrangement as &dyn Widget; + let sequencer = sequencer as &dyn Widget; + add(&Split::new(direction, 20, arrangement, sequencer.min_y(20))) + } else { + add(&self.arrangement) + } + }))?; + if let Some(ref modal) = self.modal { + add(&Background(COLOR_BG1))?; + add(&Foreground(COLOR_BG2))?; + //add(modal as &dyn Widget)?; } - ))); - } -} -impl Focusable for Arranger { - fn is_focused (&self) -> bool { - self.focused - } - fn set_focused (&mut self, focused: bool) { - self.focused = focused + Ok(()) + }) } } + +/// Handle top-level events in standalone arranger. impl Handle for Arranger { fn handle (&mut self, from: &TuiInput) -> Perhaps { if let Some(modal) = self.modal.as_mut() { @@ -30,6 +38,93 @@ impl Handle for Arranger { } return Ok(result) } + let focus = self.focus; + let is_first_row = self.arrangement.is_first_row(); + let is_last_row = self.arrangement.is_last_row(); + let mut focused_handle = || { + if focus == 2 { + self.arrangement.sequencer_mut().handle(from) + } else { + self.focused_mut().handle(from) + } + }; + match from.event() { + key!(KeyCode::Char(' ')) => { + if let Some(ref mut transport) = self.transport { + transport.write().unwrap().toggle_play()?; + } else { + return Ok(None) + } + }, + key!(KeyCode::Tab) => { + self.focus_next(); + }, + key!(KeyCode::BackTab) => { + self.focus_prev(); + }, + key!(KeyCode::Down) => { + if focus == 0 { + self.focus_next(); + } else if focus == 1 && is_last_row { + self.focus_next(); + } else { + return focused_handle() + } + }, + key!(KeyCode::Up) => { + if focus == 1 && is_first_row { + self.focus_prev(); + } else { + return focused_handle() + } + }, + _ => return focused_handle() + } + Ok(Some(true)) + } +} + +/// Focusable items in standalone arranger. +impl Focus<3, Tui> for Arranger { + fn focus (&self) -> usize { + self.focus + } + fn focus_mut (&mut self) -> &mut usize { + &mut self.focus + } + fn focusable (&self) -> [&dyn Focusable;3] { + focusables!(self.transport, self.arrangement, self.sequencer_proxy) + } + fn focusable_mut (&mut self) -> [&mut dyn Focusable;3] { + focusables_mut!(self.transport, self.arrangement, self.sequencer_proxy) + } +} + +impl Arranger { + pub fn rename_selected (&mut self) { + self.modal = Some(Box::new(ArrangerRenameModal::new( + self.arrangement.selected, + &match self.arrangement.selected { + ArrangerFocus::Mix => self.arrangement.name.clone(), + ArrangerFocus::Track(t) => self.arrangement.tracks[t].name.clone(), + ArrangerFocus::Scene(s) => self.arrangement.scenes[s].name.clone(), + ArrangerFocus::Clip(t, s) => self.arrangement.tracks[t].phrases[s].read().unwrap().name.clone(), + } + ))); + } +} + +impl Focusable for Arrangement { + fn is_focused (&self) -> bool { + self.focused + } + fn set_focused (&mut self, focused: bool) { + self.focused = focused + } +} + +impl Handle for Arrangement { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { // mode_switch: switch the display mode key!(KeyCode::Char('`')) => { @@ -95,9 +190,9 @@ impl Handle for Arranger { self.track_add(None)?; }, // rename: add a new scene - key!(KeyCode::Char('n')) => { - self.rename_selected(); - }, + //key!(KeyCode::Char('n')) => { + //self.rename_selected(); + //}, // length: add a new scene key!(KeyCode::Char('l')) => if let Some(phrase) = self.phrase() { phrase.write().unwrap().toggle_loop() @@ -111,7 +206,8 @@ impl Handle for Arranger { Ok(Some(true)) } } -impl Content for Arranger { + +impl Content for Arrangement { type Engine = Tui; fn content (&self) -> impl Widget { Layers::new(move |add|{ @@ -341,7 +437,7 @@ impl<'a> Widget for VerticalArrangerCursor<'a> { impl<'a> Content for HorizontalArranger<'a, Tui> { type Engine = Tui; fn content (&self) -> impl Widget { - let Arranger { tracks, focused, selected, scenes, .. } = self.0; + let Arrangement { tracks, focused, selected, scenes, .. } = self.0; let _tracks = tracks.as_slice(); lay!( focused.then_some(Background(Color::Rgb(40, 50, 30))), @@ -510,7 +606,7 @@ impl<'a> Content for HorizontalArranger<'a, Tui> { CustomWidget::new(|_|{ todo!() }, |to: &mut TuiOutput|{ - let Arranger { tracks, scenes, selected, .. } = self.0; + let Arrangement { tracks, scenes, selected, .. } = self.0; let area = to.area(); let mut x2 = 0; let [x, y, _, height] = area; diff --git a/crates/tek_sequencer/src/sequencer.rs b/crates/tek_sequencer/src/sequencer.rs index 51acedf6..feda3e01 100644 --- a/crates/tek_sequencer/src/sequencer.rs +++ b/crates/tek_sequencer/src/sequencer.rs @@ -412,3 +412,20 @@ pub fn write_midi_output (writer: &mut MidiWriter, output: &MIDIChunk, frames: u } } } + +#[derive(Default)] +pub struct SequencerProxy(pub PhantomData, pub bool); + +impl Handle for SequencerProxy { + fn handle (&mut self, _: &TuiInput) -> Perhaps { unreachable!() } +} + +impl Content for SequencerProxy { + type Engine = Tui; + fn content (&self) -> impl Widget { "" } +} + +impl Focusable for SequencerProxy { + fn is_focused (&self) -> bool { self.1 } + fn set_focused (&mut self, focus: bool) { self.1 = focus } +} diff --git a/crates/tek_sequencer/src/sequencer_cli.rs b/crates/tek_sequencer/src/sequencer_cli.rs index 3f50b6ab..5fb513ae 100644 --- a/crates/tek_sequencer/src/sequencer_cli.rs +++ b/crates/tek_sequencer/src/sequencer_cli.rs @@ -1,8 +1,7 @@ //! Phrase editor. include!("lib.rs"); -pub fn main () -> Usually<()> { SequencerCli::parse().run() } - use tek_core::clap::{self, Parser}; +pub fn main () -> Usually<()> { SequencerCli::parse().run() } #[derive(Debug, Parser)] #[command(version, about, long_about = None)]