diff --git a/crates/egui-winit/src/clipboard.rs b/crates/egui-winit/src/clipboard.rs index 2410c3ee..a974ec1a 100644 --- a/crates/egui-winit/src/clipboard.rs +++ b/crates/egui-winit/src/clipboard.rs @@ -23,6 +23,21 @@ pub struct Clipboard { ))] smithay: Option, + /// The clipboard of winit's Wayland event loop, which uses winit's own data device. + /// When it is set, smithay-clipboard is not started. + #[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", + ))] + winit_wayland: Option, + /// Fallback manual clipboard. clipboard: String, } @@ -30,6 +45,33 @@ pub struct Clipboard { impl Clipboard { /// Construct a new instance pub fn new(_raw_display_handle: Option) -> Self { + #[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", + ))] + if let Some(winit_wayland) = init_winit_wayland_clipboard(_raw_display_handle) { + // Binding a second data device for smithay-clipboard would take the selection away + // from winit's on compositors that serve only one device per client (Hyprland). + return Self { + #[cfg(all( + not(any(target_os = "android", target_os = "ios")), + feature = "arboard", + ))] + arboard: init_arboard(), + #[cfg(feature = "smithay-clipboard")] + smithay: None, + winit_wayland: Some(winit_wayland), + clipboard: Default::default(), + }; + } + Self { #[cfg(all( not(any(target_os = "android", target_os = "ios")), @@ -49,11 +91,51 @@ impl Clipboard { ))] smithay: init_smithay_clipboard(_raw_display_handle), + #[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", + ))] + winit_wayland: None, + clipboard: Default::default(), } } pub fn get(&mut self) -> Option { + #[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", + ))] + if let Some(clipboard) = &self.winit_wayland { + match clipboard.load_text() { + Ok(text) => return Some(text), + // The clipboard is empty, or holds no text (perhaps an image for arboard). + Err(err) + if matches!( + err.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::InvalidData + ) => {} + Err(err) => { + // Not fatal: we fall back to arboard below. + log::debug!("winit Wayland paste error: {err}"); + } + } + } + #[cfg(all( any( target_os = "linux", @@ -91,6 +173,24 @@ impl Clipboard { } pub fn set_text(&mut self, text: String) { + #[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", + ))] + if let Some(clipboard) = &self.winit_wayland { + if let Err(err) = clipboard.store_text(text) { + log::error!("Wayland copy/cut error: {err}"); + } + return; + } + #[cfg(all( any( target_os = "linux", @@ -144,6 +244,30 @@ impl Clipboard { } } +#[cfg(all( + any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "wayland", + feature = "clipboard", +))] +fn init_winit_wayland_clipboard( + raw_display_handle: Option, +) -> Option { + let Some(RawDisplayHandle::Wayland(display)) = raw_display_handle else { + return None; + }; + let clipboard = winit::platform::wayland::Clipboard::for_display(display.display); + if clipboard.is_some() { + log::trace!("Using the clipboard of winit's Wayland event loop"); + } + clipboard +} + #[cfg(all( not(any(target_os = "android", target_os = "ios")), feature = "arboard",