From ced3f2ffd8eb4da7266967d078e1276e05311d44 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Thu, 15 Feb 2024 04:51:01 +0100 Subject: [PATCH] feat(section): conditionally show when `Section::show_while` is set --- page/src/section.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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.