From c9711ff48fa1ad16365bc7eb37fa7153143bcee6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 2 Sep 2021 13:46:01 +0700 Subject: Handle `clipboard::Action` in `iced_winit` shell --- glutin/src/application.rs | 23 ++++++++++++++++++----- native/src/clipboard.rs | 4 ++-- winit/src/application.rs | 42 +++++++++++++++++++++++++++++++++--------- winit/src/clipboard.rs | 3 +++ winit/src/lib.rs | 2 +- 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 1368f7f8..cef49ea0 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -33,6 +33,8 @@ where debug.startup_started(); let event_loop = EventLoop::with_user_event(); + let mut proxy = event_loop.create_proxy(); + let mut runtime = { let executor = E::new().map_err(Error::ExecutorCreationFailed)?; let proxy = Proxy::new(event_loop.create_proxy()); @@ -48,9 +50,6 @@ where let subscription = application.subscription(); - application::run_command(init_command, &mut runtime); - runtime.track(subscription); - let context = { let builder = settings .window @@ -90,6 +89,16 @@ where })? }; + let mut clipboard = Clipboard::connect(context.window()); + + application::run_command( + init_command, + &mut runtime, + &mut clipboard, + &mut proxy, + ); + runtime.track(subscription); + let (mut sender, receiver) = mpsc::unbounded(); let mut instance = Box::pin(run_instance::( @@ -97,6 +106,8 @@ where compositor, renderer, runtime, + clipboard, + proxy, debug, receiver, context, @@ -145,6 +156,8 @@ async fn run_instance( mut compositor: C, mut renderer: A::Renderer, mut runtime: Runtime, A::Message>, + mut clipboard: Clipboard, + mut proxy: glutin::event_loop::EventLoopProxy, mut debug: Debug, mut receiver: mpsc::UnboundedReceiver>, mut context: glutin::ContextWrapper, @@ -157,8 +170,6 @@ async fn run_instance( use glutin::event; use iced_winit::futures::stream::StreamExt; - let mut clipboard = Clipboard::connect(context.window()); - let mut state = application::State::new(&application, context.window()); let mut viewport_version = state.viewport_version(); let mut user_interface = @@ -210,6 +221,8 @@ async fn run_instance( application::update( &mut application, &mut runtime, + &mut clipboard, + &mut proxy, &mut debug, &mut messages, ); diff --git a/native/src/clipboard.rs b/native/src/clipboard.rs index 62dfc130..4d59d960 100644 --- a/native/src/clipboard.rs +++ b/native/src/clipboard.rs @@ -24,7 +24,7 @@ impl Clipboard for Null { pub enum Action { Read(Box) -> T>), - Write(Box T>), + Write(String), } impl Action { @@ -34,7 +34,7 @@ impl Action { { 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)))), + Self::Write(content) => Action::Write(content), } } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 99118ad1..f97f9bf1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,11 +3,12 @@ mod state; pub use state::State; +use crate::clipboard::{self, Clipboard}; use crate::conversion; use crate::mouse; use crate::{ - Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, - Settings, Size, Subscription, + Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, + Size, Subscription, }; use iced_futures::futures; @@ -127,6 +128,7 @@ where debug.startup_started(); let event_loop = EventLoop::with_user_event(); + let mut proxy = event_loop.create_proxy(); let mut runtime = { let proxy = Proxy::new(event_loop.create_proxy()); @@ -143,9 +145,6 @@ where let subscription = application.subscription(); - run_command(init_command, &mut runtime); - runtime.track(subscription); - let window = settings .window .into_builder( @@ -158,6 +157,11 @@ where .build(&event_loop) .map_err(Error::WindowCreationFailed)?; + let mut clipboard = Clipboard::connect(&window); + + run_command(init_command, &mut runtime, &mut clipboard, &mut proxy); + runtime.track(subscription); + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; let (mut sender, receiver) = mpsc::unbounded(); @@ -167,6 +171,8 @@ where compositor, renderer, runtime, + clipboard, + proxy, debug, receiver, window, @@ -215,6 +221,8 @@ async fn run_instance( mut compositor: C, mut renderer: A::Renderer, mut runtime: Runtime, A::Message>, + mut clipboard: Clipboard, + mut proxy: winit::event_loop::EventLoopProxy, mut debug: Debug, mut receiver: mpsc::UnboundedReceiver>, window: winit::window::Window, @@ -228,7 +236,6 @@ async fn run_instance( use winit::event; let mut surface = compositor.create_surface(&window); - let mut clipboard = Clipboard::connect(&window); let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); @@ -289,6 +296,8 @@ async fn run_instance( update( &mut application, &mut runtime, + &mut clipboard, + &mut proxy, &mut debug, &mut messages, ); @@ -490,6 +499,8 @@ pub fn build_user_interface<'a, A: Application>( pub fn update( application: &mut A, runtime: &mut Runtime, A::Message>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy, debug: &mut Debug, messages: &mut Vec, ) { @@ -500,7 +511,7 @@ pub fn update( let command = runtime.enter(|| application.update(message)); debug.update_finished(); - run_command(command, runtime); + run_command(command, runtime, clipboard, proxy); } let subscription = application.subscription(); @@ -508,9 +519,11 @@ pub fn update( } /// Runs the actions of a [`Command`]. -pub fn run_command( +pub fn run_command( command: Command, runtime: &mut Runtime, Message>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy, ) { use iced_native::command; @@ -519,7 +532,18 @@ pub fn run_command( command::Action::Future(future) => { runtime.spawn(future); } - command::Action::Clipboard(_action) => unimplemented! {}, + 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) => unimplemented! {}, } } diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index ca25c065..9d0663fa 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,3 +1,6 @@ +//! Access the clipboard. +pub use iced_native::clipboard::Action; + /// A buffer for short-term storage and transfer within and between /// applications. #[allow(missing_debug_implementations)] diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 1707846a..12df9d8e 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -25,10 +25,10 @@ pub use iced_native::*; pub use winit; pub mod application; +pub mod clipboard; pub mod conversion; pub mod settings; -mod clipboard; mod error; mod mode; mod position; -- cgit