Replace beforeunload with pagehide

This commit is contained in:
dAxpeDDa 2023-06-05 02:11:53 +02:00 committed by daxpedda
parent 12fb37d827
commit ab46aa5b79
3 changed files with 6 additions and 11 deletions

View file

@ -74,6 +74,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, use the correct canvas size when calculating the new size during scale factor change,
instead of using the output bitmap size.
- On Web, scale factor and dark mode detection are now more robust.
- On Web, fix the bfcache by not using the `beforeunload` event.
# 0.28.6

View file

@ -133,7 +133,6 @@ version = "0.3.22"
features = [
'console',
'CssStyleDeclaration',
'BeforeUnloadEvent',
'Document',
'DomRect',
'Element',

View file

@ -15,7 +15,7 @@ use crate::dpi::{LogicalSize, Size};
use crate::platform::web::WindowExtWebSys;
use crate::window::Window;
use wasm_bindgen::closure::Closure;
use web_sys::{BeforeUnloadEvent, Element, HtmlCanvasElement};
use web_sys::{Element, HtmlCanvasElement};
pub fn throw(msg: &str) {
wasm_bindgen::throw_str(msg);
@ -28,18 +28,13 @@ pub fn exit_fullscreen(window: &web_sys::Window) {
}
pub struct UnloadEventHandle {
_listener: event_handle::EventListenerHandle<dyn FnMut(BeforeUnloadEvent)>,
_listener: event_handle::EventListenerHandle<dyn FnMut()>,
}
pub fn on_unload(
window: &web_sys::Window,
mut handler: impl FnMut() + 'static,
) -> UnloadEventHandle {
let closure = Closure::wrap(
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
);
pub fn on_unload(window: &web_sys::Window, handler: impl FnMut() + 'static) -> UnloadEventHandle {
let closure = Closure::new(handler);
let listener = event_handle::EventListenerHandle::new(window, "beforeunload", closure);
let listener = event_handle::EventListenerHandle::new(window, "pagehide", closure);
UnloadEventHandle {
_listener: listener,
}