From 1e8e180c44eacf3810c24fbb8c9c36c2c47622b0 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 19 Nov 2021 11:13:51 +0100 Subject: [PATCH 1/2] Make implementors of HasRawWindowHandle v0.4.1 also implement v0.3's Uses the semver trick, see https://github.com/dtolnay/semver-trick The idea is that this will allow crates whose purpose is to provide an `HasRawWindowHandle` implementation to upgrade first (e.g. `winit`, `sdl2`, `glfw`, `fltk`, ...). When a sufficient amount of "provider" crates have upgraded, "consumer" crates (e.g. `gfx`, `wgpu`, `rfd`, ...) can safely start upgrading without breaking for downstream users. --- Cargo.toml | 1 + src/android.rs | 9 ++++++++ src/ios.rs | 11 ++++++++++ src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/macos.rs | 10 +++++++++ src/unix.rs | 30 +++++++++++++++++++++++++ src/web.rs | 9 ++++++++ src/windows.rs | 10 +++++++++ 8 files changed, 139 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ae52b5b..ab83d6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ appveyor = { repository = "rust-windowing/raw-window-handle" } [dependencies] libc = {version="0.2", features=[]} +new = { version = "0.4.1", package = "raw-window-handle" } [features] nightly-docs = [] diff --git a/src/android.rs b/src/android.rs index abf65dd..049d8d7 100644 --- a/src/android.rs +++ b/src/android.rs @@ -29,3 +29,12 @@ impl AndroidHandle { } } } + +impl From for AndroidHandle { + fn from(handle: new::AndroidNdkHandle) -> Self { + Self { + a_native_window: handle.a_native_window, + ..Self::empty() + } + } +} diff --git a/src/ios.rs b/src/ios.rs index 03820c2..2156141 100644 --- a/src/ios.rs +++ b/src/ios.rs @@ -32,3 +32,14 @@ impl IOSHandle { } } } + +impl From for IOSHandle { + fn from(handle: new::UiKitHandle) -> Self { + Self { + ui_window: handle.ui_window, + ui_view: handle.ui_view, + ui_view_controller: handle.ui_view_controller, + ..Self::empty() + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 6bd8072..60a2dbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,6 +95,65 @@ pub unsafe trait HasRawWindowHandle { fn raw_window_handle(&self) -> RawWindowHandle; } +unsafe impl HasRawWindowHandle for T { + fn raw_window_handle(&self) -> RawWindowHandle { + match new::HasRawWindowHandle::raw_window_handle(self) { + #[cfg(target_os = "ios")] + new::RawWindowHandle::UiKit(handle) => { + RawWindowHandle::IOS(handle.into()) + } + #[cfg(target_os = "macos")] + new::RawWindowHandle::AppKit(handle) => { + RawWindowHandle::MacOS(handle.into()) + } + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + new::RawWindowHandle::Xlib(handle) => { + RawWindowHandle::Xlib(handle.into()) + } + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + new::RawWindowHandle::Xcb(handle) => { + RawWindowHandle::Xcb(handle.into()) + } + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + new::RawWindowHandle::Wayland(handle) => { + RawWindowHandle::Wayland(handle.into()) + } + #[cfg(target_os = "windows")] + new::RawWindowHandle::Win32(handle) => { + RawWindowHandle::Windows(handle.into()) + } + #[cfg(target_arch = "wasm32")] + new::RawWindowHandle::Web(handle) => { + RawWindowHandle::Web(handle.into()) + } + #[cfg(target_os = "android")] + new::RawWindowHandle::AndroidNdk(handle) => { + RawWindowHandle::Android(handle.into()) + } + _ => panic!("Invalid handle for this platform. Please update raw-window-handle to > 0.4.1!") + } + } +} + + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum RawWindowHandle { #[cfg_attr(feature = "nightly-docs", doc(cfg(target_os = "ios")))] diff --git a/src/macos.rs b/src/macos.rs index 9e167c8..7c55c14 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -31,3 +31,13 @@ impl MacOSHandle { } } } + +impl From for MacOSHandle { + fn from(handle: new::AppKitHandle) -> Self { + Self { + ns_window: handle.ns_window, + ns_view: handle.ns_view, + ..Self::empty() + } + } +} diff --git a/src/unix.rs b/src/unix.rs index be8d6f8..597da15 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -96,3 +96,33 @@ impl WaylandHandle { } } } + +impl From for XlibHandle { + fn from(handle: new::XlibHandle) -> Self { + Self { + window: handle.window, + display: handle.display, + ..Self::empty() + } + } +} + +impl From for XcbHandle { + fn from(handle: new::XcbHandle) -> Self { + Self { + window: handle.window, + connection: handle.connection, + ..Self::empty() + } + } +} + +impl From for WaylandHandle { + fn from(handle: new::WaylandHandle) -> Self { + Self { + surface: handle.surface, + display: handle.display, + ..Self::empty() + } + } +} diff --git a/src/web.rs b/src/web.rs index 846d78c..f3915b8 100644 --- a/src/web.rs +++ b/src/web.rs @@ -31,3 +31,12 @@ impl WebHandle { } } } + +impl From for WebHandle { + fn from(handle: new::WebHandle) -> Self { + Self { + id: handle.id, + ..Self::empty() + } + } +} diff --git a/src/windows.rs b/src/windows.rs index 365886e..f1361fa 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -32,3 +32,13 @@ impl WindowsHandle { } } } + +impl From for WindowsHandle { + fn from(handle: new::Win32Handle) -> Self { + Self { + hwnd: handle.hwnd, + hinstance: handle.hinstance, + ..Self::empty() + } + } +} From 681a07c0b1763c328d6805f05b762030f40c9580 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 19 Nov 2021 13:10:24 +0100 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a76f393..b73fadd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Unreleased +* Add `HasRawWindowHandle` implementation for `HasRawWindowHandle` in the newer + version `0.4.1`. + + This allows "provider" crates that implement `HasRawWindowHandle` (like + `winit`, `sdl2`, `glfw`, `fltk`, ...) to upgrade to `v0.4.1` without a + breaking change. + + Afterwards "consumer" crates (like `gfx`, `wgpu`, `rfd`, ...) can start + upgrading with minimal breakage for their users. + # 0.3.3 (2019-12-1) * Add missing `Hash` implementation for `AndroidHandle`.