This commit is contained in:
🪞👃🪞 2025-09-09 01:07:19 +03:00
parent ca862b9802
commit 90fc869e14
42 changed files with 645 additions and 1158 deletions

View file

@ -2,54 +2,45 @@ use crate::*;
/// Show an item only when a condition is true.
pub struct When<O, T>(bool, T, PhantomData<O>);
impl<O, T> When<O, T> {
impl<O: Out, T: Content<O>> When<O, T> {
/// Create a binary condition.
pub const fn new (c: bool, a: T) -> Self { Self(c, a, PhantomData) }
}
impl<O: Out, T: Layout<O>> Layout<O> for When<O, T> {
fn layout (&self, to: O::Area) -> O::Area {
let Self(cond, item, ..) = self;
let mut area = O::Area::zero();
if *cond {
let item_area = item.layout(to);
area[0] = item_area.x();
area[1] = item_area.y();
area[2] = item_area.w();
area[3] = item_area.h();
}
area.into()
if *cond { item.layout(to) } else { O::Area::zero().into() }
}
}
impl<O: Out, T: Draw<O>> Draw<O> for When<O, T> {
impl<O: Out, T: Content<O>> Draw<O> for When<O, T> {
fn draw (&self, to: &mut O) {
let Self(cond, item, ..) = self;
if *cond { item.draw(to) }
if *cond { Bound(self.layout(to.area()), item).draw(to) }
}
}
/// Show one item if a condition is true and another if the condition is false
pub struct Either<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>>(pub bool, pub A, pub B, pub PhantomData<E>);
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Either<E, A, B> {
pub struct Either<E: Out, A, B>(pub bool, pub A, pub B, pub PhantomData<E>);
impl<E: Out, A: Content<E>, B: Content<E>> Either<E, A, B> {
/// Create a ternary view condition.
pub const fn new (c: bool, a: A, b: B) -> Self {
Self(c, a, b, PhantomData)
}
}
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Layout<E> for Either<E, A, B> {
impl<E: Out, A: Layout<E>, B: Layout<E>> Layout<E> for Either<E, 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) }
}
}
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Draw<E> for Either<E, A, B> {
impl<E: Out, A: Content<E>, B: Content<E>> Draw<E> for Either<E, A, B> {
fn draw (&self, to: &mut E) {
let Self(cond, a, b, ..) = self;
if *cond { a.draw(to) } else { b.draw(to) }
let area = self.layout(to.area());
if *cond { Bound(area, a).draw(to) } else { Bound(area, b).draw(to) }
}
}
///////////////////////////////////////////////////////////////////////////////
@ -116,7 +107,7 @@ impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Draw<E> for Either<
//return Ok(Some(match words.next() {
//Some(Token{value: Key($x),..}) => Self::x(content),
//Some(Token{value: Key($y),..}) => Self::y(content),
//Some(Token{value: Key($xy),..}) => Self::xy(content),
//Some(Token{value: Key($xy),..}) => Self::XY(content),
//_ => unreachable!()
//}))
//} else {
@ -134,7 +125,7 @@ impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Draw<E> for Either<
//state.give_or_fail(words, ||"y: no unit")?,
//state.give_or_fail(words, ||"y: no content")?,
//),
//Some(Token { value: Key($x), .. }) => Self::xy(
//Some(Token { value: Key($x), .. }) => Self::XY(
//state.give_or_fail(words, ||"xy: no unit x")?,
//state.give_or_fail(words, ||"xy: no unit y")?,
//state.give_or_fail(words, ||"xy: no content")?