events.rs 11 KB

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