1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::imports::*;
use web_sys::Element;
use workflow_dom::inject::*;

static mut DOM_INIT: bool = false;

pub struct Container {
    element: Element,
}

unsafe impl Sync for Container {}
unsafe impl Send for Container {}

impl Container {
    pub async fn try_init() -> Result<()> {
        if !unsafe { DOM_INIT } {
            let layout_css = include_str!("container.css");
            inject_css(None, layout_css)?;
            unsafe {
                DOM_INIT = true;
            }
        }

        Ok(())
    }

    pub async fn try_new(window: &web_sys::Window) -> Result<Container> {
        let document = window.document().unwrap();
        let element = document.create_element("div").unwrap();
        element.set_class_name("layout");

        let body = document
            .query_selector("body")
            .unwrap()
            .ok_or_else(|| "Unable to get body element".to_string())?;

        body.append_child(&element).unwrap();

        let layout = Container { element };

        Ok(layout)
    }

    pub fn element(&self) -> &Element {
        &self.element
    }
}