Skip to main content

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