Skip to content

Commit

Permalink
Merge pull request #773 from hecrj/feature/clipboard-access-in-update
Browse files Browse the repository at this point in the history
Clipboard access in `Application::update`
  • Loading branch information
hecrj authored Mar 12, 2021
2 parents 7eb5127 + 7da3fb1 commit c1f70f1
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 58 deletions.
10 changes: 7 additions & 3 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced::{
canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke},
executor, time, Application, Color, Command, Container, Element, Length,
Point, Rectangle, Settings, Subscription, Vector,
executor, time, Application, Clipboard, Color, Command, Container, Element,
Length, Point, Rectangle, Settings, Subscription, Vector,
};

pub fn main() -> iced::Result {
Expand Down Expand Up @@ -40,7 +40,11 @@ impl Application for Clock {
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 {
Message::Tick(local_time) => {
let now = local_time;
Expand Down
10 changes: 7 additions & 3 deletions examples/download_progress/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
button, executor, Align, Application, Button, Column, Command, Container,
Element, Length, ProgressBar, Settings, Subscription, Text,
button, executor, Align, Application, Button, Clipboard, Column, Command,
Container, Element, Length, ProgressBar, Settings, Subscription, Text,
};

mod download;
Expand Down Expand Up @@ -43,7 +43,11 @@ impl Application for Example {
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 {
Message::Add => {
self.last_id = self.last_id + 1;
Expand Down
10 changes: 7 additions & 3 deletions examples/events/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
executor, Align, Application, Checkbox, Column, Command, Container,
Element, Length, Settings, Subscription, Text,
executor, Align, Application, Checkbox, Clipboard, Column, Command,
Container, Element, Length, Settings, Subscription, Text,
};

pub fn main() -> iced::Result {
Expand Down Expand Up @@ -32,7 +32,11 @@ impl Application for Events {
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 {
Message::EventOccurred(event) => {
self.last.push(event);
Expand Down
10 changes: 7 additions & 3 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use iced::pick_list::{self, PickList};
use iced::slider::{self, Slider};
use iced::time;
use iced::{
Align, Application, Checkbox, Column, Command, Container, Element, Length,
Row, Settings, Subscription, Text,
Align, Application, Checkbox, Clipboard, Column, Command, Container,
Element, Length, Row, Settings, Subscription, Text,
};
use preset::Preset;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -65,7 +65,11 @@ impl Application for GameOfLife {
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 {
Message::Grid(message, version) => {
if version == self.version {
Expand Down
11 changes: 8 additions & 3 deletions examples/integration/src/controls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced_wgpu::Renderer;
use iced_winit::{
slider, Align, Color, Column, Command, Element, Length, Program, Row,
Slider, Text,
slider, Align, Clipboard, Color, Column, Command, Element, Length, Program,
Row, Slider, Text,
};

pub struct Controls {
Expand Down Expand Up @@ -30,8 +30,13 @@ impl Controls {
impl Program for Controls {
type Renderer = Renderer;
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 {
Message::BackgroundColorChanged(color) => {
self.background_color = color;
Expand Down
11 changes: 8 additions & 3 deletions examples/pane_grid/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use iced::{
button, executor, keyboard, pane_grid, scrollable, Align, Application,
Button, Color, Column, Command, Container, Element, HorizontalAlignment,
Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text,
Button, Clipboard, Color, Column, Command, Container, Element,
HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings,
Subscription, Text,
};
use iced_native::{event, subscription, Event};

Expand Down Expand Up @@ -49,7 +50,11 @@ impl Application for Example {
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 {
Message::Split(axis, pane) => {
let result = self.panes.split(
Expand Down
10 changes: 7 additions & 3 deletions examples/pokedex/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
button, futures, image, Align, Application, Button, Column, Command,
Container, Element, Length, Row, Settings, Text,
button, futures, image, Align, Application, Button, Clipboard, Column,
Command, Container, Element, Length, Row, Settings, Text,
};

pub fn main() -> iced::Result {
Expand Down Expand Up @@ -48,7 +48,11 @@ impl Application for Pokedex {
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 {
Message::PokemonFound(Ok(pokemon)) => {
*self = Pokedex::Loaded {
Expand Down
10 changes: 7 additions & 3 deletions examples/solar_system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::{
canvas::{self, Cursor, Path, Stroke},
executor, time, window, Application, Canvas, Color, Command, Element,
Length, Point, Rectangle, Settings, Size, Subscription, Vector,
executor, time, window, Application, Canvas, Clipboard, Color, Command,
Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector,
};

use std::time::Instant;
Expand Down Expand Up @@ -48,7 +48,11 @@ impl Application for SolarSystem {
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 {
Message::Tick(instant) => {
self.state.update(instant);
Expand Down
10 changes: 7 additions & 3 deletions examples/stopwatch/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
button, executor, time, Align, Application, Button, Column, Command,
Container, Element, HorizontalAlignment, Length, Row, Settings,
button, executor, time, Align, Application, Button, Clipboard, Column,
Command, Container, Element, HorizontalAlignment, Length, Row, Settings,
Subscription, Text,
};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -49,7 +49,11 @@ impl Application for Stopwatch {
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 {
Message::Toggle => match self.state {
State::Idle => {
Expand Down
10 changes: 7 additions & 3 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced::{
button, scrollable, text_input, Align, Application, Button, Checkbox,
Column, Command, Container, Element, Font, HorizontalAlignment, Length,
Row, Scrollable, Settings, Text, TextInput,
Clipboard, Column, Command, Container, Element, Font, HorizontalAlignment,
Length, Row, Scrollable, Settings, Text, TextInput,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -58,7 +58,11 @@ impl Application for Todos {
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 {
Todos::Loading => {
match message {
Expand Down
1 change: 1 addition & 0 deletions glutin/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ async fn run_instance<A, E, C>(
&mut application,
&mut runtime,
&mut debug,
&mut clipboard,
&mut messages,
);

Expand Down
2 changes: 1 addition & 1 deletion glutin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use iced_native::*;
pub mod application;

pub use iced_winit::settings;
pub use iced_winit::{Error, Mode};
pub use iced_winit::{Clipboard, Error, Mode};

#[doc(no_inline)]
pub use application::Application;
Expand Down
11 changes: 9 additions & 2 deletions native/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Build interactive programs using The Elm Architecture.
use crate::{Command, Element, Renderer};
use crate::{Clipboard, Command, Element, Renderer};

mod state;

Expand All @@ -13,6 +13,9 @@ pub trait Program: Sized {
/// The type of __messages__ your [`Program`] will produce.
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`].
///
/// This is where you define your __update logic__. All the __messages__,
Expand All @@ -21,7 +24,11 @@ pub trait Program: Sized {
///
/// Any [`Command`] returned will be executed immediately in the
/// 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`].
///
Expand Down
7 changes: 3 additions & 4 deletions native/src/program/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
Cache, Clipboard, Command, Debug, Event, Point, Program, Renderer, Size,
UserInterface,
Cache, Command, Debug, Event, Point, Program, Renderer, Size, UserInterface,
};

/// The execution state of a [`Program`]. It leverages caching, event
Expand Down Expand Up @@ -92,7 +91,7 @@ where
bounds: Size,
cursor_position: Point,
renderer: &mut P::Renderer,
clipboard: &mut dyn Clipboard,
clipboard: &mut P::Clipboard,
debug: &mut Debug,
) -> Option<Command<P::Message>> {
let mut user_interface = build_user_interface(
Expand Down Expand Up @@ -136,7 +135,7 @@ where
debug.log_message(&message);

debug.update_started();
let command = self.program.update(message);
let command = self.program.update(message, clipboard);
debug.update_finished();

command
Expand Down
33 changes: 24 additions & 9 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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.
///
Expand Down Expand Up @@ -57,7 +59,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// says "Hello, world!":
///
/// ```no_run
/// use iced::{executor, Application, Command, Element, Settings, Text};
/// use iced::{executor, Application, Clipboard, Command, Element, Settings, Text};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
Expand All @@ -78,7 +80,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// 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()
/// }
///
Expand Down Expand Up @@ -127,7 +129,11 @@ pub trait Application: Sized {
/// this method.
///
/// 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
/// application.
Expand Down Expand Up @@ -228,9 +234,14 @@ where
{
type Renderer = crate::renderer::Renderer;
type Message = A::Message;

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
type Clipboard = iced_winit::Clipboard;

fn update(
&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> {
Expand Down Expand Up @@ -294,8 +305,12 @@ where
self.0.title()
}

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
fn update(
&mut self,
message: Self::Message,
clipboard: &mut Clipboard,
) -> Command<Self::Message> {
self.0.update(message, clipboard)
}

fn subscription(&self) -> Subscription<Self::Message> {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub use sandbox::Sandbox;
pub use settings::Settings;

pub use runtime::{
futures, Align, Background, Color, Command, Font, HorizontalAlignment,
Length, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment,
futures, Align, Background, Clipboard, Color, Command, Font,
HorizontalAlignment, Length, Point, Rectangle, Size, Subscription, Vector,
VerticalAlignment,
};
Loading

0 comments on commit c1f70f1

Please sign in to comment.