From e0bdb203f2596a65b6ce8c6fb939f82a6dc24048 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Nov 2019 07:02:38 +0100 Subject: Implement future-based `Command` in `iced_core` --- core/Cargo.toml | 7 +++++++ core/src/command.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ core/src/lib.rs | 4 ++++ 3 files changed, 60 insertions(+) create mode 100644 core/src/command.rs (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index a244bcba..f2492345 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,3 +6,10 @@ edition = "2018" description = "The essential concepts of Iced" license = "MIT" repository = "https://github.com/hecrj/iced" + +[features] +# Exposes a future-based `Command` type +command = ["futures"] + +[dependencies] +futures = { version = "0.3", optional = true } diff --git a/core/src/command.rs b/core/src/command.rs new file mode 100644 index 00000000..ae901bd8 --- /dev/null +++ b/core/src/command.rs @@ -0,0 +1,49 @@ +use futures::future::{BoxFuture, Future, FutureExt}; + +pub struct Command { + futures: Vec>, +} + +impl Command { + pub fn none() -> Self { + Self { + futures: Vec::new(), + } + } + + pub fn attempt( + future: impl Future + 'static + Send, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command { + Command { + futures: vec![future.map(f).boxed()], + } + } + + pub fn batch(commands: impl Iterator>) -> Self { + Self { + futures: commands.flat_map(|command| command.futures).collect(), + } + } + + pub fn futures(self) -> Vec> { + self.futures + } +} + +impl From for Command +where + A: Future + 'static + Send, +{ + fn from(future: A) -> Self { + Self { + futures: vec![future.boxed()], + } + } +} + +impl std::fmt::Debug for Command { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Command").finish() + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index b61f2eae..3816f8a2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -3,6 +3,8 @@ pub mod widget; mod align; mod background; mod color; +#[cfg(feature = "command")] +mod command; mod font; mod length; mod point; @@ -12,6 +14,8 @@ mod vector; pub use align::Align; pub use background::Background; pub use color::Color; +#[cfg(feature = "command")] +pub use command::Command; pub use font::Font; pub use length::Length; pub use point::Point; -- cgit From e640b875900a3833fd38efa195e99b40ec3f6820 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Nov 2019 07:03:22 +0100 Subject: Derive `Clone` for `text_input::State` --- core/src/widget/text_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/widget/text_input.rs b/core/src/widget/text_input.rs index 450a7cae..16c67954 100644 --- a/core/src/widget/text_input.rs +++ b/core/src/widget/text_input.rs @@ -80,7 +80,7 @@ where } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub struct State { pub is_focused: bool, cursor_position: usize, -- cgit From a803ab240b9b5d1a073454f814229bad9d8bdafc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Nov 2019 00:10:40 +0100 Subject: Rename `Command::attempt` to `Command::perform` --- core/src/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/command.rs b/core/src/command.rs index ae901bd8..e1c865dd 100644 --- a/core/src/command.rs +++ b/core/src/command.rs @@ -11,7 +11,7 @@ impl Command { } } - pub fn attempt( + pub fn perform( future: impl Future + 'static + Send, f: impl Fn(T) -> A + 'static + Send, ) -> Command { -- cgit