mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-01-12 02:56:42 +01:00
wip: big flat pt.4: extract layout crate
This commit is contained in:
parent
cb680ab096
commit
34e731f111
21 changed files with 2125 additions and 83 deletions
43
layout/src/logic.rs
Normal file
43
layout/src/logic.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use crate::*;
|
||||
|
||||
/// Conditional rendering, in unary and binary forms.
|
||||
pub struct Cond;
|
||||
|
||||
impl Cond {
|
||||
/// Render `item` when `cond` is true.
|
||||
pub fn when <E: Engine, A: Render<E>> (cond: bool, item: A) -> When<E, A> {
|
||||
When(cond, item, Default::default())
|
||||
}
|
||||
/// Render `item` if `cond` is true, otherwise render `other`.
|
||||
pub fn either <E: Engine, A: Render<E>, B: Render<E>> (cond: bool, item: A, other: B) -> Either<E, A, B> {
|
||||
Either(cond, item, other, Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders `self.1` when `self.0` is true.
|
||||
pub struct When<E: Engine, A: Render<E>>(bool, A, PhantomData<E>);
|
||||
|
||||
impl<E: Engine, A: Render<E>> Render<E> for When<E, A> {
|
||||
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
let Self(cond, item, ..) = self;
|
||||
if *cond { item.min_size(to) } else { Ok(Some([0.into(), 0.into()].into())) }
|
||||
}
|
||||
fn render (&self, to: &mut E::Output) -> Usually<()> {
|
||||
let Self(cond, item, ..) = self;
|
||||
if *cond { item.render(to) } else { Ok(()) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders `self.1` when `self.0` is true, otherwise renders `self.2`
|
||||
pub struct Either<E: Engine, A: Render<E>, B: Render<E>>(bool, A, B, PhantomData<E>);
|
||||
|
||||
impl<E: Engine, A: Render<E>, B: Render<E>> Render<E> for Either<E, A, B> {
|
||||
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
let Self(cond, item, other, ..) = self;
|
||||
if *cond { item.min_size(to) } else { other.min_size(to) }
|
||||
}
|
||||
fn render (&self, to: &mut E::Output) -> Usually<()> {
|
||||
let Self(cond, item, other, ..) = self;
|
||||
if *cond { item.render(to) } else { other.render(to) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue