From a14b39555e5c480422c24df7d708dd1addd0a67b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Dec 2019 07:45:49 +0100 Subject: Allow clipboard access in `Widget::on_event` --- winit/src/application.rs | 14 ++++++++++---- winit/src/clipboard.rs | 13 +++++++++++++ winit/src/lib.rs | 8 +++++--- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 winit/src/clipboard.rs (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 3b8ac16b..a8612b1a 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -2,8 +2,8 @@ use crate::{ conversion, input::{keyboard, mouse}, renderer::{Target, Windowed}, - subscription, Cache, Command, Container, Debug, Element, Event, Length, - MouseCursor, Settings, Subscription, UserInterface, + subscription, Cache, Clipboard, Command, Container, Debug, Element, Event, + Length, MouseCursor, Settings, Subscription, UserInterface, }; /// An interactive, native cross-platform application. @@ -139,6 +139,7 @@ pub trait Application: Sized { let mut size = window.inner_size(); let mut resized = false; + let clipboard = Clipboard::new(&window); let mut renderer = Self::Renderer::new(); let mut target = { @@ -193,8 +194,13 @@ pub trait Application: Sized { subscription_pool.broadcast_event(*event) }); - let mut messages = - user_interface.update(&renderer, events.drain(..)); + let mut messages = user_interface.update( + &renderer, + clipboard + .as_ref() + .map(|c| c as &dyn iced_native::Clipboard), + events.drain(..), + ); messages.extend(external_messages.drain(..)); debug.event_processing_finished(); diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs new file mode 100644 index 00000000..4739c603 --- /dev/null +++ b/winit/src/clipboard.rs @@ -0,0 +1,13 @@ +pub struct Clipboard(window_clipboard::Clipboard); + +impl Clipboard { + pub fn new(window: &winit::window::Window) -> Option { + window_clipboard::Clipboard::new(window).map(Clipboard).ok() + } +} + +impl iced_native::Clipboard for Clipboard { + fn content(&self) -> Option { + self.0.read().ok() + } +} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 8a1dc870..ffc662fb 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -29,11 +29,9 @@ pub mod conversion; pub mod settings; mod application; +mod clipboard; mod subscription; -pub use application::Application; -pub use settings::Settings; - // We disable debug capabilities on release builds unless the `debug` feature // is explicitly enabled. #[cfg(feature = "debug")] @@ -43,4 +41,8 @@ mod debug; #[path = "debug/null.rs"] mod debug; +pub use application::Application; +pub use settings::Settings; + +use clipboard::Clipboard; use debug::Debug; -- cgit