From f92e01e913480e1450696f3d37af4bff09f661d0 Mon Sep 17 00:00:00 2001 From: Maja Kądziołka Date: Sun, 11 Aug 2024 22:33:17 +0200 Subject: iced_winit: drop Clipboard before Window Fixes #2482, avoids nasal daemons --- winit/src/clipboard.rs | 31 +++++++++++++++++++++++-------- winit/src/program.rs | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) (limited to 'winit/src') diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index 5237ca01..f8b90777 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,6 +1,8 @@ //! Access the clipboard. use crate::core::clipboard::Kind; +use winit::window::Window; +use std::sync::Arc; /// A buffer for short-term storage and transfer within and between /// applications. @@ -10,18 +12,31 @@ 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, + }, Unavailable, } impl Clipboard { /// Creates a new [`Clipboard`] for the given window. - pub fn connect(window: &winit::window::Window) -> Clipboard { + pub fn connect(window: Arc) -> Clipboard { #[allow(unsafe_code)] - let state = unsafe { window_clipboard::Clipboard::connect(window) } - .ok() - .map(State::Connected) - .unwrap_or(State::Unavailable); + // SAFETY: The window handle will stay alive throughout the entire + // lifetime of the `window_clipboard::Clipboard` because we hold + // the `Arc` together with `State`, and enum variant fields + // get dropped in declaration order. + 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 +52,7 @@ impl Clipboard { /// Reads the current content of the [`Clipboard`] as text. pub fn read(&self, kind: Kind) -> Option { 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 +63,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 => { diff --git a/winit/src/program.rs b/winit/src/program.rs index 3d709b7e..139b2b8f 100644 --- a/winit/src/program.rs +++ b/winit/src/program.rs @@ -307,7 +307,7 @@ where } }; - let clipboard = Clipboard::connect(&window); + let clipboard = Clipboard::connect(window.clone()); let finish_boot = async move { let mut compositor = -- cgit From 7decbb3d5d0e72fd4667840568411bcb867feca5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Aug 2024 03:07:11 +0200 Subject: Fix formatting in `iced_winit::clipboard` --- winit/src/clipboard.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'winit/src') diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index f8b90777..7ae646fc 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,8 +1,8 @@ //! Access the clipboard. use crate::core::clipboard::Kind; -use winit::window::Window; use std::sync::Arc; +use winit::window::Window; /// A buffer for short-term storage and transfer within and between /// applications. @@ -27,12 +27,14 @@ enum State { impl Clipboard { /// Creates a new [`Clipboard`] for the given window. pub fn connect(window: Arc) -> Clipboard { - #[allow(unsafe_code)] // SAFETY: The window handle will stay alive throughout the entire // lifetime of the `window_clipboard::Clipboard` because we hold // the `Arc` together with `State`, and enum variant fields // get dropped in declaration order. - let clipboard = unsafe { window_clipboard::Clipboard::connect(&window) }; + #[allow(unsafe_code)] + let clipboard = + unsafe { window_clipboard::Clipboard::connect(&window) }; + let state = match clipboard { Ok(clipboard) => State::Connected { clipboard, window }, Err(_) => State::Unavailable, -- cgit