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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
pub mod element;

use std::{fmt::Display, ops::Deref};

use wasm_bindgen::{JsCast, JsValue, prelude::Closure};

pub use we_derive::{we_builder, WebElement};
pub use element::{Element, WebElement, WebElementBuilder, elem};
use web_sys::KeyboardEvent;

#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
    JsError(JsValue),
    Cast(&'static str),
    Window,
    Document,
    Body,
    Value,
}

impl From<JsValue> for Error {
    fn from(from: JsValue) -> Self {
        Error::JsError(from)
    }
}

impl From<Error> for JsValue {
    fn from(e: Error) -> Self {
        e.as_jsvalue()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::JsError(s) => {
                if let Some(s) = s.as_string() {
                    write!(f, "{}", s)
                } else {
                    Err(std::fmt::Error)
                }
            }
            Error::Cast(t) => writeln!(f, "unable to cast value to type `{}`", t),
            n => writeln!(f, "{:?}", n)
        }
    }
}

impl Error {
    pub fn as_jsvalue(&self) -> JsValue {
        if let Self::JsError(jsvalue) = self {
            jsvalue.clone()
        } else {
            JsValue::from_str(&self.to_string())
        }
    }

    pub fn js_str(value: impl AsRef<str>) -> Error {
        Error::JsError(JsValue::from_str(value.as_ref()))
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

pub struct Window {
    window: web_sys::Window
}

impl Deref for Window {
    type Target = web_sys::Window;

    fn deref(&self) -> &Self::Target {
        &self.window
    }
}

impl Window {
    pub fn on_animation(&self, callback: impl FnMut() + 'static ) -> Result<()> {
        let closure = Closure::wrap(Box::new(callback) as Box<dyn FnMut()>);
        self.request_animation_frame(closure.as_ref().unchecked_ref())
            .map_err(Error::JsError)?;
        closure.forget();
        Ok(())
    }
}

pub fn window() -> Result<Window> {
    Ok(Window {
        window: web_sys::window().ok_or(Error::Window)?
    })
}

pub struct Document {
    document: web_sys::Document
}

impl Document {
    pub fn on_key(&self, mut callback: impl FnMut(KeyboardEvent) + 'static ) -> Result<()> {
        let closure = Closure::wrap(Box::new( move |e| {
            callback(e)
        }) as Box<dyn FnMut(KeyboardEvent)>);
        self.document
            .add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())
            .map_err(Error::JsError)?;
        closure.forget();
        Ok(())
    }

    pub fn body(&self) -> Result<Element<crate::elem::Base>> {
        let element = self.document.body().ok_or(Error::Body)?;
        Ok(Element::from_element(element))
    }
}

impl Deref for Document {
    type Target = web_sys::Document;

    fn deref(&self) -> &Self::Target {
        &self.document
    }
}

pub fn document() -> Result<Document> {
    Ok(Document{
        document: window()?.document().ok_or(Error::Document)?
    })
}

pub trait Loggable {
    fn log(self);
}

impl<T> Loggable for Result<T> {
    fn log(self) {
        if let Err(err) = self {
            log(format!("{}", err))
        }
    }
}

#[allow(unused_unsafe)]
pub fn log<S: AsRef<str>>(str: S) {
    unsafe {
        web_sys::console::log_1(&JsValue::from_str(str.as_ref()));
    }
}