mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. // super into will make a closure that is owned by the current owner (either the child component or the parent component).
  28. // We can't change that behavior in a minor version because it would cause issues with Components that accept event handlers.
  29. // Instead we run super into with an owner that is moved into the listener closure so it will be dropped when the closure is dropped.
  30. let owner = <::generational_box::UnsyncStorage as ::generational_box::AnyStorage>::owner();
  31. let event_handler = ::dioxus_core::prelude::with_owner(owner.clone(), || _f.super_into());
  32. ::dioxus_core::Attribute::new(
  33. impl_event!(@name $name $($js_name)?),
  34. ::dioxus_core::AttributeValue::listener(move |e: ::dioxus_core::Event<crate::PlatformEventData>| {
  35. // Force the owner to be moved into the event handler
  36. _ = &owner;
  37. event_handler.call(e.map(|e| e.into()));
  38. }),
  39. None,
  40. false,
  41. ).into()
  42. }
  43. #[doc(hidden)]
  44. $( #[$attr] )*
  45. pub mod $name {
  46. use super::*;
  47. // When expanding the macro, we use this version of the function if we see an inline closure to give better type inference
  48. $( #[$attr] )*
  49. pub fn call_with_explicit_closure<
  50. __Marker,
  51. Return: ::dioxus_core::SpawnIfAsync<__Marker> + 'static,
  52. >(
  53. event_handler: impl FnMut(::dioxus_core::Event<$data>) -> Return + 'static,
  54. ) -> ::dioxus_core::Attribute {
  55. #[allow(deprecated)]
  56. super::$name(event_handler)
  57. }
  58. }
  59. )*
  60. };
  61. (@name $name:ident $js_name:literal) => {
  62. $js_name
  63. };
  64. (@name $name:ident) => {
  65. stringify!($name)
  66. };
  67. }
  68. static EVENT_CONVERTER: RwLock<Option<Box<dyn HtmlEventConverter>>> = RwLock::new(None);
  69. #[inline]
  70. pub fn set_event_converter(converter: Box<dyn HtmlEventConverter>) {
  71. *EVENT_CONVERTER.write().unwrap() = Some(converter);
  72. }
  73. #[inline]
  74. pub(crate) fn with_event_converter<F, R>(f: F) -> R
  75. where
  76. F: FnOnce(&dyn HtmlEventConverter) -> R,
  77. {
  78. let converter = EVENT_CONVERTER.read().unwrap();
  79. f(converter.as_ref().unwrap().as_ref())
  80. }
  81. /// A platform specific event.
  82. pub struct PlatformEventData {
  83. event: Box<dyn Any>,
  84. }
  85. impl PlatformEventData {
  86. pub fn new(event: Box<dyn Any>) -> Self {
  87. Self { event }
  88. }
  89. pub fn inner(&self) -> &Box<dyn Any> {
  90. &self.event
  91. }
  92. pub fn downcast<T: 'static>(&self) -> Option<&T> {
  93. self.event.downcast_ref::<T>()
  94. }
  95. pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
  96. self.event.downcast_mut::<T>()
  97. }
  98. pub fn into_inner<T: 'static>(self) -> Option<T> {
  99. self.event.downcast::<T>().ok().map(|e| *e)
  100. }
  101. }
  102. /// 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.
  103. pub trait HtmlEventConverter: Send + Sync {
  104. /// Convert a general event to an animation data event
  105. fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData;
  106. /// Convert a general event to a clipboard data event
  107. fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData;
  108. /// Convert a general event to a composition data event
  109. fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData;
  110. /// Convert a general event to a drag data event
  111. fn convert_drag_data(&self, event: &PlatformEventData) -> DragData;
  112. /// Convert a general event to a focus data event
  113. fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData;
  114. /// Convert a general event to a form data event
  115. fn convert_form_data(&self, event: &PlatformEventData) -> FormData;
  116. /// Convert a general event to an image data event
  117. fn convert_image_data(&self, event: &PlatformEventData) -> ImageData;
  118. /// Convert a general event to a keyboard data event
  119. fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData;
  120. /// Convert a general event to a media data event
  121. fn convert_media_data(&self, event: &PlatformEventData) -> MediaData;
  122. /// Convert a general event to a mounted data event
  123. fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData;
  124. /// Convert a general event to a mouse data event
  125. fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData;
  126. /// Convert a general event to a pointer data event
  127. fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData;
  128. /// Convert a general event to a resize data event
  129. fn convert_resize_data(&self, event: &PlatformEventData) -> ResizeData;
  130. /// Convert a general event to a scroll data event
  131. fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData;
  132. /// Convert a general event to a selection data event
  133. fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData;
  134. /// Convert a general event to a toggle data event
  135. fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData;
  136. /// Convert a general event to a touch data event
  137. fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData;
  138. /// Convert a general event to a transition data event
  139. fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData;
  140. /// Convert a general event to a visible data event
  141. fn convert_visible_data(&self, event: &PlatformEventData) -> VisibleData;
  142. /// Convert a general event to a wheel data event
  143. fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData;
  144. }
  145. impl From<&PlatformEventData> for AnimationData {
  146. fn from(val: &PlatformEventData) -> Self {
  147. with_event_converter(|c| c.convert_animation_data(val))
  148. }
  149. }
  150. impl From<&PlatformEventData> for ClipboardData {
  151. fn from(val: &PlatformEventData) -> Self {
  152. with_event_converter(|c| c.convert_clipboard_data(val))
  153. }
  154. }
  155. impl From<&PlatformEventData> for CompositionData {
  156. fn from(val: &PlatformEventData) -> Self {
  157. with_event_converter(|c| c.convert_composition_data(val))
  158. }
  159. }
  160. impl From<&PlatformEventData> for DragData {
  161. fn from(val: &PlatformEventData) -> Self {
  162. with_event_converter(|c| c.convert_drag_data(val))
  163. }
  164. }
  165. impl From<&PlatformEventData> for FocusData {
  166. fn from(val: &PlatformEventData) -> Self {
  167. with_event_converter(|c| c.convert_focus_data(val))
  168. }
  169. }
  170. impl From<&PlatformEventData> for FormData {
  171. fn from(val: &PlatformEventData) -> Self {
  172. with_event_converter(|c| c.convert_form_data(val))
  173. }
  174. }
  175. impl From<&PlatformEventData> for ImageData {
  176. fn from(val: &PlatformEventData) -> Self {
  177. with_event_converter(|c| c.convert_image_data(val))
  178. }
  179. }
  180. impl From<&PlatformEventData> for KeyboardData {
  181. fn from(val: &PlatformEventData) -> Self {
  182. with_event_converter(|c| c.convert_keyboard_data(val))
  183. }
  184. }
  185. impl From<&PlatformEventData> for MediaData {
  186. fn from(val: &PlatformEventData) -> Self {
  187. with_event_converter(|c| c.convert_media_data(val))
  188. }
  189. }
  190. impl From<&PlatformEventData> for MountedData {
  191. fn from(val: &PlatformEventData) -> Self {
  192. with_event_converter(|c| c.convert_mounted_data(val))
  193. }
  194. }
  195. impl From<&PlatformEventData> for MouseData {
  196. fn from(val: &PlatformEventData) -> Self {
  197. with_event_converter(|c| c.convert_mouse_data(val))
  198. }
  199. }
  200. impl From<&PlatformEventData> for PointerData {
  201. fn from(val: &PlatformEventData) -> Self {
  202. with_event_converter(|c| c.convert_pointer_data(val))
  203. }
  204. }
  205. impl From<&PlatformEventData> for ResizeData {
  206. fn from(val: &PlatformEventData) -> Self {
  207. with_event_converter(|c| c.convert_resize_data(val))
  208. }
  209. }
  210. impl From<&PlatformEventData> for ScrollData {
  211. fn from(val: &PlatformEventData) -> Self {
  212. with_event_converter(|c| c.convert_scroll_data(val))
  213. }
  214. }
  215. impl From<&PlatformEventData> for SelectionData {
  216. fn from(val: &PlatformEventData) -> Self {
  217. with_event_converter(|c| c.convert_selection_data(val))
  218. }
  219. }
  220. impl From<&PlatformEventData> for ToggleData {
  221. fn from(val: &PlatformEventData) -> Self {
  222. with_event_converter(|c| c.convert_toggle_data(val))
  223. }
  224. }
  225. impl From<&PlatformEventData> for TouchData {
  226. fn from(val: &PlatformEventData) -> Self {
  227. with_event_converter(|c| c.convert_touch_data(val))
  228. }
  229. }
  230. impl From<&PlatformEventData> for TransitionData {
  231. fn from(val: &PlatformEventData) -> Self {
  232. with_event_converter(|c| c.convert_transition_data(val))
  233. }
  234. }
  235. impl From<&PlatformEventData> for VisibleData {
  236. fn from(val: &PlatformEventData) -> Self {
  237. with_event_converter(|c| c.convert_visible_data(val))
  238. }
  239. }
  240. impl From<&PlatformEventData> for WheelData {
  241. fn from(val: &PlatformEventData) -> Self {
  242. with_event_converter(|c| c.convert_wheel_data(val))
  243. }
  244. }
  245. mod animation;
  246. mod clipboard;
  247. mod composition;
  248. mod drag;
  249. mod focus;
  250. mod form;
  251. mod image;
  252. mod keyboard;
  253. mod media;
  254. mod mounted;
  255. mod mouse;
  256. mod pointer;
  257. mod resize;
  258. mod scroll;
  259. mod selection;
  260. mod toggle;
  261. mod touch;
  262. mod transition;
  263. mod visible;
  264. mod wheel;
  265. pub use animation::*;
  266. pub use clipboard::*;
  267. pub use composition::*;
  268. pub use drag::*;
  269. pub use focus::*;
  270. pub use form::*;
  271. pub use image::*;
  272. pub use keyboard::*;
  273. pub use media::*;
  274. pub use mounted::*;
  275. pub use mouse::*;
  276. pub use pointer::*;
  277. pub use resize::*;
  278. pub use scroll::*;
  279. pub use selection::*;
  280. pub use toggle::*;
  281. pub use touch::*;
  282. pub use transition::*;
  283. pub use visible::*;
  284. pub use wheel::*;