still dark; refactor and document layout crate

This commit is contained in:
🪞👃🪞 2024-12-31 19:57:03 +01:00
parent ed72ab1635
commit 675d376100
12 changed files with 510 additions and 659 deletions

View file

@ -1,88 +1,14 @@
//! Groupings of elements.
//mod split; pub use self::split::*;
//mod stack; pub use self::stack::*;
use crate::*;
use std::sync::RwLock;
/// A function or closure that emits renderables.
pub trait Collector<E: Engine>: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
impl<E: Engine> Layout<E> for E {}
/// Any function or closure that emits renderables for the given engine matches [CollectCallback].
impl<E, F> Collector<E> for F
where E: Engine, F: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
pub trait Layout<E: Engine> {
/// Content `item` when `cond` is true.
fn when <A: Content<E>> (cond: bool, item: A) -> When<E, A> {
When(cond, item, Default::default())
}
/// Content `item` if `cond` is true, otherwise render `other`.
fn either <A: Content<E>, B: Content<E>> (cond: bool, a: A, b: B)
-> Either<E, A, B>
{
Either(cond, a, b, Default::default())
}
/// Maps an [Option<T>] through a callback `F`
fn opt <A, F: Fn(A)->R, R: Content<E>> (option: Option<A>, cb: F)
-> Opt<E, A, F, R>
{
Opt(option, cb, Default::default())
}
fn map <T, I, R, F>(iterator: I, callback: F) -> Map<E, T, I, R, F> where
E: Engine,
I: Iterator<Item = T> + Send + Sync,
R: Content<E>,
F: Fn(T, usize)->R + Send + Sync
{
Map(Default::default(), RwLock::new(iterator), callback)
}
//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>,
//F: Fn(R, T, usize) -> R + Send + Sync
//{
//Reduce(Default::default(), iterator, callback)
//}
}
pub struct Opt<E: Engine, A, F: Fn(A)->R, R: Content<E>>(Option<A>, F, PhantomData<E>);
/// Contents `self.1` when `self.0` is true.
pub struct When<E: Engine, A>(bool, A, PhantomData<E>);
impl<E: Engine, A: Content<E>> Content<E> for When<E, A> {
fn area (&self, to: E::Area) -> E::Area {
let Self(cond, item, ..) = self;
let mut area = E::Area::zero();
if *cond {
let item_area = item.area(to);
area[0] = item_area.x();
area[1] = item_area.y();
area[2] = item_area.w();
area[3] = item_area.h();
}
area.into()
}
fn render (&self, to: &mut E::Output) {
let Self(cond, item, ..) = self;
if *cond { item.render(to) }
}
}
/// Contents `self.1` when `self.0` is true, otherwise renders `self.2`
pub struct Either<E: Engine, A, B>(bool, A, B, PhantomData<E>);
impl<E: Engine, A: Content<E>, B: Content<E>> Content<E> for Either<E, A, B> {
fn area (&self, to: E::Area) -> E::Area {
let Self(cond, a, b, ..) = self;
if *cond { a.area(to) } else { b.area(to) }
}
fn render (&self, to: &mut E::Output) {
let Self(cond, a, b, ..) = self;
if *cond { a.render(to) } else { b.render(to) }
}
}
trait Render<E: Engine> {
pub trait Render<E: Engine> {
fn area (&self, to: E::Area) -> E::Area;
fn render (&self, to: &mut E::Output);
}
@ -95,55 +21,3 @@ impl<E: Engine, C: Content<E>> Render<E> for C {
Content::render(self, to)
}
}
/// A function or closure that emits renderables.
pub trait Collector<E: Engine>: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
/// Any function or closure that emits renderables for the given engine matches [CollectCallback].
impl<E, F> Collector<E> for F
where E: Engine, F: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
/// Rendering of iterable collections, one-to-one and many-to one.
pub struct Coll;
impl Coll {
}
pub struct Map<E, T, I, R, F>(PhantomData<E>, RwLock<I>, F) where
E: Engine,
I: Iterator<Item = T> + Send + Sync,
R: Content<E>,
F: Fn(T, usize)->R + Send + Sync;
impl<E, T, I, R, F> Content<E> for Map<E, T, I, R, F> where
E: Engine,
I: Iterator<Item = T> + Send + Sync,
R: Content<E>,
F: Fn(T, usize)->R + Send + Sync
{
fn render (&self, to: &mut E::Output) {
let mut index = 0;
for item in &mut*self.1.write().unwrap() {
(self.2)(item, index).render(to);
index += 1;
}
}
}
/*
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>,
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>,
F: Fn(R, T, usize) -> R + Send + Sync
{
fn render (&self, to: &mut E::Output) {
todo!()
}
}
*/