Skip to content

Commit 0d5548c

Browse files
authored
Update raw window handle and fix raw-win-handle feature (#2238)
1 parent 235d721 commit 0d5548c

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

druid-shell/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ keyboard-types = { version = "0.6.2", default_features = false }
7676

7777
# Optional dependencies
7878
image = { version = "0.23.12", optional = true, default_features = false }
79-
raw-window-handle = { version = "0.4.2", optional = true, default_features = false }
79+
raw-window-handle = { version = "0.5.0", optional = true, default_features = false }
8080

8181
[target.'cfg(target_os="windows")'.dependencies]
8282
scopeguard = "1.1.0"

druid-shell/src/backend/gtk/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use instant::Duration;
4444
use tracing::{error, warn};
4545

4646
#[cfg(feature = "raw-win-handle")]
47-
use raw_window_handle::{unix::XcbHandle, HasRawWindowHandle, RawWindowHandle};
47+
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, XcbWindowHandle};
4848

4949
use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
5050
use crate::piet::{Piet, PietText, RenderContext};
@@ -126,7 +126,7 @@ unsafe impl HasRawWindowHandle for WindowHandle {
126126
fn raw_window_handle(&self) -> RawWindowHandle {
127127
error!("HasRawWindowHandle trait not implemented for gtk.");
128128
// GTK is not a platform, and there's no empty generic handle. Pick XCB randomly as fallback.
129-
RawWindowHandle::Xcb(XcbHandle::empty())
129+
RawWindowHandle::Xcb(XcbWindowHandle::empty())
130130
}
131131
}
132132

druid-shell/src/backend/mac/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use objc::{class, msg_send, sel, sel_impl};
4040
use tracing::{debug, error, info};
4141

4242
#[cfg(feature = "raw-win-handle")]
43-
use raw_window_handle::{AppKitHandle, HasRawWindowHandle, RawWindowHandle};
43+
use raw_window_handle::{AppKitWindowHandle, HasRawWindowHandle, RawWindowHandle};
4444

4545
use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
4646
use crate::piet::{Piet, PietText, RenderContext};
@@ -1417,7 +1417,7 @@ impl WindowHandle {
14171417
unsafe impl HasRawWindowHandle for WindowHandle {
14181418
fn raw_window_handle(&self) -> RawWindowHandle {
14191419
let nsv = self.nsview.load();
1420-
let mut handle = AppKitHandle::empty();
1420+
let mut handle = AppKitWindowHandle::empty();
14211421
handle.ns_view = *nsv as *mut _;
14221422
RawWindowHandle::AppKit(handle)
14231423
}

druid-shell/src/backend/wayland/window.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use wayland_protocols::xdg_shell::client::xdg_popup;
1818
use wayland_protocols::xdg_shell::client::xdg_positioner;
1919
use wayland_protocols::xdg_shell::client::xdg_surface;
2020

21+
#[cfg(feature = "raw-win-handle")]
22+
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, WaylandWindowHandle};
23+
2124
use super::application::{self, Timer};
2225
use super::{error::Error, menu::Menu, outputs, surfaces};
2326

@@ -305,6 +308,14 @@ impl std::default::Default for WindowHandle {
305308
}
306309
}
307310

311+
#[cfg(feature = "raw-win-handle")]
312+
unsafe impl HasRawWindowHandle for WindowHandle {
313+
fn raw_window_handle(&self) -> RawWindowHandle {
314+
tracing::error!("HasRawWindowHandle trait not implemented for wasm.");
315+
RawWindowHandle::Wayland(WaylandWindowHandle::empty())
316+
}
317+
}
318+
308319
#[derive(Clone, PartialEq)]
309320
pub struct CustomCursor;
310321

druid-shell/src/backend/web/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use wasm_bindgen::prelude::*;
2525
use wasm_bindgen::JsCast;
2626

2727
#[cfg(feature = "raw-win-handle")]
28-
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
28+
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, WebWindowHandle};
2929

3030
use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
3131

@@ -94,7 +94,7 @@ impl Eq for WindowHandle {}
9494
unsafe impl HasRawWindowHandle for WindowHandle {
9595
fn raw_window_handle(&self) -> RawWindowHandle {
9696
error!("HasRawWindowHandle trait not implemented for wasm.");
97-
RawWindowHandle::Web(WebHandle::empty())
97+
RawWindowHandle::Web(WebWindowHandle::empty())
9898
}
9999
}
100100

druid-shell/src/backend/windows/window.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use winapi::Interface;
4848
use wio::com::ComPtr;
4949

5050
#[cfg(feature = "raw-win-handle")]
51-
use raw_window_handle::{windows::WindowsHandle, HasRawWindowHandle, RawWindowHandle};
51+
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32WindowHandle};
5252

5353
use piet_common::d2d::{D2DFactory, DeviceContext};
5454
use piet_common::dwrite::DwriteFactory;
@@ -185,18 +185,16 @@ impl Eq for WindowHandle {}
185185
unsafe impl HasRawWindowHandle for WindowHandle {
186186
fn raw_window_handle(&self) -> RawWindowHandle {
187187
if let Some(hwnd) = self.get_hwnd() {
188-
let handle = WindowsHandle {
189-
hwnd: hwnd as *mut core::ffi::c_void,
190-
hinstance: unsafe {
191-
winapi::um::libloaderapi::GetModuleHandleW(0 as winapi::um::winnt::LPCWSTR)
192-
as *mut core::ffi::c_void
193-
},
194-
..WindowsHandle::empty()
188+
let mut handle = Win32WindowHandle::empty();
189+
handle.hwnd = hwnd as *mut core::ffi::c_void;
190+
handle.hinstance = unsafe {
191+
winapi::um::libloaderapi::GetModuleHandleW(0 as winapi::um::winnt::LPCWSTR)
192+
as *mut core::ffi::c_void
195193
};
196-
RawWindowHandle::Windows(handle)
194+
RawWindowHandle::Win32(handle)
197195
} else {
198196
error!("Cannot retrieved HWND for window.");
199-
RawWindowHandle::Windows(WindowsHandle::empty())
197+
RawWindowHandle::Win32(Win32WindowHandle::empty())
200198
}
201199
}
202200
}

druid-shell/src/backend/x11/window.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use x11rb::wrapper::ConnectionExt as _;
4242
use x11rb::xcb_ffi::XCBConnection;
4343

4444
#[cfg(feature = "raw-win-handle")]
45-
use raw_window_handle::{unix::XcbHandle, HasRawWindowHandle, RawWindowHandle};
45+
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, XcbWindowHandle};
4646

4747
use crate::backend::shared::Timer;
4848
use crate::common_util::IdleCallback;
@@ -494,7 +494,7 @@ impl WindowBuilder {
494494
window.set_position(pos);
495495
}
496496

497-
let handle = WindowHandle::new(id, Rc::downgrade(&window));
497+
let handle = WindowHandle::new(id, visual_type.visual_id, Rc::downgrade(&window));
498498
window.connect(handle.clone())?;
499499

500500
self.app.add_window(id, window)?;
@@ -1581,6 +1581,8 @@ impl IdleHandle {
15811581
#[derive(Clone, Default)]
15821582
pub(crate) struct WindowHandle {
15831583
id: u32,
1584+
#[allow(dead_code)] // Only used with the raw-win-handle feature
1585+
visual_id: u32,
15841586
window: Weak<Window>,
15851587
}
15861588
impl PartialEq for WindowHandle {
@@ -1591,8 +1593,12 @@ impl PartialEq for WindowHandle {
15911593
impl Eq for WindowHandle {}
15921594

15931595
impl WindowHandle {
1594-
fn new(id: u32, window: Weak<Window>) -> WindowHandle {
1595-
WindowHandle { id, window }
1596+
fn new(id: u32, visual_id: u32, window: Weak<Window>) -> WindowHandle {
1597+
WindowHandle {
1598+
id,
1599+
visual_id,
1600+
window,
1601+
}
15961602
}
15971603

15981604
pub fn show(&self) {
@@ -1846,19 +1852,9 @@ impl WindowHandle {
18461852
#[cfg(feature = "raw-win-handle")]
18471853
unsafe impl HasRawWindowHandle for WindowHandle {
18481854
fn raw_window_handle(&self) -> RawWindowHandle {
1849-
let mut handle = XcbHandle {
1850-
window: self.id,
1851-
..XcbHandle::empty()
1852-
};
1853-
1854-
if let Some(window) = self.window.upgrade() {
1855-
handle.connection = window.app.connection().get_raw_xcb_connection();
1856-
} else {
1857-
// Documentation for HasRawWindowHandle encourages filling in all fields possible,
1858-
// leaving those empty that cannot be derived.
1859-
error!("Failed to get XCBConnection, returning incomplete handle");
1860-
}
1861-
1855+
let mut handle = XcbWindowHandle::empty();
1856+
handle.window = self.id;
1857+
handle.visual_id = self.visual_id;
18621858
RawWindowHandle::Xcb(handle)
18631859
}
18641860
}

0 commit comments

Comments
 (0)