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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::dom::{dom_node::intern, util, window, Cmd};
use futures::channel::mpsc;
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::MouseEvent;

/// Provides function for window related functions
#[derive(Clone, Copy)]
pub struct Window;

impl Window {
    /// Create a recurring Cmd which will be triggered
    /// everytime the window is resized
    pub fn on_resize<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(i32, i32) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let resize_callback: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |_e: web_sys::Event| {
                let (w, h) = util::get_window_size();
                let msg = cb(w, h);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("resize"),
                resize_callback.as_ref().unchecked_ref(),
            )
            .expect("add event callback");

        Cmd::recurring(rx, resize_callback)
    }

    ///
    pub fn on_mousemove<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
                let msg = cb(mouse_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("mousemove"),
                mousemove_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, mousemove_cb)
    }

    ///
    pub fn on_mouseup<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
                let msg = cb(mouse_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("mouseup"),
                mousemove_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, mousemove_cb)
    }

    ///
    pub fn on_mousedown<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
                let msg = cb(mouse_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("mousedown"),
                mousemove_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, mousemove_cb)
    }

    ///
    pub fn on_click<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
                let msg = cb(mouse_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("click"),
                mousemove_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, mousemove_cb)
    }

    ///
    pub fn on_keyup<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::KeyboardEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let key_event: web_sys::KeyboardEvent =
                    event.dyn_into().expect("must be key event");
                let msg = cb(key_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(intern("keyup"), closure_cb.as_ref().unchecked_ref())
            .expect("add event callback");
        Cmd::recurring(rx, closure_cb)
    }

    ///
    pub fn on_keydown<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::KeyboardEvent) -> MSG + Clone + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let key_event: web_sys::KeyboardEvent =
                    event.dyn_into().expect("must be key event");
                let msg = cb(key_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("keydown"),
                closure_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, closure_cb)
    }

    /// scroll the window to the top of the document
    pub fn scroll_to_top<MSG>(msg: MSG) -> Cmd<MSG>
    where
        MSG: 'static,
    {
        use std::future::ready;
        Cmd::once(ready({
            util::scroll_window_to_top();
            msg
        }))
    }

    ///
    pub fn on_popstate<F, MSG>(mut cb: F) -> Cmd<MSG>
    where
        F: FnMut(web_sys::PopStateEvent) -> MSG + 'static,
        MSG: 'static,
    {
        let (mut tx, rx) = mpsc::unbounded();
        let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
            Closure::new(move |event: web_sys::Event| {
                let popstate_event: web_sys::PopStateEvent =
                    event.dyn_into().expect("popstate event");
                let msg = cb(popstate_event);
                tx.start_send(msg).expect("send");
            });
        window()
            .add_event_listener_with_callback(
                intern("popstate"),
                closure_cb.as_ref().unchecked_ref(),
            )
            .expect("add event callback");
        Cmd::recurring(rx, closure_cb)
    }
}