From 11986547cbe772fa0bb863cefc3fff7877d1a6d5 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Mon, 18 Apr 2022 08:51:49 -0700 Subject: add example --- Cargo.toml | 1 + examples/pure/stateless_component/Cargo.toml | 12 +++ examples/pure/stateless_component/src/main.rs | 101 ++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 examples/pure/stateless_component/Cargo.toml create mode 100644 examples/pure/stateless_component/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index c6ccc5df..b11be495 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,7 @@ members = [ "examples/pure/pick_list", "examples/pure/todos", "examples/pure/tour", + "examples/pure/stateless_component", "examples/websocket", ] diff --git a/examples/pure/stateless_component/Cargo.toml b/examples/pure/stateless_component/Cargo.toml new file mode 100644 index 00000000..d89ebf85 --- /dev/null +++ b/examples/pure/stateless_component/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "stateless_component" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../../..", features = ["debug", "pure"] } +iced_native = { path = "../../../native" } +iced_lazy = { path = "../../../lazy", features = ["pure"] } +iced_pure = { path = "../../../pure" } diff --git a/examples/pure/stateless_component/src/main.rs b/examples/pure/stateless_component/src/main.rs new file mode 100644 index 00000000..066887c9 --- /dev/null +++ b/examples/pure/stateless_component/src/main.rs @@ -0,0 +1,101 @@ +use iced::pure::{button, text}; +use iced::pure::{Element, Sandbox}; +use iced::Settings; + +use custom_text::custom_text; + +pub fn main() -> iced::Result { + Mode::run(Settings::default()) +} + +enum Mode { + Standard, + Custom, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + SwitchToComponent, +} + +impl Sandbox for Mode { + type Message = Message; + + fn new() -> Self { + Self::Standard + } + + fn title(&self) -> String { + String::from("Stateless Component Bug") + } + + fn update(&mut self, message: Message) { + match message { + Message::SwitchToComponent => *self = Mode::Custom, + } + } + + fn view(&self) -> Element { + match self { + Self::Standard => button(text("Click to Panic")) + .on_press(Message::SwitchToComponent) + .into(), + Self::Custom => button(custom_text("..")).into(), + } + } +} + +mod custom_text { + use iced::pure::text; + use iced_lazy::pure::{self, Component}; + use iced_native::text; + use iced_pure::Element; + + pub struct CustomText<'a> { + text: &'a str, + } + + pub fn custom_text<'a>(text: &'a str) -> CustomText<'a> { + CustomText { text } + } + + #[derive(Debug, Clone)] + pub enum Event {} + + impl<'a> CustomText<'a> { + pub fn new(text: &'a str) -> Self { + Self { text } + } + } + + impl<'a, Message, Renderer> Component for CustomText<'a> + where + Renderer: text::Renderer + 'static, + { + type State = (); + type Event = Event; + + fn update( + &mut self, + _state: &mut Self::State, + _event: Event, + ) -> Option { + None + } + + fn view(&self, _state: &Self::State) -> Element { + text(self.text).into() + } + } + + impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> + where + Message: 'a, + Renderer: 'static + text::Renderer, + { + fn from(x: CustomText<'a>) -> Self { + pure::component(x) + } + } +} -- cgit From 2858c8aa681ecd9b2aba6ec4df9ee96d3f50681c Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Mon, 18 Apr 2022 09:02:40 -0700 Subject: fix: diffing issue with pure stateless components --- lazy/src/pure/component.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lazy/src/pure/component.rs b/lazy/src/pure/component.rs index 1c8237fc..f919d683 100644 --- a/lazy/src/pure/component.rs +++ b/lazy/src/pure/component.rs @@ -70,6 +70,8 @@ where }) } +struct Tag(T); + struct Instance<'a, Message, Renderer, Event, S> { state: RefCell>>, } @@ -130,7 +132,7 @@ where Renderer: iced_native::Renderer, { fn tag(&self) -> tree::Tag { - tree::Tag::of::() + tree::Tag::of::>() } fn state(&self) -> tree::State { -- cgit From b1d6ad00703063d8381dd847361e4684f18c10bd Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Mon, 18 Apr 2022 09:03:17 -0700 Subject: Revert "add example" This reverts commit 11986547cbe772fa0bb863cefc3fff7877d1a6d5. --- Cargo.toml | 1 - examples/pure/stateless_component/Cargo.toml | 12 --- examples/pure/stateless_component/src/main.rs | 101 -------------------------- 3 files changed, 114 deletions(-) delete mode 100644 examples/pure/stateless_component/Cargo.toml delete mode 100644 examples/pure/stateless_component/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b11be495..c6ccc5df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,7 +93,6 @@ members = [ "examples/pure/pick_list", "examples/pure/todos", "examples/pure/tour", - "examples/pure/stateless_component", "examples/websocket", ] diff --git a/examples/pure/stateless_component/Cargo.toml b/examples/pure/stateless_component/Cargo.toml deleted file mode 100644 index d89ebf85..00000000 --- a/examples/pure/stateless_component/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "stateless_component" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../../..", features = ["debug", "pure"] } -iced_native = { path = "../../../native" } -iced_lazy = { path = "../../../lazy", features = ["pure"] } -iced_pure = { path = "../../../pure" } diff --git a/examples/pure/stateless_component/src/main.rs b/examples/pure/stateless_component/src/main.rs deleted file mode 100644 index 066887c9..00000000 --- a/examples/pure/stateless_component/src/main.rs +++ /dev/null @@ -1,101 +0,0 @@ -use iced::pure::{button, text}; -use iced::pure::{Element, Sandbox}; -use iced::Settings; - -use custom_text::custom_text; - -pub fn main() -> iced::Result { - Mode::run(Settings::default()) -} - -enum Mode { - Standard, - Custom, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - SwitchToComponent, -} - -impl Sandbox for Mode { - type Message = Message; - - fn new() -> Self { - Self::Standard - } - - fn title(&self) -> String { - String::from("Stateless Component Bug") - } - - fn update(&mut self, message: Message) { - match message { - Message::SwitchToComponent => *self = Mode::Custom, - } - } - - fn view(&self) -> Element { - match self { - Self::Standard => button(text("Click to Panic")) - .on_press(Message::SwitchToComponent) - .into(), - Self::Custom => button(custom_text("..")).into(), - } - } -} - -mod custom_text { - use iced::pure::text; - use iced_lazy::pure::{self, Component}; - use iced_native::text; - use iced_pure::Element; - - pub struct CustomText<'a> { - text: &'a str, - } - - pub fn custom_text<'a>(text: &'a str) -> CustomText<'a> { - CustomText { text } - } - - #[derive(Debug, Clone)] - pub enum Event {} - - impl<'a> CustomText<'a> { - pub fn new(text: &'a str) -> Self { - Self { text } - } - } - - impl<'a, Message, Renderer> Component for CustomText<'a> - where - Renderer: text::Renderer + 'static, - { - type State = (); - type Event = Event; - - fn update( - &mut self, - _state: &mut Self::State, - _event: Event, - ) -> Option { - None - } - - fn view(&self, _state: &Self::State) -> Element { - text(self.text).into() - } - } - - impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> - where - Message: 'a, - Renderer: 'static + text::Renderer, - { - fn from(x: CustomText<'a>) -> Self { - pure::component(x) - } - } -} -- cgit