mod.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #![doc = include_str!("../../docs/event_handlers.md")]
  2. use std::any::Any;
  3. use std::sync::RwLock;
  4. macro_rules! impl_event {
  5. (
  6. $data:ty;
  7. $(
  8. $( #[$attr:meta] )*
  9. $name:ident $(: $js_name:literal)?
  10. )*
  11. ) => {
  12. $(
  13. $( #[$attr] )*
  14. /// <details open>
  15. /// <summary>General Event Handler Information</summary>
  16. ///
  17. #[doc = include_str!("../../docs/event_handlers.md")]
  18. ///
  19. /// </details>
  20. ///
  21. #[doc = include_str!("../../docs/common_event_handler_errors.md")]
  22. $(
  23. #[doc(alias = $js_name)]
  24. )?
  25. #[inline]
  26. pub fn $name<__Marker>(mut _f: impl ::dioxus_core::prelude::SuperInto<::dioxus_core::prelude::EventHandler<::dioxus_core::Event<$data>>, __Marker>) -> ::dioxus_core::Attribute {
  27. let event_handler = _f.super_into();
  28. ::dioxus_core::Attribute::new(
  29. impl_event!(@name $name $($js_name)?),
  30. ::dioxus_core::AttributeValue::listener(move |e: ::dioxus_core::Event<crate::PlatformEventData>| {
  31. event_handler.call(e.map(|e| e.into()));
  32. }),
  33. None,
  34. false,
  35. ).into()
  36. }
  37. #[doc(hidden)]
  38. $( #[$attr] )*
  39. pub mod $name {
  40. use super::*;
  41. // When expanding the macro, we use this version of the function if we see an inline closure to give better type inference
  42. $( #[$attr] )*
  43. pub fn call_with_explicit_closure<
  44. __Marker,
  45. Return: ::dioxus_core::SpawnIfAsync<__Marker> + 'static,
  46. >(
  47. event_handler: impl FnMut(::dioxus_core::Event<$data>) -> Return + 'static,
  48. ) -> ::dioxus_core::Attribute {
  49. #[allow(deprecated)]
  50. super::$name(event_handler)
  51. }
  52. }
  53. )*
  54. };
  55. (@name $name:ident $js_name:literal) => {
  56. $js_name
  57. };
  58. (@name $name:ident) => {
  59. stringify!($name)
  60. };
  61. }
  62. static EVENT_CONVERTER: RwLock<Option<Box<dyn HtmlEventConverter>>> = RwLock::new(None);
  63. #[inline]
  64. pub fn set_event_converter(converter: Box<dyn HtmlEventConverter>) {
  65. *EVENT_CONVERTER.write().unwrap() = Some(converter);
  66. }
  67. #[inline]
  68. pub(crate) fn with_event_converter<F, R>(f: F) -> R
  69. where
  70. F: FnOnce(&dyn HtmlEventConverter) -> R,
  71. {
  72. let converter = EVENT_CONVERTER.read().unwrap();
  73. f(converter.as_ref().unwrap().as_ref())
  74. }
  75. /// A platform specific event.
  76. pub struct PlatformEventData {
  77. event: Box<dyn Any>,
  78. }
  79. impl PlatformEventData {
  80. pub fn new(event: Box<dyn Any>) -> Self {
  81. Self { event }
  82. }
  83. pub fn inner(&self) -> &Box<dyn Any> {
  84. &self.event
  85. }
  86. pub fn downcast<T: 'static>(&self) -> Option<&T> {
  87. self.event.downcast_ref::<T>()
  88. }
  89. pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
  90. self.event.downcast_mut::<T>()
  91. }
  92. pub fn into_inner<T: 'static>(self) -> Option<T> {
  93. self.event.downcast::<T>().ok().map(|e| *e)
  94. }
  95. }
  96. /// A converter between a platform specific event and a general event. All code in a renderer that has a large binary size should be placed in this trait. Each of these functions should be snipped in high levels of optimization.
  97. pub trait HtmlEventConverter: Send + Sync {
  98. /// Convert a general event to an animation data event
  99. fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData;
  100. /// Convert a general event to a clipboard data event
  101. fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData;
  102. /// Convert a general event to a composition data event
  103. fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData;
  104. /// Convert a general event to a drag data event
  105. fn convert_drag_data(&self, event: &PlatformEventData) -> DragData;
  106. /// Convert a general event to a focus data event
  107. fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData;
  108. /// Convert a general event to a form data event
  109. fn convert_form_data(&self, event: &PlatformEventData) -> FormData;
  110. /// Convert a general event to an image data event
  111. fn convert_image_data(&self, event: &PlatformEventData) -> ImageData;
  112. /// Convert a general event to a keyboard data event
  113. fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData;
  114. /// Convert a general event to a media data event
  115. fn convert_media_data(&self, event: &PlatformEventData) -> MediaData;
  116. /// Convert a general event to a mounted data event
  117. fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData;
  118. /// Convert a general event to a mouse data event
  119. fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData;
  120. /// Convert a general event to a pointer data event
  121. fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData;
  122. /// Convert a general event to a scroll data event
  123. fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData;
  124. /// Convert a general event to a selection data event
  125. fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData;
  126. /// Convert a general event to a toggle data event
  127. fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData;
  128. /// Convert a general event to a touch data event
  129. fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData;
  130. /// Convert a general event to a transition data event
  131. fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData;
  132. /// Convert a general event to a wheel data event
  133. fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData;
  134. }
  135. impl From<&PlatformEventData> for AnimationData {
  136. fn from(val: &PlatformEventData) -> Self {
  137. with_event_converter(|c| c.convert_animation_data(val))
  138. }
  139. }
  140. impl From<&PlatformEventData> for ClipboardData {
  141. fn from(val: &PlatformEventData) -> Self {
  142. with_event_converter(|c| c.convert_clipboard_data(val))
  143. }
  144. }
  145. impl From<&PlatformEventData> for CompositionData {
  146. fn from(val: &PlatformEventData) -> Self {
  147. with_event_converter(|c| c.convert_composition_data(val))
  148. }
  149. }
  150. impl From<&PlatformEventData> for DragData {
  151. fn from(val: &PlatformEventData) -> Self {
  152. with_event_converter(|c| c.convert_drag_data(val))
  153. }
  154. }
  155. impl From<&PlatformEventData> for FocusData {
  156. fn from(val: &PlatformEventData) -> Self {
  157. with_event_converter(|c| c.convert_focus_data(val))
  158. }
  159. }
  160. impl From<&PlatformEventData> for FormData {
  161. fn from(val: &PlatformEventData) -> Self {
  162. with_event_converter(|c| c.convert_form_data(val))
  163. }
  164. }
  165. impl From<&PlatformEventData> for ImageData {
  166. fn from(val: &PlatformEventData) -> Self {
  167. with_event_converter(|c| c.convert_image_data(val))
  168. }
  169. }
  170. impl From<&PlatformEventData> for KeyboardData {
  171. fn from(val: &PlatformEventData) -> Self {
  172. with_event_converter(|c| c.convert_keyboard_data(val))
  173. }
  174. }
  175. impl From<&PlatformEventData> for MediaData {
  176. fn from(val: &PlatformEventData) -> Self {
  177. with_event_converter(|c| c.convert_media_data(val))
  178. }
  179. }
  180. impl From<&PlatformEventData> for MountedData {
  181. fn from(val: &PlatformEventData) -> Self {
  182. with_event_converter(|c| c.convert_mounted_data(val))
  183. }
  184. }
  185. impl From<&PlatformEventData> for MouseData {
  186. fn from(val: &PlatformEventData) -> Self {
  187. with_event_converter(|c| c.convert_mouse_data(val))
  188. }
  189. }
  190. impl From<&PlatformEventData> for PointerData {
  191. fn from(val: &PlatformEventData) -> Self {
  192. with_event_converter(|c| c.convert_pointer_data(val))
  193. }
  194. }
  195. impl From<&PlatformEventData> for ScrollData {
  196. fn from(val: &PlatformEventData) -> Self {
  197. with_event_converter(|c| c.convert_scroll_data(val))
  198. }
  199. }
  200. impl From<&PlatformEventData> for SelectionData {
  201. fn from(val: &PlatformEventData) -> Self {
  202. with_event_converter(|c| c.convert_selection_data(val))
  203. }
  204. }
  205. impl From<&PlatformEventData> for ToggleData {
  206. fn from(val: &PlatformEventData) -> Self {
  207. with_event_converter(|c| c.convert_toggle_data(val))
  208. }
  209. }
  210. impl From<&PlatformEventData> for TouchData {
  211. fn from(val: &PlatformEventData) -> Self {
  212. with_event_converter(|c| c.convert_touch_data(val))
  213. }
  214. }
  215. impl From<&PlatformEventData> for TransitionData {
  216. fn from(val: &PlatformEventData) -> Self {
  217. with_event_converter(|c| c.convert_transition_data(val))
  218. }
  219. }
  220. impl From<&PlatformEventData> for WheelData {
  221. fn from(val: &PlatformEventData) -> Self {
  222. with_event_converter(|c| c.convert_wheel_data(val))
  223. }
  224. }
  225. mod animation;
  226. mod clipboard;
  227. mod composition;
  228. mod drag;
  229. mod focus;
  230. mod form;
  231. mod image;
  232. mod keyboard;
  233. mod media;
  234. mod mounted;
  235. mod mouse;
  236. mod pointer;
  237. mod scroll;
  238. mod selection;
  239. mod toggle;
  240. mod touch;
  241. mod transition;
  242. mod wheel;
  243. pub use animation::*;
  244. pub use clipboard::*;
  245. pub use composition::*;
  246. pub use drag::*;
  247. pub use focus::*;
  248. pub use form::*;
  249. pub use image::*;
  250. pub use keyboard::*;
  251. pub use media::*;
  252. pub use mounted::*;
  253. pub use mouse::*;
  254. pub use pointer::*;
  255. pub use scroll::*;
  256. pub use selection::*;
  257. pub use toggle::*;
  258. pub use touch::*;
  259. pub use transition::*;
  260. pub use wheel::*;
  261. pub fn event_bubbles(evt: &str) -> bool {
  262. match evt {
  263. "copy" => true,
  264. "cut" => true,
  265. "paste" => true,
  266. "compositionend" => true,
  267. "compositionstart" => true,
  268. "compositionupdate" => true,
  269. "keydown" => true,
  270. "keypress" => true,
  271. "keyup" => true,
  272. "focus" => false,
  273. "focusout" => true,
  274. "focusin" => true,
  275. "blur" => false,
  276. "change" => true,
  277. "input" => true,
  278. "invalid" => true,
  279. "reset" => true,
  280. "submit" => true,
  281. "click" => true,
  282. "contextmenu" => true,
  283. "doubleclick" => true,
  284. "dblclick" => true,
  285. "drag" => true,
  286. "dragend" => true,
  287. "dragenter" => false,
  288. "dragexit" => false,
  289. "dragleave" => true,
  290. "dragover" => true,
  291. "dragstart" => true,
  292. "drop" => true,
  293. "mousedown" => true,
  294. "mouseenter" => false,
  295. "mouseleave" => false,
  296. "mousemove" => true,
  297. "mouseout" => true,
  298. "scroll" => false,
  299. "mouseover" => true,
  300. "mouseup" => true,
  301. "pointerdown" => true,
  302. "pointermove" => true,
  303. "pointerup" => true,
  304. "pointercancel" => true,
  305. "gotpointercapture" => true,
  306. "lostpointercapture" => true,
  307. "pointerenter" => false,
  308. "pointerleave" => false,
  309. "pointerover" => true,
  310. "pointerout" => true,
  311. "select" => true,
  312. "touchcancel" => true,
  313. "touchend" => true,
  314. "touchmove" => true,
  315. "touchstart" => true,
  316. "wheel" => true,
  317. "abort" => false,
  318. "canplay" => false,
  319. "canplaythrough" => false,
  320. "durationchange" => false,
  321. "emptied" => false,
  322. "encrypted" => true,
  323. "ended" => false,
  324. "error" => false,
  325. "loadeddata" => false,
  326. "loadedmetadata" => false,
  327. "loadstart" => false,
  328. "load" => false,
  329. "pause" => false,
  330. "play" => false,
  331. "playing" => false,
  332. "progress" => false,
  333. "ratechange" => false,
  334. "seeked" => false,
  335. "seeking" => false,
  336. "stalled" => false,
  337. "suspend" => false,
  338. "timeupdate" => false,
  339. "volumechange" => false,
  340. "waiting" => false,
  341. "animationstart" => true,
  342. "animationend" => true,
  343. "animationiteration" => true,
  344. "transitionend" => true,
  345. "toggle" => true,
  346. "mounted" => false,
  347. _ => {
  348. tracing::warn!("Unknown event name: {evt}");
  349. true
  350. }
  351. }
  352. }