mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -04:00
egui-winit: fix unsafe API of Clipboard::new (#2765)
* egui-winit: fix unsafe API of Clipboard::new The old API allowed passing an arbitrary pointer. The new API still breaks safety by allowing the object to outlive the input, but is at least safer. * Update crates/egui-winit/src/clipboard.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Fix typo * Update crates/egui-winit/src/clipboard.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * cargo fmt * egui-winit: fix init_smithay_clipboard --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::os::raw::c_void;
|
||||
use winit::event_loop::EventLoopWindowTarget;
|
||||
|
||||
/// Handles interfacing with the OS clipboard.
|
||||
///
|
||||
@@ -25,8 +25,12 @@ pub struct Clipboard {
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
#[allow(unused_variables)]
|
||||
pub fn new(#[allow(unused_variables)] wayland_display: Option<*mut c_void>) -> Self {
|
||||
/// Construct a new instance
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The returned `Clipboard` must not outlive the input `_event_loop`.
|
||||
pub fn new<T>(_event_loop: &EventLoopWindowTarget<T>) -> Self {
|
||||
Self {
|
||||
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
||||
arboard: init_arboard(),
|
||||
@@ -41,7 +45,7 @@ impl Clipboard {
|
||||
),
|
||||
feature = "smithay-clipboard"
|
||||
))]
|
||||
smithay: init_smithay_clipboard(wayland_display),
|
||||
smithay: init_smithay_clipboard(_event_loop),
|
||||
|
||||
clipboard: Default::default(),
|
||||
}
|
||||
@@ -132,15 +136,28 @@ fn init_arboard() -> Option<arboard::Clipboard> {
|
||||
),
|
||||
feature = "smithay-clipboard"
|
||||
))]
|
||||
fn init_smithay_clipboard(
|
||||
wayland_display: Option<*mut c_void>,
|
||||
fn init_smithay_clipboard<T>(
|
||||
_event_loop: &EventLoopWindowTarget<T>,
|
||||
) -> Option<smithay_clipboard::Clipboard> {
|
||||
if let Some(display) = wayland_display {
|
||||
tracing::debug!("Initializing smithay clipboard…");
|
||||
#[allow(unsafe_code)]
|
||||
Some(unsafe { smithay_clipboard::Clipboard::new(display) })
|
||||
} else {
|
||||
tracing::debug!("Cannot initialize smithay clipboard without a display handle");
|
||||
// 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() {
|
||||
tracing::debug!("Initializing smithay clipboard…");
|
||||
#[allow(unsafe_code)]
|
||||
Some(unsafe { smithay_clipboard::Clipboard::new(display) })
|
||||
} else {
|
||||
tracing::debug!("Cannot initialize smithay clipboard without a display handle");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "wayland"))]
|
||||
{
|
||||
tracing::debug!(
|
||||
"You need to enable the 'wayland' feature of 'egui-winit' to get a working clipboard"
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user