impl Content instead of Widget for transport

This commit is contained in:
🪞👃🪞 2024-09-15 18:52:23 +03:00
parent 56db9cfce1
commit 2733b6b947

View file

@ -315,28 +315,21 @@ impl Handle<Tui> for TransportBPM<Tui> {
}
}
}
impl Widget for TransportBPM<Tui> {
impl Content for TransportBPM<Tui> {
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<Engine = Tui> {
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];
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 {
let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ];
CORNERS.draw(to)?;
to.fill_bg(area, COLOR_BG1);
add(&CORNERS)?;
add(&Background(COLOR_BG1))?;
}
//Ok(Some(area))
Ok(())
})
}
}
@ -370,27 +363,21 @@ impl Handle<Tui> for TransportQuantize<Tui> {
}
}
}
impl Widget for TransportQuantize<Tui> {
impl Content for TransportQuantize<Tui> {
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<Engine = Tui> {
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];
Layers::new(move|add|{
add(&Outset::X(1u16, Split::down(|add|{
add(&"QUANT")?;
add(&ppq_to_name(*value as usize))
})))?;
if *focused {
let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ];
CORNERS.draw(to)?;
to.fill_bg(area, COLOR_BG1);
add(&CORNERS)?;
add(&Background(COLOR_BG1))?;
}
//Ok(Some(area))
Ok(())
})
}
}
@ -424,27 +411,21 @@ impl Handle<Tui> for TransportSync<Tui> {
}
}
}
impl Widget for TransportSync<Tui> {
impl Content for TransportSync<Tui> {
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<Engine = Tui> {
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];
Layers::new(move|add|{
add(&Outset::X(1u16, Split::down(|add|{
add(&"SYNC")?;
add(&ppq_to_name(*value as usize))
})))?;
if *focused {
let area = [area.x() - 1, area.y(), area.w() - 1, area.h() ];
CORNERS.draw(to)?;
to.fill_bg(area, COLOR_BG1);
add(&CORNERS)?;
add(&Background(COLOR_BG1))?;
}
//Ok(Some(area))
Ok(())
})
}
}
@ -471,32 +452,25 @@ impl Handle<Tui> for TransportClock<Tui> {
Ok(None)
}
}
impl Widget for TransportClock<Tui> {
impl Content for TransportClock<Tui> {
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<Engine = Tui> {
let Self { frame: _frame, pulse, ppq, usecs, focused, .. } = self;
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);
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()];
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 {
let area = [area.x() - 1, area.y(), area.w(), area.h() ];
CORNERS.draw(to)?;
to.fill_bg(area, COLOR_BG1);
add(&CORNERS)?;
add(&Background(COLOR_BG1))?;
}
//Ok(Some(area))
Ok(())
})
}
}