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

117
layout/src/ops.rs Normal file
View file

@ -0,0 +1,117 @@
use crate::*;
use std::sync::RwLock;
impl<E: Engine> Layout<E> for 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) }
}
}
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!()
}
}
*/