From 6aa35f5d5b6da5111f0721043074785241ba4d4c Mon Sep 17 00:00:00 2001 From: Iain Laird Date: Sun, 12 May 2024 21:40:53 +0100 Subject: [PATCH] Update raw-win-handle to 0.6 --- druid-shell/Cargo.toml | 2 +- druid-shell/src/backend/gtk/window.rs | 13 ++++---- druid-shell/src/backend/mac/window.rs | 13 ++++---- druid-shell/src/backend/wayland/window.rs | 10 +++--- druid-shell/src/backend/web/window.rs | 10 +++--- druid-shell/src/backend/windows/window.rs | 39 +++++++++++++---------- druid-shell/src/backend/x11/window.rs | 15 +++++---- druid-shell/src/window.rs | 8 ++--- druid/src/lib.rs | 2 +- 9 files changed, 59 insertions(+), 53 deletions(-) diff --git a/druid-shell/Cargo.toml b/druid-shell/Cargo.toml index fa4465848..0902d55fa 100755 --- a/druid-shell/Cargo.toml +++ b/druid-shell/Cargo.toml @@ -76,7 +76,7 @@ anyhow = "1.0.69" keyboard-types = { version = "0.6.2", default-features = false } # Optional dependencies -raw-window-handle = { version = "0.5.0", optional = true, default-features = false } +raw-window-handle = { version = "0.6.0", optional = true, default-features = false } [target.'cfg(target_os="windows")'.dependencies] scopeguard = "1.1.0" diff --git a/druid-shell/src/backend/gtk/window.rs b/druid-shell/src/backend/gtk/window.rs index 51fc78508..a4a160054 100644 --- a/druid-shell/src/backend/gtk/window.rs +++ b/druid-shell/src/backend/gtk/window.rs @@ -24,7 +24,7 @@ use std::slice; use std::sync::{Arc, Mutex, Weak}; use std::time::Instant; -use cairo::glib::{Propagation, ControlFlow}; +use cairo::glib::{ControlFlow, Propagation}; use gtk::gdk_pixbuf::Colorspace::Rgb; use gtk::gdk_pixbuf::Pixbuf; use gtk::glib::translate::FromGlib; @@ -44,7 +44,7 @@ use instant::Duration; use tracing::{error, warn}; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, XcbWindowHandle}; +use raw_window_handle::{HandleError, HasWindowHandle}; use crate::kurbo::{Insets, Point, Rect, Size, Vec2}; use crate::piet::{Piet, PietText, RenderContext}; @@ -122,11 +122,10 @@ impl PartialEq for WindowHandle { impl Eq for WindowHandle {} #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - error!("HasRawWindowHandle trait not implemented for gtk."); - // GTK is not a platform, and there's no empty generic handle. Pick XCB randomly as fallback. - RawWindowHandle::Xcb(XcbWindowHandle::empty()) +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { + tracing::error!("HasRawWindowHandle trait not implemented for gtk."); + Err(HandleError::NotSupported) } } diff --git a/druid-shell/src/backend/mac/window.rs b/druid-shell/src/backend/mac/window.rs index 77bc1484a..e09eff82b 100644 --- a/druid-shell/src/backend/mac/window.rs +++ b/druid-shell/src/backend/mac/window.rs @@ -40,7 +40,7 @@ use once_cell::sync::Lazy; use tracing::{debug, error, info}; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{AppKitWindowHandle, HasRawWindowHandle, RawWindowHandle}; +use raw_window_handle::{AppKitWindowHandle, HandleError, HasWindowHandle}; use crate::kurbo::{Insets, Point, Rect, Size, Vec2}; use crate::piet::{Piet, PietText, RenderContext}; @@ -1464,12 +1464,13 @@ impl WindowHandle { } #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { let nsv = self.nsview.load(); - let mut handle = AppKitWindowHandle::empty(); - handle.ns_view = *nsv as *mut _; - RawWindowHandle::AppKit(handle) + let mut handle = AppKitWindowHandle::new(NonNull::from(&nsv).cast()); + + let handle = unsafe { raw_window_handle::WindowHandle::borrow_raw(handle) }; + Ok(handle) } } diff --git a/druid-shell/src/backend/wayland/window.rs b/druid-shell/src/backend/wayland/window.rs index 3a7df3fdd..c3d8c86c8 100644 --- a/druid-shell/src/backend/wayland/window.rs +++ b/druid-shell/src/backend/wayland/window.rs @@ -20,7 +20,7 @@ use wayland_protocols::xdg_shell::client::xdg_positioner; use wayland_protocols::xdg_shell::client::xdg_surface; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, WaylandWindowHandle}; +use raw_window_handle::{HandleError, HasWindowHandle}; use super::application::{self, Timer}; use super::{error::Error, menu::Menu, outputs, surfaces}; @@ -327,10 +327,10 @@ impl std::default::Default for WindowHandle { } #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - tracing::error!("HasRawWindowHandle trait not implemented for wasm."); - RawWindowHandle::Wayland(WaylandWindowHandle::empty()) +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { + tracing::error!("HasRawWindowHandle trait not implemented for wayland."); + Err(HandleError::NotSupported) } } diff --git a/druid-shell/src/backend/web/window.rs b/druid-shell/src/backend/web/window.rs index 5baec562b..7b9a51c2b 100644 --- a/druid-shell/src/backend/web/window.rs +++ b/druid-shell/src/backend/web/window.rs @@ -25,7 +25,7 @@ use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, WebWindowHandle}; +use raw_window_handle::{HandleError, HasWindowHandle}; use crate::kurbo::{Insets, Point, Rect, Size, Vec2}; @@ -91,10 +91,10 @@ impl PartialEq for WindowHandle { impl Eq for WindowHandle {} #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - error!("HasRawWindowHandle trait not implemented for wasm."); - RawWindowHandle::Web(WebWindowHandle::empty()) +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { + tracing::error!("HasRawWindowHandle trait not implemented for wasm."); + Err(HandleError::NotSupported) } } diff --git a/druid-shell/src/backend/windows/window.rs b/druid-shell/src/backend/windows/window.rs index 2b671b29b..e68477daf 100644 --- a/druid-shell/src/backend/windows/window.rs +++ b/druid-shell/src/backend/windows/window.rs @@ -47,9 +47,6 @@ use winapi::um::winuser::*; use winapi::Interface; use wio::com::ComPtr; -#[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32WindowHandle}; - use piet_common::d2d::{D2DFactory, DeviceContext}; use piet_common::dwrite::DwriteFactory; @@ -187,20 +184,28 @@ impl PartialEq for WindowHandle { impl Eq for WindowHandle {} #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - if let Some(hwnd) = self.get_hwnd() { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = hwnd as *mut core::ffi::c_void; - handle.hinstance = unsafe { - winapi::um::libloaderapi::GetModuleHandleW(0 as winapi::um::winnt::LPCWSTR) - as *mut core::ffi::c_void - }; - RawWindowHandle::Win32(handle) - } else { - error!("Cannot retrieved HWND for window."); - RawWindowHandle::Win32(Win32WindowHandle::empty()) - } +impl raw_window_handle::HasWindowHandle for WindowHandle { + fn window_handle( + &self, + ) -> Result, raw_window_handle::HandleError> { + let hwnd = self + .get_hwnd() + .ok_or(raw_window_handle::HandleError::Unavailable)?; + let mut handle = raw_window_handle::Win32WindowHandle::new(unsafe { + std::num::NonZeroIsize::new_unchecked(hwnd as isize) + }); + handle.hinstance = unsafe { + std::num::NonZeroIsize::new(winapi::um::libloaderapi::GetModuleHandleW( + 0 as winapi::um::winnt::LPCWSTR, + ) as isize) + }; + let handle = unsafe { + raw_window_handle::WindowHandle::borrow_raw(raw_window_handle::RawWindowHandle::Win32( + handle, + )) + }; + + Ok(handle) } } diff --git a/druid-shell/src/backend/x11/window.rs b/druid-shell/src/backend/x11/window.rs index 162c4862c..50b0c65cf 100644 --- a/druid-shell/src/backend/x11/window.rs +++ b/druid-shell/src/backend/x11/window.rs @@ -42,7 +42,7 @@ use x11rb::wrapper::ConnectionExt as _; use x11rb::xcb_ffi::XCBConnection; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, XcbWindowHandle}; +use raw_window_handle::{HandleError, HasWindowHandle, XcbWindowHandle}; use crate::backend::shared::Timer; use crate::common_util::IdleCallback; @@ -1903,12 +1903,13 @@ impl WindowHandle { } #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = XcbWindowHandle::empty(); - handle.window = self.id; - handle.visual_id = self.visual_id; - RawWindowHandle::Xcb(handle) +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { + let mut handle = XcbWindowHandle::new(NonZeroU32::new(self.id).unwrap()); + handle.visual_id = std::num::NonZeroU32::new(self.visual_id); + + let handle = unsafe { raw_window_handle::WindowHandle::borrow_raw(handle) }; + Ok(handle) } } diff --git a/druid-shell/src/window.rs b/druid-shell/src/window.rs index 125d567d6..70280651a 100644 --- a/druid-shell/src/window.rs +++ b/druid-shell/src/window.rs @@ -31,7 +31,7 @@ use crate::scale::Scale; use crate::text::{Event, InputHandler}; use piet_common::PietText; #[cfg(feature = "raw-win-handle")] -use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; +use raw_window_handle::{HasWindowHandle, HandleError}; /// A token that uniquely identifies a running timer. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)] @@ -449,9 +449,9 @@ impl WindowHandle { } #[cfg(feature = "raw-win-handle")] -unsafe impl HasRawWindowHandle for WindowHandle { - fn raw_window_handle(&self) -> RawWindowHandle { - self.0.raw_window_handle() +impl HasWindowHandle for WindowHandle { + fn window_handle(&self) -> Result, HandleError> { + self.0.window_handle() } } diff --git a/druid/src/lib.rs b/druid/src/lib.rs index 7775e53f0..945ec57a5 100644 --- a/druid/src/lib.rs +++ b/druid/src/lib.rs @@ -217,7 +217,7 @@ pub use shell::{ }; #[cfg(feature = "raw-win-handle")] -pub use crate::shell::raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; +pub use crate::shell::raw_window_handle::{self, HandleError, HasWindowHandle, RawWindowHandle}; pub use crate::core::{WidgetPod, WidgetState}; pub use app::{AppLauncher, WindowConfig, WindowDesc, WindowSizePolicy};