wip: reenable dynamic dispatch

This commit is contained in:
🪞👃🪞 2025-01-04 10:44:20 +01:00
parent 600d0b3aca
commit ac3827b8f3
11 changed files with 997 additions and 194 deletions

View file

@ -25,7 +25,7 @@ impl<E: Engine, T: Content<E>> Content<E> for Align<T> {
}
fn layout (&self, on: E::Area) -> E::Area {
use Alignment::*;
let it = self.content().layout(on).xywh();
let it = Render::layout(&self.content(), on).xywh();
let centered = on.center_xy(it.wh());
let far_x = (on.x() + on.w()).minus(it.w());
let far_y = (on.y() + on.h()).minus(it.h());
@ -47,6 +47,8 @@ impl<E: Engine, T: Content<E>> Content<E> for Align<T> {
[x, y, centered.w(), centered.h()].into()
}
fn render (&self, render: &mut E::Output) {
render.place(self.layout(render.area()), &self.content())
let content = &self.content();
let it = Render::layout(content, render.area()).xywh();
render.place(it.into(), content)
}
}

View file

@ -3,7 +3,7 @@ use crate::*;
/// Show an item only when a condition is true.
pub struct When<A>(pub bool, pub A);
impl<E: Engine, A: Content<E>> Content<E> for When<A> {
impl<E: Engine, A: Render<E>> Content<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, item) = self;
let mut area = E::Area::zero();
@ -25,7 +25,7 @@ impl<E: Engine, A: Content<E>> Content<E> for When<A> {
/// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B);
impl<E: Engine, A: Content<E>, B: Content<E>> Content<E> for Either<A, B> {
impl<E: Engine, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, a, b) = self;
if *cond { a.layout(to) } else { b.layout(to) }
@ -43,7 +43,7 @@ pub struct Map<A, B, I, F, G>(pub F, pub G) where
impl<E, A, B, I, F, G> Content<E> for Map<A, B, I, F, G> where
E: Engine,
B: Content<E>,
B: Render<E>,
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync,
G: Fn(A, usize)->B + Send + Sync
@ -85,7 +85,7 @@ impl<E, A, B, I, F, G> Content<E> for Map<A, B, I, F, G> where
//pub fn reduce <E, T, I, R, F>(iterator: I, callback: F) -> Reduce<E, T, I, R, F> where
//E: Engine,
//I: Iterator<Item = T> + Send + Sync,
//R: Content<E>,
//R: Render<E>,
//F: Fn(R, T, usize) -> R + Send + Sync
//{
//Reduce(Default::default(), iterator, callback)
@ -93,12 +93,12 @@ impl<E, A, B, I, F, G> Content<E> for Map<A, B, I, F, G> where
pub struct Reduce<E, T, I, R, F>(PhantomData<(E, R)>, I, F) where
E: Engine,
I: Iterator<Item = T> + Send + Sync,
R: Content<E>,
R: Render<E>,
F: Fn(R, T, usize) -> R + Send + Sync;
impl<E, T, I, R, F> Content<E> for Reduce<E, T, I, R, F> where
E: Engine,
I: Iterator<Item = T> + Send + Sync,
R: Content<E>,
R: Render<E>,
F: Fn(R, T, usize) -> R + Send + Sync
{
fn render (&self, to: &mut E::Output) {
@ -132,16 +132,16 @@ impl<E, T, I, R, F> Content<E> for Reduce<E, T, I, R, F> where
//define_ops! {
//Layout<E: Engine> {
//(when <A: Content<E>,>
//(when <A: Render<E>,>
//When(cond: bool, item: A))
///// When `cond` is `true`, render `a`, otherwise render `b`.
//(either <A: Content<E>, B: Content<E>,>
//(either <A: Render<E>, B: Render<E>,>
//Either(cond: bool, a: A, b: B))
///// If `opt` is `Some(T)` renders `cb(t)`, otherwise nothing.
//(opt <A, F: Fn(A) -> B, B: Content<E>,>
//(opt <A, F: Fn(A) -> B, B: Render<E>,>
//Opt(option: Option<A>, cb: F))
///// Maps items of iterator through callback.
//(map <A, B: Content<E>, I: Iterator<Item = A>, F: Fn() -> I, G: Fn(A, usize)->B,>
//(map <A, B: Render<E>, I: Iterator<Item = A>, F: Fn() -> I, G: Fn(A, usize)->B,>
//Map(get_iterator: F, callback: G))
//}
//}

View file

@ -42,7 +42,7 @@ macro_rules! transform_xy_unit {
}
transform_xy_unit!(|self: Fixed, area|{
let [w, h] = self.content().layout(area.center_xy(match self {
let [w, h] = Render::layout(&self.content(), area.center_xy(match self {
Self::X(fw, _) => [*fw, area.h()],
Self::Y(fh, _) => [area.w(), *fh],
Self::XY(fw, fh, _) => [*fw, *fh],
@ -54,16 +54,16 @@ transform_xy_unit!(|self: Fixed, area|{
})
});
transform_xy_unit!(|self: Shrink, area|self.content().layout([
transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())
].into()));
transform_xy_unit!(|self: Expand, area|self.content().layout([
transform_xy_unit!(|self: Expand, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
].into()));
transform_xy_unit!(|self: Min, area|{
let area = self.content().layout(area);
let area = Render::layout(&self.content(), area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().max(*mh)],
@ -71,31 +71,31 @@ transform_xy_unit!(|self: Min, area|{
}});
transform_xy_unit!(|self: Max, area|{
let area = self.content().layout(area);
let area = Render::layout(&self.content(), area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().min(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().min(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().min(*mw), area.h().min(*mh)],
}});
transform_xy_unit!(|self: Push, area|self.content().layout([
transform_xy_unit!(|self: Push, area|Render::layout(&self.content(), [
area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()
].into()));
transform_xy_unit!(|self: Pull, area|{
let area = self.content().layout(area);
let area = Render::layout(&self.content(), area);
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()]
});
transform_xy_unit!(|self: Margin, area|{
let area = self.content().layout(area);
let area = Render::layout(&self.content(), area);
let dx = self.dx();
let dy = self.dy();
[area.x().minus(dx), area.y().minus(dy), area.w() + dy + dy, area.h() + dy + dy]
});
transform_xy_unit!(|self: Padding, area|{
let area = self.content().layout(area);
let area = Render::layout(&self.content(), area);
let dx = self.dx();
let dy = self.dy();
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]