Skip to main content

euv_core/event/
impl.rs

1use crate::*;
2
3/// Maps each `NativeEventName` variant to its corresponding DOM event string.
4impl NativeEventName {
5    /// Returns the string representation of this event name for DOM binding.
6    pub fn as_str(&self) -> String {
7        match self {
8            NativeEventName::Click => "click".to_string(),
9            NativeEventName::DblClick => "dblclick".to_string(),
10            NativeEventName::MouseDown => "mousedown".to_string(),
11            NativeEventName::MouseUp => "mouseup".to_string(),
12            NativeEventName::MouseMove => "mousemove".to_string(),
13            NativeEventName::MouseEnter => "mouseenter".to_string(),
14            NativeEventName::MouseLeave => "mouseleave".to_string(),
15            NativeEventName::MouseOver => "mouseover".to_string(),
16            NativeEventName::MouseOut => "mouseout".to_string(),
17            NativeEventName::ContextMenu => "contextmenu".to_string(),
18            NativeEventName::Input => "input".to_string(),
19            NativeEventName::KeyDown => "keydown".to_string(),
20            NativeEventName::KeyUp => "keyup".to_string(),
21            NativeEventName::KeyPress => "keypress".to_string(),
22            NativeEventName::Focus => "focus".to_string(),
23            NativeEventName::Blur => "blur".to_string(),
24            NativeEventName::FocusIn => "focusin".to_string(),
25            NativeEventName::FocusOut => "focusout".to_string(),
26            NativeEventName::Submit => "submit".to_string(),
27            NativeEventName::Change => "change".to_string(),
28            NativeEventName::Drag => "drag".to_string(),
29            NativeEventName::DragStart => "dragstart".to_string(),
30            NativeEventName::DragEnd => "dragend".to_string(),
31            NativeEventName::DragOver => "dragover".to_string(),
32            NativeEventName::DragEnter => "dragenter".to_string(),
33            NativeEventName::DragLeave => "dragleave".to_string(),
34            NativeEventName::Drop => "drop".to_string(),
35            NativeEventName::TouchStart => "touchstart".to_string(),
36            NativeEventName::TouchEnd => "touchend".to_string(),
37            NativeEventName::TouchMove => "touchmove".to_string(),
38            NativeEventName::TouchCancel => "touchcancel".to_string(),
39            NativeEventName::Wheel => "wheel".to_string(),
40            NativeEventName::Copy => "copy".to_string(),
41            NativeEventName::Cut => "cut".to_string(),
42            NativeEventName::Paste => "paste".to_string(),
43            NativeEventName::Play => "play".to_string(),
44            NativeEventName::Pause => "pause".to_string(),
45            NativeEventName::Ended => "ended".to_string(),
46            NativeEventName::LoadedData => "loadeddata".to_string(),
47            NativeEventName::CanPlay => "canplay".to_string(),
48            NativeEventName::VolumeChange => "volumechange".to_string(),
49            NativeEventName::TimeUpdate => "timeupdate".to_string(),
50            NativeEventName::HashChange => "hashchange".to_string(),
51            NativeEventName::EuvSignalUpdate => "__euv_signal_update__".to_string(),
52            NativeEventName::Other(name) => name.clone(),
53        }
54    }
55}
56
57/// Implements `Display` for `NativeEventName` by delegating to `as_str`.
58///
59/// This automatically provides the `ToString` trait via blanket implementation.
60impl std::fmt::Display for NativeEventName {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{}", self.as_str())
63    }
64}
65
66/// Implementation of event handler construction, cloning, and invocation.
67impl NativeEventHandler {
68    /// Creates a new event handler from an `NativeEventName` enum and callback.
69    pub fn new<F>(event_name: NativeEventName, callback: F) -> Self
70    where
71        F: FnMut(NativeEvent) + 'static,
72    {
73        NativeEventHandler {
74            event_name: event_name.as_str(),
75            callback: Rc::new(RefCell::new(callback)),
76        }
77    }
78
79    /// Invokes the underlying callback with the given event.
80    pub fn handle(&self, event: NativeEvent) {
81        let mut cb = self.get_callback().borrow_mut();
82        cb(event);
83    }
84}
85
86/// Clones the event handler, sharing the underlying callback reference.
87impl Clone for NativeEventHandler {
88    fn clone(&self) -> Self {
89        NativeEventHandler {
90            event_name: self.get_event_name().clone(),
91            callback: Rc::clone(self.get_callback()),
92        }
93    }
94}
95
96/// Converts an owned event handler into an attribute value.
97impl IntoEventAttribute for NativeEventHandler {
98    fn into_event_attribute(self) -> AttributeValue {
99        AttributeValue::Event(self)
100    }
101}
102
103/// Converts an optional event handler into an attribute value.
104impl IntoEventAttribute for Option<NativeEventHandler> {
105    fn into_event_attribute(self) -> AttributeValue {
106        match self {
107            Some(handler) => AttributeValue::Event(handler),
108            None => AttributeValue::Text(String::new()),
109        }
110    }
111}
112
113/// Implementation of NativeInputEvent construction.
114impl NativeInputEvent {
115    /// Creates a new input event with the given value and input type.
116    pub fn new(value: String, input_type: String) -> Self {
117        let mut event: NativeInputEvent = NativeInputEvent::default();
118        event.set_value(value);
119        event.set_input_type(input_type);
120        event
121    }
122}
123
124/// Implementation of NativeFocusEvent construction.
125impl NativeFocusEvent {
126    /// Creates a new focus event.
127    pub fn new(is_focus: bool, is_blur: bool) -> Self {
128        let mut event: NativeFocusEvent = NativeFocusEvent::default();
129        event.set_is_focus(is_focus);
130        event.set_is_blur(is_blur);
131        event
132    }
133}
134
135/// Implementation of NativeSubmitEvent construction.
136impl NativeSubmitEvent {
137    /// Creates a new submit event with the given submitter.
138    pub fn new(submitter: Option<String>) -> Self {
139        let mut event: NativeSubmitEvent = NativeSubmitEvent::default();
140        event.set_submitter(submitter);
141        event
142    }
143}
144
145/// Implementation of NativeChangeEvent construction.
146impl NativeChangeEvent {
147    /// Creates a new change event with the given value and checked state.
148    pub fn new(value: String, checked: bool) -> Self {
149        let mut event: NativeChangeEvent = NativeChangeEvent::default();
150        event.set_value(value);
151        event.set_checked(checked);
152        event
153    }
154}
155
156/// Implementation of NativeDragEvent construction.
157impl NativeDragEvent {
158    /// Creates a new drag event with the given parameters.
159    pub fn new(client_x: i32, client_y: i32, types: Vec<String>) -> Self {
160        let mut event: NativeDragEvent = NativeDragEvent::default();
161        event.set_client_x(client_x);
162        event.set_client_y(client_y);
163        event.set_types(types);
164        event
165    }
166}
167
168/// Implementation of NativeTouchEvent construction.
169impl NativeTouchEvent {
170    /// Creates a new touch event with the given parameters.
171    pub fn new(touches_count: u32, client_x: i32, client_y: i32) -> Self {
172        let mut event: NativeTouchEvent = NativeTouchEvent::default();
173        event.set_touches_count(touches_count);
174        event.set_client_x(client_x);
175        event.set_client_y(client_y);
176        event
177    }
178}
179
180/// Implementation of NativeWheelEvent construction.
181impl NativeWheelEvent {
182    /// Creates a new wheel event with the given parameters.
183    pub fn new(delta_x: f64, delta_y: f64, delta_mode: u32) -> Self {
184        let mut event: NativeWheelEvent = NativeWheelEvent::default();
185        event.set_delta_x(delta_x);
186        event.set_delta_y(delta_y);
187        event.set_delta_mode(delta_mode);
188        event
189    }
190}
191
192/// Implementation of NativeClipboardEvent construction.
193impl NativeClipboardEvent {
194    /// Creates a new clipboard event with the given data.
195    pub fn new(data: Option<String>) -> Self {
196        let mut event: NativeClipboardEvent = NativeClipboardEvent::default();
197        event.set_data(data);
198        event
199    }
200}
201
202/// Implementation of NativeMediaEvent construction.
203impl NativeMediaEvent {
204    /// Creates a new media event with the given event type.
205    pub fn new(event_type: String) -> Self {
206        let mut event: NativeMediaEvent = NativeMediaEvent::default();
207        event.set_event_type(event_type);
208        event
209    }
210}