From 897188317b5875cc00a0f1c797790df8ac13687f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 17:50:12 +0700 Subject: Rename `iced_virtual` to `iced_pure` `virtual` is a reserved keyword in Rust :grimacing: --- pure/src/widget/tree.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pure/src/widget/tree.rs (limited to 'pure/src/widget/tree.rs') 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 { + pub state: Box, + pub children: Vec>, + types_: PhantomData<(Message, Renderer)>, +} + +impl Tree { + pub fn new(element: &Element) -> 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, + new: &Element, + ) { + 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); + } + } +} -- cgit