From cd0b8a68123937b60531cf0a74f5acd9bfce0e5f Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 28 Sep 2024 19:51:10 +0300 Subject: [PATCH] implement fixed Split --- crates/tek_core/src/lib.rs | 6 ++-- crates/tek_core/src/space.rs | 39 +++++++++++++++++++++++ crates/tek_mixer/src/sampler.rs | 2 +- crates/tek_sequencer/src/main_arranger.rs | 18 ++++------- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/crates/tek_core/src/lib.rs b/crates/tek_core/src/lib.rs index cb1f541e..9e88c347 100644 --- a/crates/tek_core/src/lib.rs +++ b/crates/tek_core/src/lib.rs @@ -62,7 +62,8 @@ pub trait Number: Send + Sync + Copy + Div + Ord + PartialEq + Eq + Debug + Display + Default - + From + + From + Into + + Into { fn minus (self, other: Self) -> Self { if self >= other { @@ -81,5 +82,6 @@ impl Number for T where + Div + Ord + PartialEq + Eq + Debug + Display + Default - + From + + From + Into + + Into {} diff --git a/crates/tek_core/src/space.rs b/crates/tek_core/src/space.rs index ce17cada..bce8d9bf 100644 --- a/crates/tek_core/src/space.rs +++ b/crates/tek_core/src/space.rs @@ -92,6 +92,15 @@ pub trait Area: Copy { #[inline] fn clip (&self, wh: impl Size) -> [N;4] { [self.x(), self.y(), wh.w(), wh.h()] } + #[inline] fn split_fixed (&self, direction: Direction, a: N) -> ([N;4],[N;4]) { + match direction { + Direction::Down => ( + [self.x(), self.y(), self.w(), a], + [self.x(), self.y() + a, self.w(), self.h() - a], + ), + _ => todo!(), + } + } } impl Area for (N, N, N, N) { @@ -829,3 +838,33 @@ where #[macro_export] macro_rules! row { ($($expr:expr),* $(,)?) => { Stack::right(move|add|{ $(add(&$expr)?;)* Ok(()) }) } } + +/// A binary split with fixed proportion +pub struct Split, B: Widget>( + pub Direction, pub E::Unit, A, B, PhantomData +); + +impl, B: Widget> Split { + pub fn new (direction: Direction, proportion: E::Unit, a: A, b: B) -> Self { + Self(direction, proportion, a, b, Default::default()) + } +} + +impl, B: Widget> Widget for Split { + type Engine = E; + fn layout (&self, to: E::Size) -> Perhaps { + Ok(Some(to)) + } + fn render (&self, to: &mut E::Output) -> Usually<()> { + let (a, b) = to.area().split_fixed(self.0, self.1); + to.render_in(a.into(), &self.2)?; + to.render_in(b.into(), &self.3)?; + Ok(()) + } +} + +/// A scrollable area. +pub struct Scroll< + E: Engine, + F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> +>(pub F, pub Direction, pub u64, PhantomData); diff --git a/crates/tek_mixer/src/sampler.rs b/crates/tek_mixer/src/sampler.rs index d784dbbc..6a0879c9 100644 --- a/crates/tek_mixer/src/sampler.rs +++ b/crates/tek_mixer/src/sampler.rs @@ -393,7 +393,7 @@ impl Widget for AddSampleModal { impl Handle for AddSampleModal { fn handle (&mut self, from: &TuiInput) -> Perhaps { - if handle_keymap(self, &from.event(), KEYMAP_ADD_SAMPLE)? { + if from.handle_keymap(self, KEYMAP_ADD_SAMPLE)? { return Ok(Some(true)) } Ok(Some(true)) diff --git a/crates/tek_sequencer/src/main_arranger.rs b/crates/tek_sequencer/src/main_arranger.rs index c3c0c47c..111e65c8 100644 --- a/crates/tek_sequencer/src/main_arranger.rs +++ b/crates/tek_sequencer/src/main_arranger.rs @@ -72,30 +72,24 @@ struct ArrangerStandalone { impl Content for ArrangerStandalone { type Engine = Tui; fn content (&self) -> impl Widget { - Layers::new(|add|{ + Layers::new(move|add|{ add(&Stack::down(move|add|{ add(&(&self.transport as &dyn Widget).debug())?; if let (Some(direction), Some(sequencer)) = ( self.show_sequencer, self.arranger.sequencer(), ) { - add(&Stack::new(direction, move|add|{ - add(&(&self.arranger as &dyn Widget) - .shrink_y(30) - .debug())?; - add(&(sequencer as &dyn Widget) - .min_y(20) - .debug())?; - Ok(()) - })) + let arranger = &self.arranger as &dyn Widget; + let sequencer = sequencer as &dyn Widget; + add(&Split::new(direction, 40, 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)?; + //add(modal as &dyn Widget)?; } Ok(()) })