feat(section): conditionally show when Section::show_while is set
This commit is contained in:
parent
9bebc7b1f6
commit
ced3f2ffd8
1 changed files with 22 additions and 0 deletions
|
|
@ -11,6 +11,8 @@ slotmap::new_key_type! {
|
||||||
pub struct Entity;
|
pub struct Entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type ShowWhileFn<Message> = Box<dyn for<'a> Fn(&'a dyn Page<Message>) -> bool>;
|
||||||
|
|
||||||
pub type ViewFn<Message> = Box<
|
pub type ViewFn<Message> = Box<
|
||||||
dyn for<'a> Fn(
|
dyn for<'a> Fn(
|
||||||
&'a Binder<Message>,
|
&'a Binder<Message>,
|
||||||
|
|
@ -29,6 +31,8 @@ pub struct Section<Message> {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub descriptions: Vec<String>,
|
pub descriptions: Vec<String>,
|
||||||
#[setters(skip)]
|
#[setters(skip)]
|
||||||
|
pub show_while: Option<ShowWhileFn<Message>>,
|
||||||
|
#[setters(skip)]
|
||||||
pub view_fn: ViewFn<Message>,
|
pub view_fn: ViewFn<Message>,
|
||||||
#[setters(bool)]
|
#[setters(bool)]
|
||||||
pub search_ignore: bool,
|
pub search_ignore: bool,
|
||||||
|
|
@ -39,6 +43,7 @@ impl<Message: 'static> Default for Section<Message> {
|
||||||
Self {
|
Self {
|
||||||
title: String::new(),
|
title: String::new(),
|
||||||
descriptions: Vec::new(),
|
descriptions: Vec::new(),
|
||||||
|
show_while: None,
|
||||||
view_fn: Box::new(unimplemented),
|
view_fn: Box::new(unimplemented),
|
||||||
search_ignore: false,
|
search_ignore: false,
|
||||||
}
|
}
|
||||||
|
|
@ -65,6 +70,23 @@ impl<Message: 'static> Section<Message> {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn show_while<Model: Page<Message>>(
|
||||||
|
mut self,
|
||||||
|
func: impl for<'a> Fn(&'a Model) -> bool + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.show_while = Some(Box::new(move |model: &dyn Page<Message>| {
|
||||||
|
let model = model.downcast_ref::<Model>().unwrap_or_else(|| {
|
||||||
|
panic!(
|
||||||
|
"page model type mismatch: expected {}",
|
||||||
|
std::any::type_name::<Model>()
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
func(model)
|
||||||
|
}));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Will panic if the `Model` type does not match the page type.
|
/// Will panic if the `Model` type does not match the page type.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue