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') 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