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

Implement HasRawWindowHandle trait for win/mac #1586

Merged
merged 5 commits into from
Feb 20, 2021
Merged
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
3 changes: 3 additions & 0 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ default-target = "x86_64-pc-windows-msvc"
default = ["gtk"]
gtk = ["gio", "gdk", "gdk-sys", "glib", "glib-sys", "gtk-sys", "gtk-rs", "gdk-pixbuf"]
x11 = ["x11rb", "nix", "cairo-sys-rs"]
# Implement HasRawWindowHandle for WindowHandle
raw-win-handle = ["raw-window-handle"]

# passing on all the image features. AVIF is not supported because it does not
# support decoding, and that's all we use `Image` for.
Expand Down Expand Up @@ -51,6 +53,7 @@ keyboard-types = { version = "0.5.0", default_features = false }

# Optional dependencies
image = { version = "0.23.12", optional = true, default_features = false }
raw-window-handle = { version = "0.3.3", optional = true, default_features = false }

[target.'cfg(target_os="windows")'.dependencies]
scopeguard = "1.1.0"
Expand Down
12 changes: 12 additions & 0 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ use gio::ApplicationExt;
use gtk::prelude::*;
use gtk::{AccelGroup, ApplicationWindow, DrawingArea, SettingsExt};

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

use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
use crate::piet::{Piet, PietText, RenderContext};

Expand Down Expand Up @@ -94,6 +97,15 @@ pub struct WindowHandle {
marker: std::marker::PhantomData<*const ()>,
}

#[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(XcbHandle::empty())
}
}

/// Operations that we defer in order to avoid re-entrancy. See the documentation in the windows
/// backend for more details.
enum DeferredOp {
Expand Down
15 changes: 15 additions & 0 deletions druid-shell/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ use objc::rc::WeakPtr;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};

#[cfg(feature = "raw-win-handle")]
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};

use crate::kurbo::{Insets, Point, Rect, Size, Vec2};
use crate::piet::{Piet, PietText, RenderContext};

Expand Down Expand Up @@ -1214,6 +1217,18 @@ impl WindowHandle {
}
}

#[cfg(feature = "raw-win-handle")]
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
let nsv = self.nsview.load();
let handle = MacOSHandle {
ns_view: *nsv as *mut _,
..MacOSHandle::empty()
};
RawWindowHandle::MacOS(handle)
}
}

unsafe impl Send for IdleHandle {}

impl IdleHandle {
Expand Down
11 changes: 11 additions & 0 deletions druid-shell/src/platform/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use instant::Instant;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

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

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

use crate::piet::{PietText, RenderContext};
Expand Down Expand Up @@ -75,6 +78,14 @@ pub(crate) struct WindowBuilder {
#[derive(Clone, Default)]
pub struct WindowHandle(Weak<WindowState>);

#[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(WebHandle::empty())
}
}

/// A handle that can get used to schedule an idle handler. Note that
/// this handle is thread safe.
#[derive(Clone)]
Expand Down
23 changes: 23 additions & 0 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ use winapi::um::wingdi::*;
use winapi::um::winnt::*;
use winapi::um::winuser::*;

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

use piet_common::d2d::{D2DFactory, DeviceContext};
use piet_common::dwrite::DwriteFactory;

Expand Down Expand Up @@ -159,6 +162,26 @@ pub struct WindowHandle {
state: Weak<WindowState>,
}

#[cfg(feature = "raw-win-handle")]
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
if let Some(hwnd) = self.get_hwnd() {
let handle = WindowsHandle {
hwnd: hwnd as *mut core::ffi::c_void,
hinstance: unsafe {
winapi::um::libloaderapi::GetModuleHandleW(0 as winapi::um::winnt::LPCWSTR)
as *mut core::ffi::c_void
},
..WindowsHandle::empty()
};
RawWindowHandle::Windows(handle)
} else {
error!("Cannot retrieved HWND for window.");
RawWindowHandle::Windows(WindowsHandle::empty())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t it HWND

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not convinced panicking is right for any of these. I think they should return an empty window handle and maybe log a warning. Given that the raw window handle can represent empty handles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're okay with the idea that a RawWindowHandle can potentially be empty then I agree, otherwise we could consider making this an Option<RawWindowHandle>.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an external crate, that is depended on by various things like wgpu and video libraries (and some other gui/rendering libraries). The RawWindowHandles all explicitly have an empty state and consumers have to handle that.

Copy link
Contributor Author

@djeedai djeedai Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, typo. Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a panic! is justified because if we have a WindowHandle without a native HWND I'm assuming we're in serious trouble. Or is there a valid state where that happens?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible when the window is in the process of being set up or torn down? Elsewhere in the windows backend we log when we can't get the HWND, so I might just keep doing that. 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to log. These handles are meant to represent empty states and users of them are expected to handle those states (its better to have an error on screen than a program crash!)

}
}

/// A handle that can get used to schedule an idle handler. Note that
/// this handle is thread safe. If the handle is used after the hwnd
/// has been destroyed, probably not much will go wrong (the DS_RUN_IDLE
Expand Down
11 changes: 11 additions & 0 deletions druid-shell/src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ use x11rb::protocol::xproto::{
use x11rb::wrapper::ConnectionExt as _;
use x11rb::xcb_ffi::XCBConnection;

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

use crate::common_util::IdleCallback;
use crate::dialog::FileDialogOptions;
use crate::error::Error as ShellError;
Expand Down Expand Up @@ -1547,3 +1550,11 @@ impl WindowHandle {
}
}
}

#[cfg(feature = "raw-win-handle")]
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
error!("HasRawWindowHandle trait not implemented for x11.");
RawWindowHandle::Xcb(XcbHandle::empty())
}
}
9 changes: 9 additions & 0 deletions druid-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use crate::platform::window as platform;
use crate::region::Region;
use crate::scale::Scale;
use piet_common::PietText;
#[cfg(feature = "raw-win-handle")]
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

/// A token that uniquely identifies a running timer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
Expand Down Expand Up @@ -338,6 +340,13 @@ impl WindowHandle {
}
}

#[cfg(feature = "raw-win-handle")]
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
self.0.raw_window_handle()
}
}

/// A builder type for creating new windows.
pub struct WindowBuilder(platform::WindowBuilder);

Expand Down