On Web, use the new WebCanvasWindowHandle (#3270)

This commit is contained in:
daxpedda 2023-12-22 22:33:50 +01:00 committed by GitHub
parent 2c15de7cf9
commit 8cd3aaa8a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 54 additions and 20 deletions

View file

@ -1,4 +1,5 @@
use std::cell::Cell;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
@ -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>>>,
@ -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(),
@ -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
}
}

View file

@ -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| {
@ -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| {