use crate::dom;
pub use wasm_bindgen_futures::spawn_local;
use web_sys::ScrollToOptions;
thread_local!(static WINDOW: web_sys::Window = web_sys::window().expect("no global `window` exists"));
thread_local!(static DOCUMENT: web_sys::Document = window().document().expect("should have a document on window"));
pub fn window() -> web_sys::Window {
WINDOW.with(|window| window.clone())
}
pub fn document() -> web_sys::Document {
DOCUMENT.with(|document| document.clone())
}
pub fn history() -> web_sys::History {
window().history().expect("should have a history object")
}
pub fn inject_style(style: &str) {
let head = document().head().expect("must have a head");
let style_node = document()
.create_element("style")
.expect("create style element");
let style_css = document().create_text_node(style);
style_node
.append_child(&style_css)
.expect("append to style");
head.append_child(&style_node).expect("must append to head");
}
pub fn body() -> web_sys::HtmlElement {
document().body().expect("document should have a body")
}
pub fn performance() -> web_sys::Performance {
window()
.performance()
.expect("should have performance on window")
}
pub fn now() -> f64 {
performance().now()
}
pub fn scroll_window_to_top() {
let mut options = ScrollToOptions::new();
options.top(0.0);
options.left(0.0);
window().scroll_to_with_scroll_to_options(&options);
}
pub fn set_location_hash(hash: &str) {
let location = window().location();
location.set_hash(hash).expect("must set the location hash");
}
pub fn get_location_hash() -> String {
window().location().hash().expect("must have a hash")
}
pub fn get_window_size() -> (i32, i32) {
let window = dom::window();
let window_width = window
.inner_width()
.expect("unable to get window width")
.as_f64()
.expect("cant convert to f64");
let window_height = window
.inner_height()
.expect("unable to get height")
.as_f64()
.expect("cant convert to f64");
(window_width as i32, window_height as i32)
}
pub fn set_window_title(title: &str) {
document().set_title(title);
}