diff options
author | 2022-02-11 17:50:12 +0700 | |
---|---|---|
committer | 2022-02-11 17:50:12 +0700 | |
commit | 897188317b5875cc00a0f1c797790df8ac13687f (patch) | |
tree | 2a2eeedc8d582f616eaa0030000d15d403c1092b /pure/src/widget/tree.rs | |
parent | e03de019881f31d69b70a64c3e278ae5200d5080 (diff) | |
download | iced-897188317b5875cc00a0f1c797790df8ac13687f.tar.gz iced-897188317b5875cc00a0f1c797790df8ac13687f.tar.bz2 iced-897188317b5875cc00a0f1c797790df8ac13687f.zip |
Rename `iced_virtual` to `iced_pure`
`virtual` is a reserved keyword in Rust :grimacing:
Diffstat (limited to 'pure/src/widget/tree.rs')
-rw-r--r-- | pure/src/widget/tree.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs new file mode 100644 index 00000000..75f50a2f --- /dev/null +++ b/pure/src/widget/tree.rs @@ -0,0 +1,58 @@ +use crate::widget::Element; + +use std::any::Any; +use std::marker::PhantomData; + +pub struct Tree<Message, Renderer> { + pub state: Box<dyn Any>, + pub children: Vec<Tree<Message, Renderer>>, + types_: PhantomData<(Message, Renderer)>, +} + +impl<Message, Renderer> Tree<Message, Renderer> { + pub fn new(element: &Element<Message, Renderer>) -> Self { + Self { + state: element.as_widget().state(), + children: element + .as_widget() + .children() + .iter() + .map(Self::new) + .collect(), + types_: PhantomData, + } + } + + pub fn diff( + &mut self, + current: &Element<Message, Renderer>, + new: &Element<Message, Renderer>, + ) { + if current.as_widget().tag() == new.as_widget().tag() { + let current_children = current.as_widget().children(); + let new_children = new.as_widget().children(); + + if current_children.len() > new_children.len() { + self.children.truncate(new_children.len()); + } + + for (child_state, (current, new)) in self + .children + .iter_mut() + .zip(current_children.iter().zip(new_children.iter())) + { + child_state.diff(current, new); + } + + if current_children.len() < new_children.len() { + self.children.extend( + new_children[current_children.len()..] + .iter() + .map(Self::new), + ); + } + } else { + *self = Self::new(new); + } + } +} |