Add `clipboard` argument to `Application::update`

This commit is contained in:
Héctor Ramón Jiménez 2021-03-11 03:38:20 +01:00
parent 7eb5127748
commit ae517b9fa0
18 changed files with 117 additions and 49 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, Color, Command, Container, Element, Length, executor, time, Application, Clipboard, Color, Command, Container, Element,
Point, Rectangle, Settings, Subscription, Vector, Length, Point, Rectangle, Settings, Subscription, Vector,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -40,7 +40,11 @@ impl Application for Clock {
String::from("Clock - Iced") String::from("Clock - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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, Column, Command, Container, button, executor, Align, Application, Button, Clipboard, Column, Command,
Element, Length, ProgressBar, Settings, Subscription, Text, Container, Element, Length, ProgressBar, Settings, Subscription, Text,
}; };
mod download; mod download;
@ -43,7 +43,11 @@ impl Application for Example {
String::from("Download progress - Iced") String::from("Download progress - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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,6 +1,6 @@
use iced::{ use iced::{
executor, Align, Application, Checkbox, Column, Command, Container, executor, Align, Application, Checkbox, Clipboard, Column, Command,
Element, Length, Settings, Subscription, Text, Container, Element, Length, Settings, Subscription, Text,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -32,7 +32,11 @@ impl Application for Events {
String::from("Events - Iced") String::from("Events - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match message { match message {
Message::EventOccurred(event) => { Message::EventOccurred(event) => {
self.last.push(event); self.last.push(event);

View File

@ -10,8 +10,8 @@ use iced::pick_list::{self, PickList};
use iced::slider::{self, Slider}; use iced::slider::{self, Slider};
use iced::time; use iced::time;
use iced::{ use iced::{
Align, Application, Checkbox, Column, Command, Container, Element, Length, Align, Application, Checkbox, Clipboard, Column, Command, Container,
Row, Settings, Subscription, Text, Element, Length, Row, Settings, Subscription, Text,
}; };
use preset::Preset; use preset::Preset;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -65,7 +65,11 @@ impl Application for GameOfLife {
String::from("Game of Life - Iced") String::from("Game of Life - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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_wgpu::Renderer; use iced_wgpu::Renderer;
use iced_winit::{ use iced_winit::{
slider, Align, Color, Column, Command, Element, Length, Program, Row, slider, Align, Clipboard, Color, Column, Command, Element, Length, Program,
Slider, Text, Row, Slider, Text,
}; };
pub struct Controls { pub struct Controls {
@ -30,8 +30,13 @@ 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(&mut self, message: Message) -> Command<Message> { fn update(
&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,8 @@
use iced::{ use iced::{
button, executor, keyboard, pane_grid, scrollable, Align, Application, button, executor, keyboard, pane_grid, scrollable, Align, Application,
Button, Color, Column, Command, Container, Element, HorizontalAlignment, Button, Clipboard, Color, Column, Command, Container, Element,
Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text, HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings,
Subscription, Text,
}; };
use iced_native::{event, subscription, Event}; use iced_native::{event, subscription, Event};
@ -49,7 +50,11 @@ impl Application for Example {
String::from("Pane grid - Iced") String::from("Pane grid - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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, Column, Command, button, futures, image, Align, Application, Button, Clipboard, Column,
Container, Element, Length, Row, Settings, Text, Command, Container, Element, Length, Row, Settings, Text,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -48,7 +48,11 @@ impl Application for Pokedex {
format!("{} - Pokédex", subtitle) format!("{} - Pokédex", subtitle)
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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, Color, Command, Element, executor, time, window, Application, Canvas, Clipboard, Color, Command,
Length, Point, Rectangle, Settings, Size, Subscription, Vector, Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector,
}; };
use std::time::Instant; use std::time::Instant;
@ -48,7 +48,11 @@ impl Application for SolarSystem {
String::from("Solar system - Iced") String::from("Solar system - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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, Column, Command, button, executor, time, Align, Application, Button, Clipboard, Column,
Container, Element, HorizontalAlignment, Length, Row, Settings, Command, Container, Element, HorizontalAlignment, Length, Row, Settings,
Subscription, Text, Subscription, Text,
}; };
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -49,7 +49,11 @@ impl Application for Stopwatch {
String::from("Stopwatch - Iced") String::from("Stopwatch - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&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,
Column, Command, Container, Element, Font, HorizontalAlignment, Length, Clipboard, Column, Command, Container, Element, Font, HorizontalAlignment,
Row, Scrollable, Settings, Text, TextInput, Length, Row, Scrollable, Settings, Text, TextInput,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -58,7 +58,11 @@ impl Application for Todos {
format!("Todos{} - Iced", if dirty { "*" } else { "" }) format!("Todos{} - Iced", if dirty { "*" } else { "" })
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(
&mut self,
message: Message,
_clipboard: &mut Clipboard,
) -> Command<Message> {
match self { match self {
Todos::Loading => { Todos::Loading => {
match message { match message {

View File

@ -190,6 +190,7 @@ async fn run_instance<A, E, C>(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug, &mut debug,
&mut clipboard,
&mut messages, &mut messages,
); );

View File

@ -17,7 +17,7 @@ pub use iced_native::*;
pub mod application; pub mod application;
pub use iced_winit::settings; pub use iced_winit::settings;
pub use iced_winit::{Error, Mode}; pub use iced_winit::{Clipboard, Error, Mode};
#[doc(no_inline)] #[doc(no_inline)]
pub use application::Application; pub use application::Application;

View File

@ -1,5 +1,5 @@
//! Build interactive programs using The Elm Architecture. //! Build interactive programs using The Elm Architecture.
use crate::{Command, Element, Renderer}; use crate::{Clipboard, Command, Element, Renderer};
mod state; mod state;
@ -13,6 +13,9 @@ 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 + Send; type Message: std::fmt::Debug + 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__,
@ -21,7 +24,11 @@ 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(&mut self, message: Self::Message) -> Command<Self::Message>; fn update(
&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,6 +1,5 @@
use crate::{ use crate::{
Cache, Clipboard, Command, Debug, Event, Point, Program, Renderer, Size, Cache, Command, Debug, Event, Point, Program, Renderer, Size, UserInterface,
UserInterface,
}; };
/// The execution state of a [`Program`]. It leverages caching, event /// The execution state of a [`Program`]. It leverages caching, event
@ -92,7 +91,7 @@ where
bounds: Size, bounds: Size,
cursor_position: Point, cursor_position: Point,
renderer: &mut P::Renderer, renderer: &mut P::Renderer,
clipboard: &mut dyn Clipboard, clipboard: &mut P::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(
@ -136,7 +135,7 @@ where
debug.log_message(&message); debug.log_message(&message);
debug.update_started(); debug.update_started();
let command = self.program.update(message); let command = self.program.update(message, clipboard);
debug.update_finished(); debug.update_finished();
command command

View File

@ -1,5 +1,7 @@
use crate::window; use crate::window;
use crate::{Color, Command, Element, Executor, Settings, Subscription}; use crate::{
Clipboard, Color, Command, Element, Executor, Settings, Subscription,
};
/// An interactive cross-platform application. /// An interactive cross-platform application.
/// ///
@ -57,7 +59,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// says "Hello, world!": /// says "Hello, world!":
/// ///
/// ```no_run /// ```no_run
/// use iced::{executor, Application, Command, Element, Settings, Text}; /// use iced::{executor, Application, Clipboard, Command, Element, Settings, Text};
/// ///
/// pub fn main() -> iced::Result { /// pub fn main() -> iced::Result {
/// Hello::run(Settings::default()) /// Hello::run(Settings::default())
@ -78,7 +80,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// String::from("A cool application") /// String::from("A cool application")
/// } /// }
/// ///
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> { /// fn update(&mut self, _message: Self::Message, _clipboard: &mut Clipboard) -> Command<Self::Message> {
/// Command::none() /// Command::none()
/// } /// }
/// ///
@ -127,7 +129,11 @@ 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(&mut self, message: Self::Message) -> Command<Self::Message>; fn update(
&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.
@ -228,9 +234,14 @@ 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(&mut self, message: Self::Message) -> Command<Self::Message> { fn update(
self.0.update(message) &mut self,
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> {

View File

@ -245,6 +245,7 @@ pub use sandbox::Sandbox;
pub use settings::Settings; pub use settings::Settings;
pub use runtime::{ pub use runtime::{
futures, Align, Background, Color, Command, Font, HorizontalAlignment, futures, Align, Background, Clipboard, Color, Command, Font,
Length, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment, HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector,
VerticalAlignment,
}; };

View File

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

@ -29,7 +29,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 { pub trait Application: Program<Clipboard = Clipboard> {
/// The data needed to initialize your [`Application`]. /// The data needed to initialize your [`Application`].
type Flags; type Flags;
@ -257,6 +257,7 @@ async fn run_instance<A, E, C>(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug, &mut debug,
&mut clipboard,
&mut messages, &mut messages,
); );
@ -409,13 +410,14 @@ 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>,
debug: &mut Debug, debug: &mut Debug,
clipboard: &mut A::Clipboard,
messages: &mut Vec<A::Message>, messages: &mut Vec<A::Message>,
) { ) {
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)); let command = runtime.enter(|| application.update(message, clipboard));
debug.update_finished(); debug.update_finished();
runtime.spawn(command); runtime.spawn(command);