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: RefMut<dyn FnMut(NativeEvent)> = 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}