E4: egui-winit: use winit's Wayland clipboard instead of smithay-clipboard when available

Port of emilk/egui#8657 (draft) to 0.36.2, the companion of W15 (rust-windowing/winit#4729): winit now binds a wl_data_device per seat for file drops, and a second device from smithay-clipboard loses the selection on compositors that serve only the first device per client (Hyprland). With winit's clipboard available, smithay-clipboard is not started. Only the clipboard.rs change is taken; the PR's temporary [patch] of egui's own winit is not.
This commit is contained in:
Carmine Paolino authored and skylartaylor committed 2026-10-21 18:16:56 +02:00
1 parent b02321d740
commit c01acc2acd
1 file changed
+124
+124
View File
@@ -23,6 +23,21 @@ pub struct Clipboard {
))]
smithay: Option<smithay_clipboard::Clipboard>,
/// 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<winit::platform::wayland::Clipboard>,
/// 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<RawDisplayHandle>) -> 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<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 {
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<RawDisplayHandle>,
) -> Option<winit::platform::wayland::Clipboard> {
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",