summaryrefslogtreecommitdiffstats
path: root/winit/src/clipboard.rs
diff options
context:
space:
mode:
Diffstat (limited to 'winit/src/clipboard.rs')
-rw-r--r--winit/src/clipboard.rs41
1 files changed, 33 insertions, 8 deletions
diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs
index 5237ca01..d54a1fe0 100644
--- a/winit/src/clipboard.rs
+++ b/winit/src/clipboard.rs
@@ -1,6 +1,8 @@
//! Access the clipboard.
use crate::core::clipboard::Kind;
+use std::sync::Arc;
+use winit::window::{Window, WindowId};
/// A buffer for short-term storage and transfer within and between
/// applications.
@@ -10,18 +12,33 @@ pub struct Clipboard {
}
enum State {
- Connected(window_clipboard::Clipboard),
+ Connected {
+ clipboard: window_clipboard::Clipboard,
+ // Held until drop to satisfy the safety invariants of
+ // `window_clipboard::Clipboard`.
+ //
+ // Note that the field ordering is load-bearing.
+ #[allow(dead_code)]
+ window: Arc<Window>,
+ },
Unavailable,
}
impl Clipboard {
/// Creates a new [`Clipboard`] for the given window.
- pub fn connect(window: &winit::window::Window) -> Clipboard {
+ pub fn connect(window: Arc<Window>) -> Clipboard {
+ // SAFETY: The window handle will stay alive throughout the entire
+ // lifetime of the `window_clipboard::Clipboard` because we hold
+ // the `Arc<Window>` together with `State`, and enum variant fields
+ // get dropped in declaration order.
#[allow(unsafe_code)]
- let state = unsafe { window_clipboard::Clipboard::connect(window) }
- .ok()
- .map(State::Connected)
- .unwrap_or(State::Unavailable);
+ let clipboard =
+ unsafe { window_clipboard::Clipboard::connect(&window) };
+
+ let state = match clipboard {
+ Ok(clipboard) => State::Connected { clipboard, window },
+ Err(_) => State::Unavailable,
+ };
Clipboard { state }
}
@@ -37,7 +54,7 @@ impl Clipboard {
/// Reads the current content of the [`Clipboard`] as text.
pub fn read(&self, kind: Kind) -> Option<String> {
match &self.state {
- State::Connected(clipboard) => match kind {
+ State::Connected { clipboard, .. } => match kind {
Kind::Standard => clipboard.read().ok(),
Kind::Primary => clipboard.read_primary().and_then(Result::ok),
},
@@ -48,7 +65,7 @@ impl Clipboard {
/// Writes the given text contents to the [`Clipboard`].
pub fn write(&mut self, kind: Kind, contents: String) {
match &mut self.state {
- State::Connected(clipboard) => {
+ State::Connected { clipboard, .. } => {
let result = match kind {
Kind::Standard => clipboard.write(contents),
Kind::Primary => {
@@ -66,6 +83,14 @@ impl Clipboard {
State::Unavailable => {}
}
}
+
+ /// Returns the identifier of the window used to create the [`Clipboard`], if any.
+ pub fn window_id(&self) -> Option<WindowId> {
+ match &self.state {
+ State::Connected { window, .. } => Some(window.id()),
+ State::Unavailable => None,
+ }
+ }
}
impl crate::core::Clipboard for Clipboard {