From 4155edab8d123b767ddad67e24ca2d4c50f31ece Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Wed, 7 Feb 2024 17:25:40 +0100 Subject: Add support for primary clipboard --- winit/src/application.rs | 12 ++++++++++++ winit/src/clipboard.rs | 34 ++++++++++++++++++++++++++++++++++ winit/src/multi_window.rs | 12 ++++++++++++ 3 files changed, 58 insertions(+) (limited to 'winit/src') 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( 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 { + 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 { + 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( 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(); -- cgit From 508b3fe1f1405bdb8b860d0d63e2c7adfbbd51ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Feb 2024 03:14:08 +0100 Subject: Introduce `Kind` in `core::clipboard` --- winit/src/application.rs | 20 +++------------ winit/src/clipboard.rs | 64 ++++++++++++++++------------------------------- winit/src/multi_window.rs | 20 +++------------ 3 files changed, 30 insertions(+), 74 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 2d3ac52c..0fc67adc 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -704,27 +704,15 @@ pub fn run_command( runtime.run(stream); } command::Action::Clipboard(action) => match action { - clipboard::Action::Read(tag) => { - let message = tag(clipboard.read()); + clipboard::Action::Read(tag, kind) => { + let message = tag(clipboard.read(kind)); proxy .send_event(message) .expect("Send message to event loop"); } - clipboard::Action::Write(contents) => { - 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); + clipboard::Action::Write(contents, kind) => { + clipboard.write(kind, contents); } }, command::Action::Window(action) => match action { diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index a1496fa3..5237ca01 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,5 +1,7 @@ //! Access the clipboard. +use crate::core::clipboard::Kind; + /// A buffer for short-term storage and transfer within and between /// applications. #[allow(missing_debug_implementations)] @@ -33,46 +35,32 @@ impl Clipboard { } /// Reads the current content of the [`Clipboard`] as text. - pub fn read(&self) -> Option { + pub fn read(&self, kind: Kind) -> Option { match &self.state { - State::Connected(clipboard) => clipboard.read().ok(), + State::Connected(clipboard) => match kind { + Kind::Standard => clipboard.read().ok(), + Kind::Primary => clipboard.read_primary().and_then(Result::ok), + }, State::Unavailable => None, } } /// Writes the given text contents to the [`Clipboard`]. - pub fn write(&mut self, contents: String) { + pub fn write(&mut self, kind: Kind, contents: String) { match &mut self.state { - State::Connected(clipboard) => match clipboard.write(contents) { - Ok(()) => {} - Err(error) => { - log::warn!("error writing to clipboard: {error}"); - } - }, - State::Unavailable => {} - } - } - - /// Reads the current content of primary as text. - pub fn read_primary(&self) -> Option { - match &self.state { State::Connected(clipboard) => { - clipboard.read_primary().and_then(|r| r.ok()) - } - State::Unavailable => None, - } - } + let result = match kind { + Kind::Standard => clipboard.write(contents), + Kind::Primary => { + clipboard.write_primary(contents).unwrap_or(Ok(())) + } + }; - /// 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}"); + match result { + Ok(()) => {} + Err(error) => { + log::warn!("error writing to clipboard: {error}"); } - None => {} } } State::Unavailable => {} @@ -81,19 +69,11 @@ impl Clipboard { } impl crate::core::Clipboard for Clipboard { - fn read(&self) -> Option { - self.read() - } - - fn write(&mut self, contents: String) { - self.write(contents); - } - - fn read_primary(&self) -> Option { - self.read_primary() + fn read(&self, kind: Kind) -> Option { + self.read(kind) } - fn write_primary(&mut self, contents: String) { - self.write_primary(contents); + fn write(&mut self, kind: Kind, contents: String) { + self.write(kind, contents); } } diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 33f521c4..ed00abdf 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -876,27 +876,15 @@ fn run_command( runtime.run(Box::pin(stream)); } command::Action::Clipboard(action) => match action { - clipboard::Action::Read(tag) => { - let message = tag(clipboard.read()); + clipboard::Action::Read(tag, kind) => { + let message = tag(clipboard.read(kind)); proxy .send_event(message) .expect("Send message to event loop"); } - clipboard::Action::Write(contents) => { - 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); + clipboard::Action::Write(contents, kind) => { + clipboard.write(kind, contents); } }, command::Action::Window(action) => match action { -- cgit