use crate::*; pub struct When(pub bool, pub T); impl> Draw for When { fn draw (&self, to: &mut S) -> Drawn { #[cfg(feature = "prof")] profiling::scope!("when"); if self.0 { self.1.draw(to) } else { Ok(Default::default()) } } } impl When { 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. /// /// ``` /// # use ::tengri::*; fn test <'a> () -> impl Draw { /// when(true, "Yes") /// # } /// ``` pub const fn when <'a, T: Screen> (condition: bool, item: impl Draw) -> impl Draw { 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(||{ either(state.namespace(&expr.nth(1)?)?.unwrap(), thunk_a, thunk_b).draw(output) })) }); /// Render one thing if a condition is true and another false. /// /// ``` /// # use ::tengri::*; fn test <'a> () -> impl Draw { /// either(true, "Yes", "No") /// # } /// ``` pub const fn either <'a, T: Screen> ( condition: bool, a: impl Draw, b: impl Draw ) -> impl Draw { draw(move|to: &mut T|{ #[cfg(feature = "prof")] profiling::scope!("either"); if condition { a.draw(to) } else { b.draw(to) } }) }