Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update raw win handle to 0.6 #2401

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ hdr = ["piet-common/hdr"]
serde = ["piet-common/serde"]

[dependencies]
piet-common = "0.6.2"
piet-common = { git = "https://github.com/linebender/piet.git", rev = "303b9a932de61cca5a12a8a1ecfbba742638dc52" }

tracing = "0.1.37"
once_cell = "1.17.1"
Expand All @@ -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 All @@ -100,14 +100,14 @@ bitflags = "1.3.2"
[target.'cfg(any(target_os = "freebsd", target_os="linux", target_os="openbsd"))'.dependencies]
ashpd = { version = "0.3.2", optional = true }
# TODO(x11/dependencies): only use feature "xcb" if using X11
cairo-rs = { version = "0.16.7", default-features = false, features = ["xcb"] }
cairo-sys-rs = { version = "0.16.3", default-features = false, optional = true }
cairo-rs = { version = "0.18.0", default-features = false, features = ["xcb"] }
cairo-sys-rs = { version = "0.18.0", default-features = false, optional = true }
futures = { version = "0.3.26", optional = true, features = ["executor"]}
gdk-sys = { version = "0.16.0", optional = true }
gdk-sys = { version = "0.18.0", optional = true }
# `gtk` gets renamed to `gtk-rs` so that we can use `gtk` as the feature name.
gtk-rs = { version = "0.16.2", package = "gtk", optional = true }
glib-sys = { version = "0.16.3", optional = true }
gtk-sys = { version = "0.16.0", optional = true }
gtk-rs = { version = "0.18.0", package = "gtk", optional = true }
glib-sys = { version = "0.18.0", optional = true }
gtk-sys = { version = "0.18.0", optional = true }
nix = { version = "0.24.3", optional = true }
x11rb = { version = "0.10.1", features = ["allow-unsafe-code", "present", "render", "randr", "xfixes", "xkb", "resource_manager", "cursor"], optional = true }
wayland-client = { version = "0.29.5", optional = true }
Expand All @@ -127,7 +127,7 @@ version = "0.3.61"
features = ["Window", "MouseEvent", "CssStyleDeclaration", "WheelEvent", "KeyEvent", "KeyboardEvent", "Navigator"]

[dev-dependencies]
piet-common = { version = "0.6.2", features = ["png"] }
piet-common = { git = "https://github.com/linebender/piet.git", rev = "303b9a932de61cca5a12a8a1ecfbba742638dc52", features = ["png"] }
static_assertions = "1.1.0"
test-log = { version = "0.2.11", features = ["trace"], default-features = false }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/backend/gtk/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn get_file_dialog_path(
if let Some(file_types) = &options.allowed_types {
for f in file_types {
let filter = file_filter(f);
dialog.add_filter(&filter);
dialog.add_filter(filter.clone());

if let Some(default) = &options.default_type {
if default == f {
Expand Down
1 change: 1 addition & 0 deletions druid-shell/src/backend/gtk/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use crate::kurbo::{Point, Rect, Size};
use crate::screen::Monitor;
use gtk::gdk::{Display, DisplayManager, Rectangle};
use gtk_rs::prelude::MonitorExt as _;

fn translate_gdk_rectangle(r: Rectangle) -> Rect {
Rect::from_origin_size(
Expand Down
55 changes: 29 additions & 26 deletions druid-shell/src/backend/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use std::slice;
use std::sync::{Arc, Mutex, Weak};
use std::time::Instant;

use cairo::glib::{ControlFlow, Propagation};
use gtk::gdk_pixbuf::Colorspace::Rgb;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::glib::source::Continue;
use gtk::glib::translate::FromGlib;
use gtk::prelude::*;
use gtk::traits::SettingsExt;
use gtk::traits::GtkSettingsExt;
use gtk::{AccelGroup, ApplicationWindow, DrawingArea};

use gdk_sys::GdkKeymapKey;
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 Expand Up @@ -427,7 +426,7 @@ impl WindowBuilder {
.connect_enter_notify_event(|widget, _| {
widget.grab_focus();

Inhibit(true)
cairo::glib::Propagation::Stop
});

// Set the minimum size
Expand Down Expand Up @@ -539,7 +538,7 @@ impl WindowBuilder {
}
}

Inhibit(false)
cairo::glib::Propagation::Proceed
}));

win_state.drawing_area.connect_screen_changed(
Expand Down Expand Up @@ -595,7 +594,7 @@ impl WindowBuilder {
});
}

Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state.drawing_area.connect_button_release_event(clone!(handle => move |_widget, event| {
Expand All @@ -622,7 +621,7 @@ impl WindowBuilder {
});
}

Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state.drawing_area.connect_motion_notify_event(
Expand All @@ -643,7 +642,7 @@ impl WindowBuilder {
state.with_handler(|h| h.mouse_move(&mouse_event));
}

Inhibit(true)
cairo::glib::Propagation::Stop
}),
);

Expand All @@ -653,7 +652,7 @@ impl WindowBuilder {
state.with_handler(|h| h.mouse_leave());
}

Inhibit(true)
cairo::glib::Propagation::Stop
}),
);

Expand Down Expand Up @@ -709,7 +708,7 @@ impl WindowBuilder {
}
}

Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state
Expand All @@ -727,7 +726,7 @@ impl WindowBuilder {
);
}

Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state
Expand All @@ -745,7 +744,7 @@ impl WindowBuilder {
);
}

Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state
Expand All @@ -754,7 +753,7 @@ impl WindowBuilder {
if let Some(state) = handle.state.upgrade() {
state.with_handler(|h| h.got_focus());
}
Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state
Expand All @@ -763,17 +762,21 @@ impl WindowBuilder {
if let Some(state) = handle.state.upgrade() {
state.with_handler(|h| h.lost_focus());
}
Inhibit(true)
cairo::glib::Propagation::Stop
}));

win_state
.window
.connect_delete_event(clone!(handle => move |_widget, _ev| {
if let Some(state) = handle.state.upgrade() {
state.with_handler(|h| h.request_close());
Inhibit(!state.closing.get())
if !state.closing.get() {
Propagation::Stop
} else {
Propagation::Proceed
}
} else {
Inhibit(false)
cairo::glib::Propagation::Proceed
}
}));

Expand Down Expand Up @@ -1221,9 +1224,9 @@ impl WindowHandle {
if let Some(state) = self.state.upgrade() {
gtk::glib::timeout_add(interval, move || {
if state.with_handler(|h| h.timer(token)).is_some() {
return Continue(false);
return ControlFlow::Break;
}
Continue(true)
ControlFlow::Continue
});
}
token
Expand Down Expand Up @@ -1382,7 +1385,7 @@ impl IdleHandle {
}
}

fn run_idle(state: &Arc<WindowState>) -> Continue {
fn run_idle(state: &Arc<WindowState>) -> ControlFlow {
util::assert_main_thread();
let result = state.with_handler(|handler| {
let queue: Vec<_> = std::mem::take(&mut state.idle_queue.lock().unwrap());
Expand All @@ -1404,7 +1407,7 @@ fn run_idle(state: &Arc<WindowState>) -> Continue {
let timeout = Duration::from_millis(16);
gtk::glib::timeout_add(timeout, move || run_idle(&state));
}
Continue(false)
ControlFlow::Break
}

fn make_gdk_cursor(cursor: &Cursor, gdk_window: &Window) -> Option<gtk::gdk::Cursor> {
Expand Down Expand Up @@ -1504,7 +1507,7 @@ fn make_key_event(key: &EventKey, repeat: bool, state: KeyState) -> KeyEvent {
let keyval = key.keyval();
let hardware_keycode = key.hardware_keycode();

let keycode = hardware_keycode_to_keyval(hardware_keycode).unwrap_or_else(|| keyval.clone());
let keycode = hardware_keycode_to_keyval(hardware_keycode).unwrap_or(keyval);

let text = keyval.to_unicode();
let mods = get_modifiers(key.state());
Expand Down
14 changes: 8 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, RawWindowHandle};

use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
use crate::piet::{Piet, PietText, RenderContext};
Expand Down Expand Up @@ -1464,12 +1464,14 @@ 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 handle = AppKitWindowHandle::new(std::ptr::NonNull::from(&nsv).cast());

let handle =
unsafe { raw_window_handle::WindowHandle::borrow_raw(RawWindowHandle::AppKit(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
Loading
Loading