1use crate::*;
2
3impl NativeEventName {
5 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
57impl 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
66impl NativeEventHandler {
68 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 pub fn handle(&self, event: NativeEvent) {
81 let mut cb = self.get_callback().borrow_mut();
82 cb(event);
83 }
84}
85
86impl 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
96impl IntoEventAttribute for NativeEventHandler {
98 fn into_event_attribute(self) -> AttributeValue {
99 AttributeValue::Event(self)
100 }
101}
102
103impl 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
113impl NativeInputEvent {
115 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
124impl NativeFocusEvent {
126 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
135impl NativeSubmitEvent {
137 pub fn new(submitter: Option<String>) -> Self {
139 let mut event: NativeSubmitEvent = NativeSubmitEvent::default();
140 event.set_submitter(submitter);
141 event
142 }
143}
144
145impl NativeChangeEvent {
147 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
156impl NativeDragEvent {
158 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
168impl NativeTouchEvent {
170 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
180impl NativeWheelEvent {
182 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
192impl NativeClipboardEvent {
194 pub fn new(data: Option<String>) -> Self {
196 let mut event: NativeClipboardEvent = NativeClipboardEvent::default();
197 event.set_data(data);
198 event
199 }
200}
201
202impl NativeMediaEvent {
204 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}