@rust-js/resources 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,238 @@
1
+ //! [React's events](https://react.dev/reference/react-dom/components/common#react-event-object),
2
+ //! which wrap the DOM's. Each derefs to the one it extends, as React's do:
3
+ //! a [`Pointer`] is a [`Mouse`], which is a [`Ui`], which is an [`Event`].
4
+
5
+ use core::marker::PhantomData;
6
+ use core::ops::Deref;
7
+
8
+ use js::JsObject;
9
+
10
+ /// A [React event](https://react.dev/reference/react-dom/components/common#react-event-object):
11
+ /// what every handler gets.
12
+ pub struct Event(PhantomData<JsObject>);
13
+
14
+ impl Event {
15
+ /// Stop the browser's default action, like submitting a form.
16
+ #[cfg_attr(rust_js, rust_js::link_name = "preventDefault")]
17
+ pub fn prevent_default(&self) {
18
+ unreachable!()
19
+ }
20
+
21
+ /// Stop parents' handlers seeing it.
22
+ #[cfg_attr(rust_js, rust_js::link_name = "stopPropagation")]
23
+ pub fn stop_propagation(&self) {
24
+ unreachable!()
25
+ }
26
+
27
+ #[cfg_attr(rust_js, rust_js::link_name = "isDefaultPrevented")]
28
+ pub fn is_default_prevented(&self) -> bool {
29
+ unreachable!()
30
+ }
31
+
32
+ #[cfg_attr(rust_js, rust_js::link_name = "isPropagationStopped")]
33
+ pub fn is_propagation_stopped(&self) -> bool {
34
+ unreachable!()
35
+ }
36
+ }
37
+
38
+ /// Getters, one per React event field: `client_x` is `e.clientX`.
39
+ macro_rules! fields {
40
+ ($type:ident { $($(#[doc = $doc:literal])* $method:ident: $ty:ty = $js:literal;)* }) => {
41
+ impl $type {
42
+ $(
43
+ $(#[doc = $doc])*
44
+ #[cfg_attr(rust_js, rust_js::link_name = concat!("get ", $js))]
45
+ pub fn $method(&self) -> $ty {
46
+ unreachable!()
47
+ }
48
+ )*
49
+ }
50
+ };
51
+ }
52
+
53
+ fields!(Event {
54
+ bubbles: bool = "bubbles";
55
+ cancelable: bool = "cancelable";
56
+ /// The element whose handler this is.
57
+ current_target: &'static webapi::Element = "currentTarget";
58
+ default_prevented: bool = "defaultPrevented";
59
+ event_phase: u32 = "eventPhase";
60
+ is_trusted: bool = "isTrusted";
61
+ /// Where it happened.
62
+ target: &'static webapi::Element = "target";
63
+ time_stamp: f64 = "timeStamp";
64
+ /// The DOM's event that this wraps.
65
+ native_event: &'static webapi::Event = "nativeEvent";
66
+ /// Its name, like `"click"`.
67
+ type_: String = "type";
68
+ });
69
+
70
+ /// Declares an event type that extends another.
71
+ macro_rules! events {
72
+ ($($(#[doc = $doc:literal])* $name:ident: $parent:ident { $($body:tt)* })*) => {
73
+ $(
74
+ $(#[doc = $doc])*
75
+ pub struct $name(PhantomData<JsObject>);
76
+
77
+ impl Deref for $name {
78
+ type Target = $parent;
79
+
80
+ fn deref(&self) -> &$parent {
81
+ // Never runs: rust-js compiles this `Deref` to the object itself.
82
+ unsafe { &*(self as *const Self as *const $parent) }
83
+ }
84
+ }
85
+
86
+ fields!($name { $($body)* });
87
+ )*
88
+ };
89
+ }
90
+
91
+ events! {
92
+ /// A [UI event](https://developer.mozilla.org/docs/Web/API/UIEvent), like a scroll.
93
+ Ui: Event {
94
+ detail: i32 = "detail";
95
+ view: &'static webapi::Window = "view";
96
+ }
97
+
98
+ /// A click, or another [mouse event](https://developer.mozilla.org/docs/Web/API/MouseEvent).
99
+ Mouse: Ui {
100
+ alt_key: bool = "altKey";
101
+ /// Which button: 0 is the main one.
102
+ button: i32 = "button";
103
+ buttons: i32 = "buttons";
104
+ client_x: f64 = "clientX";
105
+ client_y: f64 = "clientY";
106
+ ctrl_key: bool = "ctrlKey";
107
+ meta_key: bool = "metaKey";
108
+ movement_x: f64 = "movementX";
109
+ movement_y: f64 = "movementY";
110
+ page_x: f64 = "pageX";
111
+ page_y: f64 = "pageY";
112
+ related_target: Option<&'static webapi::Element> = "relatedTarget";
113
+ screen_x: f64 = "screenX";
114
+ screen_y: f64 = "screenY";
115
+ shift_key: bool = "shiftKey";
116
+ }
117
+
118
+ /// A [pointer event](https://developer.mozilla.org/docs/Web/API/PointerEvent):
119
+ /// mouse, pen or touch.
120
+ Pointer: Mouse {
121
+ height: f64 = "height";
122
+ is_primary: bool = "isPrimary";
123
+ pointer_id: i32 = "pointerId";
124
+ /// `"mouse"`, `"pen"` or `"touch"`.
125
+ pointer_type: String = "pointerType";
126
+ pressure: f64 = "pressure";
127
+ tangential_pressure: f64 = "tangentialPressure";
128
+ tilt_x: f64 = "tiltX";
129
+ tilt_y: f64 = "tiltY";
130
+ twist: f64 = "twist";
131
+ width: f64 = "width";
132
+ }
133
+
134
+ /// A [drag event](https://developer.mozilla.org/docs/Web/API/DragEvent).
135
+ /// Call `prevent_default` in `on_drag_over` to allow a drop.
136
+ Drag: Mouse {
137
+ data_transfer: &'static webapi::DataTransfer = "dataTransfer";
138
+ }
139
+
140
+ /// A [wheel event](https://developer.mozilla.org/docs/Web/API/WheelEvent).
141
+ Wheel: Mouse {
142
+ delta_mode: u32 = "deltaMode";
143
+ delta_x: f64 = "deltaX";
144
+ delta_y: f64 = "deltaY";
145
+ delta_z: f64 = "deltaZ";
146
+ }
147
+
148
+ /// Focus coming or going: `on_focus` and `on_blur`, which bubble in React.
149
+ Focus: Ui {
150
+ /// Where focus went, or came from.
151
+ related_target: Option<&'static webapi::Element> = "relatedTarget";
152
+ }
153
+
154
+ /// A key pressed or let go.
155
+ Keyboard: Ui {
156
+ alt_key: bool = "altKey";
157
+ /// Which key it is on the keyboard, like `"KeyA"`.
158
+ code: String = "code";
159
+ ctrl_key: bool = "ctrlKey";
160
+ /// What the key means, like `"Enter"` or `"a"`.
161
+ key: String = "key";
162
+ locale: String = "locale";
163
+ location: u32 = "location";
164
+ meta_key: bool = "metaKey";
165
+ repeat: bool = "repeat";
166
+ shift_key: bool = "shiftKey";
167
+ }
168
+
169
+ /// A [touch event](https://developer.mozilla.org/docs/Web/API/TouchEvent).
170
+ Touch: Ui {
171
+ alt_key: bool = "altKey";
172
+ changed_touches: &'static webapi::TouchList = "changedTouches";
173
+ ctrl_key: bool = "ctrlKey";
174
+ meta_key: bool = "metaKey";
175
+ shift_key: bool = "shiftKey";
176
+ target_touches: &'static webapi::TouchList = "targetTouches";
177
+ touches: &'static webapi::TouchList = "touches";
178
+ }
179
+
180
+ /// A CSS [animation event](https://developer.mozilla.org/docs/Web/API/AnimationEvent).
181
+ Animation: Event {
182
+ animation_name: String = "animationName";
183
+ elapsed_time: f64 = "elapsedTime";
184
+ pseudo_element: String = "pseudoElement";
185
+ }
186
+
187
+ /// A CSS [transition event](https://developer.mozilla.org/docs/Web/API/TransitionEvent).
188
+ Transition: Event {
189
+ elapsed_time: f64 = "elapsedTime";
190
+ property_name: String = "propertyName";
191
+ pseudo_element: String = "pseudoElement";
192
+ }
193
+
194
+ /// Copying, cutting or pasting.
195
+ Clipboard: Event {
196
+ clipboard_data: &'static webapi::DataTransfer = "clipboardData";
197
+ }
198
+
199
+ /// Text being composed with an input method.
200
+ Composition: Event {
201
+ data: String = "data";
202
+ }
203
+
204
+ /// `on_before_input`: text about to be typed.
205
+ Input: Event {
206
+ data: Option<String> = "data";
207
+ }
208
+
209
+ /// A popover or `<details>` opening or closing: `on_toggle`, `on_before_toggle`.
210
+ Toggle: Event {
211
+ /// `"open"` or `"closed"`.
212
+ new_state: String = "newState";
213
+ old_state: String = "oldState";
214
+ }
215
+
216
+ /// An `<input>`, `<select>` or `<textarea>` changing: `on_change`, `on_input`.
217
+ Change: Event {
218
+ /// What's in it now: `e.target.value`.
219
+ value: String = "target.value";
220
+ /// Whether a checkbox is checked now: `e.target.checked`.
221
+ checked: bool = "target.checked";
222
+ }
223
+ }
224
+
225
+ impl Mouse {
226
+ /// Whether a modifier key, like `"Shift"` or `"CapsLock"`, is down.
227
+ #[cfg_attr(rust_js, rust_js::link_name = "getModifierState")]
228
+ pub fn get_modifier_state(&self, key: &str) -> bool {
229
+ unreachable!()
230
+ }
231
+ }
232
+
233
+ impl Keyboard {
234
+ #[cfg_attr(rust_js, rust_js::link_name = "getModifierState")]
235
+ pub fn get_modifier_state(&self, key: &str) -> bool {
236
+ unreachable!()
237
+ }
238
+ }