From 2733b6b9474c93573e703aa9f07dfaa5d4391ffe Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 15 Sep 2024 18:52:23 +0300 Subject: [PATCH] impl Content instead of Widget for transport --- crates/tek_sequencer/src/transport.rs | 138 +++++++++++--------------- 1 file changed, 56 insertions(+), 82 deletions(-) diff --git a/crates/tek_sequencer/src/transport.rs b/crates/tek_sequencer/src/transport.rs index 6e1d083d..08697ccf 100644 --- a/crates/tek_sequencer/src/transport.rs +++ b/crates/tek_sequencer/src/transport.rs @@ -315,28 +315,21 @@ impl Handle for TransportBPM { } } } -impl Widget for TransportBPM { +impl Content for TransportBPM { type Engine = Tui; - fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { - area.expect_min(10, 1)?; - Ok(Some([10, 1])) - } - fn render (&self, to: &mut TuiOutput) -> Usually<()> { - let area = to.area(); - let [x, y, ..] = area; + fn content (&self) -> impl Widget { let Self { value, focused, .. } = self; - to.blit(&"BPM", x, y, Some(NOT_DIM)); - let bpm = format!("{}.{:03}", value, (value * 1000.0) % 1000.0); - to.blit(&bpm, x, y + 1, Some(NOT_DIM_BOLD)); - let width = bpm.len() as u16; - let area = [x, y, (width + 2).max(10), 2]; - if *focused { - let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ]; - CORNERS.draw(to)?; - to.fill_bg(area, COLOR_BG1); - } - //Ok(Some(area)) - Ok(()) + Layers::new(move|add|{ + add(&Outset::X(1u16, Split::down(|add|{ + add(&"BPM")?; + add(&format!("{}.{:03}", value, (value * 1000.0) % 1000.0).as_str()) + })))?; + if *focused { + add(&CORNERS)?; + add(&Background(COLOR_BG1))?; + } + Ok(()) + }) } } @@ -370,27 +363,21 @@ impl Handle for TransportQuantize { } } } -impl Widget for TransportQuantize { +impl Content for TransportQuantize { type Engine = Tui; - fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { - area.expect_min(10, 1)?; - Ok(Some([10, 1])) - } - fn render (&self, to: &mut TuiOutput) -> Usually<()> { - let [x, y, ..] = to.area(); + fn content (&self) -> impl Widget { let Self { value, focused, .. } = self; - to.blit(&"QUANT", x, y, Some(NOT_DIM)); - let name = ppq_to_name(*value as usize); - let width = name.len() as u16; - to.blit(&name, x, y + 1, Some(NOT_DIM_BOLD)); - let area = [x, y, (width + 2).max(10), 2]; - if *focused { - let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ]; - CORNERS.draw(to)?; - to.fill_bg(area, COLOR_BG1); - } - //Ok(Some(area)) - Ok(()) + 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(()) + }) } } @@ -424,27 +411,21 @@ impl Handle for TransportSync { } } } -impl Widget for TransportSync { +impl Content for TransportSync { type Engine = Tui; - fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { - area.expect_min(10, 1)?; - Ok(Some([10, 1])) - } - fn render (&self, to: &mut TuiOutput) -> Usually<()> { - let [x, y, ..] = to.area(); + fn content (&self) -> impl Widget { let Self { value, focused, .. } = self; - to.blit(&"SYNC", x, y, Some(NOT_DIM)); - let name = ppq_to_name(*value as usize); - let width = name.len() as u16; - to.blit(&name, x, y + 1, Some(NOT_DIM_BOLD)); - let area = [x, y, (width + 2).max(10), 2]; - if *focused { - let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ]; - CORNERS.draw(to)?; - to.fill_bg(area, COLOR_BG1); - } - //Ok(Some(area)) - Ok(()) + 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(()) + }) } } @@ -471,32 +452,25 @@ impl Handle for TransportClock { Ok(None) } } -impl Widget for TransportClock { +impl Content for TransportClock { type Engine = Tui; - fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { - area.expect_min(20, 1)?; - Ok(Some([20, 1])) - } - fn render (&self, to: &mut TuiOutput) -> Usually<()> { - let [x, y, width, _] = to.area(); + fn content (&self) -> impl Widget { let Self { frame: _frame, pulse, ppq, usecs, focused, .. } = self; - let (beats, pulses) = if *ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) }; - let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1); - let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000); - let (minutes, seconds) = (seconds / 60, seconds % 60); - let timer = format!("{bars}.{beats}.{pulses:02}"); - to.blit(&timer, x + width - timer.len() as u16 - 1, y + 0, Some(NOT_DIM)); - let timer = format!("{minutes}:{seconds:02}:{msecs:03}"); - to.blit(&timer, x + width - timer.len() as u16 - 1, y + 1, Some(NOT_DIM)); - let area = to.area(); - let area = [area.x(), area.y(), area.w() + 1, area.h()]; - if *focused { - let area = [area.x() - 1, area.y(), area.w(), area.h() ]; - CORNERS.draw(to)?; - to.fill_bg(area, COLOR_BG1); - } - //Ok(Some(area)) - Ok(()) + Layers::new(move|add|{ + let (beats, pulses) = if *ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) }; + let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1); + let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000); + let (minutes, seconds) = (seconds / 60, seconds % 60); + 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(()) + }) } }