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
use crate::{
Attribute,
Callback,
Event,
};
use wasm_bindgen::JsCast;
#[cfg(web_sys_unstable_apis)]
pub use web_sys::ClipboardEvent;
pub use web_sys::{
AnimationEvent,
HashChangeEvent,
KeyboardEvent,
MouseEvent,
TransitionEvent,
};
use web_sys::{
EventTarget,
HtmlInputElement,
HtmlTextAreaElement,
};
pub fn on<F, MSG>(event_name: &'static str, f: F) -> Attribute<MSG>
where
F: Fn(Event) -> MSG + 'static,
{
mt_dom::on(event_name, Callback::from(f))
}
pub fn on_click<F, MSG>(f: F) -> Attribute<MSG>
where
F: Fn(MouseEvent) -> MSG + 'static,
{
on("click", move |event: Event| f(to_mouse_event(event)))
}
pub fn on_enter<F, MSG>(f: F) -> Attribute<MSG>
where
F: Fn(KeyboardEvent) -> MSG + 'static,
{
on("enter", move |event: Event| f(to_keyboard_event(event)))
}
pub fn on_scroll<F, MSG>(f: F) -> Attribute<MSG>
where
F: Fn((i32, i32)) -> MSG + 'static,
MSG: 'static,
{
on("scroll", move |event: Event| {
let target = event.target().expect("can't get target");
let element: &web_sys::Element =
target.dyn_ref().expect("Cant cast to Element");
let scroll_top = element.scroll_top();
let scroll_left = element.scroll_left();
f((scroll_top, scroll_left))
})
}
macro_rules! declare_events {
( $(
$(#[$attr:meta])*
$name:ident => $event:ident => $mapper:ident => $ret:ty;
)*
) => {
$(
doc_comment!{
concat!("attach an [",stringify!($name),"](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/",stringify!($name),") event to the html element"),
$(#[$attr])*
#[inline]
pub fn $name<CB, MSG>(cb: CB) -> crate::Attribute<MSG>
where CB: Fn($ret) -> MSG + 'static,
MSG: 'static,
{
on(stringify!($event), move|event:Event|{
cb($mapper(event))
})
}
}
)*
}
}
macro_rules! declare_html_events{
( $(
$(#[$attr:meta])*
$name:ident => $event:ident => $mapper:ident => $ret:ty;
)*
) => {
declare_events!{
$(
$(#[$attr])*
$name => $event => $mapper => $ret;
)*
}
pub const HTML_EVENTS: [&'static str; 31] = [$(stringify!($event),)*];
}
}
fn to_mouse_event(event: Event) -> MouseEvent {
event.dyn_into().expect("Unable to cast to mouse event")
}
fn to_keyboard_event(event: Event) -> KeyboardEvent {
event.dyn_into().expect("unable to cast to keyboard event")
}
fn to_animation_event(event: Event) -> AnimationEvent {
event.dyn_into().expect("unable to cast to animation event")
}
fn to_transition_event(event: Event) -> TransitionEvent {
event
.dyn_into()
.expect("unable to cast to transition event")
}
fn as_is(event: Event) -> Event {
event
}
fn to_hashchange_event(event: Event) -> HashChangeEvent {
event
.dyn_into()
.expect("unable to cast to hashchange event")
}
pub struct InputEvent {
pub value: String,
pub event: Event,
}
impl InputEvent {
fn new(value: String, event: Event) -> Self {
InputEvent { value, event }
}
}
fn to_input_event(event: Event) -> InputEvent {
let target: EventTarget =
event.target().expect("Unable to get event target");
if let Some(input) = target.dyn_ref::<HtmlInputElement>() {
InputEvent::new(input.value(), event)
} else if let Some(textarea) = target.dyn_ref::<HtmlTextAreaElement>() {
InputEvent::new(textarea.value(), event)
} else {
panic!("fail in mapping event into input event");
}
}
#[cfg(web_sys_unstable_apis)]
fn to_clipboard_event(event: Event) -> ClipboardEvent {
event.dyn_into().expect("unable to cast to clipboard event")
}
declare_html_events! {
on_auxclick => auxclick => to_mouse_event => MouseEvent;
on_animationend => animationend => to_animation_event => AnimationEvent;
on_transitionend => transitionend => to_transition_event => TransitionEvent;
on_contextmenu => contextmenu => to_mouse_event => MouseEvent;
on_dblclick => dblclick => to_mouse_event => MouseEvent;
on_mousedown => mousedown => to_mouse_event => MouseEvent;
on_mouseenter => mouseenter => to_mouse_event => MouseEvent;
on_mouseleave => mouseleave => to_mouse_event => MouseEvent;
on_mousemove => mousemove => to_mouse_event => MouseEvent;
on_mouseover => mouseover => to_mouse_event => MouseEvent;
on_mouseout => mouseout => to_mouse_event => MouseEvent;
on_mouseup => mouseup => to_mouse_event => MouseEvent;
on_pointerlockchange => pointerlockchange => to_mouse_event => MouseEvent;
on_pointerlockerror => pointerlockerror => to_mouse_event => MouseEvent;
on_select => select => as_is => Event;
on_wheel => wheel => to_mouse_event => MouseEvent;
on_doubleclick => dblclick => to_mouse_event => MouseEvent;
on_keydown => keydown => to_keyboard_event => KeyboardEvent;
on_keypress => keypress => to_keyboard_event => KeyboardEvent;
on_keyup => keyup => to_keyboard_event => KeyboardEvent;
on_focus => focus => as_is => Event;
on_blur => blur => as_is => Event;
on_reset => reset => as_is => Event;
on_submit => submit => as_is => Event;
on_input => input => to_input_event => InputEvent;
#[cfg(web_sys_unstable_apis)]
on_paste => paste => to_clipboard_event => ClipboardEvent;
#[cfg(web_sys_unstable_apis)]
on_copy => copy => to_clipboard_event => ClipboardEvent;
on_change => change => to_input_event => InputEvent;
on_broadcast => broadcast => to_input_event => InputEvent;
on_hashchange => hashchange => to_hashchange_event => HashChangeEvent;
on_readystatechange => readystatechange => as_is => Event;
}