diff options
-rw-r--r-- | core/src/clipboard.rs | 12 | ||||
-rw-r--r-- | runtime/src/clipboard.rs | 14 | ||||
-rw-r--r-- | runtime/src/command/action.rs | 9 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | winit/src/application.rs | 12 | ||||
-rw-r--r-- | winit/src/clipboard.rs | 34 | ||||
-rw-r--r-- | winit/src/multi_window.rs | 12 |
7 files changed, 96 insertions, 1 deletions
diff --git a/core/src/clipboard.rs b/core/src/clipboard.rs index 081b4004..ff2e31d0 100644 --- a/core/src/clipboard.rs +++ b/core/src/clipboard.rs @@ -8,6 +8,12 @@ pub trait Clipboard { /// Writes the given text contents to the [`Clipboard`]. fn write(&mut self, contents: String); + + /// Reads the current content of Primary as text. + fn read_primary(&self) -> Option<String>; + + /// Writes the given text contents to Primary. + fn write_primary(&mut self, contents: String); } /// A null implementation of the [`Clipboard`] trait. @@ -20,4 +26,10 @@ impl Clipboard for Null { } fn write(&mut self, _contents: String) {} + + fn read_primary(&self) -> Option<String> { + None + } + + fn write_primary(&mut self, _contents: String) {} } diff --git a/runtime/src/clipboard.rs b/runtime/src/clipboard.rs index bc450912..49044a0f 100644 --- a/runtime/src/clipboard.rs +++ b/runtime/src/clipboard.rs @@ -51,3 +51,17 @@ pub fn read<Message>( pub fn write<Message>(contents: String) -> Command<Message> { Command::single(command::Action::Clipboard(Action::Write(contents))) } + +/// Read the current contents of primary. +pub fn read_primary<Message>( + f: impl Fn(Option<String>) -> Message + 'static, +) -> Command<Message> { + Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new( + f, + )))) +} + +/// Write the given contents to primary. +pub fn write_primary<Message>(contents: String) -> Command<Message> { + Command::single(command::Action::ClipboardPrimary(Action::Write(contents))) +} diff --git a/runtime/src/command/action.rs b/runtime/src/command/action.rs index c9ffe801..f04c642c 100644 --- a/runtime/src/command/action.rs +++ b/runtime/src/command/action.rs @@ -26,6 +26,9 @@ pub enum Action<T> { /// Run a clipboard action. Clipboard(clipboard::Action<T>), + /// Run a clipboard action on primary. + ClipboardPrimary(clipboard::Action<T>), + /// Run a window action. Window(window::Action<T>), @@ -66,6 +69,9 @@ impl<T> Action<T> { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))), Self::Clipboard(action) => Action::Clipboard(action.map(f)), + Self::ClipboardPrimary(action) => { + Action::ClipboardPrimary(action.map(f)) + } Self::Window(window) => Action::Window(window.map(f)), Self::System(system) => Action::System(system.map(f)), Self::Widget(operation) => { @@ -88,6 +94,9 @@ impl<T> fmt::Debug for Action<T> { Self::Clipboard(action) => { write!(f, "Action::Clipboard({action:?})") } + Self::ClipboardPrimary(action) => { + write!(f, "Action::ClipboardPrimary({action:?})") + } Self::Window(action) => { write!(f, "Action::Window({action:?})") } @@ -200,7 +200,9 @@ pub use crate::core::{ pub mod clipboard { //! Access the clipboard. - pub use crate::runtime::clipboard::{read, write}; + pub use crate::runtime::clipboard::{ + read, read_primary, write, write_primary, + }; } pub mod executor { diff --git a/winit/src/application.rs b/winit/src/application.rs index 77e2c83e..2d3ac52c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -715,6 +715,18 @@ pub fn run_command<A, C, E>( clipboard.write(contents); } }, + command::Action::ClipboardPrimary(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read_primary()); + + proxy + .send_event(message) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write_primary(contents); + } + }, command::Action::Window(action) => match action { window::Action::Close(_id) => { *should_exit = true; diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 8f5c5e63..a1496fa3 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -52,6 +52,32 @@ impl Clipboard { State::Unavailable => {} } } + + /// Reads the current content of primary as text. + pub fn read_primary(&self) -> Option<String> { + match &self.state { + State::Connected(clipboard) => { + clipboard.read_primary().and_then(|r| r.ok()) + } + State::Unavailable => None, + } + } + + /// Writes the given text contents to primary. + pub fn write_primary(&mut self, contents: String) { + match &mut self.state { + State::Connected(clipboard) => { + match clipboard.write_primary(contents) { + Some(Ok(())) => {} + Some(Err(error)) => { + log::warn!("error writing to primary: {error}"); + } + None => {} + } + } + State::Unavailable => {} + } + } } impl crate::core::Clipboard for Clipboard { @@ -62,4 +88,12 @@ impl crate::core::Clipboard for Clipboard { fn write(&mut self, contents: String) { self.write(contents); } + + fn read_primary(&self) -> Option<String> { + self.read_primary() + } + + fn write_primary(&mut self, contents: String) { + self.write_primary(contents); + } } diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 23b2f3c4..33f521c4 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -887,6 +887,18 @@ fn run_command<A, C, E>( clipboard.write(contents); } }, + command::Action::ClipboardPrimary(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read_primary()); + + proxy + .send_event(message) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write_primary(contents); + } + }, command::Action::Window(action) => match action { window::Action::Spawn(id, settings) => { let monitor = window_manager.last_monitor(); |