Skip to content

Commit

Permalink
Update raw-win-handle to 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
atlanticaccent committed May 12, 2024
1 parent cc7cb62 commit 6aa35f5
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 6 additions & 7 deletions druid-shell/src/backend/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, HandleError> {
tracing::error!("HasRawWindowHandle trait not implemented for gtk.");
Err(HandleError::NotSupported)
}
}

Expand Down
13 changes: 7 additions & 6 deletions druid-shell/src/backend/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, 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)
}
}

Expand Down
10 changes: 5 additions & 5 deletions druid-shell/src/backend/wayland/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, HandleError> {
tracing::error!("HasRawWindowHandle trait not implemented for wayland.");
Err(HandleError::NotSupported)
}
}

Expand Down
10 changes: 5 additions & 5 deletions druid-shell/src/backend/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, HandleError> {
tracing::error!("HasRawWindowHandle trait not implemented for wasm.");
Err(HandleError::NotSupported)
}
}

Expand Down
39 changes: 22 additions & 17 deletions druid-shell/src/backend/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::WindowHandle<'_>, 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)
}
}

Expand Down
15 changes: 8 additions & 7 deletions druid-shell/src/backend/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, 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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions druid-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<raw_window_handle::WindowHandle<'_>, HandleError> {
self.0.window_handle()
}
}

Expand Down
2 changes: 1 addition & 1 deletion druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 6aa35f5

Please sign in to comment.