Skip to content

Commit

Permalink
On Web, use the new WebCanvasWindowHandle (rust-windowing#3270)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and hecrj committed Mar 19, 2024
1 parent fc1b6ee commit 592bd15
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
14 changes: 12 additions & 2 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,14 @@ impl Inner {
}

#[cfg(feature = "rwh_06")]
pub fn raw_window_handle_rwh_06(&self) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle {
let mut window_handle = rwh_06::UiKitWindowHandle::new({
let ui_view = Id::as_ptr(&self.view) as _;
std::ptr::NonNull::new(ui_view).expect("Id<T> should never be null")
});
window_handle.ui_view_controller =
std::ptr::NonNull::new(Id::as_ptr(&self.view_controller) as _);
Ok(rwh_06::RawWindowHandle::UiKit(window_handle))
rwh_06::RawWindowHandle::UiKit(window_handle)
}

#[cfg(feature = "rwh_06")]
Expand Down Expand Up @@ -518,6 +518,16 @@ impl Window {
pub(crate) fn maybe_wait_on_main<R: Send>(&self, f: impl FnOnce(&Inner) -> R + Send) -> R {
self.inner.get_on_main(|inner, _mtm| f(inner))
}

#[cfg(feature = "rwh_06")]
#[inline]
pub(crate) fn raw_window_handle_rwh_06(
&self,
) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
Ok(self
.maybe_wait_on_main(|w| crate::SendSyncWrapper(w.raw_window_handle_rwh_06()))
.0)
}
}

// WindowExtIOS
Expand Down
14 changes: 12 additions & 2 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ impl Window {
) -> R {
self.window.get_on_main(|window, _mtm| f(window))
}

#[cfg(feature = "rwh_06")]
#[inline]
pub(crate) fn raw_window_handle_rwh_06(
&self,
) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
Ok(self
.maybe_wait_on_main(|w| crate::SendSyncWrapper(w.raw_window_handle_rwh_06()))
.0)
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -1378,12 +1388,12 @@ impl WinitWindow {

#[cfg(feature = "rwh_06")]
#[inline]
pub fn raw_window_handle_rwh_06(&self) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle {
let window_handle = rwh_06::AppKitWindowHandle::new({
let ptr = Id::as_ptr(&self.contentView()) as *mut _;
std::ptr::NonNull::new(ptr).expect("Id<T> should never be null")
});
Ok(rwh_06::RawWindowHandle::AppKit(window_handle))
rwh_06::RawWindowHandle::AppKit(window_handle)
}

#[cfg(feature = "rwh_06")]
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl<T> EventLoopWindowTarget<T> {
) {
let canvas_clone = canvas.clone();
let mut canvas = canvas.borrow_mut();
#[cfg(any(feature = "rwh_04", feature = "rwh_05"))]
canvas.set_attribute("data-raw-handle", &id.0.to_string());

canvas.on_touch_start(prevent_default);
Expand Down
12 changes: 9 additions & 3 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::Cell;
use std::ops::Deref;
use std::rc::{Rc, Weak};
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -50,7 +51,8 @@ pub struct Common {
pub window: web_sys::Window,
pub document: Document,
/// Note: resizing the HTMLCanvasElement should go through `backend::set_canvas_size` to ensure the DPI factor is maintained.
pub raw: HtmlCanvasElement,
/// Note: this is read-only because we use a pointer to this for [`WindowHandle`](rwh_06::WindowHandle).
raw: Rc<HtmlCanvasElement>,
style: CssStyleDeclaration,
old_size: Rc<Cell<PhysicalSize<u32>>>,
current_size: Rc<Cell<PhysicalSize<u32>>>,
Expand Down Expand Up @@ -102,7 +104,7 @@ impl Canvas {
let common = Common {
window: window.clone(),
document: document.clone(),
raw: canvas.clone(),
raw: Rc::new(canvas.clone()),
style,
old_size: Rc::default(),
current_size: Rc::default(),
Expand Down Expand Up @@ -552,7 +554,11 @@ impl Common {
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
F: 'static + FnMut(E),
{
EventListenerHandle::new(self.raw.clone(), event_name, Closure::new(handler))
EventListenerHandle::new(self.raw.deref().clone(), event_name, Closure::new(handler))
}

pub fn raw(&self) -> &HtmlCanvasElement {
&self.raw
}

// The difference between add_event and add_user_event is that the latter has a special meaning
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/web_sys/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ impl PointerHandler {
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw.clone();
self.on_pointer_press = Some(canvas_common.add_transient_event(
let canvas = canvas_common.raw().clone();
self.on_pointer_press = Some(canvas_common.add_event(
"pointerdown",
move |event: PointerEvent| {
if prevent_default {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl PointerHandler {
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw.clone();
let canvas = canvas_common.raw().clone();
self.on_cursor_move = Some(canvas_common.add_event(
"pointermove",
move |event: PointerEvent| {
Expand Down
23 changes: 16 additions & 7 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ impl Window {
.value()
.map(|inner| inner.canvas.borrow().raw().clone())
}

#[cfg(feature = "rwh_06")]
#[inline]
pub fn raw_window_handle_rwh_06(&self) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
self.inner
.value()
.map(|inner| {
let canvas = inner.canvas.borrow();
// SAFETY: This will only work if the reference to `HtmlCanvasElement` stays valid.
let canvas: &wasm_bindgen::JsValue = canvas.raw();
let window_handle =
rwh_06::WebCanvasWindowHandle::new(std::ptr::NonNull::from(canvas).cast());
rwh_06::RawWindowHandle::WebCanvas(window_handle)
})
.ok_or(rwh_06::HandleError::Unavailable)
}
}

impl Inner {
Expand Down Expand Up @@ -379,13 +395,6 @@ impl Inner {
rwh_05::RawDisplayHandle::Web(rwh_05::WebDisplayHandle::empty())
}

#[cfg(feature = "rwh_06")]
#[inline]
pub fn raw_window_handle_rwh_06(&self) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
let window_handle = rwh_06::WebWindowHandle::new(self.id.0);
Ok(rwh_06::RawWindowHandle::Web(window_handle))
}

#[cfg(feature = "rwh_06")]
#[inline]
pub fn raw_display_handle_rwh_06(
Expand Down
5 changes: 1 addition & 4 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,10 +1517,7 @@ impl Window {
#[cfg(feature = "rwh_06")]
impl rwh_06::HasWindowHandle for Window {
fn window_handle(&self) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
let raw = self
.window
.maybe_wait_on_main(|w| w.raw_window_handle_rwh_06().map(SendSyncWrapper))?
.0;
let raw = self.window.raw_window_handle_rwh_06()?;

// SAFETY: The window handle will never be deallocated while the window is alive.
Ok(unsafe { rwh_06::WindowHandle::borrow_raw(raw) })
Expand Down

0 comments on commit 592bd15

Please sign in to comment.