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]> {