Skip to content

Commit

Permalink
Make sure pointer to HtmlCanvasElement doesn't change
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 22, 2023
1 parent e10ff48 commit 12ccb7d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
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;
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -49,7 +50,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: Style,
old_size: Rc<Cell<PhysicalSize<u32>>>,
current_size: Rc<Cell<PhysicalSize<u32>>>,
Expand Down Expand Up @@ -101,7 +103,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 @@ -539,7 +541,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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/web/web_sys/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl PointerHandler {
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw.clone();
let canvas = canvas_common.raw().clone();
self.on_pointer_press = Some(canvas_common.add_event(
"pointerdown",
move |event: PointerEvent| {
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
1 change: 1 addition & 0 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Window {
.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());
Expand Down

0 comments on commit 12ccb7d

Please sign in to comment.