1use crate::dom;
4pub use wasm_bindgen_futures::spawn_local;
5use web_sys::ScrollToOptions;
6
7thread_local!(static WINDOW: web_sys::Window = web_sys::window().expect("no global `window` exists"));
9thread_local!(static DOCUMENT: web_sys::Document = window().document().expect("should have a document on window"));
10
11pub fn window() -> web_sys::Window {
13 WINDOW.with(|window| window.clone())
14}
15
16pub fn document() -> web_sys::Document {
18 DOCUMENT.with(|document| document.clone())
19}
20
21pub fn history() -> web_sys::History {
23 window().history().expect("should have a history object")
24}
25
26pub fn inject_style(style: &str) {
28 let head = document().head().expect("must have a head");
29 let style_node = document()
30 .create_element("style")
31 .expect("create style element");
32 let style_css = document().create_text_node(style);
33 style_node
34 .append_child(&style_css)
35 .expect("append to style");
36 head.append_child(&style_node).expect("must append to head");
37}
38
39pub fn body() -> web_sys::HtmlElement {
41 document().body().expect("document should have a body")
42}
43
44pub fn performance() -> web_sys::Performance {
46 window()
47 .performance()
48 .expect("should have performance on window")
49}
50
51pub fn now() -> f64 {
53 performance().now()
54}
55
56pub fn scroll_window_to_top() {
58 let options = ScrollToOptions::new();
59 options.set_top(0.0);
60 options.set_left(0.0);
61 window().scroll_to_with_scroll_to_options(&options);
62}
63
64pub fn set_location_hash(hash: &str) {
66 let location = window().location();
67 location.set_hash(hash).expect("must set the location hash");
68}
69
70pub fn get_location_hash() -> String {
73 window().location().hash().expect("must have a hash")
74}
75
76pub fn get_window_size() -> (i32, i32) {
78 let window = dom::window();
79 let window_width = window
80 .inner_width()
81 .expect("unable to get window width")
82 .as_f64()
83 .expect("cant convert to f64");
84 let window_height = window
85 .inner_height()
86 .expect("unable to get height")
87 .as_f64()
88 .expect("cant convert to f64");
89 (window_width as i32, window_height as i32)
90}
91
92pub fn set_window_title(title: &str) {
94 document().set_title(title);
95}