Skip to content

Commit

Permalink
Web: add support for more RawWindowHandle variants (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Dec 17, 2023
1 parent 211db6f commit 4e8735c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- MacOS: Fix double-free of `NSWindow`.
- On MacOS, Fix double-free of `NSWindow`.
- On Web, add support for more `RawWindowHandle` variants.

# 0.4.0

Expand All @@ -25,7 +26,7 @@

* On X11, fix the length of the returned buffer when using the wire-transferred buffer.
* On Web, fix incorrect starting coordinates when handling buffer damage.
* Or Redox, use `MAP_SHARED`; fixing behavior with latest Orbital.
* On Redox, use `MAP_SHARED`; fixing behavior with latest Orbital.
* Error instead of segfault on macOS if size isn't set.
* Add `OffscreenCanvas` support in web backend.
* Add DRM/KMS backend, for running on tty without X/Wayland.
Expand Down
31 changes: 21 additions & 10 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,29 @@ enum Canvas {
impl<D: HasDisplayHandle, W: HasWindowHandle> WebImpl<D, W> {
pub(crate) fn new(display: &WebDisplayImpl<D>, window: W) -> Result<Self, InitError<W>> {
let raw = window.window_handle()?.as_raw();
let handle = match raw {
RawWindowHandle::Web(handle) => handle,
let canvas: HtmlCanvasElement = match raw {
RawWindowHandle::Web(handle) => {
display
.document
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
// `querySelector` only throws an error if the selector is invalid.
.unwrap()
.swbuf_err("No canvas found with the given id")?
// We already made sure this was a canvas in `querySelector`.
.unchecked_into()
}
RawWindowHandle::WebCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
value.clone().unchecked_into()
}
RawWindowHandle::WebOffscreenCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
let canvas: OffscreenCanvas = value.clone().unchecked_into();

return Self::from_offscreen_canvas(canvas, window).map_err(InitError::Failure);
}
_ => return Err(InitError::Unsupported(window)),
};
let canvas: HtmlCanvasElement = display
.document
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
// `querySelector` only throws an error if the selector is invalid.
.unwrap()
.swbuf_err("No canvas found with the given id")?
// We already made sure this was a canvas in `querySelector`.
.unchecked_into();

Self::from_canvas(canvas, window).map_err(InitError::Failure)
}
Expand Down

0 comments on commit 4e8735c

Please sign in to comment.