Implement pure version of todos example 🎉

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`.
This commit is contained in:
Héctor Ramón Jiménez 2022-02-12 17:21:28 +07:00
parent e3108494e5
commit bd22cc0bc0
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
18 changed files with 917 additions and 96 deletions

View file

@ -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,27 +32,32 @@ impl Tree {
new: &Element<'_, Message, Renderer>,
) {
if self.tag == new.as_widget().tag() {
let new_children = new.as_widget().children();
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);
}
if self.children.len() < new_children.len() {
self.children.extend(
new_children[self.children.len()..].iter().map(Self::new),
);
}
new.as_widget().diff(self)
} else {
*self = Self::new(new);
}
}
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);
}
if self.children.len() < new_children.len() {
self.children.extend(
new_children[self.children.len()..].iter().map(Self::new),
);
}
}
}
pub struct State(Box<dyn Any>);