Merge pull request #1019 from hecrj/command-actions

Platform-specific `Command` implementations
This commit is contained in:
Héctor Ramón 2021-09-13 11:49:06 +07:00 committed by GitHub
commit 93fec8d273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 559 additions and 326 deletions

View file

@ -1,7 +1,7 @@
use iced::{ use iced::{
canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke}, canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke},
executor, time, Application, Clipboard, Color, Command, Container, Element, executor, time, Application, Color, Command, Container, Element, Length,
Length, Point, Rectangle, Settings, Subscription, Vector, Point, Rectangle, Settings, Subscription, Vector,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -40,11 +40,7 @@ impl Application for Clock {
String::from("Clock - Iced") String::from("Clock - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Tick(local_time) => { Message::Tick(local_time) => {
let now = local_time; let now = local_time;

View file

@ -1,6 +1,6 @@
use iced::{ use iced::{
button, executor, Align, Application, Button, Clipboard, Column, Command, button, executor, Align, Application, Button, Column, Command, Container,
Container, Element, Length, ProgressBar, Settings, Subscription, Text, Element, Length, ProgressBar, Settings, Subscription, Text,
}; };
mod download; mod download;
@ -43,11 +43,7 @@ impl Application for Example {
String::from("Download progress - Iced") String::from("Download progress - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Add => { Message::Add => {
self.last_id = self.last_id + 1; self.last_id = self.last_id + 1;

View file

@ -1,7 +1,7 @@
use iced::{ use iced::{
button, executor, Align, Application, Button, Checkbox, Clipboard, Column, button, executor, Align, Application, Button, Checkbox, Column, Command,
Command, Container, Element, HorizontalAlignment, Length, Settings, Container, Element, HorizontalAlignment, Length, Settings, Subscription,
Subscription, Text, Text,
}; };
use iced_native::{window, Event}; use iced_native::{window, Event};
@ -40,11 +40,7 @@ impl Application for Events {
String::from("Events - Iced") String::from("Events - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::EventOccurred(event) if self.enabled => { Message::EventOccurred(event) if self.enabled => {
self.last.push(event); self.last.push(event);

View file

@ -12,8 +12,8 @@ use iced::slider::{self, Slider};
use iced::time; use iced::time;
use iced::window; use iced::window;
use iced::{ use iced::{
Align, Application, Checkbox, Clipboard, Column, Command, Container, Align, Application, Checkbox, Column, Command, Container, Element, Length,
Element, Length, Row, Settings, Subscription, Text, Row, Settings, Subscription, Text,
}; };
use preset::Preset; use preset::Preset;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -71,11 +71,7 @@ impl Application for GameOfLife {
String::from("Game of Life - Iced") String::from("Game of Life - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Grid(message, version) => { Message::Grid(message, version) => {
if version == self.version { if version == self.version {

View file

@ -1,7 +1,7 @@
use iced_glow::Renderer; use iced_glow::Renderer;
use iced_glutin::{ use iced_glutin::{
slider, Align, Clipboard, Color, Column, Command, Element, Length, Program, slider, Align, Color, Column, Command, Element, Length, Program, Row,
Row, Slider, Text, Slider, Text,
}; };
pub struct Controls { pub struct Controls {
@ -30,13 +30,8 @@ impl Controls {
impl Program for Controls { impl Program for Controls {
type Renderer = Renderer; type Renderer = Renderer;
type Message = Message; type Message = Message;
type Clipboard = Clipboard;
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::BackgroundColorChanged(color) => { Message::BackgroundColorChanged(color) => {
self.background_color = color; self.background_color = color;

View file

@ -1,7 +1,7 @@
use iced_wgpu::Renderer; use iced_wgpu::Renderer;
use iced_winit::{ use iced_winit::{
slider, Align, Clipboard, Color, Column, Command, Element, Length, Program, slider, Align, Color, Column, Command, Element, Length, Program, Row,
Row, Slider, Text, Slider, Text,
}; };
pub struct Controls { pub struct Controls {
@ -30,13 +30,8 @@ impl Controls {
impl Program for Controls { impl Program for Controls {
type Renderer = Renderer; type Renderer = Renderer;
type Message = Message; type Message = Message;
type Clipboard = Clipboard;
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::BackgroundColorChanged(color) => { Message::BackgroundColorChanged(color) => {
self.background_color = color; self.background_color = color;

View file

@ -1,7 +1,6 @@
use iced::menu::{self, Menu}; use iced::menu::{self, Menu};
use iced::{ use iced::{
executor, Application, Clipboard, Command, Container, Element, Length, executor, Application, Command, Container, Element, Length, Settings, Text,
Settings, Text,
}; };
use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; use iced_native::keyboard::{Hotkey, KeyCode, Modifiers};
@ -92,11 +91,7 @@ impl Application for App {
]) ])
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::MenuActivated(entry) => self.selected = Some(entry), Message::MenuActivated(entry) => self.selected = Some(entry),
} }

View file

@ -1,8 +1,7 @@
use iced::{ use iced::{
button, executor, keyboard, pane_grid, scrollable, Align, Application, button, executor, keyboard, pane_grid, scrollable, Align, Application,
Button, Clipboard, Color, Column, Command, Container, Element, Button, Color, Column, Command, Container, Element, HorizontalAlignment,
HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings, Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text,
Subscription, Text,
}; };
use iced_native::{event, subscription, Event}; use iced_native::{event, subscription, Event};
@ -51,11 +50,7 @@ impl Application for Example {
String::from("Pane grid - Iced") String::from("Pane grid - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Split(axis, pane) => { Message::Split(axis, pane) => {
let result = self.panes.split( let result = self.panes.split(

View file

@ -1,6 +1,6 @@
use iced::{ use iced::{
button, futures, image, Align, Application, Button, Clipboard, Column, button, futures, image, Align, Application, Button, Column, Command,
Command, Container, Element, Length, Row, Settings, Text, Container, Element, Length, Row, Settings, Text,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -48,11 +48,7 @@ impl Application for Pokedex {
format!("{} - Pokédex", subtitle) format!("{} - Pokédex", subtitle)
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::PokemonFound(Ok(pokemon)) => { Message::PokemonFound(Ok(pokemon)) => {
*self = Pokedex::Loaded { *self = Pokedex::Loaded {

View file

@ -8,8 +8,8 @@
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::{ use iced::{
canvas::{self, Cursor, Path, Stroke}, canvas::{self, Cursor, Path, Stroke},
executor, time, window, Application, Canvas, Clipboard, Color, Command, executor, time, window, Application, Canvas, Color, Command, Element,
Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, Length, Point, Rectangle, Settings, Size, Subscription, Vector,
}; };
use std::time::Instant; use std::time::Instant;
@ -48,11 +48,7 @@ impl Application for SolarSystem {
String::from("Solar system - Iced") String::from("Solar system - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Tick(instant) => { Message::Tick(instant) => {
self.state.update(instant); self.state.update(instant);

View file

@ -1,6 +1,6 @@
use iced::{ use iced::{
button, executor, time, Align, Application, Button, Clipboard, Column, button, executor, time, Align, Application, Button, Column, Command,
Command, Container, Element, HorizontalAlignment, Length, Row, Settings, Container, Element, HorizontalAlignment, Length, Row, Settings,
Subscription, Text, Subscription, Text,
}; };
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -49,11 +49,7 @@ impl Application for Stopwatch {
String::from("Stopwatch - Iced") String::from("Stopwatch - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::Toggle => match self.state { Message::Toggle => match self.state {
State::Idle => { State::Idle => {

View file

@ -1,7 +1,7 @@
use iced::{ use iced::{
button, scrollable, text_input, Align, Application, Button, Checkbox, button, scrollable, text_input, Align, Application, Button, Checkbox,
Clipboard, Column, Command, Container, Element, Font, HorizontalAlignment, Column, Command, Container, Element, Font, HorizontalAlignment, Length,
Length, Row, Scrollable, Settings, Text, TextInput, Row, Scrollable, Settings, Text, TextInput,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -58,11 +58,7 @@ impl Application for Todos {
format!("Todos{} - Iced", if dirty { "*" } else { "" }) format!("Todos{} - Iced", if dirty { "*" } else { "" })
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match self { match self {
Todos::Loading => { Todos::Loading => {
match message { match message {

View file

@ -1,6 +1,6 @@
use iced::{ use iced::{
executor, Application, Clipboard, Command, Container, Element, Length, executor, Application, Command, Container, Element, Length, Settings,
Settings, Subscription, Text, Subscription, Text,
}; };
use iced_native::{ use iced_native::{
event::{MacOS, PlatformSpecific}, event::{MacOS, PlatformSpecific},
@ -34,11 +34,7 @@ impl Application for App {
String::from("Url - Iced") String::from("Url - Iced")
} }
fn update( fn update(&mut self, message: Message) -> Command<Message> {
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::EventOccurred(event) => { Message::EventOccurred(event) => {
if let Event::PlatformSpecific(PlatformSpecific::MacOS( if let Event::PlatformSpecific(PlatformSpecific::MacOS(

View file

@ -1,91 +1,25 @@
use crate::BoxFuture; /// A set of asynchronous actions to be performed by some runtime.
use futures::future::{Future, FutureExt}; #[derive(Debug)]
pub struct Command<T>(Internal<T>);
/// A collection of async operations. #[derive(Debug)]
/// enum Internal<T> {
/// You should be able to turn a future easily into a [`Command`], either by None,
/// using the `From` trait or [`Command::perform`]. Single(T),
pub struct Command<T> { Batch(Vec<T>),
futures: Vec<BoxFuture<T>>,
} }
impl<T> Command<T> { impl<T> Command<T> {
/// Creates an empty [`Command`]. /// Creates an empty [`Command`].
/// ///
/// In other words, a [`Command`] that does nothing. /// In other words, a [`Command`] that does nothing.
pub fn none() -> Self { pub const fn none() -> Self {
Self { Self(Internal::None)
futures: Vec::new(),
}
} }
/// Creates a [`Command`] that performs the action of the given future. /// Creates a [`Command`] that performs a single [`Action`].
#[cfg(not(target_arch = "wasm32"))] pub const fn single(action: T) -> Self {
pub fn perform<A>( Self(Internal::Single(action))
future: impl Future<Output = T> + 'static + Send,
f: impl Fn(T) -> A + 'static + Send,
) -> Command<A> {
Command {
futures: vec![Box::pin(future.map(f))],
}
}
/// Creates a [`Command`] that performs the action of the given future.
#[cfg(target_arch = "wasm32")]
pub fn perform<A>(
future: impl Future<Output = T> + 'static,
f: impl Fn(T) -> A + 'static + Send,
) -> Command<A> {
Command {
futures: vec![Box::pin(future.map(f))],
}
}
/// Applies a transformation to the result of a [`Command`].
#[cfg(not(target_arch = "wasm32"))]
pub fn map<A>(
mut self,
f: impl Fn(T) -> A + 'static + Send + Sync,
) -> Command<A>
where
T: 'static,
{
let f = std::sync::Arc::new(f);
Command {
futures: self
.futures
.drain(..)
.map(|future| {
let f = f.clone();
Box::pin(future.map(move |result| f(result)))
as BoxFuture<A>
})
.collect(),
}
}
/// Applies a transformation to the result of a [`Command`].
#[cfg(target_arch = "wasm32")]
pub fn map<A>(mut self, f: impl Fn(T) -> A + 'static) -> Command<A>
where
T: 'static,
{
let f = std::rc::Rc::new(f);
Command {
futures: self
.futures
.drain(..)
.map(|future| {
let f = f.clone();
Box::pin(future.map(move |result| f(result)))
as BoxFuture<A>
})
.collect(),
}
} }
/// Creates a [`Command`] that performs the actions of all the given /// Creates a [`Command`] that performs the actions of all the given
@ -93,46 +27,43 @@ impl<T> Command<T> {
/// ///
/// Once this command is run, all the commands will be executed at once. /// Once this command is run, all the commands will be executed at once.
pub fn batch(commands: impl IntoIterator<Item = Command<T>>) -> Self { pub fn batch(commands: impl IntoIterator<Item = Command<T>>) -> Self {
Self { let mut batch = Vec::new();
futures: commands
.into_iter() for Command(command) in commands {
.flat_map(|command| command.futures) match command {
.collect(), Internal::None => {}
Internal::Single(command) => batch.push(command),
Internal::Batch(commands) => batch.extend(commands),
}
}
Self(Internal::Batch(batch))
}
/// Applies a transformation to the result of a [`Command`].
pub fn map<A>(self, f: impl Fn(T) -> A) -> Command<A>
where
T: 'static,
{
let Command(command) = self;
match command {
Internal::None => Command::none(),
Internal::Single(action) => Command::single(f(action)),
Internal::Batch(batch) => {
Command(Internal::Batch(batch.into_iter().map(f).collect()))
}
} }
} }
/// Converts a [`Command`] into its underlying list of futures. /// Returns all of the actions of the [`Command`].
pub fn futures(self) -> Vec<BoxFuture<T>> { pub fn actions(self) -> Vec<T> {
self.futures let Command(command) = self;
}
}
#[cfg(not(target_arch = "wasm32"))] match command {
impl<T, A> From<A> for Command<T> Internal::None => Vec::new(),
where Internal::Single(action) => vec![action],
A: Future<Output = T> + 'static + Send, Internal::Batch(batch) => batch,
{
fn from(future: A) -> Self {
Self {
futures: vec![future.boxed()],
} }
} }
} }
#[cfg(target_arch = "wasm32")]
impl<T, A> From<A> for Command<T>
where
A: Future<Output = T> + 'static,
{
fn from(future: A) -> Self {
Self {
futures: vec![future.boxed_local()],
}
}
}
impl<T> std::fmt::Debug for Command<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Command").finish()
}
}

View file

@ -1,5 +1,6 @@
//! Run commands and keep track of subscriptions. //! Run commands and keep track of subscriptions.
use crate::{subscription, Command, Executor, Subscription}; use crate::BoxFuture;
use crate::{subscription, Executor, Subscription};
use futures::{channel::mpsc, Sink}; use futures::{channel::mpsc, Sink};
use std::marker::PhantomData; use std::marker::PhantomData;
@ -51,22 +52,18 @@ where
/// ///
/// The resulting `Message` will be forwarded to the `Sender` of the /// The resulting `Message` will be forwarded to the `Sender` of the
/// [`Runtime`]. /// [`Runtime`].
pub fn spawn(&mut self, command: Command<Message>) { pub fn spawn(&mut self, future: BoxFuture<Message>) {
use futures::{FutureExt, SinkExt}; use futures::{FutureExt, SinkExt};
let futures = command.futures(); let mut sender = self.sender.clone();
for future in futures { let future = future.then(|message| async move {
let mut sender = self.sender.clone(); let _ = sender.send(message).await;
let future = future.then(|message| async move { ()
let _ = sender.send(message).await; });
() self.executor.spawn(future);
});
self.executor.spawn(future);
}
} }
/// Tracks a [`Subscription`] in the [`Runtime`]. /// Tracks a [`Subscription`] in the [`Runtime`].

View file

@ -33,6 +33,8 @@ where
debug.startup_started(); debug.startup_started();
let event_loop = EventLoop::with_user_event(); let event_loop = EventLoop::with_user_event();
let mut proxy = event_loop.create_proxy();
let mut runtime = { let mut runtime = {
let executor = E::new().map_err(Error::ExecutorCreationFailed)?; let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
let proxy = Proxy::new(event_loop.create_proxy()); let proxy = Proxy::new(event_loop.create_proxy());
@ -48,9 +50,6 @@ where
let subscription = application.subscription(); let subscription = application.subscription();
runtime.spawn(init_command);
runtime.track(subscription);
let context = { let context = {
let builder = settings let builder = settings
.window .window
@ -90,6 +89,17 @@ where
})? })?
}; };
let mut clipboard = Clipboard::connect(context.window());
application::run_command(
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
context.window(),
);
runtime.track(subscription);
let (mut sender, receiver) = mpsc::unbounded(); let (mut sender, receiver) = mpsc::unbounded();
let mut instance = Box::pin(run_instance::<A, E, C>( let mut instance = Box::pin(run_instance::<A, E, C>(
@ -97,6 +107,8 @@ where
compositor, compositor,
renderer, renderer,
runtime, runtime,
clipboard,
proxy,
debug, debug,
receiver, receiver,
context, context,
@ -145,6 +157,8 @@ async fn run_instance<A, E, C>(
mut compositor: C, mut compositor: C,
mut renderer: A::Renderer, mut renderer: A::Renderer,
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut clipboard: Clipboard,
mut proxy: glutin::event_loop::EventLoopProxy<A::Message>,
mut debug: Debug, mut debug: Debug,
mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>, mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>,
mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>, mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>,
@ -157,8 +171,6 @@ async fn run_instance<A, E, C>(
use glutin::event; use glutin::event;
use iced_winit::futures::stream::StreamExt; use iced_winit::futures::stream::StreamExt;
let mut clipboard = Clipboard::connect(context.window());
let mut state = application::State::new(&application, context.window()); let mut state = application::State::new(&application, context.window());
let mut viewport_version = state.viewport_version(); let mut viewport_version = state.viewport_version();
let mut user_interface = let mut user_interface =
@ -210,9 +222,11 @@ async fn run_instance<A, E, C>(
application::update( application::update(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug,
&mut clipboard, &mut clipboard,
&mut proxy,
&mut debug,
&mut messages, &mut messages,
context.window(),
); );
// Update window // Update window

View file

@ -16,10 +16,14 @@ pub use iced_native::*;
pub mod application; pub mod application;
pub use iced_winit::clipboard;
pub use iced_winit::settings; pub use iced_winit::settings;
pub use iced_winit::{Clipboard, Error, Mode}; pub use iced_winit::window;
pub use iced_winit::{Error, Mode};
#[doc(no_inline)] #[doc(no_inline)]
pub use application::Application; pub use application::Application;
#[doc(no_inline)] #[doc(no_inline)]
pub use clipboard::Clipboard;
#[doc(no_inline)]
pub use settings::Settings; pub use settings::Settings;

View file

@ -1,4 +1,5 @@
//! Access the clipboard. //! Access the clipboard.
use std::fmt;
/// A buffer for short-term storage and transfer within and between /// A buffer for short-term storage and transfer within and between
/// applications. /// applications.
@ -21,3 +22,36 @@ impl Clipboard for Null {
fn write(&mut self, _contents: String) {} fn write(&mut self, _contents: String) {}
} }
/// A clipboard action to be performed by some [`Command`].
///
/// [`Command`]: crate::Command
pub enum Action<T> {
/// Read the clipboard and produce `T` with the result.
Read(Box<dyn Fn(Option<String>) -> T>),
/// Write the given contents to the clipboard.
Write(String),
}
impl<T> Action<T> {
/// Maps the output of a clipboard [`Action`] using the provided closure.
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(content) => Action::Write(content),
}
}
}
impl<T> fmt::Debug for Action<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Read(_) => write!(f, "Action::Read"),
Self::Write(_) => write!(f, "Action::Write"),
}
}
}

72
native/src/command.rs Normal file
View file

@ -0,0 +1,72 @@
//! 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.
pub struct Command<T>(iced_futures::Command<Action<T>>);
impl<T> Command<T> {
/// Creates an empty [`Command`].
///
/// In other words, a [`Command`] that does nothing.
pub const fn none() -> Self {
Self(iced_futures::Command::none())
}
/// Creates a [`Command`] that performs a single [`Action`].
pub const fn single(action: Action<T>) -> Self {
Self(iced_futures::Command::single(action))
}
/// 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))))
}
/// 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 {
Self(iced_futures::Command::batch(
commands.into_iter().map(|Command(command)| command),
))
}
/// 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,
{
let Command(command) = self;
Command(command.map(move |action| action.map(f.clone())))
}
/// Returns all of the actions of the [`Command`].
pub fn actions(self) -> Vec<Action<T>> {
let Command(command) = self;
command.actions()
}
}
impl<T> fmt::Debug for Command<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Command(command) = self;
command.fmt(f)
}
}

View file

@ -0,0 +1,46 @@
use crate::clipboard;
use crate::window;
use std::fmt;
/// An action that a [`Command`] can perform.
///
/// [`Command`]: crate::Command
pub enum Action<T> {
/// Run a [`Future`] to completion.
Future(iced_futures::BoxFuture<T>),
/// Run a clipboard action.
Clipboard(clipboard::Action<T>),
/// Run a window action.
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),
}
}
}
impl<T> fmt::Debug for Action<T> {
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),
}
}
}

View file

@ -34,6 +34,7 @@
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#![forbid(rust_2018_idioms)] #![forbid(rust_2018_idioms)]
pub mod clipboard; pub mod clipboard;
pub mod command;
pub mod event; pub mod event;
pub mod keyboard; pub mod keyboard;
pub mod layout; pub mod layout;
@ -64,12 +65,13 @@ pub use iced_core::{
menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu, menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu,
Padding, Point, Rectangle, Size, Vector, VerticalAlignment, Padding, Point, Rectangle, Size, Vector, VerticalAlignment,
}; };
pub use iced_futures::{executor, futures, Command}; pub use iced_futures::{executor, futures};
#[doc(no_inline)] #[doc(no_inline)]
pub use executor::Executor; pub use executor::Executor;
pub use clipboard::Clipboard; pub use clipboard::Clipboard;
pub use command::Command;
pub use debug::Debug; pub use debug::Debug;
pub use element::Element; pub use element::Element;
pub use event::Event; pub use event::Event;

View file

@ -1,5 +1,5 @@
//! Build interactive programs using The Elm Architecture. //! Build interactive programs using The Elm Architecture.
use crate::{Clipboard, Command, Element, Renderer}; use crate::{Command, Element, Renderer};
mod state; mod state;
@ -13,9 +13,6 @@ pub trait Program: Sized {
/// The type of __messages__ your [`Program`] will produce. /// The type of __messages__ your [`Program`] will produce.
type Message: std::fmt::Debug + Clone + Send; 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`]. /// Handles a __message__ and updates the state of the [`Program`].
/// ///
/// This is where you define your __update logic__. All the __messages__, /// 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 /// Any [`Command`] returned will be executed immediately in the
/// background by shells. /// background by shells.
fn update( fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
&mut self,
message: Self::Message,
clipboard: &mut Self::Clipboard,
) -> Command<Self::Message>;
/// Returns the widgets to display in the [`Program`]. /// Returns the widgets to display in the [`Program`].
/// ///

View file

@ -1,5 +1,6 @@
use crate::{ 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 /// The execution state of a [`Program`]. It leverages caching, event
@ -91,7 +92,7 @@ where
bounds: Size, bounds: Size,
cursor_position: Point, cursor_position: Point,
renderer: &mut P::Renderer, renderer: &mut P::Renderer,
clipboard: &mut P::Clipboard, clipboard: &mut dyn Clipboard,
debug: &mut Debug, debug: &mut Debug,
) -> Option<Command<P::Message>> { ) -> Option<Command<P::Message>> {
let mut user_interface = build_user_interface( let mut user_interface = build_user_interface(
@ -135,7 +136,7 @@ where
debug.log_message(&message); debug.log_message(&message);
debug.update_started(); debug.update_started();
let command = self.program.update(message, clipboard); let command = self.program.update(message);
debug.update_finished(); debug.update_finished();
command command

View file

@ -1,4 +1,6 @@
//! Build window-based GUI applications. //! Build window-based GUI applications.
mod action;
mod event; mod event;
pub use action::Action;
pub use event::Event; pub use event::Event;

View 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,
},
}

View file

@ -13,10 +13,9 @@ pub enum Event {
/// A window was resized. /// A window was resized.
Resized { Resized {
/// The new width of the window (in units) /// The new logical width of the window
width: u32, width: u32,
/// The new logical height of the window
/// The new height of the window (in units)
height: u32, height: u32,
}, },

View file

@ -1,7 +1,5 @@
use crate::window; use crate::window;
use crate::{ use crate::{Color, Command, Element, Executor, Menu, Settings, Subscription};
Clipboard, Color, Command, Element, Executor, Menu, Settings, Subscription,
};
/// An interactive cross-platform application. /// An interactive cross-platform application.
/// ///
@ -59,7 +57,7 @@ use crate::{
/// says "Hello, world!": /// says "Hello, world!":
/// ///
/// ```no_run /// ```no_run
/// use iced::{executor, Application, Clipboard, Command, Element, Settings, Text}; /// use iced::{executor, Application, Command, Element, Settings, Text};
/// ///
/// pub fn main() -> iced::Result { /// pub fn main() -> iced::Result {
/// Hello::run(Settings::default()) /// Hello::run(Settings::default())
@ -80,7 +78,7 @@ use crate::{
/// String::from("A cool application") /// String::from("A cool application")
/// } /// }
/// ///
/// fn update(&mut self, _message: Self::Message, _clipboard: &mut Clipboard) -> Command<Self::Message> { /// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
/// Command::none() /// Command::none()
/// } /// }
/// ///
@ -129,11 +127,7 @@ pub trait Application: Sized {
/// this method. /// this method.
/// ///
/// Any [`Command`] returned will be executed immediately in the background. /// Any [`Command`] returned will be executed immediately in the background.
fn update( fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
&mut self,
message: Self::Message,
clipboard: &mut Clipboard,
) -> Command<Self::Message>;
/// Returns the event [`Subscription`] for the current state of the /// Returns the event [`Subscription`] for the current state of the
/// application. /// application.
@ -249,14 +243,9 @@ where
{ {
type Renderer = crate::renderer::Renderer; type Renderer = crate::renderer::Renderer;
type Message = A::Message; type Message = A::Message;
type Clipboard = iced_winit::Clipboard;
fn update( fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
&mut self, self.0.update(message)
message: Self::Message,
clipboard: &mut iced_winit::Clipboard,
) -> Command<Self::Message> {
self.0.update(message, clipboard)
} }
fn view(&mut self) -> Element<'_, Self::Message> { fn view(&mut self) -> Element<'_, Self::Message> {
@ -329,12 +318,8 @@ where
self.0.title() self.0.title()
} }
fn update( fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
&mut self, self.0.update(message)
message: Self::Message,
clipboard: &mut Clipboard,
) -> Command<Self::Message> {
self.0.update(message, clipboard)
} }
fn subscription(&self) -> Subscription<Self::Message> { fn subscription(&self) -> Subscription<Self::Message> {

3
src/clipboard.rs Normal file
View file

@ -0,0 +1,3 @@
//! Access the clipboard.
#[cfg(not(target_arch = "wasm32"))]
pub use crate::runtime::clipboard::{read, write};

View file

@ -183,6 +183,7 @@ mod error;
mod result; mod result;
mod sandbox; mod sandbox;
pub mod clipboard;
pub mod executor; pub mod executor;
pub mod keyboard; pub mod keyboard;
pub mod mouse; pub mod mouse;
@ -245,7 +246,7 @@ pub use sandbox::Sandbox;
pub use settings::Settings; pub use settings::Settings;
pub use runtime::{ pub use runtime::{
futures, menu, Align, Background, Clipboard, Color, Command, Font, futures, menu, Align, Background, Color, Command, Font,
HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription, HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription,
Vector, VerticalAlignment, Vector, VerticalAlignment,
}; };

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
Application, Clipboard, Color, Command, Element, Error, Settings, Application, Color, Command, Element, Error, Settings, Subscription,
Subscription,
}; };
/// A sandboxed [`Application`]. /// A sandboxed [`Application`].
@ -162,11 +161,7 @@ where
T::title(self) T::title(self)
} }
fn update( fn update(&mut self, message: T::Message) -> Command<T::Message> {
&mut self,
message: T::Message,
_clipboard: &mut Clipboard,
) -> Command<T::Message> {
T::update(self, message); T::update(self, message);
Command::none() Command::none()

View file

@ -9,3 +9,6 @@ pub use icon::Icon;
pub use mode::Mode; pub use mode::Mode;
pub use position::Position; pub use position::Position;
pub use settings::Settings; pub use settings::Settings;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::runtime::window::{move_to, resize};

View file

@ -1,21 +0,0 @@
/// A buffer for short-term storage and transfer within and between
/// applications.
#[derive(Debug, Clone, Copy)]
pub struct Clipboard;
impl Clipboard {
/// Creates a new [`Clipboard`].
pub fn new() -> Self {
Self
}
/// Reads the current content of the [`Clipboard`] as text.
pub fn read(&self) -> Option<String> {
unimplemented! {}
}
/// Writes the given text contents to the [`Clipboard`].
pub fn write(&mut self, _contents: String) {
unimplemented! {}
}
}

72
web/src/command.rs Normal file
View file

@ -0,0 +1,72 @@
mod action;
pub use action::Action;
use std::fmt;
#[cfg(target_arch = "wasm32")]
use std::future::Future;
/// A set of asynchronous actions to be performed by some runtime.
pub struct Command<T>(iced_futures::Command<Action<T>>);
impl<T> Command<T> {
/// Creates an empty [`Command`].
///
/// In other words, a [`Command`] that does nothing.
pub const fn none() -> Self {
Self(iced_futures::Command::none())
}
/// Creates a [`Command`] that performs a single [`Action`].
pub const fn single(action: Action<T>) -> Self {
Self(iced_futures::Command::single(action))
}
/// Creates a [`Command`] that performs the action of the given future.
#[cfg(target_arch = "wasm32")]
pub fn perform<A>(
future: impl Future<Output = T> + 'static,
f: impl Fn(T) -> A + 'static + Send,
) -> Command<A> {
use iced_futures::futures::FutureExt;
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<Item = Command<T>>) -> Self {
Self(iced_futures::Command::batch(
commands.into_iter().map(|Command(command)| command),
))
}
/// Applies a transformation to the result of a [`Command`].
#[cfg(target_arch = "wasm32")]
pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Clone) -> Command<A>
where
T: 'static,
{
let Command(command) = self;
Command(command.map(move |action| action.map(f.clone())))
}
/// Returns all of the actions of the [`Command`].
pub fn actions(self) -> Vec<Action<T>> {
let Command(command) = self;
command.actions()
}
}
impl<T> fmt::Debug for Command<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Command(command) = self;
command.fmt(f)
}
}

28
web/src/command/action.rs Normal file
View file

@ -0,0 +1,28 @@
pub enum Action<T> {
Future(iced_futures::BoxFuture<T>),
}
use std::fmt;
impl<T> Action<T> {
/// Applies a transformation to the result of a [`Command`].
#[cfg(target_arch = "wasm32")]
pub fn map<A>(self, f: impl Fn(T) -> A + 'static) -> Action<A>
where
T: 'static,
{
use iced_futures::futures::FutureExt;
match self {
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
}
}
}
impl<T> fmt::Debug for Action<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Future(_) => write!(f, "Action::Future"),
}
}
}

View file

@ -59,7 +59,7 @@ use dodrio::bumpalo;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
mod bus; mod bus;
mod clipboard; mod command;
mod element; mod element;
mod hasher; mod hasher;
@ -68,7 +68,7 @@ pub mod subscription;
pub mod widget; pub mod widget;
pub use bus::Bus; pub use bus::Bus;
pub use clipboard::Clipboard; pub use command::Command;
pub use css::Css; pub use css::Css;
pub use dodrio; pub use dodrio;
pub use element::Element; pub use element::Element;
@ -77,7 +77,7 @@ pub use iced_core::{
keyboard, menu, mouse, Align, Background, Color, Font, HorizontalAlignment, keyboard, menu, mouse, Align, Background, Color, Font, HorizontalAlignment,
Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment,
}; };
pub use iced_futures::{executor, futures, Command}; pub use iced_futures::{executor, futures};
pub use subscription::Subscription; pub use subscription::Subscription;
#[doc(no_inline)] #[doc(no_inline)]
@ -128,11 +128,7 @@ pub trait Application {
/// this method. /// this method.
/// ///
/// Any [`Command`] returned will be executed immediately in the background. /// Any [`Command`] returned will be executed immediately in the background.
fn update( fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
&mut self,
message: Self::Message,
clipboard: &mut Clipboard,
) -> Command<Self::Message>;
/// Returns the widgets to display in the [`Application`]. /// Returns the widgets to display in the [`Application`].
/// ///
@ -162,8 +158,6 @@ pub trait Application {
let document = window.document().unwrap(); let document = window.document().unwrap();
let body = document.body().unwrap(); let body = document.body().unwrap();
let mut clipboard = Clipboard::new();
let (sender, receiver) = let (sender, receiver) =
iced_futures::futures::channel::mpsc::unbounded(); iced_futures::futures::channel::mpsc::unbounded();
@ -177,7 +171,7 @@ pub trait Application {
let mut title = app.title(); let mut title = app.title();
document.set_title(&title); document.set_title(&title);
runtime.spawn(command); run_command(command, &mut runtime);
let application = Rc::new(RefCell::new(app)); let application = Rc::new(RefCell::new(app));
@ -190,8 +184,7 @@ pub trait Application {
let event_loop = receiver.for_each(move |message| { let event_loop = receiver.for_each(move |message| {
let (command, subscription) = runtime.enter(|| { let (command, subscription) = runtime.enter(|| {
let command = let command = application.borrow_mut().update(message);
application.borrow_mut().update(message, &mut clipboard);
let subscription = application.borrow().subscription(); let subscription = application.borrow().subscription();
(command, subscription) (command, subscription)
@ -199,7 +192,7 @@ pub trait Application {
let new_title = application.borrow().title(); let new_title = application.borrow().title();
runtime.spawn(command); run_command(command, &mut runtime);
runtime.track(subscription); runtime.track(subscription);
if title != new_title { if title != new_title {
@ -350,8 +343,7 @@ pub trait Embedded {
); );
let (app, command) = runtime.enter(|| Self::new(flags)); let (app, command) = runtime.enter(|| Self::new(flags));
run_command(command, &mut runtime);
runtime.spawn(command);
let application = Rc::new(RefCell::new(app)); let application = Rc::new(RefCell::new(app));
@ -370,7 +362,7 @@ pub trait Embedded {
(command, subscription) (command, subscription)
}); });
runtime.spawn(command); run_command(command, &mut runtime);
runtime.track(subscription); runtime.track(subscription);
vdom.weak().schedule_render(); vdom.weak().schedule_render();
@ -382,6 +374,25 @@ pub trait Embedded {
} }
} }
fn run_command<Message: 'static + Send, E: Executor>(
command: Command<Message>,
runtime: &mut iced_futures::Runtime<
Hasher,
(),
E,
iced_futures::futures::channel::mpsc::UnboundedSender<Message>,
Message,
>,
) {
for action in command.actions() {
match action {
command::Action::Future(future) => {
runtime.spawn(future);
}
}
}
}
struct EmbeddedInstance<A: Embedded> { struct EmbeddedInstance<A: Embedded> {
application: Rc<RefCell<A>>, application: Rc<RefCell<A>>,
bus: Bus<A::Message>, bus: Bus<A::Message>,

View file

@ -3,11 +3,12 @@ mod state;
pub use state::State; pub use state::State;
use crate::clipboard::{self, Clipboard};
use crate::conversion; use crate::conversion;
use crate::mouse; use crate::mouse;
use crate::{ use crate::{
Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings,
Settings, Size, Subscription, Size, Subscription,
}; };
use iced_futures::futures; use iced_futures::futures;
@ -30,7 +31,7 @@ use std::mem::ManuallyDrop;
/// ///
/// When using an [`Application`] with the `debug` feature enabled, a debug view /// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`. /// can be toggled by pressing `F12`.
pub trait Application: Program<Clipboard = Clipboard> { pub trait Application: Program {
/// The data needed to initialize your [`Application`]. /// The data needed to initialize your [`Application`].
type Flags; type Flags;
@ -127,6 +128,7 @@ where
debug.startup_started(); debug.startup_started();
let event_loop = EventLoop::with_user_event(); let event_loop = EventLoop::with_user_event();
let mut proxy = event_loop.create_proxy();
let mut runtime = { let mut runtime = {
let proxy = Proxy::new(event_loop.create_proxy()); let proxy = Proxy::new(event_loop.create_proxy());
@ -143,9 +145,6 @@ where
let subscription = application.subscription(); let subscription = application.subscription();
runtime.spawn(init_command);
runtime.track(subscription);
let window = settings let window = settings
.window .window
.into_builder( .into_builder(
@ -158,6 +157,17 @@ where
.build(&event_loop) .build(&event_loop)
.map_err(Error::WindowCreationFailed)?; .map_err(Error::WindowCreationFailed)?;
let mut clipboard = Clipboard::connect(&window);
run_command(
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
&window,
);
runtime.track(subscription);
let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;
let (mut sender, receiver) = mpsc::unbounded(); let (mut sender, receiver) = mpsc::unbounded();
@ -167,6 +177,8 @@ where
compositor, compositor,
renderer, renderer,
runtime, runtime,
clipboard,
proxy,
debug, debug,
receiver, receiver,
window, window,
@ -215,6 +227,8 @@ async fn run_instance<A, E, C>(
mut compositor: C, mut compositor: C,
mut renderer: A::Renderer, mut renderer: A::Renderer,
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut clipboard: Clipboard,
mut proxy: winit::event_loop::EventLoopProxy<A::Message>,
mut debug: Debug, mut debug: Debug,
mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>, mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
window: winit::window::Window, window: winit::window::Window,
@ -228,7 +242,6 @@ async fn run_instance<A, E, C>(
use winit::event; use winit::event;
let mut surface = compositor.create_surface(&window); let mut surface = compositor.create_surface(&window);
let mut clipboard = Clipboard::connect(&window);
let mut state = State::new(&application, &window); let mut state = State::new(&application, &window);
let mut viewport_version = state.viewport_version(); let mut viewport_version = state.viewport_version();
@ -289,9 +302,11 @@ async fn run_instance<A, E, C>(
update( update(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug,
&mut clipboard, &mut clipboard,
&mut proxy,
&mut debug,
&mut messages, &mut messages,
&window,
); );
// Update window // Update window
@ -491,20 +506,68 @@ pub fn build_user_interface<'a, A: Application>(
pub fn update<A: Application, E: Executor>( pub fn update<A: Application, E: Executor>(
application: &mut A, application: &mut A,
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
clipboard: &mut Clipboard,
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
debug: &mut Debug, debug: &mut Debug,
clipboard: &mut A::Clipboard,
messages: &mut Vec<A::Message>, messages: &mut Vec<A::Message>,
window: &winit::window::Window,
) { ) {
for message in messages.drain(..) { for message in messages.drain(..) {
debug.log_message(&message); debug.log_message(&message);
debug.update_started(); debug.update_started();
let command = runtime.enter(|| application.update(message, clipboard)); let command = runtime.enter(|| application.update(message));
debug.update_finished(); debug.update_finished();
runtime.spawn(command); run_command(command, runtime, clipboard, proxy, window);
} }
let subscription = application.subscription(); let subscription = application.subscription();
runtime.track(subscription); runtime.track(subscription);
} }
/// Runs the actions of a [`Command`].
pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
command: Command<Message>,
runtime: &mut Runtime<E, Proxy<Message>, Message>,
clipboard: &mut Clipboard,
proxy: &mut winit::event_loop::EventLoopProxy<Message>,
window: &winit::window::Window,
) {
use iced_native::command;
use iced_native::window;
for action in command.actions() {
match action {
command::Action::Future(future) => {
runtime.spawn(future);
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read());
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write(contents);
}
},
command::Action::Window(action) => match action {
window::Action::Resize { width, height } => {
window.set_inner_size(winit::dpi::LogicalSize {
width,
height,
});
}
window::Action::Move { x, y } => {
window.set_outer_position(winit::dpi::LogicalPosition {
x,
y,
});
}
},
}
}
}

View file

@ -1,3 +1,8 @@
//! Access the clipboard.
pub use iced_native::clipboard::Action;
use crate::command::{self, Command};
/// A buffer for short-term storage and transfer within and between /// A buffer for short-term storage and transfer within and between
/// applications. /// applications.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
@ -52,3 +57,15 @@ impl iced_native::Clipboard for Clipboard {
self.write(contents) self.write(contents)
} }
} }
/// Read the current contents of the clipboard.
pub fn read<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Read(Box::new(f))))
}
/// Write the given contents to the clipboard.
pub fn write<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Write(contents)))
}

View file

@ -25,10 +25,11 @@ pub use iced_native::*;
pub use winit; pub use winit;
pub mod application; pub mod application;
pub mod clipboard;
pub mod conversion; pub mod conversion;
pub mod settings; pub mod settings;
pub mod window;
mod clipboard;
mod error; mod error;
mod mode; mod mode;
mod position; mod position;

18
winit/src/window.rs Normal file
View file

@ -0,0 +1,18 @@
//! Interact with the window of your application.
use crate::command::{self, Command};
use iced_native::window;
pub use window::Event;
/// Resizes the window to the given logical dimensions.
pub fn resize<Message>(width: u32, height: u32) -> Command<Message> {
Command::single(command::Action::Window(window::Action::Resize {
width,
height,
}))
}
/// Moves a window to the given logical coordinates.
pub fn move_to<Message>(x: i32, y: i32) -> Command<Message> {
Command::single(command::Action::Window(window::Action::Move { x, y }))
}