removing engine generic from transforms

This commit is contained in:
🪞👃🪞 2025-01-18 02:52:54 +01:00
parent 452bdf9598
commit a949117017
8 changed files with 184 additions and 184 deletions

View file

@ -1,30 +1,42 @@
use crate::*;
use RefAtom::*;
/// Show an item only when a condition is true.
pub struct When<E, A>(pub PhantomData<E>, pub bool, pub A);
impl<E, A> When<E, A> {
pub fn new (c: bool, a: A) -> Self {
Self(Default::default(), c, a)
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for When<E, RenderBox<'a, E>> {
fn try_from_atom (
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
if let (Key("when"), [condition, content]) = (head.to_ref(), tail) {
Some(Self(
Default::default(),
state.get_bool(condition).expect("when: no condition"),
state.get_content(content).expect("when: no content")
))
} else {
None
pub struct When<A>(pub bool, pub A);
impl<A> When<A> { pub fn new (c: bool, a: A) -> Self { Self(c, 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<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_atoms!(<'a, E>: When<RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if (head.kind(), head.text()) == (TokenKind::Key, "when") {
let condition = atoms.next();
if let Some(condition) = condition {
let condition = state.get_bool(condition).expect("no condition");
if let Some(content) = atoms.next() {
let content = state.get_content(content).expect("no atom");
return Some(Self(condition, content))
}
}
}
}
impl<E: Output, A: Render<E>> Content<E> for When<E, A> {
});
try_from_atoms!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, atoms| {
let head = atoms.next()?;
if (head.kind(), head.text()) == (TokenKind::Key, "either") {
let condition = atoms.next();
if let Some(condition) = condition {
let condition = state.get_bool(condition).expect("no condition");
if let Some(content1) = atoms.next() {
let content1 = state.get_content(content1).expect("no content1");
if let Some(content2) = atoms.next() {
let content2 = state.get_content(content2).expect("no content2");
return Some(Self(condition, content1, content2))
}
}
}
}
});
impl<E: Output, A: Render<E>> Content<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(_, cond, item) = self;
let Self(cond, item) = self;
let mut area = E::Area::zero();
if *cond {
let item_area = item.layout(to);
@ -36,37 +48,17 @@ impl<E: Output, A: Render<E>> Content<E> for When<E, A> {
area.into()
}
fn render (&self, to: &mut E) {
let Self(_, cond, item) = self;
let Self(cond, item) = self;
if *cond { item.render(to) }
}
}
/// Show one item if a condition is true and another if the condition is false
pub struct Either<E, A, B>(pub PhantomData<E>, pub bool, pub A, pub B);
impl<E, A, B> Either<E, A, B> {
pub fn new (c: bool, a: A, b: B) -> Self {
Self(Default::default(), c, a, b)
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
fn try_from_atom (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) -> Option<Self> {
if let (Key("either"), [condition, content, alternative]) = (head.to_ref(), tail) {
Some(Self::new(
state.get_bool(condition).expect("either: no condition"),
state.get_content(content).expect("either: no content"),
state.get_content(alternative).expect("either: no alternative")
))
} else {
None
}
}
}
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<E, A, B> {
impl<E: Output, 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;
let Self(cond, a, b) = self;
if *cond { a.layout(to) } else { b.layout(to) }
}
fn render (&self, to: &mut E) {
let Self(_, cond, a, b) = self;
let Self(cond, a, b) = self;
if *cond { a.render(to) } else { b.render(to) }
}
}