support multipass layout, barely

This commit is contained in:
facile pop culture reference 2026-08-04 07:59:20 +03:00
parent 083ce8ba76
commit 7f9b7091e2
20 changed files with 1581 additions and 1783 deletions

46
src/layout/cond.rs Normal file
View file

@ -0,0 +1,46 @@
use crate::*;
fn_kw_layout!(kw_when |state, output, expr| {
Ok(matches!(expr.head()?, Some("when")).then(||{
when(state.namespace(expr.tail().head())?.unwrap(),
draw(move|output: &mut O|{state.interpret(output, &expr.tail().tail().head())})
).draw(output)
}).transpose()?.flatten())
});
/// Only render when condition is true.
///
/// ```
/// # use tengri::*;
/// # fn test () -> impl Draw<Tui> {
/// when(true, "Yes")
/// # }
/// ```
pub const fn when <T: Screen> (condition: bool, item: impl Draw<T>) -> impl Draw<T> {
draw(move|to: &mut T|if condition { item.draw(to) } else { Ok(Default::default()) })
}
fn_kw_layout!(kw_either |state, output, expr| {
Ok(matches!(expr.head()?, Some("either")).then(||{
either(state.namespace(expr.tail().head()?)?.unwrap(),
draw(move|output: &mut O|{
state.interpret(output, &expr.tail().tail().head()?)
}),
draw(move|output: &mut O|{
state.interpret(output, &expr.tail().tail().tail().head()?)
}),
).draw(output)
}).transpose()?.flatten())
});
/// Render one thing if a condition is true and another false.
///
/// ```
/// # use tengri::*;
/// # fn test () -> impl Draw<Tui> {
/// either(true, "Yes", "No")
/// # }
/// ```
pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
draw(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
}