From ede3ded977f0f6be8fc5c967ef2c014da4bfd63f Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Wed, 19 Apr 2023 00:14:23 -0700 Subject: [PATCH] egui-winit: Use RawDisplayHandle for smithay clipboard init (#2914) --- Cargo.lock | 1 + crates/egui-winit/Cargo.toml | 1 + crates/egui-winit/src/clipboard.rs | 39 ++++++++++++------------------ crates/egui-winit/src/lib.rs | 8 +++--- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1cd7a701a..dbef430a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1242,6 +1242,7 @@ dependencies = [ "instant", "log", "puffin", + "raw-window-handle", "serde", "smithay-clipboard", "webbrowser", diff --git a/crates/egui-winit/Cargo.toml b/crates/egui-winit/Cargo.toml index 345b4d79c..386f34f17 100644 --- a/crates/egui-winit/Cargo.toml +++ b/crates/egui-winit/Cargo.toml @@ -74,6 +74,7 @@ webbrowser = { version = "0.8.3", optional = true } [target.'cfg(not(target_arch="wasm32"))'.dependencies] instant = { version = "0.1" } +raw-window-handle = "0.5.0" [target.'cfg(target_arch="wasm32")'.dependencies] instant = { version = "0.1", features = [ diff --git a/crates/egui-winit/src/clipboard.rs b/crates/egui-winit/src/clipboard.rs index 642838639..e80414961 100644 --- a/crates/egui-winit/src/clipboard.rs +++ b/crates/egui-winit/src/clipboard.rs @@ -1,4 +1,4 @@ -use winit::event_loop::EventLoopWindowTarget; +use raw_window_handle::HasRawDisplayHandle; /// Handles interfacing with the OS clipboard. /// @@ -29,8 +29,8 @@ impl Clipboard { /// /// # Safety /// - /// The returned `Clipboard` must not outlive the input `_event_loop`. - pub fn new(_event_loop: &EventLoopWindowTarget) -> Self { + /// The returned `Clipboard` must not outlive the input `_display_target`. + pub fn new(_display_target: &dyn HasRawDisplayHandle) -> Self { Self { #[cfg(all(feature = "arboard", not(target_os = "android")))] arboard: init_arboard(), @@ -45,7 +45,7 @@ impl Clipboard { ), feature = "smithay-clipboard" ))] - smithay: init_smithay_clipboard(_event_loop), + smithay: init_smithay_clipboard(_display_target), clipboard: Default::default(), } @@ -136,27 +136,20 @@ fn init_arboard() -> Option { ), feature = "smithay-clipboard" ))] -fn init_smithay_clipboard( - _event_loop: &EventLoopWindowTarget, +fn init_smithay_clipboard( + _display_target: &dyn HasRawDisplayHandle, ) -> Option { - // Note: ideally "smithay-clipboard" would imply "wayland", but it doesn't. - #[cfg(feature = "wayland")] - { - use winit::platform::wayland::EventLoopWindowTargetExtWayland as _; - if let Some(display) = _event_loop.wayland_display() { - log::debug!("Initializing smithay clipboard…"); - #[allow(unsafe_code)] - Some(unsafe { smithay_clipboard::Clipboard::new(display) }) - } else { - log::debug!("Cannot initialize smithay clipboard without a display handle"); - None - } - } - - #[cfg(not(feature = "wayland"))] - { + use raw_window_handle::RawDisplayHandle; + if let RawDisplayHandle::Wayland(display) = _display_target.raw_display_handle() { + log::debug!("Initializing smithay clipboard…"); + #[allow(unsafe_code)] + Some(unsafe { smithay_clipboard::Clipboard::new(display.display) }) + } else { + #[cfg(feature = "wayland")] + log::debug!("Cannot init smithay clipboard without a Wayland display handle"); + #[cfg(not(feature = "wayland"))] log::debug!( - "You need to enable the 'wayland' feature of 'egui-winit' to get a working clipboard" + "Cannot init smithay clipboard: the 'wayland' feature of 'egui-winit' is not enabled" ); None } diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 67f585fc7..e45080512 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -21,7 +21,7 @@ mod window_settings; pub use window_settings::WindowSettings; -use winit::event_loop::EventLoopWindowTarget; +use raw_window_handle::HasRawDisplayHandle; pub fn native_pixels_per_point(window: &winit::window::Window) -> f32 { window.scale_factor() as f32 @@ -87,8 +87,8 @@ impl State { /// /// # Safety /// - /// The returned `State` must not outlive the input `_event_loop`. - pub fn new(event_loop: &EventLoopWindowTarget) -> Self { + /// The returned `State` must not outlive the input `display_target`. + pub fn new(display_target: &dyn HasRawDisplayHandle) -> Self { let egui_input = egui::RawInput { has_focus: false, // winit will tell us when we have focus ..Default::default() @@ -102,7 +102,7 @@ impl State { current_cursor_icon: None, current_pixels_per_point: 1.0, - clipboard: clipboard::Clipboard::new(event_loop), + clipboard: clipboard::Clipboard::new(display_target), simulate_touch_screen: false, pointer_touch_id: None,