From 76698ff2b5753e637b14533650c0d28e681be3c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Sep 2021 19:21:49 +0700 Subject: Make `Command` implementations platform-specific This allows us to introduce a platform-specific `Action` to both `iced_native` and `iced_web` and remove the `Clipboard` from `Application::update` to maintain purity. Additionally, this should let us implement further actions to let users query and modify the shell environment (e.g. window, clipboard, and more!) --- native/src/clipboard.rs | 17 ++++++++++ native/src/command.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++ native/src/command/action.rs | 24 ++++++++++++++ native/src/lib.rs | 8 +++-- native/src/program.rs | 11 ++----- native/src/program/state.rs | 7 ++-- native/src/window.rs | 2 ++ native/src/window/action.rs | 18 +++++++++++ native/src/window/event.rs | 5 ++- 9 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 native/src/command.rs create mode 100644 native/src/command/action.rs create mode 100644 native/src/window/action.rs (limited to 'native/src') diff --git a/native/src/clipboard.rs b/native/src/clipboard.rs index 081b4004..62dfc130 100644 --- a/native/src/clipboard.rs +++ b/native/src/clipboard.rs @@ -21,3 +21,20 @@ impl Clipboard for Null { fn write(&mut self, _contents: String) {} } + +pub enum Action { + Read(Box) -> T>), + Write(Box T>), +} + +impl Action { + pub fn map(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action + where + T: 'static, + { + match self { + Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))), + Self::Write(o) => Action::Write(Box::new(move |s| f(o(s)))), + } + } +} diff --git a/native/src/command.rs b/native/src/command.rs new file mode 100644 index 00000000..16fd73ba --- /dev/null +++ b/native/src/command.rs @@ -0,0 +1,77 @@ +mod action; + +pub use action::Action; + +use std::future::Future; + +/// A set of asynchronous actions to be performed by some runtime. +pub enum Command { + None, + Single(Action), + Batch(Vec>), +} + +impl Command { + /// Creates an empty [`Command`]. + /// + /// In other words, a [`Command`] that does nothing. + pub fn none() -> Self { + Self::None + } + + /// Creates a [`Command`] that performs the action of the given future. + pub fn perform( + future: impl Future + 'static + Send, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command { + use iced_futures::futures::FutureExt; + + Command::Single(Action::Future(Box::pin(future.map(f)))) + } + + /// Applies a transformation to the result of a [`Command`]. + pub fn map( + self, + f: impl Fn(T) -> A + 'static + Send + Sync + Clone, + ) -> Command + where + T: 'static, + { + match self { + Self::None => Command::None, + Self::Single(action) => Command::Single(action.map(f)), + Self::Batch(batch) => Command::Batch( + batch + .into_iter() + .map(|action| action.map(f.clone())) + .collect(), + ), + } + } + + /// Creates a [`Command`] that performs the actions of all the given + /// commands. + /// + /// Once this command is run, all the commands will be executed at once. + pub fn batch(commands: impl IntoIterator>) -> Self { + let mut batch = Vec::new(); + + for command in commands { + match command { + Self::None => {} + Self::Single(command) => batch.push(command), + Self::Batch(commands) => batch.extend(commands), + } + } + + Self::Batch(batch) + } + + pub fn actions(self) -> Vec> { + match self { + Self::None => Vec::new(), + Self::Single(action) => vec![action], + Self::Batch(batch) => batch, + } + } +} diff --git a/native/src/command/action.rs b/native/src/command/action.rs new file mode 100644 index 00000000..23d6e96e --- /dev/null +++ b/native/src/command/action.rs @@ -0,0 +1,24 @@ +use crate::clipboard; +use crate::window; + +pub enum Action { + Future(iced_futures::BoxFuture), + Clipboard(clipboard::Action), + Window(window::Action), +} + +impl Action { + /// Applies a transformation to the result of a [`Command`]. + pub fn map(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action + where + T: 'static, + { + use iced_futures::futures::FutureExt; + + match self { + Self::Future(future) => Action::Future(Box::pin(future.map(f))), + Self::Clipboard(action) => Action::Clipboard(action.map(f)), + Self::Window(window) => Action::Window(window), + } + } +} diff --git a/native/src/lib.rs b/native/src/lib.rs index cbb02506..f300e7b9 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -28,12 +28,13 @@ //! [`druid`]: https://github.com/xi-editor/druid //! [`raw-window-handle`]: https://github.com/rust-windowing/raw-window-handle //! [renderer]: crate::renderer -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] +//#![deny(missing_docs)] +//#![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] pub mod clipboard; +pub mod command; pub mod event; pub mod keyboard; pub mod layout; @@ -64,12 +65,13 @@ pub use iced_core::{ menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, }; -pub use iced_futures::{executor, futures, Command}; +pub use iced_futures::{executor, futures}; #[doc(no_inline)] pub use executor::Executor; pub use clipboard::Clipboard; +pub use command::Command; pub use debug::Debug; pub use element::Element; pub use event::Event; diff --git a/native/src/program.rs b/native/src/program.rs index 75fab094..fa83c0b1 100644 --- a/native/src/program.rs +++ b/native/src/program.rs @@ -1,5 +1,5 @@ //! Build interactive programs using The Elm Architecture. -use crate::{Clipboard, Command, Element, Renderer}; +use crate::{Command, Element, Renderer}; mod state; @@ -13,9 +13,6 @@ pub trait Program: Sized { /// The type of __messages__ your [`Program`] will produce. type Message: std::fmt::Debug + Clone + Send; - /// The type of [`Clipboard`] your [`Program`] will use. - type Clipboard: Clipboard; - /// Handles a __message__ and updates the state of the [`Program`]. /// /// This is where you define your __update logic__. All the __messages__, @@ -24,11 +21,7 @@ pub trait Program: Sized { /// /// Any [`Command`] returned will be executed immediately in the /// background by shells. - fn update( - &mut self, - message: Self::Message, - clipboard: &mut Self::Clipboard, - ) -> Command; + fn update(&mut self, message: Self::Message) -> Command; /// Returns the widgets to display in the [`Program`]. /// diff --git a/native/src/program/state.rs b/native/src/program/state.rs index fd1f2b52..3f5f6069 100644 --- a/native/src/program/state.rs +++ b/native/src/program/state.rs @@ -1,5 +1,6 @@ use crate::{ - Cache, Command, Debug, Event, Point, Program, Renderer, Size, UserInterface, + Cache, Clipboard, Command, Debug, Event, Point, Program, Renderer, Size, + UserInterface, }; /// The execution state of a [`Program`]. It leverages caching, event @@ -91,7 +92,7 @@ where bounds: Size, cursor_position: Point, renderer: &mut P::Renderer, - clipboard: &mut P::Clipboard, + clipboard: &mut dyn Clipboard, debug: &mut Debug, ) -> Option> { let mut user_interface = build_user_interface( @@ -135,7 +136,7 @@ where debug.log_message(&message); debug.update_started(); - let command = self.program.update(message, clipboard); + let command = self.program.update(message); debug.update_finished(); command diff --git a/native/src/window.rs b/native/src/window.rs index 220bb3be..62487fb9 100644 --- a/native/src/window.rs +++ b/native/src/window.rs @@ -1,4 +1,6 @@ //! Build window-based GUI applications. +mod action; mod event; +pub use action::Action; pub use event::Event; diff --git a/native/src/window/action.rs b/native/src/window/action.rs new file mode 100644 index 00000000..01294e83 --- /dev/null +++ b/native/src/window/action.rs @@ -0,0 +1,18 @@ +/// An operation to be performed on some window. +#[derive(Debug)] +pub enum Action { + /// Resize the window. + Resize { + /// The new logical width of the window + width: u32, + /// The new logical height of the window + height: u32, + }, + /// Move the window. + Move { + /// The new logical x location of the window + x: i32, + /// The new logical y location of the window + y: i32, + }, +} diff --git a/native/src/window/event.rs b/native/src/window/event.rs index 64f2b8d8..691af29a 100644 --- a/native/src/window/event.rs +++ b/native/src/window/event.rs @@ -13,10 +13,9 @@ pub enum Event { /// A window was resized. Resized { - /// The new width of the window (in units) + /// The new logical width of the window width: u32, - - /// The new height of the window (in units) + /// The new logical height of the window height: u32, }, -- cgit From c9711ff48fa1ad16365bc7eb37fa7153143bcee6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 2 Sep 2021 13:46:01 +0700 Subject: Handle `clipboard::Action` in `iced_winit` shell --- native/src/clipboard.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src') diff --git a/native/src/clipboard.rs b/native/src/clipboard.rs index 62dfc130..4d59d960 100644 --- a/native/src/clipboard.rs +++ b/native/src/clipboard.rs @@ -24,7 +24,7 @@ impl Clipboard for Null { pub enum Action { Read(Box) -> T>), - Write(Box T>), + Write(String), } impl Action { @@ -34,7 +34,7 @@ impl Action { { match self { Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))), - Self::Write(o) => Action::Write(Box::new(move |s| f(o(s)))), + Self::Write(content) => Action::Write(content), } } } -- cgit From 6fce35393fb2dc3dcbc5f423fa8472f5ce1f7027 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 2 Sep 2021 15:50:00 +0700 Subject: Hide implementation details of `Command` in `iced_futures` --- native/src/command.rs | 65 ++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) (limited to 'native/src') diff --git a/native/src/command.rs b/native/src/command.rs index 16fd73ba..1d41539b 100644 --- a/native/src/command.rs +++ b/native/src/command.rs @@ -5,18 +5,19 @@ pub use action::Action; use std::future::Future; /// A set of asynchronous actions to be performed by some runtime. -pub enum Command { - None, - Single(Action), - Batch(Vec>), -} +pub struct Command(iced_futures::Command>); impl Command { /// Creates an empty [`Command`]. /// /// In other words, a [`Command`] that does nothing. - pub fn none() -> Self { - Self::None + pub const fn none() -> Self { + Self(iced_futures::Command::none()) + } + + /// Creates a [`Command`] that performs a single [`Action`]. + pub const fn single(action: Action) -> Self { + Self(iced_futures::Command::single(action)) } /// Creates a [`Command`] that performs the action of the given future. @@ -26,7 +27,17 @@ impl Command { ) -> Command { use iced_futures::futures::FutureExt; - Command::Single(Action::Future(Box::pin(future.map(f)))) + Command::single(Action::Future(Box::pin(future.map(f)))) + } + + /// Creates a [`Command`] that performs the actions of all the given + /// commands. + /// + /// Once this command is run, all the commands will be executed at once. + pub fn batch(commands: impl IntoIterator>) -> Self { + Self(iced_futures::Command::batch( + commands.into_iter().map(|Command(command)| command), + )) } /// Applies a transformation to the result of a [`Command`]. @@ -37,41 +48,15 @@ impl Command { where T: 'static, { - match self { - Self::None => Command::None, - Self::Single(action) => Command::Single(action.map(f)), - Self::Batch(batch) => Command::Batch( - batch - .into_iter() - .map(|action| action.map(f.clone())) - .collect(), - ), - } - } + let Command(command) = self; - /// Creates a [`Command`] that performs the actions of all the given - /// commands. - /// - /// Once this command is run, all the commands will be executed at once. - pub fn batch(commands: impl IntoIterator>) -> Self { - let mut batch = Vec::new(); - - for command in commands { - match command { - Self::None => {} - Self::Single(command) => batch.push(command), - Self::Batch(commands) => batch.extend(commands), - } - } - - Self::Batch(batch) + Command(command.map(move |action| action.map(f.clone()))) } + /// Returns all of the actions of the [`Command`]. pub fn actions(self) -> Vec> { - match self { - Self::None => Vec::new(), - Self::Single(action) => vec![action], - Self::Batch(batch) => batch, - } + let Command(command) = self; + + command.actions() } } -- cgit From 2c630809d403f7fa87c3230be031299e5fb3af17 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 13 Sep 2021 11:20:54 +0700 Subject: Write missing docs and `Debug` implementations for `native` --- native/src/clipboard.rs | 17 +++++++++++++++++ native/src/command.rs | 10 ++++++++++ native/src/command/action.rs | 22 ++++++++++++++++++++++ native/src/lib.rs | 4 ++-- 4 files changed, 51 insertions(+), 2 deletions(-) (limited to 'native/src') diff --git a/native/src/clipboard.rs b/native/src/clipboard.rs index 4d59d960..60703c31 100644 --- a/native/src/clipboard.rs +++ b/native/src/clipboard.rs @@ -1,4 +1,5 @@ //! Access the clipboard. +use std::fmt; /// A buffer for short-term storage and transfer within and between /// applications. @@ -22,12 +23,19 @@ impl Clipboard for Null { fn write(&mut self, _contents: String) {} } +/// A clipboard action to be performed by some [`Command`]. +/// +/// [`Command`]: crate::Command pub enum Action { + /// Read the clipboard and produce `T` with the result. Read(Box) -> T>), + + /// Write the given contents to the clipboard. Write(String), } impl Action { + /// Maps the output of a clipboard [`Action`] using the provided closure. pub fn map(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action where T: 'static, @@ -38,3 +46,12 @@ impl Action { } } } + +impl fmt::Debug for Action { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Read(_) => write!(f, "Action::Read"), + Self::Write(_) => write!(f, "Action::Write"), + } + } +} diff --git a/native/src/command.rs b/native/src/command.rs index 1d41539b..6fe518d7 100644 --- a/native/src/command.rs +++ b/native/src/command.rs @@ -1,7 +1,9 @@ +//! Run asynchronous actions. mod action; pub use action::Action; +use std::fmt; use std::future::Future; /// A set of asynchronous actions to be performed by some runtime. @@ -60,3 +62,11 @@ impl Command { command.actions() } } + +impl fmt::Debug for Command { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Command(command) = self; + + command.fmt(f) + } +} diff --git a/native/src/command/action.rs b/native/src/command/action.rs index 23d6e96e..77be1b59 100644 --- a/native/src/command/action.rs +++ b/native/src/command/action.rs @@ -1,9 +1,19 @@ use crate::clipboard; use crate::window; +use std::fmt; + +/// An action that a [`Command`] can perform. +/// +/// [`Command`]: crate::Command pub enum Action { + /// Run a [`Future`] to completion. Future(iced_futures::BoxFuture), + + /// Run a clipboard action. Clipboard(clipboard::Action), + + /// Run a window action. Window(window::Action), } @@ -22,3 +32,15 @@ impl Action { } } } + +impl fmt::Debug for Action { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Future(_) => write!(f, "Action::Future"), + Self::Clipboard(action) => { + write!(f, "Action::Clipboard({:?})", action) + } + Self::Window(action) => write!(f, "Action::Window({:?})", action), + } + } +} diff --git a/native/src/lib.rs b/native/src/lib.rs index f300e7b9..573be51d 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -28,8 +28,8 @@ //! [`druid`]: https://github.com/xi-editor/druid //! [`raw-window-handle`]: https://github.com/rust-windowing/raw-window-handle //! [renderer]: crate::renderer -//#![deny(missing_docs)] -//#![deny(missing_debug_implementations)] +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] -- cgit