1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

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

This commit is contained in:
Ryan Hileman
2023-04-19 00:14:23 -07:00
committed by GitHub
parent b8e798777d
commit ede3ded977
4 changed files with 22 additions and 27 deletions

View File

@@ -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
}