2019-06-25 03:15:34 +02:00
|
|
|
mod canvas;
|
2019-06-25 18:07:47 +02:00
|
|
|
mod event;
|
2019-06-25 03:15:34 +02:00
|
|
|
mod timeout;
|
|
|
|
|
|
|
|
|
|
pub use self::canvas::Canvas;
|
|
|
|
|
pub use self::timeout::Timeout;
|
|
|
|
|
|
2019-06-25 18:07:47 +02:00
|
|
|
use crate::platform::web::WindowExtWebSys;
|
|
|
|
|
use crate::window::Window;
|
2019-09-11 11:47:03 -04:00
|
|
|
use wasm_bindgen::{closure::Closure, JsCast};
|
|
|
|
|
use web_sys::{BeforeUnloadEvent, HtmlCanvasElement};
|
2019-06-25 18:07:47 +02:00
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
pub fn throw(msg: &str) {
|
|
|
|
|
wasm_bindgen::throw_str(msg);
|
|
|
|
|
}
|
2019-06-25 18:07:47 +02:00
|
|
|
|
2019-09-11 11:47:03 -04:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 18:07:47 +02:00
|
|
|
impl WindowExtWebSys for Window {
|
|
|
|
|
fn canvas(&self) -> HtmlCanvasElement {
|
|
|
|
|
self.window.canvas().raw().clone()
|
|
|
|
|
}
|
|
|
|
|
}
|