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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
extern crate wasm_bindgen;

use crate::native;
use std::collections::HashMap;
use std::convert::From;
use wasm_bindgen::JsCast;

/// Events for Html<Msg>
pub struct Events<Msg> {
    pub handlers: HashMap<String, Box<FnMut(native::Event) -> Msg>>,
}

/// Props of MouseEvent
pub struct MouseEvent {
    pub alt_key: bool,
    pub buttons: u32,
    pub client_x: i32,
    pub client_y: i32,
    pub ctrl_key: bool,
    pub meta_key: bool,
    pub movement_x: i32,
    pub movement_y: i32,
    pub offset_x: i32,
    pub offset_y: i32,
    pub page_x: i32,
    pub page_y: i32,
    pub screen_x: i32,
    pub screen_y: i32,
    pub shift_key: bool,
}

/// Props of DragEvent
pub struct DragEvent {}

/// Props of KeyboardEvent
pub struct KeyboardEvent {
    pub alt_key: bool,
    pub code: String,
    pub key: String,
    pub locale: String,
    pub location: u64,
    pub meta_key: bool,
    pub repeat: bool,
    pub shift_key: bool,
}

impl<Msg> Events<Msg> {
    /// Creates new empty Events
    pub fn new() -> Self {
        Self {
            handlers: HashMap::new(),
        }
    }

    /// Adds event handler
    pub fn on(
        mut self,
        type_: impl Into<String>,
        handler: impl FnMut(native::Event) -> Msg + 'static,
    ) -> Self {
        self.handlers.insert(type_.into(), Box::new(handler));
        self
    }

    pub fn on_click(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("click", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_contextmenu(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("contextmenu", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_dblclick(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("dblclick", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mousedown(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mousedown", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mouseenter(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mouseenter", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mouseleave(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mouseleave", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mousemove(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mousemove", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mouseover(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mouseover", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mouseout(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mouseout", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_mouseup(self, mut handler: impl FnMut(MouseEvent) -> Msg + 'static) -> Self {
        self.on("mouseup", move |e| {
            if let Ok(e) = e.dyn_into::<native::MouseEvent>() {
                handler(MouseEvent::from(&e))
            } else {
                handler(MouseEvent::empty())
            }
        })
    }

    pub fn on_drag(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("drag", move |_| handler(DragEvent::empty()))
    }

    pub fn on_dragend(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("dragend", move |_| handler(DragEvent::empty()))
    }

    pub fn on_dragenter(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("dragenter", move |_| handler(DragEvent::empty()))
    }

    pub fn on_dragstart(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("dragstart", move |_| handler(DragEvent::empty()))
    }

    pub fn on_dragleave(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("dragleave", move |_| handler(DragEvent::empty()))
    }

    pub fn on_dragover(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("dragover", move |_| handler(DragEvent::empty()))
    }

    pub fn on_drop(self, mut handler: impl FnMut(DragEvent) -> Msg + 'static) -> Self {
        self.on("drop", move |_| handler(DragEvent::empty()))
    }

    pub fn on_keydown(self, mut handler: impl FnMut(KeyboardEvent) -> Msg + 'static) -> Self {
        self.on("keydown", move |e| {
            if let Ok(e) = e.dyn_into::<native::KeyboardEvent>() {
                handler(KeyboardEvent::from(&e))
            } else {
                handler(KeyboardEvent::empty())
            }
        })
    }

    pub fn on_keypress(self, mut handler: impl FnMut(KeyboardEvent) -> Msg + 'static) -> Self {
        self.on("keypress", move |e| {
            if let Ok(e) = e.dyn_into::<native::KeyboardEvent>() {
                handler(KeyboardEvent::from(&e))
            } else {
                handler(KeyboardEvent::empty())
            }
        })
    }

    pub fn on_keyup(self, mut handler: impl FnMut(KeyboardEvent) -> Msg + 'static) -> Self {
        self.on("keyup", move |e| {
            if let Ok(e) = e.dyn_into::<native::KeyboardEvent>() {
                handler(KeyboardEvent::from(&e))
            } else {
                handler(KeyboardEvent::empty())
            }
        })
    }

    pub fn on_input(self, mut handler: impl FnMut(String) -> Msg + 'static) -> Self {
        self.on("input", move |e| {
            if let Ok(target) = e.target().dyn_into::<native::HTMLInputElement>() {
                handler(target.value())
            } else {
                handler("".to_string())
            }
        })
    }
}

impl MouseEvent {
    fn empty() -> Self {
        MouseEvent {
            alt_key: false,
            buttons: 0,
            client_x: 0,
            client_y: 0,
            ctrl_key: false,
            meta_key: false,
            movement_x: 0,
            movement_y: 0,
            offset_x: 0,
            offset_y: 0,
            page_x: 0,
            page_y: 0,
            screen_x: 0,
            screen_y: 0,
            shift_key: false,
        }
    }
}

impl From<&native::MouseEvent> for MouseEvent {
    fn from(e: &native::MouseEvent) -> Self {
        MouseEvent {
            alt_key: e.alt_key(),
            buttons: e.buttons(),
            client_x: e.client_x(),
            client_y: e.client_y(),
            ctrl_key: e.ctrl_key(),
            meta_key: e.meta_key(),
            movement_x: e.movement_x(),
            movement_y: e.movement_y(),
            offset_x: e.offset_x(),
            offset_y: e.offset_y(),
            page_x: e.page_x(),
            page_y: e.page_y(),
            screen_x: e.screen_x(),
            screen_y: e.screen_y(),
            shift_key: e.shift_key(),
        }
    }
}

impl DragEvent {
    fn empty() -> Self {
        DragEvent {}
    }
}

impl KeyboardEvent {
    fn empty() -> Self {
        KeyboardEvent {
            alt_key: false,
            code: "".to_string(),
            key: "".to_string(),
            locale: "".to_string(),
            location: 0,
            meta_key: false,
            repeat: false,
            shift_key: false,
        }
    }
}

impl From<&native::KeyboardEvent> for KeyboardEvent {
    fn from(e: &native::KeyboardEvent) -> Self {
        KeyboardEvent {
            alt_key: e.alt_key(),
            code: e.code(),
            key: e.key(),
            locale: e.locale(),
            location: e.location(),
            meta_key: e.meta_key(),
            repeat: e.repeat(),
            shift_key: e.shift_key(),
        }
    }
}