Invalidate widget tree from Responsive widget

... by introducing a new `invalidate_widgets` method to `Shell`
This commit is contained in:
Héctor Ramón Jiménez 2022-01-11 13:47:43 +07:00
parent 90c20ac46b
commit 6ab4611a6e
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
8 changed files with 85 additions and 33 deletions

View file

@ -8,6 +8,7 @@
pub struct Shell<'a, Message> {
messages: &'a mut Vec<Message>,
is_layout_invalid: bool,
are_widgets_invalid: bool,
}
impl<'a, Message> Shell<'a, Message> {
@ -16,12 +17,13 @@ impl<'a, Message> Shell<'a, Message> {
Self {
messages,
is_layout_invalid: false,
are_widgets_invalid: false,
}
}
/// Triggers the given function if the layout is invalid, cleaning it in the
/// process.
pub fn with_invalid_layout(&mut self, f: impl FnOnce()) {
pub fn revalidate_layout(&mut self, f: impl FnOnce()) {
if self.is_layout_invalid {
self.is_layout_invalid = false;
@ -41,6 +43,13 @@ impl<'a, Message> Shell<'a, Message> {
self.is_layout_invalid = true;
}
/// Invalidates the current application widgets.
///
/// The shell will rebuild and relayout the widget tree.
pub fn invalidate_widgets(&mut self) {
self.are_widgets_invalid = true;
}
/// Merges the current [`Shell`] with another one by applying the given
/// function to the messages of the latter.
///
@ -50,5 +59,14 @@ impl<'a, Message> Shell<'a, Message> {
self.is_layout_invalid =
self.is_layout_invalid || other.is_layout_invalid;
self.are_widgets_invalid =
self.are_widgets_invalid || other.are_widgets_invalid;
}
/// Returns whether the widgets of the current application have been
/// invalidated.
pub fn are_widgets_invalid(&self) -> bool {
self.are_widgets_invalid
}
}