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 From 01c5004959c9b11f2580840f4553ad7d706f4564 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 22:07:21 +0700 Subject: Allow pure widgets to borrow from `Application` data :tada: --- pure/src/widget/tree.rs | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 75f50a2f..2353edc5 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -1,17 +1,27 @@ use crate::widget::Element; -use std::any::Any; -use std::marker::PhantomData; +use std::any::{self, Any}; -pub struct Tree { +pub struct Tree { + pub tag: any::TypeId, pub state: Box, - pub children: Vec>, - types_: PhantomData<(Message, Renderer)>, + pub children: Vec, } -impl Tree { - pub fn new(element: &Element) -> Self { +impl Tree { + pub fn empty() -> Self { Self { + tag: any::TypeId::of::<()>(), + state: Box::new(()), + children: Vec::new(), + } + } + + pub fn new( + element: &Element<'_, Message, Renderer>, + ) -> Self { + Self { + tag: element.as_widget().tag(), state: element.as_widget().state(), children: element .as_widget() @@ -19,36 +29,29 @@ impl Tree { .iter() .map(Self::new) .collect(), - types_: PhantomData, } } - pub fn diff( + pub fn diff( &mut self, - current: &Element, - new: &Element, + new: &Element<'_, Message, Renderer>, ) { - if current.as_widget().tag() == new.as_widget().tag() { - let current_children = current.as_widget().children(); + if self.tag == new.as_widget().tag() { let new_children = new.as_widget().children(); - if current_children.len() > new_children.len() { + if self.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())) + for (child_state, new) in + self.children.iter_mut().zip(new_children.iter()) { - child_state.diff(current, new); + child_state.diff(new); } - if current_children.len() < new_children.len() { + if self.children.len() < new_children.len() { self.children.extend( - new_children[current_children.len()..] - .iter() - .map(Self::new), + new_children[self.children.len()..].iter().map(Self::new), ); } } else { -- cgit From ecb3df8e018930c407e469ce2b8f4208a9d15426 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 23:17:07 +0700 Subject: Expose reusable `Button` logic ... and reuse it in `iced_pure`! --- pure/src/widget/tree.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 2353edc5..1ab6d80b 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -58,4 +58,18 @@ impl Tree { *self = Self::new(new); } } + + pub fn state(&self) -> &T + where + T: 'static, + { + self.state.downcast_ref().expect("Downcast widget state") + } + + pub fn state_mut(&mut self) -> &mut T + where + T: 'static, + { + self.state.downcast_mut().expect("Downcast widget state") + } } -- cgit From b2670e8752eb96a4018f93b9cb8945da81a7ebff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 12 Feb 2022 15:17:44 +0700 Subject: Implement `Scrollable` in `iced_pure` --- pure/src/widget/tree.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 1ab6d80b..98e976ad 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -4,7 +4,7 @@ use std::any::{self, Any}; pub struct Tree { pub tag: any::TypeId, - pub state: Box, + pub state: State, pub children: Vec, } @@ -12,7 +12,7 @@ impl Tree { pub fn empty() -> Self { Self { tag: any::TypeId::of::<()>(), - state: Box::new(()), + state: State(Box::new(())), children: Vec::new(), } } @@ -22,7 +22,7 @@ impl Tree { ) -> Self { Self { tag: element.as_widget().tag(), - state: element.as_widget().state(), + state: State(element.as_widget().state()), children: element .as_widget() .children() @@ -58,18 +58,22 @@ impl Tree { *self = Self::new(new); } } +} + +pub struct State(Box); - pub fn state(&self) -> &T +impl State { + pub fn downcast_ref(&self) -> &T where T: 'static, { - self.state.downcast_ref().expect("Downcast widget state") + self.0.downcast_ref().expect("Downcast widget state") } - pub fn state_mut(&mut self) -> &mut T + pub fn downcast_mut(&mut self) -> &mut T where T: 'static, { - self.state.downcast_mut().expect("Downcast widget state") + self.0.downcast_mut().expect("Downcast widget state") } } -- cgit 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 From 35e9b75e415ef3b9124051696b60628ef56afe47 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Feb 2022 15:44:50 +0700 Subject: Introduce `Tag` and `State` opaque types in `iced_pure::widget::tree` --- pure/src/widget/tree.rs | 52 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 3a5f4433..33f5693a 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -3,7 +3,7 @@ use crate::widget::Element; use std::any::{self, Any}; pub struct Tree { - pub tag: any::TypeId, + pub tag: Tag, pub state: State, pub children: Vec, } @@ -11,8 +11,8 @@ pub struct Tree { impl Tree { pub fn empty() -> Self { Self { - tag: any::TypeId::of::<()>(), - state: State(Box::new(())), + tag: Tag::stateless(), + state: State::None, children: Vec::new(), } } @@ -22,8 +22,8 @@ impl Tree { ) -> Self { Self { tag: element.as_widget().tag(), - state: State(element.as_widget().state()), - children: element.as_widget().children_state(), + state: element.as_widget().state(), + children: element.as_widget().children(), } } @@ -60,20 +60,56 @@ impl Tree { } } -pub struct State(Box); +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)] +pub struct Tag(any::TypeId); + +impl Tag { + pub fn of() -> Self + where + T: 'static, + { + Self(any::TypeId::of::()) + } + + pub fn stateless() -> Self { + Self::of::<()>() + } +} + +pub enum State { + None, + Some(Box), +} impl State { + pub fn new(state: T) -> Self + where + T: 'static, + { + State::Some(Box::new(state)) + } + pub fn downcast_ref(&self) -> &T where T: 'static, { - self.0.downcast_ref().expect("Downcast widget state") + match self { + State::None => panic!("Downcast on stateless state"), + State::Some(state) => { + state.downcast_ref().expect("Downcast widget state") + } + } } pub fn downcast_mut(&mut self) -> &mut T where T: 'static, { - self.0.downcast_mut().expect("Downcast widget state") + match self { + State::None => panic!("Downcast on stateless state"), + State::Some(state) => { + state.downcast_mut().expect("Downcast widget state") + } + } } } -- cgit From 6dd187ff0822230f084e43636b1aabeb1baf06f6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 10 Mar 2022 19:25:57 +0700 Subject: Implement `pure` version of `PaneGrid` widget --- pure/src/widget/tree.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 33f5693a..3fcf0922 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -41,6 +41,19 @@ impl Tree { pub fn diff_children( &mut self, new_children: &[Element<'_, Message, Renderer>], + ) { + self.diff_children_custom( + new_children, + |new, child_state| child_state.diff(new), + Self::new, + ) + } + + pub fn diff_children_custom( + &mut self, + new_children: &[T], + diff: impl Fn(&T, &mut Tree), + new_state: impl Fn(&T) -> Self, ) { if self.children.len() > new_children.len() { self.children.truncate(new_children.len()); @@ -49,12 +62,12 @@ impl Tree { for (child_state, new) in self.children.iter_mut().zip(new_children.iter()) { - child_state.diff(new); + diff(new, child_state); } if self.children.len() < new_children.len() { self.children.extend( - new_children[self.children.len()..].iter().map(Self::new), + new_children[self.children.len()..].iter().map(new_state), ); } } -- cgit From d7100fd2597da82d97eaf196d50573ea64f3f8ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Mar 2022 17:37:19 +0700 Subject: Export widget modules in `iced_pure` ... and fix collisions with the new `helpers` --- pure/src/widget/tree.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pure/src/widget/tree.rs') diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs index 3fcf0922..bd7c259c 100644 --- a/pure/src/widget/tree.rs +++ b/pure/src/widget/tree.rs @@ -1,4 +1,4 @@ -use crate::widget::Element; +use crate::Element; use std::any::{self, Any}; -- cgit