From d577449b726fb4ee5411d4a1bb200b1c4e07a66d Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 17 Sep 2024 00:16:16 +0300 Subject: [PATCH] handle transport focus in parent --- crates/tek_sequencer/src/transport.rs | 89 ++++++++++----------------- 1 file changed, 32 insertions(+), 57 deletions(-) diff --git a/crates/tek_sequencer/src/transport.rs b/crates/tek_sequencer/src/transport.rs index d231a41b..3181d453 100644 --- a/crates/tek_sequencer/src/transport.rs +++ b/crates/tek_sequencer/src/transport.rs @@ -155,27 +155,29 @@ impl Audio for TransportToolbar { impl Handle for TransportToolbar { fn handle (&mut self, from: &TuiInput) -> Perhaps { match from.event() { - key!(KeyCode::Left) => { - self.focus_prev(); - Ok(Some(true)) - }, - key!(KeyCode::Right) => { - self.focus_next(); - Ok(Some(true)) - }, - _ => self.focused_mut().handle(from) + key!(KeyCode::Left) => { self.focus_prev(); }, + key!(KeyCode::Right) => { self.focus_next(); }, + _ => return self.focused_mut().handle(from) } + Ok(Some(true)) } } impl Content for TransportToolbar { type Engine = Tui; fn content (&self) -> impl Widget { Split::right(|add|{ - add(&self.playing)?; - add(&self.bpm)?; - add(&self.quant)?; - add(&self.sync)?; - add(&self.clock)?; + let focus_wrap = |focused, component|Layers::new(move |add|{ + if focused { + add(&CORNERS)?; + add(&Background(COLOR_BG1))?; + } + add(component) + }); + add(&focus_wrap(self.focused && self.playing.focused, &self.playing))?; + add(&focus_wrap(self.focused && self.bpm.focused, &self.bpm))?; + add(&focus_wrap(self.focused && self.quant.focused, &self.quant))?; + add(&focus_wrap(self.focused && self.sync.focused, &self.sync))?; + add(&focus_wrap(self.focused && self.clock.focused, &self.clock))?; Ok(()) }) } @@ -260,7 +262,6 @@ impl Content for TransportPlayPauseButton { type Engine = Tui; fn content (&self) -> impl Widget { Layers::new(|add|{ - add(&self.focused.then_some(CORNERS))?; add(&Plus::X(1, Min::Y(2, Styled(match self.value { Some(TransportState::Stopped) => Some(GRAY_DIM.bold()), Some(TransportState::Starting) => Some(GRAY_NOT_DIM_BOLD), @@ -307,18 +308,11 @@ impl Handle for TransportBPM { impl Content for TransportBPM { type Engine = Tui; fn content (&self) -> impl Widget { - let Self { value, focused, .. } = self; - Layers::new(move|add|{ - add(&Outset::X(1u16, Split::down(|add|{ - add(&"BPM")?; - add(&format!("{}.{:03}", *value as usize, (value * 1000.0) % 1000.0).as_str()) - })))?; - if *focused { - add(&CORNERS)?; - add(&Background(COLOR_BG1))?; - } - Ok(()) - }) + let Self { value, .. } = self; + Outset::X(1u16, Split::down(move |add|{ + add(&"BPM")?; + add(&format!("{}.{:03}", *value as usize, (value * 1000.0) % 1000.0).as_str()) + })) } } @@ -355,18 +349,11 @@ impl Handle for TransportQuantize { impl Content for TransportQuantize { type Engine = Tui; fn content (&self) -> impl Widget { - let Self { value, focused, .. } = self; - Layers::new(move|add|{ - add(&Outset::X(1u16, Split::down(|add|{ - add(&"QUANT")?; - add(&ppq_to_name(*value as usize)) - })))?; - if *focused { - add(&CORNERS)?; - add(&Background(COLOR_BG1))?; - } - Ok(()) - }) + let Self { value, .. } = self; + Outset::X(1u16, Split::down(|add|{ + add(&"QUANT")?; + add(&ppq_to_name(*value as usize)) + })) } } @@ -403,18 +390,11 @@ impl Handle for TransportSync { impl Content for TransportSync { type Engine = Tui; fn content (&self) -> impl Widget { - let Self { value, focused, .. } = self; - Layers::new(move|add|{ - add(&Outset::X(1u16, Split::down(|add|{ - add(&"SYNC")?; - add(&ppq_to_name(*value as usize)) - })))?; - if *focused { - add(&CORNERS)?; - add(&Background(COLOR_BG1))?; - } - Ok(()) - }) + let Self { value, .. } = self; + Outset::X(1u16, Split::down(|add|{ + add(&"SYNC")?; + add(&ppq_to_name(*value as usize)) + })) } } @@ -453,12 +433,7 @@ impl Content for TransportClock { add(&Outset::X(1u16, Split::down(|add|{ add(&format!("{bars}.{beats}.{pulses:02}").as_str())?; add(&format!("{minutes}:{seconds:02}:{msecs:03}").as_str()) - })))?; - if *focused { - add(&CORNERS)?; - add(&Background(COLOR_BG1))?; - } - Ok(()) + }))) }) } }