implement Measure component

This commit is contained in:
🪞👃🪞 2024-10-31 21:53:49 +02:00
parent 4983523da6
commit 75c9a4ce49
3 changed files with 53 additions and 53 deletions

View file

@ -9,6 +9,7 @@ pub trait Coordinate: Send + Sync + Copy
+ Ord + PartialEq + Eq + Ord + PartialEq + Eq
+ Debug + Display + Default + Debug + Display + Default
+ From<u16> + Into<u16> + From<u16> + Into<u16>
+ Into<usize>
+ Into<f64> + Into<f64>
{ {
fn minus (self, other: Self) -> Self { fn minus (self, other: Self) -> Self {
@ -29,6 +30,7 @@ impl<T> Coordinate for T where
+ Ord + PartialEq + Eq + Ord + PartialEq + Eq
+ Debug + Display + Default + Debug + Display + Default
+ From<u16> + Into<u16> + From<u16> + Into<u16>
+ Into<usize>
+ Into<f64> + Into<f64>
{} {}
@ -87,12 +89,10 @@ pub trait Size<N: Coordinate> {
} }
} }
} }
impl<N: Coordinate> Size<N> for (N, N) { impl<N: Coordinate> Size<N> for (N, N) {
fn x (&self) -> N { self.0 } fn x (&self) -> N { self.0 }
fn y (&self) -> N { self.1 } fn y (&self) -> N { self.1 }
} }
impl<N: Coordinate> Size<N> for [N;2] { impl<N: Coordinate> Size<N> for [N;2] {
fn x (&self) -> N { self[0] } fn x (&self) -> N { self[0] }
fn y (&self) -> N { self[1] } fn y (&self) -> N { self[1] }
@ -880,6 +880,31 @@ impl<E: Engine, A: Widget<Engine = E>, B: Widget<Engine = E>> Widget for Split<E
} }
} }
/// A widget that tracks its render width and height
pub struct Measure<E: Engine>(PhantomData<E>, AtomicUsize, AtomicUsize);
impl<E: Engine> Measure<E> {
pub fn w (&self) -> usize { self.1.load(Ordering::Relaxed) }
pub fn h (&self) -> usize { self.2.load(Ordering::Relaxed) }
pub fn wh (&self) -> [usize;2] { [self.w(), self.h()] }
pub fn set_w (&self, w: impl Into<usize>) { self.1.store(w.into(), Ordering::Relaxed) }
pub fn set_h (&self, h: impl Into<usize>) { self.2.store(h.into(), Ordering::Relaxed) }
pub fn set_wh (&self, w: impl Into<usize>, h: impl Into<usize>) { self.set_w(w); self.set_h(h); }
pub fn new () -> Self { Self(PhantomData::default(), 0.into(), 0.into()) }
}
impl<E: Engine> Widget for Measure<E> {
type Engine = E;
fn layout (&self, _: E::Size) -> Perhaps<E::Size> {
Ok(Some([0u16.into(), 0u16.into()].into()))
}
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.set_w(to.area().w());
self.set_h(to.area().h());
Ok(())
}
}
/// A scrollable area. /// A scrollable area.
pub struct Scroll< pub struct Scroll<
E: Engine, E: Engine,

View file

@ -66,10 +66,8 @@ pub struct Arrangement<E: Engine> {
pub focused: bool, pub focused: bool,
/// Background color of arrangement /// Background color of arrangement
pub color: Color, pub color: Color,
/// Width of arrangement area at last render /// Width and height of arrangement area at last render
pub width: AtomicUsize, pub size: Measure<E>,
/// Height of arrangement area at last render
pub height: AtomicUsize,
} }
/// Represents a track in the arrangement /// Represents a track in the arrangement
pub struct ArrangementTrack<E: Engine> { pub struct ArrangementTrack<E: Engine> {
@ -238,8 +236,7 @@ impl<E: Engine> Arrangement<E> {
tracks: vec![], tracks: vec![],
focused: false, focused: false,
color: Color::Rgb(28, 35, 25), color: Color::Rgb(28, 35, 25),
width: 0.into(), size: Measure::new(),
height: 0.into(),
} }
} }
pub fn activate (&mut self) { pub fn activate (&mut self) {

View file

@ -146,30 +146,21 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
let rows: &[(usize, usize)] = rows.as_ref(); let rows: &[(usize, usize)] = rows.as_ref();
let cols: &[(usize, usize)] = cols.as_ref(); let cols: &[(usize, usize)] = cols.as_ref();
let any_size = |_|Ok(Some([0,0])); let any_size = |_|Ok(Some([0,0]));
// store render area // store render area
add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{ add(&self.0.size)?;
let [x, y, w, h] = to.area();
self.0.width.store(w as usize, Ordering::Relaxed);
self.0.height.store(h as usize, Ordering::Relaxed);
Ok(())
}))?;
// column separators // column separators
add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{ add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{
let area = to.area(); let area = to.area();
let style = Some(Style::default().fg(COLOR_SEPARATOR)); let style = Some(Style::default().fg(COLOR_SEPARATOR));
for x in cols.iter().map(|col|col.1) { Ok(for x in cols.iter().map(|col|col.1) {
let x = scene_title_w + area.x() + x as u16; let x = scene_title_w + area.x() + x as u16;
for y in area.y()..area.y2() { to.blit(&"", x, y, style); } for y in area.y()..area.y2() { to.blit(&"", x, y, style); }
} })
Ok(())
}))?; }))?;
// row separators // row separators
add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{ add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{
let area = to.area(); let area = to.area();
for y in rows.iter().map(|row|row.1) { Ok(for y in rows.iter().map(|row|row.1) {
let y = area.y() + (y / PPQ) as u16 + 1; let y = area.y() + (y / PPQ) as u16 + 1;
if y >= to.buffer.area.height { break } if y >= to.buffer.area.height { break }
for x in area.x()..area.x2().saturating_sub(2) { for x in area.x()..area.x2().saturating_sub(2) {
@ -179,10 +170,8 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
cell.underline_color = COLOR_SEPARATOR; cell.underline_color = COLOR_SEPARATOR;
} }
} }
} })
Ok(())
}))?; }))?;
// track titles // track titles
let header = row!((track, w) in tracks.iter().zip(cols.iter().map(|col|col.0))=>{ let header = row!((track, w) in tracks.iter().zip(cols.iter().map(|col|col.0))=>{
let name = track.name.read().unwrap(); let name = track.name.read().unwrap();
@ -211,13 +200,11 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
.bg(track.color) .bg(track.color)
.push_x(scene_title_w) .push_x(scene_title_w)
}); });
// scene titles // scene titles
let scene_name = |scene, playing: bool, height|row!( let scene_name = |scene, playing: bool, height|row!(
if playing { "" } else { " " }, if playing { "" } else { " " },
TuiStyle::bold((scene as &Scene).name.read().unwrap().as_str(), true), TuiStyle::bold((scene as &Scene).name.read().unwrap().as_str(), true),
).fixed_xy(scene_title_w, height); ).fixed_xy(scene_title_w, height);
// scene clips // scene clips
let scene_clip = |scene, track: usize, w: u16, h: u16|Layers::new(move |add|{ let scene_clip = |scene, track: usize, w: u16, h: u16|Layers::new(move |add|{
let mut color = clip_bg; let mut color = clip_bg;
@ -233,27 +220,22 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
}; };
add(&Background(color)) add(&Background(color))
}).fixed_xy(w, h); }).fixed_xy(w, h);
// tracks and scenes
add(&col!( add(&col!(header, col!(
header, // scenes:
col!( (scene, pulses) in scenes.iter().zip(rows.iter().map(|row|row.0)) => {
// scenes: let height = 1.max((pulses / PPQ) as u16);
(scene, pulses) in scenes.iter().zip(rows.iter().map(|row|row.0)) => { let playing = scene.is_playing(tracks);
let height = 1.max((pulses / PPQ) as u16); Stack::right(move |add| {
let playing = scene.is_playing(tracks); // scene title:
Stack::right(move |add| { add(&scene_name(scene, playing, height).bg(scene.color))?;
// scene title: // clip per track:
add(&scene_name(scene, playing, height).bg(scene.color))?; Ok(for (track, w) in cols.iter().map(|col|col.0).enumerate() {
// clip per track: add(&scene_clip(scene, track, w as u16, height))?;
for (track, w) in cols.iter().map(|col|col.0).enumerate() { })
add(&scene_clip(scene, track, w as u16, height))?; }).fixed_y(height)
} }
Ok(()) )))?;
}).fixed_y(height)
}
),
))?;
// cursor // cursor
add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{ add(&CustomWidget::new(any_size, move|to: &mut TuiOutput|{
let area = to.area(); let area = to.area();
@ -317,18 +299,14 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
} }
} }
Ok(()) Ok(())
}))?; }))
Ok(())
}).bg(bg).grow_y(1).border(border); }).bg(bg).grow_y(1).border(border);
let title_color = if self.0.focused { let title_color = if self.0.focused {
Color::Rgb(150, 160, 90) Color::Rgb(150, 160, 90)
} else { } else {
Color::Rgb(120, 130, 100) Color::Rgb(120, 130, 100)
}; };
let w = self.0.width.load(Ordering::Relaxed); let [w, h] = self.0.size.wh();
let h = self.0.height.load(Ordering::Relaxed);
let lower_right = TuiStyle::fg(format!("{w}x{h}"), title_color) let lower_right = TuiStyle::fg(format!("{w}x{h}"), title_color)
.pull_x(1).align_se().fill_xy(); .pull_x(1).align_se().fill_xy();
lay!(content, lower_right) lay!(content, lower_right)