From bd22cc0bc0f7551d29cf2acd22520f4a906f253c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 12 Feb 2022 17:21:28 +0700 Subject: 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`. --- pure/src/widget/tree.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'pure/src/widget/tree.rs') 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( + &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), + ); } } } -- cgit