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 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 // 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 struct Layers<'a, E: Engine + 'a, I: std::iter::IntoIterator<Item = &'a dyn Render<E>>>(
//pub &'a I //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> { impl<T: Widget<Engine = Tui>> Widget for Align<T> {
type Engine = Tui; type Engine = Tui;
fn layout (&self, outer_area: [u16;4]) -> Perhaps<[u16;4]> { 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; type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> { fn content (&self) -> impl Widget<Engine = Tui> {
area.expect_min(10, 1)?; Layers2::new(|add|{
Ok(Some([area.x(), area.y(), 10, 1])) //add(&self.focused.then_some(CORNERS))?;
} add(&Styled(match self.value {
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 {
Some(TransportState::Stopped) => Some(GRAY_DIM.bold()), Some(TransportState::Stopped) => Some(GRAY_DIM.bold()),
Some(TransportState::Starting) => Some(GRAY_NOT_DIM_BOLD), Some(TransportState::Starting) => Some(GRAY_NOT_DIM_BOLD),
Some(TransportState::Rolling) => Some(WHITE_NOT_DIM_BOLD), Some(TransportState::Rolling) => Some(WHITE_NOT_DIM_BOLD),
_ => unreachable!(), _ => unreachable!(),
}, match value { }, match self.value {
Some(TransportState::Rolling) => "▶ PLAYING", Some(TransportState::Rolling) => "▶ PLAYING",
Some(TransportState::Starting) => "READY ...", Some(TransportState::Starting) => "READY ...",
Some(TransportState::Stopped) => "⏹ STOPPED", Some(TransportState::Stopped) => "⏹ STOPPED",
_ => unreachable!(), _ => unreachable!(),
})) }))?;
]).render(to) Ok(())
//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))
} }
} }

View file

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