mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-28 05:06:56 +02:00
support multipass layout, barely
This commit is contained in:
parent
083ce8ba76
commit
7f9b7091e2
20 changed files with 1581 additions and 1783 deletions
46
src/layout/cond.rs
Normal file
46
src/layout/cond.rs
Normal 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) })
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue