From 9b23ea698e98f5731a1253410e23697329083c78 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 17 Feb 2022 19:09:26 +0700 Subject: Implement `pure` version of `component` example --- examples/pure/component/src/main.rs | 168 ++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 examples/pure/component/src/main.rs (limited to 'examples/pure/component/src') diff --git a/examples/pure/component/src/main.rs b/examples/pure/component/src/main.rs new file mode 100644 index 00000000..0de7bdd9 --- /dev/null +++ b/examples/pure/component/src/main.rs @@ -0,0 +1,168 @@ +use iced::pure::widget::container; +use iced::pure::{Element, Sandbox}; +use iced::{Length, Settings}; + +use numeric_input::numeric_input; + +pub fn main() -> iced::Result { + Component::run(Settings::default()) +} + +#[derive(Default)] +struct Component { + value: Option, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + NumericInputChanged(Option), +} + +impl Sandbox for Component { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Component - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::NumericInputChanged(value) => { + self.value = value; + } + } + } + + fn view(&self) -> Element { + container(numeric_input(self.value, Message::NumericInputChanged)) + .padding(20) + .height(Length::Fill) + .center_y() + .into() + } +} + +mod numeric_input { + use iced::pure::widget::Element; + use iced::pure::widget::{row, text, text_input}; + use iced_lazy::pure::component::{self, Component}; + use iced_native::alignment::{self, Alignment}; + use iced_native::text; + use iced_native::Length; + + pub struct NumericInput { + value: Option, + on_change: Box) -> Message>, + } + + pub fn numeric_input( + value: Option, + on_change: impl Fn(Option) -> Message + 'static, + ) -> NumericInput { + NumericInput::new(value, on_change) + } + + #[derive(Debug, Clone)] + pub enum Event { + InputChanged(String), + IncrementPressed, + DecrementPressed, + } + + impl NumericInput { + pub fn new( + value: Option, + on_change: impl Fn(Option) -> Message + 'static, + ) -> Self { + Self { + value, + on_change: Box::new(on_change), + } + } + } + + impl Component for NumericInput + where + Renderer: text::Renderer + 'static, + { + type State = (); + type Event = Event; + + fn update( + &mut self, + _state: &mut Self::State, + event: Event, + ) -> Option { + match event { + Event::IncrementPressed => Some((self.on_change)(Some( + self.value.unwrap_or_default().saturating_add(1), + ))), + Event::DecrementPressed => Some((self.on_change)(Some( + self.value.unwrap_or_default().saturating_sub(1), + ))), + Event::InputChanged(value) => { + if value.is_empty() { + Some((self.on_change)(None)) + } else { + value + .parse() + .ok() + .map(Some) + .map(self.on_change.as_ref()) + } + } + } + } + + fn view(&self, _state: &Self::State) -> Element { + let button = |label, on_press| { + use iced::pure::widget::button; + + button( + text(label) + .width(Length::Fill) + .height(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .vertical_alignment(alignment::Vertical::Center), + ) + .width(Length::Units(50)) + .on_press(on_press) + }; + + row() + .push(button("-", Event::DecrementPressed)) + .push( + text_input( + "Type a number", + self.value + .as_ref() + .map(u32::to_string) + .as_ref() + .map(String::as_str) + .unwrap_or(""), + Event::InputChanged, + ) + .padding(10), + ) + .push(button("+", Event::IncrementPressed)) + .align_items(Alignment::Fill) + .spacing(10) + .into() + } + } + + impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> + where + Message: 'a, + Renderer: 'static + text::Renderer, + { + fn from(numeric_input: NumericInput) -> Self { + component::view(numeric_input) + } + } +} -- cgit From b50e208f31e51c2d947aeaf9fea8a9f287f26aa1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Mar 2022 18:04:13 +0700 Subject: Implement `pure::Responsive` in `iced_lazy` --- examples/pure/component/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/pure/component/src') diff --git a/examples/pure/component/src/main.rs b/examples/pure/component/src/main.rs index 0de7bdd9..ab10317f 100644 --- a/examples/pure/component/src/main.rs +++ b/examples/pure/component/src/main.rs @@ -49,7 +49,7 @@ impl Sandbox for Component { mod numeric_input { use iced::pure::widget::Element; use iced::pure::widget::{row, text, text_input}; - use iced_lazy::pure::component::{self, Component}; + use iced_lazy::pure::{self, Component}; use iced_native::alignment::{self, Alignment}; use iced_native::text; use iced_native::Length; @@ -162,7 +162,7 @@ mod numeric_input { Renderer: 'static + text::Renderer, { fn from(numeric_input: NumericInput) -> Self { - component::view(numeric_input) + pure::component(numeric_input) } } } -- 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` --- examples/pure/component/src/main.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'examples/pure/component/src') diff --git a/examples/pure/component/src/main.rs b/examples/pure/component/src/main.rs index ab10317f..b38d6fca 100644 --- a/examples/pure/component/src/main.rs +++ b/examples/pure/component/src/main.rs @@ -1,4 +1,4 @@ -use iced::pure::widget::container; +use iced::pure::container; use iced::pure::{Element, Sandbox}; use iced::{Length, Settings}; @@ -47,12 +47,12 @@ impl Sandbox for Component { } mod numeric_input { - use iced::pure::widget::Element; - use iced::pure::widget::{row, text, text_input}; + use iced::pure::{button, row, text, text_input}; use iced_lazy::pure::{self, Component}; use iced_native::alignment::{self, Alignment}; use iced_native::text; use iced_native::Length; + use iced_pure::Element; pub struct NumericInput { value: Option, @@ -120,8 +120,6 @@ mod numeric_input { fn view(&self, _state: &Self::State) -> Element { let button = |label, on_press| { - use iced::pure::widget::button; - button( text(label) .width(Length::Fill) -- cgit