diff --git a/page/src/section.rs b/page/src/section.rs index e8e4f22..6d75b08 100644 --- a/page/src/section.rs +++ b/page/src/section.rs @@ -11,6 +11,8 @@ slotmap::new_key_type! { pub struct Entity; } +pub type ShowWhileFn = Box Fn(&'a dyn Page) -> bool>; + pub type ViewFn = Box< dyn for<'a> Fn( &'a Binder, @@ -29,6 +31,8 @@ pub struct Section { pub title: String, pub descriptions: Vec, #[setters(skip)] + pub show_while: Option>, + #[setters(skip)] pub view_fn: ViewFn, #[setters(bool)] pub search_ignore: bool, @@ -39,6 +43,7 @@ impl Default for Section { Self { title: String::new(), descriptions: Vec::new(), + show_while: None, view_fn: Box::new(unimplemented), search_ignore: false, } @@ -65,6 +70,23 @@ impl Section { false } + pub fn show_while>( + mut self, + func: impl for<'a> Fn(&'a Model) -> bool + 'static, + ) -> Self { + self.show_while = Some(Box::new(move |model: &dyn Page| { + let model = model.downcast_ref::().unwrap_or_else(|| { + panic!( + "page model type mismatch: expected {}", + std::any::type_name::() + ) + }); + + func(model) + })); + self + } + /// # Panics /// /// Will panic if the `Model` type does not match the page type.