Send a LoopDestroyed event when the browser is closed (#1155)
* Add the plumbing for handling browser closes * Implement the business logic for handling closes
This commit is contained in:
parent
7ee9d5639b
commit
e87bc3db20
4 changed files with 31 additions and 1 deletions
|
|
@ -7,12 +7,25 @@ pub use self::timeout::Timeout;
|
|||
|
||||
use crate::platform::web::WindowExtWebSys;
|
||||
use crate::window::Window;
|
||||
use web_sys::HtmlCanvasElement;
|
||||
use wasm_bindgen::{closure::Closure, JsCast};
|
||||
use web_sys::{BeforeUnloadEvent, HtmlCanvasElement};
|
||||
|
||||
pub fn throw(msg: &str) {
|
||||
wasm_bindgen::throw_str(msg);
|
||||
}
|
||||
|
||||
pub fn on_unload(mut handler: impl FnMut() + 'static) {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
|
||||
let closure = Closure::wrap(
|
||||
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
|
||||
);
|
||||
|
||||
window
|
||||
.add_event_listener_with_callback("beforeunload", &closure.as_ref().unchecked_ref())
|
||||
.expect("Failed to add close listener");
|
||||
}
|
||||
|
||||
impl WindowExtWebSys for Window {
|
||||
fn canvas(&self) -> HtmlCanvasElement {
|
||||
self.window.canvas().raw().clone()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue