events.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. use crate::{runtime::with_runtime, ScopeId};
  2. use std::{
  3. cell::{Cell, RefCell},
  4. rc::Rc,
  5. };
  6. /// A wrapper around some generic data that handles the event's state
  7. ///
  8. ///
  9. /// Prevent this event from continuing to bubble up the tree to parent elements.
  10. ///
  11. /// # Example
  12. ///
  13. /// ```rust, ignore
  14. /// rsx! {
  15. /// button {
  16. /// onclick: move |evt: Event<MouseData>| {
  17. /// evt.cancel_bubble();
  18. ///
  19. /// }
  20. /// }
  21. /// }
  22. /// ```
  23. pub struct Event<T: 'static + ?Sized> {
  24. /// The data associated with this event
  25. pub data: Rc<T>,
  26. pub(crate) propagates: Rc<Cell<bool>>,
  27. }
  28. impl<T> Event<T> {
  29. /// Prevent this event from continuing to bubble up the tree to parent elements.
  30. ///
  31. /// # Example
  32. ///
  33. /// ```rust, ignore
  34. /// rsx! {
  35. /// button {
  36. /// onclick: move |evt: Event<MouseData>| {
  37. /// evt.cancel_bubble();
  38. /// }
  39. /// }
  40. /// }
  41. /// ```
  42. #[deprecated = "use stop_propagation instead"]
  43. pub fn cancel_bubble(&self) {
  44. self.propagates.set(false);
  45. }
  46. /// Prevent this event from continuing to bubble up the tree to parent elements.
  47. ///
  48. /// # Example
  49. ///
  50. /// ```rust, ignore
  51. /// rsx! {
  52. /// button {
  53. /// onclick: move |evt: Event<MouseData>| {
  54. /// evt.stop_propagation();
  55. /// }
  56. /// }
  57. /// }
  58. /// ```
  59. pub fn stop_propagation(&self) {
  60. self.propagates.set(false);
  61. }
  62. /// Get a reference to the inner data from this event
  63. ///
  64. /// ```rust, ignore
  65. /// rsx! {
  66. /// button {
  67. /// onclick: move |evt: Event<MouseData>| {
  68. /// let data = evt.inner.clone();
  69. /// cx.spawn(async move {
  70. /// println!("{:?}", data);
  71. /// });
  72. /// }
  73. /// }
  74. /// }
  75. /// ```
  76. pub fn inner(&self) -> &Rc<T> {
  77. &self.data
  78. }
  79. }
  80. impl<T: ?Sized> Clone for Event<T> {
  81. fn clone(&self) -> Self {
  82. Self {
  83. propagates: self.propagates.clone(),
  84. data: self.data.clone(),
  85. }
  86. }
  87. }
  88. impl<T> std::ops::Deref for Event<T> {
  89. type Target = Rc<T>;
  90. fn deref(&self) -> &Self::Target {
  91. &self.data
  92. }
  93. }
  94. impl<T: std::fmt::Debug> std::fmt::Debug for Event<T> {
  95. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  96. f.debug_struct("UiEvent")
  97. .field("bubble_state", &self.propagates)
  98. .field("data", &self.data)
  99. .finish()
  100. }
  101. }
  102. #[doc(hidden)]
  103. /// The callback type generated by the `rsx!` macro when an `on` field is specified for components.
  104. ///
  105. /// This makes it possible to pass `move |evt| {}` style closures into components as property fields.
  106. ///
  107. ///
  108. /// # Example
  109. ///
  110. /// ```rust, ignore
  111. /// rsx!{
  112. /// MyComponent { onclick: move |evt| tracing::debug!("clicked") }
  113. /// }
  114. ///
  115. /// #[derive(Props)]
  116. /// struct MyProps<'a> {
  117. /// onclick: EventHandler<'a, MouseEvent>,
  118. /// }
  119. ///
  120. /// fn MyComponent(cx: Scope<'a, MyProps<'a>>) -> Element {
  121. /// cx.render(rsx!{
  122. /// button {
  123. /// onclick: move |evt| cx.props.onclick.call(evt),
  124. /// }
  125. /// })
  126. /// }
  127. ///
  128. /// ```
  129. pub struct EventHandler<'bump, T = ()> {
  130. pub(crate) origin: ScopeId,
  131. pub(super) callback: RefCell<Option<ExternalListenerCallback<'bump, T>>>,
  132. }
  133. impl<T> Default for EventHandler<'_, T> {
  134. fn default() -> Self {
  135. Self {
  136. origin: ScopeId::ROOT,
  137. callback: Default::default(),
  138. }
  139. }
  140. }
  141. type ExternalListenerCallback<'bump, T> = bumpalo::boxed::Box<'bump, dyn FnMut(T) + 'bump>;
  142. impl<T> EventHandler<'_, T> {
  143. /// Call this event handler with the appropriate event type
  144. ///
  145. /// This borrows the event using a RefCell. Recursively calling a listener will cause a panic.
  146. pub fn call(&self, event: T) {
  147. if let Some(callback) = self.callback.borrow_mut().as_mut() {
  148. with_runtime(|rt| {
  149. rt.scope_stack.borrow_mut().push(self.origin);
  150. });
  151. callback(event);
  152. with_runtime(|rt| {
  153. rt.scope_stack.borrow_mut().pop();
  154. });
  155. }
  156. }
  157. /// Forcibly drop the internal handler callback, releasing memory
  158. ///
  159. /// This will force any future calls to "call" to not doing anything
  160. pub fn release(&self) {
  161. self.callback.replace(None);
  162. }
  163. }