From 5cc1aa31c774894d9a48ee005e857913c9720908 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 6 Apr 2022 10:27:45 -0700 Subject: add example --- examples/pure/nested_component/Cargo.toml | 12 +++ examples/pure/nested_component/src/main.rs | 166 +++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 examples/pure/nested_component/Cargo.toml create mode 100644 examples/pure/nested_component/src/main.rs (limited to 'examples/pure') diff --git a/examples/pure/nested_component/Cargo.toml b/examples/pure/nested_component/Cargo.toml new file mode 100644 index 00000000..6b1b3570 --- /dev/null +++ b/examples/pure/nested_component/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pure_nested_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/nested_component/src/main.rs b/examples/pure/nested_component/src/main.rs new file mode 100644 index 00000000..d2f0ac37 --- /dev/null +++ b/examples/pure/nested_component/src/main.rs @@ -0,0 +1,166 @@ +use iced::pure::container; +use iced::pure::{Element, Sandbox}; +use iced::{Length, Settings}; + +use counter::counter; + +pub fn main() -> iced::Result { + Component::run(Settings::default()) +} + +#[derive(Default)] +struct Component; + +#[derive(Debug, Clone, Copy)] +enum Message {} + +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) {} + + fn view(&self) -> Element { + container(counter()) + .padding(20) + .height(Length::Fill) + .center_y() + .into() + } +} + +mod counter { + use iced::pure::{column, text}; + use iced_lazy::pure::{self, Component}; + use iced_native::text; + use iced_pure::Element; + + use custom_button::custom_button; + + pub struct Counter; + + pub fn counter() -> Counter { + Counter::new() + } + + #[derive(Debug, Clone)] + pub enum Event { + IncrementPressed, + } + + impl Counter { + pub fn new() -> Self { + Self {} + } + } + + #[derive(Default)] + pub struct State { + count: u32, + } + + impl Component for Counter + where + Renderer: text::Renderer + 'static, + { + type State = State; + type Event = Event; + + fn update( + &mut self, + state: &mut Self::State, + event: Event, + ) -> Option { + match event { + Event::IncrementPressed => { + state.count += 1; + + None + } + } + } + + fn view(&self, state: &Self::State) -> Element { + column() + .push(text(&state.count.to_string())) + .push(custom_button("Increment", Event::IncrementPressed)) + .into() + } + } + + impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> + where + Message: 'a, + Renderer: 'static + text::Renderer, + { + fn from(counter: Counter) -> Self { + pure::component(counter) + } + } + + mod custom_button { + use iced::pure::{button, text}; + use iced_lazy::pure::{self, Component}; + use iced_native::text; + use iced_pure::Element; + + pub struct CustomButton<'a, Message> { + text: &'a str, + on_press: Message, + } + + pub fn custom_button<'a, Message>( + text: &'a str, + on_press: Message, + ) -> CustomButton<'a, Message> { + CustomButton { text, on_press } + } + + #[derive(Clone)] + pub enum Event { + Press, + } + + impl<'a, Message, Renderer> Component + for CustomButton<'a, Message> + where + Message: Clone, + Renderer: iced_native::Renderer + text::Renderer + 'static, + { + type State = (); + type Event = Event; + + fn update( + &mut self, + _state: &mut Self::State, + event: Event, + ) -> Option { + match event { + Event::Press => Some(self.on_press.clone()), + } + } + + fn view(&self, _state: &Self::State) -> Element { + button(text(self.text)).on_press(Event::Press).into() + } + } + + impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> + where + Message: Clone + 'a, + Renderer: 'static + text::Renderer, + { + fn from(custom_button: CustomButton<'a, Message>) -> Self { + pure::component(custom_button) + } + } + } +} -- cgit