On Web, use the new WebCanvasWindowHandle (#3270)
This commit is contained in:
parent
2c15de7cf9
commit
8cd3aaa8a2
8 changed files with 54 additions and 20 deletions
|
|
@ -83,6 +83,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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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| {
|
||||
|
|
|
|||
|
|
@ -85,6 +85,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 {
|
||||
|
|
@ -378,13 +394,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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue