summaryrefslogtreecommitdiffstats
path: root/pure/src/widget/tree.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-12 17:21:28 +0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-02-12 17:21:28 +0700
commitbd22cc0bc0f7551d29cf2acd22520f4a906f253c (patch)
treefae6435d0e1500204fca73fa7872fb99a41b6eb4 /pure/src/widget/tree.rs
parente3108494e5886c34312184292ec05dddeb8bf3ca (diff)
downloadiced-bd22cc0bc0f7551d29cf2acd22520f4a906f253c.tar.gz
iced-bd22cc0bc0f7551d29cf2acd22520f4a906f253c.tar.bz2
iced-bd22cc0bc0f7551d29cf2acd22520f4a906f253c.zip
Implement pure version of `todos` example :tada:
The `Widget` trait in `iced_pure` needed to change a bit to make the implementation of `Element::map` possible. Specifically, the `children` method has been split into `diff` and `children_state`.
Diffstat (limited to 'pure/src/widget/tree.rs')
-rw-r--r--pure/src/widget/tree.rs44
1 files changed, 22 insertions, 22 deletions
diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs
index 98e976ad..3a5f4433 100644
--- a/pure/src/widget/tree.rs
+++ b/pure/src/widget/tree.rs
@@ -23,12 +23,7 @@ impl Tree {
Self {
tag: element.as_widget().tag(),
state: State(element.as_widget().state()),
- children: element
- .as_widget()
- .children()
- .iter()
- .map(Self::new)
- .collect(),
+ children: element.as_widget().children_state(),
}
}
@@ -37,25 +32,30 @@ impl Tree {
new: &Element<'_, Message, Renderer>,
) {
if self.tag == new.as_widget().tag() {
- let new_children = new.as_widget().children();
+ new.as_widget().diff(self)
+ } else {
+ *self = Self::new(new);
+ }
+ }
- if self.children.len() > new_children.len() {
- self.children.truncate(new_children.len());
- }
+ pub fn diff_children<Message, Renderer>(
+ &mut self,
+ new_children: &[Element<'_, Message, Renderer>],
+ ) {
+ if self.children.len() > new_children.len() {
+ self.children.truncate(new_children.len());
+ }
- for (child_state, new) in
- self.children.iter_mut().zip(new_children.iter())
- {
- child_state.diff(new);
- }
+ for (child_state, new) in
+ self.children.iter_mut().zip(new_children.iter())
+ {
+ child_state.diff(new);
+ }
- if self.children.len() < new_children.len() {
- self.children.extend(
- new_children[self.children.len()..].iter().map(Self::new),
- );
- }
- } else {
- *self = Self::new(new);
+ if self.children.len() < new_children.len() {
+ self.children.extend(
+ new_children[self.children.len()..].iter().map(Self::new),
+ );
}
}
}