From d575f4541126e2ab25908fe55c6805f16716b2a5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 5 Dec 2019 06:10:13 +0100 Subject: Draft first version of event subscriptions :tada: --- native/src/lib.rs | 4 ++-- native/src/widget/checkbox.rs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'native/src') diff --git a/native/src/lib.rs b/native/src/lib.rs index 45c3c699..9afb3bc9 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -52,8 +52,8 @@ mod size; mod user_interface; pub use iced_core::{ - Align, Background, Color, Command, Font, HorizontalAlignment, Length, - Point, Rectangle, Vector, VerticalAlignment, + subscription, Align, Background, Color, Command, Font, HorizontalAlignment, + Length, Point, Rectangle, Subscription, Vector, VerticalAlignment, }; pub use element::Element; diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 9563291c..159cba84 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -31,6 +31,7 @@ pub struct Checkbox { on_toggle: Box Message>, label: String, label_color: Option, + width: Length, } impl Checkbox { @@ -53,6 +54,7 @@ impl Checkbox { on_toggle: Box::new(f), label: String::from(label), label_color: None, + width: Length::Fill, } } @@ -63,6 +65,14 @@ impl Checkbox { self.label_color = Some(color.into()); self } + + /// Sets the width of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } } impl Widget for Checkbox @@ -70,7 +80,7 @@ where Renderer: self::Renderer + text::Renderer + row::Renderer, { fn width(&self) -> Length { - Length::Fill + Length::Shrink } fn height(&self) -> Length { @@ -85,6 +95,7 @@ where let size = self::Renderer::default_size(renderer); Row::<(), Renderer>::new() + .width(self.width) .spacing(15) .align_items(Align::Center) .push( @@ -92,7 +103,7 @@ where .width(Length::Units(size as u16)) .height(Length::Units(size as u16)), ) - .push(Text::new(&self.label)) + .push(Text::new(&self.label).width(self.width)) .layout(renderer, limits) } -- cgit From 98160406f714728afe718f305bf9d12be1676b2d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 8 Dec 2019 08:21:26 +0100 Subject: Allow listening to runtime events in subscriptions --- native/src/lib.rs | 8 +++++--- native/src/subscription.rs | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 native/src/subscription.rs (limited to 'native/src') diff --git a/native/src/lib.rs b/native/src/lib.rs index 9afb3bc9..af937a2f 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -34,7 +34,7 @@ //! [`Windowed`]: renderer/trait.Windowed.html //! [`UserInterface`]: struct.UserInterface.html //! [renderer]: renderer/index.html -#![deny(missing_docs)] +//#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![deny(unsafe_code)] @@ -42,6 +42,7 @@ pub mod input; pub mod layout; pub mod renderer; +pub mod subscription; pub mod widget; mod element; @@ -52,8 +53,8 @@ mod size; mod user_interface; pub use iced_core::{ - subscription, Align, Background, Color, Command, Font, HorizontalAlignment, - Length, Point, Rectangle, Subscription, Vector, VerticalAlignment, + Align, Background, Color, Command, Font, HorizontalAlignment, Length, + Point, Rectangle, Vector, VerticalAlignment, }; pub use element::Element; @@ -63,5 +64,6 @@ pub use layout::Layout; pub use mouse_cursor::MouseCursor; pub use renderer::Renderer; pub use size::Size; +pub use subscription::Subscription; pub use user_interface::{Cache, UserInterface}; pub use widget::*; diff --git a/native/src/subscription.rs b/native/src/subscription.rs new file mode 100644 index 00000000..5fa026a2 --- /dev/null +++ b/native/src/subscription.rs @@ -0,0 +1,6 @@ +use crate::Event; + +pub type Subscription = iced_core::Subscription; +pub type Input = futures::channel::mpsc::Receiver; + +pub use iced_core::subscription::Connection; -- 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` --- native/src/subscription.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native/src') diff --git a/native/src/subscription.rs b/native/src/subscription.rs index 5fa026a2..4d000490 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -1,6 +1,6 @@ -use crate::Event; +use crate::{Event, Hasher}; -pub type Subscription = iced_core::Subscription; +pub type Subscription = iced_core::Subscription; pub type Input = futures::channel::mpsc::Receiver; -pub use iced_core::subscription::Connection; +pub use iced_core::subscription::Recipe; -- 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` --- native/src/subscription.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'native/src') diff --git a/native/src/subscription.rs b/native/src/subscription.rs index 4d000490..c25e1cb4 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -4,3 +4,28 @@ pub type Subscription = iced_core::Subscription; pub type Input = futures::channel::mpsc::Receiver; pub use iced_core::subscription::Recipe; + +pub fn events() -> Subscription { + Subscription::from_recipe(Events) +} + +struct Events; + +impl Recipe for Events { + type Output = Event; + + fn hash(&self, state: &mut Hasher) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + } + + fn stream( + self: Box, + input: Input, + ) -> futures::stream::BoxStream<'static, Self::Output> { + use futures::StreamExt; + + input.boxed() + } +} -- cgit From 293314405f5b8d4003db5ef8f428e659ae36872d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 04:49:13 +0100 Subject: Make `iced_native` subscription input opaque --- native/src/subscription.rs | 31 ++++++++----------------------- native/src/subscription/events.rs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 native/src/subscription/events.rs (limited to 'native/src') diff --git a/native/src/subscription.rs b/native/src/subscription.rs index c25e1cb4..c49e24d2 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -1,31 +1,16 @@ use crate::{Event, Hasher}; +use futures::stream::BoxStream; -pub type Subscription = iced_core::Subscription; -pub type Input = futures::channel::mpsc::Receiver; +pub type EventStream = BoxStream<'static, Event>; -pub use iced_core::subscription::Recipe; - -pub fn events() -> Subscription { - Subscription::from_recipe(Events) -} +pub type Subscription = iced_core::Subscription; -struct Events; - -impl Recipe for Events { - type Output = Event; - - fn hash(&self, state: &mut Hasher) { - use std::hash::Hash; +pub use iced_core::subscription::Recipe; - std::any::TypeId::of::().hash(state); - } +mod events; - fn stream( - self: Box, - input: Input, - ) -> futures::stream::BoxStream<'static, Self::Output> { - use futures::StreamExt; +use events::Events; - input.boxed() - } +pub fn events() -> Subscription { + Subscription::from_recipe(Events) } diff --git a/native/src/subscription/events.rs b/native/src/subscription/events.rs new file mode 100644 index 00000000..b7301828 --- /dev/null +++ b/native/src/subscription/events.rs @@ -0,0 +1,23 @@ +use crate::{ + subscription::{EventStream, Recipe}, + Event, Hasher, +}; + +pub struct Events; + +impl Recipe for Events { + type Output = Event; + + fn hash(&self, state: &mut Hasher) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + } + + fn stream( + self: Box, + event_stream: EventStream, + ) -> futures::stream::BoxStream<'static, Self::Output> { + event_stream + } +} -- 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 --- native/src/lib.rs | 2 +- native/src/subscription.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'native/src') diff --git a/native/src/lib.rs b/native/src/lib.rs index af937a2f..c4d72df8 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -34,7 +34,7 @@ //! [`Windowed`]: renderer/trait.Windowed.html //! [`UserInterface`]: struct.UserInterface.html //! [renderer]: renderer/index.html -//#![deny(missing_docs)] +#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![deny(unsafe_code)] diff --git a/native/src/subscription.rs b/native/src/subscription.rs index c49e24d2..db88867a 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -1,16 +1,42 @@ +//! Listen to external events in your application. use crate::{Event, Hasher}; use futures::stream::BoxStream; -pub type EventStream = BoxStream<'static, Event>; - +/// A request to listen to external events. +/// +/// Besides performing async actions on demand with [`Command`], most +/// applications also need to listen to external events passively. +/// +/// A [`Subscription`] is normally provided to some runtime, like a [`Command`], +/// and it will generate events as long as the user keeps requesting it. +/// +/// For instance, you can use a [`Subscription`] to listen to a WebSocket +/// connection, keyboard presses, mouse events, time ticks, etc. +/// +/// [`Command`]: ../struct.Command.html +/// [`Subscription`]: struct.Subscription.html pub type Subscription = iced_core::Subscription; +/// A stream of runtime events. +/// +/// It is the input of a [`Subscription`] in the native runtime. +/// +/// [`Subscription`]: type.Subscription.html +pub type EventStream = BoxStream<'static, Event>; + pub use iced_core::subscription::Recipe; mod events; use events::Events; +/// Returns a [`Subscription`] to all the runtime events. +/// +/// This subscription will notify your application of any [`Event`] handled by +/// the runtime. +/// +/// [`Subscription`]: type.Subscription.html +/// [`Event`]: ../enum.Event.html pub fn events() -> Subscription { Subscription::from_recipe(Events) } -- cgit From 5185d6a0f3e5993247923e857164842ef8be9105 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2019 07:09:37 +0100 Subject: Fix `Widget::width` for `Checkbox` --- native/src/widget/checkbox.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src') diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 159cba84..ca4410b9 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -80,7 +80,7 @@ where Renderer: self::Renderer + text::Renderer + row::Renderer, { fn width(&self) -> Length { - Length::Shrink + self.width } fn height(&self) -> Length { -- cgit