Browse Source

egui-winit: Use RawDisplayHandle for smithay clipboard init (#2914)

pull/2933/head
Ryan Hileman 2 years ago
committed by GitHub
parent
commit
ede3ded977
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Cargo.lock
  2. 1
      crates/egui-winit/Cargo.toml
  3. 39
      crates/egui-winit/src/clipboard.rs
  4. 8
      crates/egui-winit/src/lib.rs

1
Cargo.lock

@ -1242,6 +1242,7 @@ dependencies = [
"instant",
"log",
"puffin",
"raw-window-handle",
"serde",
"smithay-clipboard",
"webbrowser",

1
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 = [

39
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<T>(_event_loop: &EventLoopWindowTarget<T>) -> 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<arboard::Clipboard> {
),
feature = "smithay-clipboard"
))]
fn init_smithay_clipboard<T>(
_event_loop: &EventLoopWindowTarget<T>,
fn init_smithay_clipboard(
_display_target: &dyn HasRawDisplayHandle,
) -> Option<smithay_clipboard::Clipboard> {
// 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
}

8
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<T>(event_loop: &EventLoopWindowTarget<T>) -> 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,

Loading…
Cancel
Save