mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-23 02:16:33 +11:00
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
|
@ -91,6 +91,7 @@ version = "0.3.22"
|
|||
optional = true
|
||||
features = [
|
||||
'console',
|
||||
'BeforeUnloadEvent',
|
||||
'Document',
|
||||
'DomRect',
|
||||
'Element',
|
||||
|
|
|
@ -48,6 +48,9 @@ impl<T: 'static> Shared<T> {
|
|||
pub fn set_listener(&self, event_handler: Box<dyn FnMut(Event<T>, &mut root::ControlFlow)>) {
|
||||
self.0.runner.replace(Some(Runner::new(event_handler)));
|
||||
self.send_event(Event::NewEvents(StartCause::Init));
|
||||
|
||||
let close_instance = self.clone();
|
||||
backend::on_unload(move || close_instance.handle_unload());
|
||||
}
|
||||
|
||||
// Add an event to the event loop runner
|
||||
|
@ -110,6 +113,12 @@ impl<T: 'static> Shared<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_unload(&self) {
|
||||
self.apply_control_flow(root::ControlFlow::Exit);
|
||||
let mut control = self.current_control_flow();
|
||||
self.handle_event(Event::LoopDestroyed, &mut control);
|
||||
}
|
||||
|
||||
// handle_event takes in events and either queues them or applies a callback
|
||||
//
|
||||
// It should only ever be called from send_event
|
||||
|
|
|
@ -9,12 +9,19 @@ use crate::platform::web::WindowExtStdweb;
|
|||
use crate::window::Window;
|
||||
|
||||
use stdweb::js;
|
||||
use stdweb::web::event::BeforeUnloadEvent;
|
||||
use stdweb::web::html_element::CanvasElement;
|
||||
use stdweb::web::window;
|
||||
use stdweb::web::IEventTarget;
|
||||
|
||||
pub fn throw(msg: &str) {
|
||||
js! { throw @{msg} }
|
||||
}
|
||||
|
||||
pub fn on_unload(mut handler: impl FnMut() + 'static) {
|
||||
window().add_event_listener(move |_: BeforeUnloadEvent| handler());
|
||||
}
|
||||
|
||||
impl WindowExtStdweb for Window {
|
||||
fn canvas(&self) -> CanvasElement {
|
||||
self.window.canvas().raw().clone()
|
||||
|
|
|
@ -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
Reference in a new issue