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!)
This commit is contained in:
parent
b7b7741578
commit
76698ff2b5
34 changed files with 363 additions and 342 deletions
|
|
@ -21,3 +21,20 @@ impl Clipboard for Null {
|
|||
|
||||
fn write(&mut self, _contents: String) {}
|
||||
}
|
||||
|
||||
pub enum Action<T> {
|
||||
Read(Box<dyn Fn(Option<String>) -> T>),
|
||||
Write(Box<dyn Fn(String) -> T>),
|
||||
}
|
||||
|
||||
impl<T> Action<T> {
|
||||
pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action<A>
|
||||
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)))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
77
native/src/command.rs
Normal file
77
native/src/command.rs
Normal file
|
|
@ -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<T> {
|
||||
None,
|
||||
Single(Action<T>),
|
||||
Batch(Vec<Action<T>>),
|
||||
}
|
||||
|
||||
impl<T> Command<T> {
|
||||
/// 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<A>(
|
||||
future: impl Future<Output = T> + 'static + Send,
|
||||
f: impl Fn(T) -> A + 'static + Send,
|
||||
) -> Command<A> {
|
||||
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<A>(
|
||||
self,
|
||||
f: impl Fn(T) -> A + 'static + Send + Sync + Clone,
|
||||
) -> Command<A>
|
||||
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<Item = Command<T>>) -> 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<Action<T>> {
|
||||
match self {
|
||||
Self::None => Vec::new(),
|
||||
Self::Single(action) => vec![action],
|
||||
Self::Batch(batch) => batch,
|
||||
}
|
||||
}
|
||||
}
|
||||
24
native/src/command/action.rs
Normal file
24
native/src/command/action.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use crate::clipboard;
|
||||
use crate::window;
|
||||
|
||||
pub enum Action<T> {
|
||||
Future(iced_futures::BoxFuture<T>),
|
||||
Clipboard(clipboard::Action<T>),
|
||||
Window(window::Action),
|
||||
}
|
||||
|
||||
impl<T> Action<T> {
|
||||
/// Applies a transformation to the result of a [`Command`].
|
||||
pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action<A>
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<Self::Message>;
|
||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
||||
|
||||
/// Returns the widgets to display in the [`Program`].
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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<Command<P::Message>> {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
//! Build window-based GUI applications.
|
||||
mod action;
|
||||
mod event;
|
||||
|
||||
pub use action::Action;
|
||||
pub use event::Event;
|
||||
|
|
|
|||
18
native/src/window/action.rs
Normal file
18
native/src/window/action.rs
Normal file
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
@ -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,
|
||||
},
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue