euv_core/event/handler/struct.rs
1use crate::*;
2
3/// Inner storage for a native event callback closure.
4///
5/// Boxes a `dyn FnMut(NativeEvent)` so it can be stored behind a raw pointer.
6/// Allocated via `Box::leak` and lives for the remainder of the program.
7pub(crate) struct NativeEventCallbackInner {
8 /// The boxed callback closure.
9 pub(crate) callback: Box<dyn FnMut(NativeEvent)>,
10}
11
12/// A wrapper around an event callback.
13///
14/// Stores the event name and a raw pointer to the heap-allocated callback closure.
15///
16/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
17/// entire program. This is safe in single-threaded WASM contexts where no
18/// concurrent access can occur.
19#[derive(CustomDebug)]
20pub struct NativeEventHandler {
21 /// The name of the event (e.g., "click", "input").
22 pub(crate) event_name: String,
23 /// Raw pointer to the heap-allocated callback closure inner state.
24 ///
25 /// SAFETY: Allocated via `Box::leak`, valid for the program lifetime.
26 #[debug(skip)]
27 pub(crate) callback: *mut NativeEventCallbackInner,
28}
29
30/// Data associated with a mouse event.
31///
32/// Captures coordinates, buttons, and modifier key states.
33#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
34pub struct NativeMouseEvent {
35 /// The X coordinate relative to the viewport.
36 #[get(pub, type(copy))]
37 #[set(pub)]
38 pub(crate) client_x: i32,
39 /// The Y coordinate relative to the viewport.
40 #[get(pub, type(copy))]
41 #[set(pub)]
42 pub(crate) client_y: i32,
43 /// The X coordinate relative to the screen.
44 #[get(pub, type(copy))]
45 #[set(pub)]
46 pub(crate) screen_x: i32,
47 /// The Y coordinate relative to the screen.
48 #[get(pub, type(copy))]
49 #[set(pub)]
50 pub(crate) screen_y: i32,
51 /// Which mouse button was pressed.
52 #[get(pub, type(copy))]
53 #[set(pub)]
54 pub(crate) button: i16,
55 /// Bitmask of pressed buttons.
56 #[get(pub, type(copy))]
57 #[set(pub)]
58 pub(crate) buttons: u16,
59 /// Whether the ctrl key was pressed.
60 #[get(pub, type(copy))]
61 #[set(pub)]
62 pub(crate) ctrl_key: bool,
63 /// Whether the shift key was pressed.
64 #[get(pub, type(copy))]
65 #[set(pub)]
66 pub(crate) shift_key: bool,
67 /// Whether the alt key was pressed.
68 #[get(pub, type(copy))]
69 #[set(pub)]
70 pub(crate) alt_key: bool,
71 /// Whether the meta key was pressed.
72 #[get(pub, type(copy))]
73 #[set(pub)]
74 pub(crate) meta_key: bool,
75}
76
77/// Data associated with an input event.
78///
79/// Contains the current value and the type of input change.
80#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
81pub struct NativeInputEvent {
82 /// The current value of the input element.
83 #[get(pub)]
84 #[set(pub)]
85 value: String,
86 /// The type of input (e.g., "insertText", "deleteContentBackward").
87 #[get(pub)]
88 #[set(pub)]
89 input_type: String,
90}
91
92/// Data associated with a keyboard event.
93///
94/// Captures the pressed key, physical code, location, and modifier states.
95#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
96pub struct NativeKeyboardEvent {
97 /// The key that was pressed.
98 #[get(pub)]
99 #[set(pub)]
100 pub(crate) key: String,
101 /// The numeric code of the key.
102 #[get(pub)]
103 #[set(pub)]
104 pub(crate) code: String,
105 /// The physical key location.
106 #[get(pub, type(copy))]
107 #[set(pub)]
108 pub(crate) location: u32,
109 /// Whether the ctrl key was pressed.
110 #[get(pub, type(copy))]
111 #[set(pub)]
112 pub(crate) ctrl_key: bool,
113 /// Whether the shift key was pressed.
114 #[get(pub, type(copy))]
115 #[set(pub)]
116 pub(crate) shift_key: bool,
117 /// Whether the alt key was pressed.
118 #[get(pub, type(copy))]
119 #[set(pub)]
120 pub(crate) alt_key: bool,
121 /// Whether the meta key was pressed.
122 #[get(pub, type(copy))]
123 #[set(pub)]
124 pub(crate) meta_key: bool,
125 /// Whether the key is being held down.
126 #[get(pub, type(copy))]
127 #[set(pub)]
128 pub(crate) repeat: bool,
129}
130
131/// Data associated with a focus event.
132///
133/// Indicates whether the element is gaining or losing focus.
134#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
135pub struct NativeFocusEvent {
136 /// Whether the element is receiving focus.
137 #[get(pub, type(copy))]
138 #[set(pub)]
139 is_focus: bool,
140 /// Whether the element is losing focus.
141 #[get(pub, type(copy))]
142 #[set(pub)]
143 is_blur: bool,
144}
145
146/// Data associated with a form submit event.
147///
148/// Identifies the element that triggered the submission.
149#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
150pub struct NativeSubmitEvent {
151 /// The submitter element identifier.
152 #[get(pub)]
153 #[set(pub)]
154 submitter: Option<String>,
155}
156
157/// Data associated with a change event.
158///
159/// Contains the new value and checked state for form controls.
160#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
161pub struct NativeChangeEvent {
162 /// The new value after the change.
163 #[get(pub)]
164 #[set(pub)]
165 value: String,
166 /// Whether the element is checked (for checkboxes/radios).
167 #[get(pub, type(copy))]
168 #[set(pub)]
169 checked: bool,
170}
171
172/// Data associated with a drag event.
173///
174/// Captures the drag position and available data transfer types.
175#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
176pub struct NativeDragEvent {
177 /// The X coordinate of the drag.
178 #[get(pub, type(copy))]
179 #[set(pub)]
180 client_x: i32,
181 /// The Y coordinate of the drag.
182 #[get(pub, type(copy))]
183 #[set(pub)]
184 client_y: i32,
185 /// The data transfer types available.
186 #[get(pub)]
187 #[set(pub)]
188 types: Vec<String>,
189}
190
191/// Data associated with a touch event.
192///
193/// Captures the number of touch points and the first touch coordinates.
194#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
195pub struct NativeTouchEvent {
196 /// The number of touch points.
197 #[get(pub, type(copy))]
198 #[set(pub)]
199 touches_count: u32,
200 /// The X coordinate of the first touch.
201 #[get(pub, type(copy))]
202 #[set(pub)]
203 client_x: i32,
204 /// The Y coordinate of the first touch.
205 #[get(pub, type(copy))]
206 #[set(pub)]
207 client_y: i32,
208}
209
210/// Data associated with a wheel event.
211///
212/// Captures scroll deltas and the delta mode.
213#[derive(Clone, Data, Debug, Default, New, PartialEq)]
214pub struct NativeWheelEvent {
215 /// Horizontal scroll delta.
216 #[get(pub, type(copy))]
217 #[set(pub)]
218 delta_x: f64,
219 /// Vertical scroll delta.
220 #[get(pub, type(copy))]
221 #[set(pub)]
222 delta_y: f64,
223 /// Scroll delta mode.
224 #[get(pub, type(copy))]
225 #[set(pub)]
226 delta_mode: u32,
227}
228
229/// Data associated with a clipboard event.
230///
231/// Contains the clipboard text data if available.
232#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
233pub struct NativeClipboardEvent {
234 /// The clipboard data if available.
235 #[get(pub)]
236 #[set(pub)]
237 data: Option<String>,
238}
239
240/// Data associated with a media event.
241///
242/// Identifies the type of media event that occurred.
243#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
244pub struct NativeMediaEvent {
245 /// The type of media event (e.g., "play", "pause", "ended").
246 #[get(pub)]
247 #[set(pub)]
248 event_type: String,
249}