From e189c22bb09e471e8f899ef184d2a99e2e22c484 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 9 Dec 2019 22:39:28 +0100 Subject: Rename `clock` example to `events` --- examples/events.rs | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 examples/events.rs (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs new file mode 100644 index 00000000..290aa975 --- /dev/null +++ b/examples/events.rs @@ -0,0 +1,129 @@ +use iced::{ + Align, Application, Checkbox, Column, Command, Container, Element, Length, + Settings, Subscription, Text, +}; + +pub fn main() { + env_logger::init(); + + Events::run(Settings::default()) +} + +#[derive(Debug, Default)] +struct Events { + last: Vec, + enabled: bool, +} + +#[derive(Debug, Clone)] +enum Message { + EventOccurred(iced_native::Event), + Toggled(bool), +} + +impl Application for Events { + type Message = Message; + + fn new() -> (Events, Command) { + (Events::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Events - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::EventOccurred(event) => { + self.last.push(event); + + if self.last.len() > 5 { + let _ = self.last.remove(0); + } + } + Message::Toggled(enabled) => { + self.enabled = enabled; + } + }; + + Command::none() + } + + fn subscriptions(&self) -> Subscription { + if self.enabled { + events::all(Message::EventOccurred) + } else { + Subscription::none() + } + } + + fn view(&mut self) -> Element { + let events = self.last.iter().fold( + Column::new().width(Length::Shrink).spacing(10), + |column, event| { + column.push( + Text::new(format!("{:?}", event)) + .size(40) + .width(Length::Shrink), + ) + }, + ); + + let toggle = Checkbox::new(self.enabled, "Enabled", Message::Toggled) + .width(Length::Shrink); + + let content = Column::new() + .width(Length::Shrink) + .align_items(Align::Center) + .spacing(20) + .push(events) + .push(toggle); + + Container::new(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} + +mod events { + use std::sync::Arc; + + pub fn all( + f: impl Fn(iced_native::Event) -> Message + 'static + Send + Sync, + ) -> iced::Subscription + where + Message: Send + 'static, + { + All(Arc::new(f)).into() + } + + struct All( + Arc Message + Send + Sync>, + ); + + impl iced_native::subscription::Connection for All + where + Message: 'static, + { + type Input = iced_native::subscription::Input; + type Output = Message; + + fn id(&self) -> u64 { + 0 + } + + fn stream( + &self, + input: iced_native::subscription::Input, + ) -> futures::stream::BoxStream<'static, Message> { + use futures::StreamExt; + + let function = self.0.clone(); + + input.map(move |event| function(event)).boxed() + } + } +} -- cgit From cdb7acf6c20fe13a09e75ea1c47d53ced6174698 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Dec 2019 03:43:00 +0100 Subject: Implement `Subscription::map` and `from_recipe` --- examples/events.rs | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index 290aa975..10758519 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -51,7 +51,7 @@ impl Application for Events { fn subscriptions(&self) -> Subscription { if self.enabled { - events::all(Message::EventOccurred) + events::all().map(Message::EventOccurred) } else { Subscription::none() } @@ -89,41 +89,33 @@ impl Application for Events { } mod events { - use std::sync::Arc; - - pub fn all( - f: impl Fn(iced_native::Event) -> Message + 'static + Send + Sync, - ) -> iced::Subscription - where - Message: Send + 'static, - { - All(Arc::new(f)).into() + pub fn all() -> iced::Subscription { + iced::Subscription::from_recipe(All) } - struct All( - Arc Message + Send + Sync>, - ); + struct All; - impl iced_native::subscription::Connection for All + impl + iced_native::subscription::Recipe + for All where - Message: 'static, + H: std::hash::Hasher, { - type Input = iced_native::subscription::Input; - type Output = Message; + type Output = iced_native::Event; - fn id(&self) -> u64 { - 0 + fn hash(&self, state: &mut H) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); } fn stream( &self, input: iced_native::subscription::Input, - ) -> futures::stream::BoxStream<'static, Message> { + ) -> futures::stream::BoxStream<'static, Self::Output> { use futures::StreamExt; - let function = self.0.clone(); - - input.map(move |event| function(event)).boxed() + input.boxed() } } } -- cgit From e06a4d1ce45be2d361879be480c4179e4532839d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Dec 2019 04:06:12 +0100 Subject: Simplify `events` example --- examples/events.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index 10758519..03e8d995 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -95,15 +95,15 @@ mod events { struct All; - impl - iced_native::subscription::Recipe - for All - where - H: std::hash::Hasher, + impl + iced_native::subscription::Recipe< + iced_native::Hasher, + iced_native::subscription::Input, + > for All { type Output = iced_native::Event; - fn hash(&self, state: &mut H) { + fn hash(&self, state: &mut iced_native::Hasher) { use std::hash::Hash; std::any::TypeId::of::().hash(state); -- cgit From ffa46898d983fc15ce6051a427622c058ce4e151 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 13 Dec 2019 23:58:23 +0100 Subject: Add `timer` example --- examples/events.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index 03e8d995..0a3d3ed3 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -4,8 +4,6 @@ use iced::{ }; pub fn main() { - env_logger::init(); - Events::run(Settings::default()) } -- cgit From c688452d7beb1b17ef8416fc101f8868767fc457 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 01:13:01 +0100 Subject: Consume `Recipe` when building a `Stream` --- examples/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index 0a3d3ed3..f9e606d8 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -108,7 +108,7 @@ mod events { } fn stream( - &self, + self: Box, input: iced_native::subscription::Input, ) -> futures::stream::BoxStream<'static, Self::Output> { use futures::StreamExt; -- cgit From ba06d458d33d98bfaa5e66b3512ce7f063e8d7ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 04:12:42 +0100 Subject: Move native events subscription to `iced_native` --- examples/events.rs | 42 +++++++----------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index f9e606d8..0b944495 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -49,7 +49,7 @@ impl Application for Events { fn subscriptions(&self) -> Subscription { if self.enabled { - events::all().map(Message::EventOccurred) + iced_native::subscription::events().map(Message::EventOccurred) } else { Subscription::none() } @@ -67,8 +67,12 @@ impl Application for Events { }, ); - let toggle = Checkbox::new(self.enabled, "Enabled", Message::Toggled) - .width(Length::Shrink); + let toggle = Checkbox::new( + self.enabled, + "Listen to runtime events", + Message::Toggled, + ) + .width(Length::Shrink); let content = Column::new() .width(Length::Shrink) @@ -85,35 +89,3 @@ impl Application for Events { .into() } } - -mod events { - pub fn all() -> iced::Subscription { - iced::Subscription::from_recipe(All) - } - - struct All; - - impl - iced_native::subscription::Recipe< - iced_native::Hasher, - iced_native::subscription::Input, - > for All - { - type Output = iced_native::Event; - - fn hash(&self, state: &mut iced_native::Hasher) { - use std::hash::Hash; - - std::any::TypeId::of::().hash(state); - } - - fn stream( - self: Box, - input: iced_native::subscription::Input, - ) -> futures::stream::BoxStream<'static, Self::Output> { - use futures::StreamExt; - - input.boxed() - } - } -} -- cgit From d6c3da21f7fe7a79bcfbc2a180dc111e42300a04 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 05:56:46 +0100 Subject: Write docs for subscriptions and reorganize a bit --- examples/events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/events.rs') diff --git a/examples/events.rs b/examples/events.rs index 0b944495..7d83fbd8 100644 --- a/examples/events.rs +++ b/examples/events.rs @@ -47,7 +47,7 @@ impl Application for Events { Command::none() } - fn subscriptions(&self) -> Subscription { + fn subscription(&self) -> Subscription { if self.enabled { iced_native::subscription::events().map(Message::EventOccurred) } else { -- cgit