mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-09-18 13:26:42 +02:00
This commit is contained in:
parent
92f992e2e6
commit
8e7286e409
6 changed files with 123 additions and 47 deletions
|
|
@ -1,11 +1,28 @@
|
|||
use crate::*;
|
||||
|
||||
fn_kw_layout!(kw_when |state, output, expr| {
|
||||
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("when")).then(||{
|
||||
when(state.namespace(&expr.nth(1)?)?.unwrap(), thunk).draw(output)
|
||||
}))
|
||||
});
|
||||
pub struct When<T>(pub bool, pub T);
|
||||
|
||||
impl<S: Screen, T: Draw<S>> Draw<S> for When<T> {
|
||||
fn draw (&self, to: &mut S) -> Drawn<S::Unit> {
|
||||
#[cfg(feature = "prof")] profiling::scope!("when");
|
||||
if self.0 {
|
||||
self.1.draw(to)
|
||||
} else {
|
||||
Ok(Default::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> When<T> {
|
||||
|
||||
fn_kw_layout!(interpret |state, output, expr| {
|
||||
#[cfg(feature = "prof")] profiling::scope!("kw_when");
|
||||
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("when")).then(||{
|
||||
when(state.namespace(&expr.nth(1)?)?.unwrap(), thunk).draw(output)
|
||||
}))
|
||||
});
|
||||
}
|
||||
|
||||
/// Only render when condition is true.
|
||||
///
|
||||
|
|
@ -15,10 +32,11 @@ fn_kw_layout!(kw_when |state, output, expr| {
|
|||
/// # }
|
||||
/// ```
|
||||
pub const fn when <'a, 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()) })
|
||||
When(condition, item)
|
||||
}
|
||||
|
||||
fn_kw_layout!(kw_either |state, output, expr| {
|
||||
#[cfg(feature = "prof")] profiling::scope!("kw_layout");
|
||||
let thunk_a = draw(move|screen|ok_flat(expr.nth(2)?.map(|x|state.interpret(screen, x))));
|
||||
let thunk_b = draw(move|screen|ok_flat(expr.nth(3)?.map(|x|state.interpret(screen, x))));
|
||||
ok_flat(matches!(expr.head()?, Some("either")).then(||{
|
||||
|
|
@ -36,5 +54,8 @@ fn_kw_layout!(kw_either |state, output, expr| {
|
|||
pub const fn either <'a, 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) })
|
||||
draw(move|to: &mut T|{
|
||||
#[cfg(feature = "prof")] profiling::scope!("either");
|
||||
if condition { a.draw(to) } else { b.draw(to) }
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue