diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index ef0b82a7..0f321ebd 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -23,7 +23,7 @@ pub trait Engine: Send + Sync + Sized { pub trait Input { type Event; fn event (&self) - -> Self::Event; + -> &Self::Event; fn is_done (&self) -> bool; fn done (&self); @@ -427,35 +427,6 @@ impl> Widget for Align { type Engine = E; fn layout (&self, outer_area: E::Size) -> Perhaps { self.inner().layout(outer_area) - //Ok(match self { - //Self::Center(_) => self.inner().layout(outer_area)?.map(|inner_area|{ - //let [_, _, w, h] = inner_area.xywh(); - //let offset_x = (outer_area.w() / 2.into()) - (w / 2.into()); - //let offset_y = (outer_area.h() / 2.into()) - (h / 2.into()); - //let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h]; - //result.into() - //}), - //Self::X(_) => self.inner().layout(outer_area)?.map(|inner_area|{ - //let [_, y, w, h] = inner_area.xywh(); - //let offset_x = (outer_area.w() - w) / 2.into(); - //let result = [outer_area.x() + offset_x, y, w, h]; - //result.into( - //}), - //Self::Y(_) => self.inner().layout(outer_area)?.map(|inner_area|{ - //let [x, _, w, h] = inner_area.xywh(); - //let offset_y = (outer_area.h() / 2.into()) - (h / 2.into()); - //let result = [x, outer_area.y() + offset_y, w, h]; - //result.into() - //}), - //Self::NW(_) => { todo!() }, - //Self::N(_) => { todo!() }, - //Self::NE(_) => { todo!() }, - //Self::W(_) => { todo!() }, - //Self::E(_) => { todo!() }, - //Self::SW(_) => { todo!() }, - //Self::S(_) => { todo!() }, - //Self::SE(_) => { todo!() }, - //}) } fn render (&self, to: &mut E::Output) -> Perhaps { let outer_area = to.area(); diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index 4a2631ca..5af2de17 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -24,7 +24,7 @@ impl Engine for Tui { type Unit = u16; type Size = [Self::Unit;2]; type Area = [Self::Unit;4]; - type Input = Self; + type Input = TuiInput; type Handled = bool; type Output = Self; fn exited (&self) -> bool { @@ -48,18 +48,6 @@ impl Engine for Tui { disable_raw_mode().map_err(Into::into) } } -impl Input for Tui { - type Event = TuiEvent; - fn event (&self) -> TuiEvent { - self.event.read().unwrap().clone().unwrap() - } - fn is_done (&self) -> bool { - self.exited.fetch_and(true, Ordering::Relaxed) - } - fn done (&self) { - self.exited.store(true, Ordering::Relaxed); - } -} impl Output for Tui { #[inline] fn area (&self) -> ::Area { self.area @@ -78,6 +66,22 @@ impl Output for Tui { Ok(next) } } +pub struct TuiInput { + event: TuiEvent, + exited: Arc, +} +impl Input for TuiInput { + type Event = TuiEvent; + fn event (&self) -> &TuiEvent { + &self.event + } + fn is_done (&self) -> bool { + self.exited.fetch_and(true, Ordering::Relaxed) + } + fn done (&self) { + self.exited.store(true, Ordering::Relaxed); + } +} impl Tui { /// Run the main loop. @@ -106,7 +110,6 @@ impl Tui { engine: &Arc>, state: &Arc>, poll: Duration ) -> JoinHandle<()> { let exited = engine.read().unwrap().exited.clone(); - let engine = engine.clone(); let state = state.clone(); spawn(move || loop { if exited.fetch_and(true, Ordering::Relaxed) { @@ -119,8 +122,10 @@ impl Tui { exited.store(true, Ordering::Relaxed); }, _ => { - *engine.write().unwrap().event.write().unwrap() = Some(event); - if let Err(e) = state.write().unwrap().handle(&*engine.read().unwrap()) { + if let Err(e) = state.write().unwrap().handle(&TuiInput { + event, + exited: exited.clone() + }) { panic!("{e}") } } diff --git a/crates/tek_mixer/src/mixer_handle.rs b/crates/tek_mixer/src/mixer_handle.rs index 425033ac..6099a84b 100644 --- a/crates/tek_mixer/src/mixer_handle.rs +++ b/crates/tek_mixer/src/mixer_handle.rs @@ -1,7 +1,7 @@ use crate::*; impl Handle for Mixer { - fn handle (&mut self, engine: &Tui) -> Perhaps { + fn handle (&mut self, engine: &TuiInput) -> Perhaps { if let TuiEvent::Input(crossterm::event::Event::Key(event)) = engine.event() { match event.code { diff --git a/crates/tek_mixer/src/plugin_handle.rs b/crates/tek_mixer/src/plugin_handle.rs index 38f326e7..4acc53a8 100644 --- a/crates/tek_mixer/src/plugin_handle.rs +++ b/crates/tek_mixer/src/plugin_handle.rs @@ -1,7 +1,7 @@ use crate::*; impl Handle for Plugin { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Up) => { self.selected = self.selected.saturating_sub(1); diff --git a/crates/tek_mixer/src/sample_add.rs b/crates/tek_mixer/src/sample_add.rs index b09a2b02..c984754d 100644 --- a/crates/tek_mixer/src/sample_add.rs +++ b/crates/tek_mixer/src/sample_add.rs @@ -72,7 +72,7 @@ impl Widget for AddSampleModal { } impl Handle for AddSampleModal { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { if handle_keymap(self, &from.event(), KEYMAP_ADD_SAMPLE)? { return Ok(Some(true)) } diff --git a/crates/tek_mixer/src/sampler_handle.rs b/crates/tek_mixer/src/sampler_handle.rs index 26d3b0a0..a3496479 100644 --- a/crates/tek_mixer/src/sampler_handle.rs +++ b/crates/tek_mixer/src/sampler_handle.rs @@ -1,6 +1,6 @@ use crate::*; impl Handle for Sampler { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Up) => { self.cursor.0 = if self.cursor.0 == 0 { diff --git a/crates/tek_mixer/src/track_handle.rs b/crates/tek_mixer/src/track_handle.rs index 062e7458..563670f1 100644 --- a/crates/tek_mixer/src/track_handle.rs +++ b/crates/tek_mixer/src/track_handle.rs @@ -1,7 +1,7 @@ use crate::*; impl Handle for Track { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { //, NONE, "chain_cursor_up", "move cursor up", || { key!(KeyCode::Up) => { diff --git a/crates/tek_sequencer/src/arranger.rs b/crates/tek_sequencer/src/arranger.rs index a34f8e54..2916bd7d 100644 --- a/crates/tek_sequencer/src/arranger.rs +++ b/crates/tek_sequencer/src/arranger.rs @@ -219,7 +219,7 @@ impl ArrangerFocus { } } impl Handle for Arranger { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { if let Some(modal) = self.modal.as_mut() { let result = modal.handle(from)?; if modal.exited() { @@ -893,7 +893,7 @@ impl Widget for ArrangerRenameModal { } impl Handle for ArrangerRenameModal { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { TuiEvent::Input(Event::Key(k)) => { match k.code { diff --git a/crates/tek_sequencer/src/arranger_main.rs b/crates/tek_sequencer/src/arranger_main.rs index 0695ca41..f68ff0f6 100644 --- a/crates/tek_sequencer/src/arranger_main.rs +++ b/crates/tek_sequencer/src/arranger_main.rs @@ -93,7 +93,7 @@ impl Content for ArrangerStandalone { } impl Handle for ArrangerStandalone { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Char(' ')) => { if let Some(ref mut transport) = self.transport { diff --git a/crates/tek_sequencer/src/sequencer.rs b/crates/tek_sequencer/src/sequencer.rs index 0be4bd45..d1bf6598 100644 --- a/crates/tek_sequencer/src/sequencer.rs +++ b/crates/tek_sequencer/src/sequencer.rs @@ -641,7 +641,7 @@ impl<'a> Widget for SequenceTimer<'a> { } impl Handle for Sequencer { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { // NONE, "seq_cursor_up", "move cursor up", |sequencer: &mut Sequencer| { key!(KeyCode::Up) => { diff --git a/crates/tek_sequencer/src/transport.rs b/crates/tek_sequencer/src/transport.rs index 75ab56fe..bffdd3cb 100644 --- a/crates/tek_sequencer/src/transport.rs +++ b/crates/tek_sequencer/src/transport.rs @@ -187,7 +187,7 @@ impl Audio for TransportToolbar { } } impl Handle for TransportToolbar { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Left) => { self.focus_prev(); @@ -249,7 +249,7 @@ impl Focusable for TransportPlayPauseButton { } } impl Handle for TransportPlayPauseButton { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Enter) => self.toggle().map(|_|Some(true)), _ => Ok(None) @@ -293,7 +293,7 @@ impl Focusable for TransportBPM { } } impl Handle for TransportBPM { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Char(',')) => { self.value += 1.0; @@ -355,7 +355,7 @@ impl Focusable for TransportQuantize { } } impl Handle for TransportQuantize { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Char(',')) => { self.value = prev_note_length(self.value); @@ -408,7 +408,7 @@ impl Focusable for TransportSync { } } impl Handle for TransportSync { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { key!(KeyCode::Char(',')) => { self.value = prev_note_length(self.value); @@ -464,7 +464,7 @@ impl Focusable for TransportClock { } } impl Handle for TransportClock { - fn handle (&mut self, from: &Tui) -> Perhaps { + fn handle (&mut self, from: &TuiInput) -> Perhaps { Ok(None) } }