wip: closure-based collections

This commit is contained in:
🪞👃🪞 2024-09-10 02:01:19 +03:00
parent 4c23aed40a
commit 1cbf8de4e4
4 changed files with 72 additions and 56 deletions

View file

@ -54,6 +54,20 @@ impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
pub struct Layers<'a, E: Engine, const N: usize>(pub [&'a dyn Widget<Engine = E>;N]);
pub struct Layers2<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
>(pub F, PhantomData<E>);
impl<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
> Layers2<E, F> {
pub fn new (build: F) -> Self {
Self(build, Default::default())
}
}
// this actually works, except for the type inference
//pub struct Layers<'a, E: Engine + 'a, I: std::iter::IntoIterator<Item = &'a dyn Render<E>>>(
//pub &'a I

View file

@ -79,6 +79,34 @@ impl<'a, const N: usize> Widget for Layers<'a, Tui, N> {
}
}
impl<F> Widget for Layers2<Tui, F>
where
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = Tui>)->Usually<()>)->Usually<()>
{
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
let [x, y, ..] = area;
let mut w = 0;
let mut h = 0;
(self.0)(&mut |layer| {
if let Some(layer_area) = layer.layout(area)? {
w = w.max(layer_area.w());
h = h.max(layer_area.h());
}
Ok(())
})?;
Ok(Some([x, y, w, h]))
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
self.layout(to.area())?
.map(|area|(self.0)(&mut |layer| {
layer.render(to.with_rect(area))?;
Ok(())
}).map(|_|area))
.transpose()
}
}
impl<T: Widget<Engine = Tui>> Widget for Align<T> {
type Engine = Tui;
fn layout (&self, outer_area: [u16;4]) -> Perhaps<[u16;4]> {

View file

@ -14,51 +14,24 @@ impl Content for TransportToolbar<Tui> {
}
}
impl Widget for TransportPlayPauseButton<Tui> {
impl Content for TransportPlayPauseButton<Tui> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
area.expect_min(10, 1)?;
Ok(Some([area.x(), area.y(), 10, 1]))
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let Self { value, focused, .. } = &self;
Layers([
&focused.then_some(CORNERS),
&Inset::W(1, Styled(match value {
fn content (&self) -> impl Widget<Engine = Tui> {
Layers2::new(|add|{
//add(&self.focused.then_some(CORNERS))?;
add(&Styled(match self.value {
Some(TransportState::Stopped) => Some(GRAY_DIM.bold()),
Some(TransportState::Starting) => Some(GRAY_NOT_DIM_BOLD),
Some(TransportState::Rolling) => Some(WHITE_NOT_DIM_BOLD),
_ => unreachable!(),
}, match value {
}, match self.value {
Some(TransportState::Rolling) => "▶ PLAYING",
Some(TransportState::Starting) => "READY ...",
Some(TransportState::Stopped) => "⏹ STOPPED",
_ => unreachable!(),
}))
]).render(to)
//let area = to.area();
//let [x, y, ..] = area;
//let Self { value, focused } = &self;
//let style = Some(match value {
//Some(TransportState::Stopped) => GRAY_DIM.bold(),
//Some(TransportState::Starting) => GRAY_NOT_DIM_BOLD,
//Some(TransportState::Rolling) => WHITE_NOT_DIM_BOLD,
//_ => unreachable!(),
//});
//let label = match value {
//Some(TransportState::Rolling) => "▶ PLAYING",
//Some(TransportState::Starting) => "READY ...",
//Some(TransportState::Stopped) => "⏹ STOPPED",
//_ => unreachable!(),
//};
//let area = to.blit(&label, x + 1, y, style)?.unwrap();
//let area = [area.x(), area.y(), area.w() + 1, area.h() + 1];
//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(())
})
}
}

View file

@ -13,32 +13,33 @@ pub struct Demo<E: Engine> {
impl Demo<Tui> {
fn new () -> Self {
let mut items: Vec<Box<dyn Widget<Engine = Tui>>> = vec![];
items.push(Box::new(tek_sequencer::TransportPlayPauseButton {
Self {
index: 0,
items: vec![
Box::new(tek_sequencer::TransportPlayPauseButton {
_engine: Default::default(),
value: Some(TransportState::Stopped),
focused: true
}));
items.push(Box::new(tek_sequencer::TransportPlayPauseButton {
}),
Box::new(tek_sequencer::TransportPlayPauseButton {
_engine: Default::default(),
value: Some(TransportState::Rolling),
focused: false
}));
Self { index: 0, items }
}),
]
}
}
}
impl Content for Demo<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
Align::Center(Layers([
&Outset::WH(2, 1, FillBg(Color::Rgb(0,128,128))),
&self.items[self.index]
//&Layers(&[
//&"---------",
//&Align::Center("...")
//])
]))
Layers2::new(|add|{
add(&Align::Center("FOOBAR"))?;
add(&Align::Center("FOO"))?;
Ok(())
})
//Align::Center(&self.items[self.index] as &dyn Widget<Engine = Tui>)
}
}