Add cleanup code to web backend, mostly web-sys (#1715)
* web-sys: Impl. event listeners removal for canvas * web-sys: Impl. media query listeners cleanup * web: Emit WindowEvent::Destroyed after Window is dropped * web-sys: Fix unload event closure being dropped early * web: Impl. cleanup on ControlFlow::Exit - Drops the Runner, which causes the event handler closure to be dropped. - (web-sys only:) Remove event listeners from DOM. * web: Do not remove canvas from DOM when dropping Window The canvas was inserted by the user, so it should be up to the user whether the canvas should be removed. * Update changelog
This commit is contained in:
parent
1c97a310b1
commit
47e7aa4209
13 changed files with 436 additions and 162 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use super::event;
|
||||
use super::event_handle::EventListenerHandle;
|
||||
use super::media_query_handle::MediaQueryListHandle;
|
||||
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
|
||||
use crate::error::OsError as RootOE;
|
||||
use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode};
|
||||
|
|
@ -18,14 +20,14 @@ mod pointer_handler;
|
|||
|
||||
pub struct Canvas {
|
||||
common: Common,
|
||||
on_focus: Option<Closure<dyn FnMut(FocusEvent)>>,
|
||||
on_blur: Option<Closure<dyn FnMut(FocusEvent)>>,
|
||||
on_keyboard_release: Option<Closure<dyn FnMut(KeyboardEvent)>>,
|
||||
on_keyboard_press: Option<Closure<dyn FnMut(KeyboardEvent)>>,
|
||||
on_received_character: Option<Closure<dyn FnMut(KeyboardEvent)>>,
|
||||
on_mouse_wheel: Option<Closure<dyn FnMut(WheelEvent)>>,
|
||||
on_fullscreen_change: Option<Closure<dyn FnMut(Event)>>,
|
||||
on_dark_mode: Option<Closure<dyn FnMut(MediaQueryListEvent)>>,
|
||||
on_focus: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
|
||||
on_blur: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
|
||||
on_keyboard_release: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
|
||||
on_keyboard_press: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
|
||||
on_received_character: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
|
||||
on_mouse_wheel: Option<EventListenerHandle<dyn FnMut(WheelEvent)>>,
|
||||
on_fullscreen_change: Option<EventListenerHandle<dyn FnMut(Event)>>,
|
||||
on_dark_mode: Option<MediaQueryListHandle>,
|
||||
mouse_state: MouseState,
|
||||
}
|
||||
|
||||
|
|
@ -35,12 +37,6 @@ struct Common {
|
|||
wants_fullscreen: Rc<RefCell<bool>>,
|
||||
}
|
||||
|
||||
impl Drop for Common {
|
||||
fn drop(&mut self) {
|
||||
self.raw.remove();
|
||||
}
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
pub fn create(attr: PlatformSpecificWindowBuilderAttributes) -> Result<Self, RootOE> {
|
||||
let canvas = match attr.canvas {
|
||||
|
|
@ -264,22 +260,12 @@ impl Canvas {
|
|||
where
|
||||
F: 'static + FnMut(bool),
|
||||
{
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
|
||||
self.on_dark_mode = window
|
||||
.match_media("(prefers-color-scheme: dark)")
|
||||
.ok()
|
||||
.flatten()
|
||||
.and_then(|media| {
|
||||
let closure = Closure::wrap(Box::new(move |event: MediaQueryListEvent| {
|
||||
handler(event.matches())
|
||||
}) as Box<dyn FnMut(_)>);
|
||||
|
||||
media
|
||||
.add_listener_with_opt_callback(Some(&closure.as_ref().unchecked_ref()))
|
||||
.map(|_| closure)
|
||||
.ok()
|
||||
});
|
||||
let closure =
|
||||
Closure::wrap(
|
||||
Box::new(move |event: MediaQueryListEvent| handler(event.matches()))
|
||||
as Box<dyn FnMut(_)>,
|
||||
);
|
||||
self.on_dark_mode = MediaQueryListHandle::new("(prefers-color-scheme: dark)", closure);
|
||||
}
|
||||
|
||||
pub fn request_fullscreen(&self) {
|
||||
|
|
@ -289,10 +275,29 @@ impl Canvas {
|
|||
pub fn is_fullscreen(&self) -> bool {
|
||||
self.common.is_fullscreen()
|
||||
}
|
||||
|
||||
pub fn remove_listeners(&mut self) {
|
||||
self.on_focus = None;
|
||||
self.on_blur = None;
|
||||
self.on_keyboard_release = None;
|
||||
self.on_keyboard_press = None;
|
||||
self.on_received_character = None;
|
||||
self.on_mouse_wheel = None;
|
||||
self.on_fullscreen_change = None;
|
||||
self.on_dark_mode = None;
|
||||
match &mut self.mouse_state {
|
||||
MouseState::HasPointerEvent(h) => h.remove_listeners(),
|
||||
MouseState::NoPointerEvent(h) => h.remove_listeners(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Common {
|
||||
fn add_event<E, F>(&self, event_name: &str, mut handler: F) -> Closure<dyn FnMut(E)>
|
||||
fn add_event<E, F>(
|
||||
&self,
|
||||
event_name: &'static str,
|
||||
mut handler: F,
|
||||
) -> EventListenerHandle<dyn FnMut(E)>
|
||||
where
|
||||
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
|
||||
F: 'static + FnMut(E),
|
||||
|
|
@ -307,17 +312,19 @@ impl Common {
|
|||
handler(event);
|
||||
}) as Box<dyn FnMut(E)>);
|
||||
|
||||
self.raw
|
||||
.add_event_listener_with_callback(event_name, &closure.as_ref().unchecked_ref())
|
||||
.expect("Failed to add event listener with callback");
|
||||
let listener = EventListenerHandle::new(&self.raw, event_name, closure);
|
||||
|
||||
closure
|
||||
listener
|
||||
}
|
||||
|
||||
// The difference between add_event and add_user_event is that the latter has a special meaning
|
||||
// for browser security. A user event is a deliberate action by the user (like a mouse or key
|
||||
// press) and is the only time things like a fullscreen request may be successfully completed.)
|
||||
fn add_user_event<E, F>(&self, event_name: &str, mut handler: F) -> Closure<dyn FnMut(E)>
|
||||
fn add_user_event<E, F>(
|
||||
&self,
|
||||
event_name: &'static str,
|
||||
mut handler: F,
|
||||
) -> EventListenerHandle<dyn FnMut(E)>
|
||||
where
|
||||
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
|
||||
F: 'static + FnMut(E),
|
||||
|
|
@ -343,9 +350,9 @@ impl Common {
|
|||
// handling to control event propagation.
|
||||
fn add_window_mouse_event<F>(
|
||||
&self,
|
||||
event_name: &str,
|
||||
event_name: &'static str,
|
||||
mut handler: F,
|
||||
) -> Closure<dyn FnMut(MouseEvent)>
|
||||
) -> EventListenerHandle<dyn FnMut(MouseEvent)>
|
||||
where
|
||||
F: 'static + FnMut(MouseEvent),
|
||||
{
|
||||
|
|
@ -364,15 +371,14 @@ impl Common {
|
|||
}
|
||||
}) as Box<dyn FnMut(_)>);
|
||||
|
||||
window
|
||||
.add_event_listener_with_callback_and_add_event_listener_options(
|
||||
event_name,
|
||||
&closure.as_ref().unchecked_ref(),
|
||||
AddEventListenerOptions::new().capture(true),
|
||||
)
|
||||
.expect("Failed to add event listener with callback and options");
|
||||
let listener = EventListenerHandle::with_options(
|
||||
&window,
|
||||
event_name,
|
||||
closure,
|
||||
AddEventListenerOptions::new().capture(true),
|
||||
);
|
||||
|
||||
closure
|
||||
listener
|
||||
}
|
||||
|
||||
pub fn request_fullscreen(&self) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue