mouse.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
  2. use crate::input_data::{
  3. decode_mouse_button_set, encode_mouse_button_set, MouseButton, MouseButtonSet,
  4. };
  5. use dioxus_core::Event;
  6. use keyboard_types::Modifiers;
  7. use std::fmt::{Debug, Formatter};
  8. pub type MouseEvent = Event<MouseData>;
  9. /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
  10. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  11. #[derive(Clone, Default, PartialEq, Eq)]
  12. /// Data associated with a mouse event
  13. ///
  14. /// Do not use the deprecated fields; they may change or become private in the future.
  15. pub struct MouseData {
  16. /// True if the alt key was down when the mouse event was fired.
  17. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  18. pub alt_key: bool,
  19. /// The button number that was pressed (if applicable) when the mouse event was fired.
  20. #[deprecated(since = "0.3.0", note = "use trigger_button() instead")]
  21. pub button: i16,
  22. /// Indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered.
  23. ///
  24. /// Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxiliary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4).
  25. ///
  26. /// - 1: Primary button (usually the left button)
  27. /// - 2: Secondary button (usually the right button)
  28. /// - 4: Auxiliary button (usually the mouse wheel button or middle button)
  29. /// - 8: 4th button (typically the "Browser Back" button)
  30. /// - 16 : 5th button (typically the "Browser Forward" button)
  31. #[deprecated(since = "0.3.0", note = "use held_buttons() instead")]
  32. pub buttons: u16,
  33. /// The horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
  34. ///
  35. /// For example, clicking on the left edge of the viewport will always result in a mouse event with a clientX value of 0, regardless of whether the page is scrolled horizontally.
  36. #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")]
  37. pub client_x: i32,
  38. /// The vertical coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
  39. ///
  40. /// For example, clicking on the top edge of the viewport will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically.
  41. #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")]
  42. pub client_y: i32,
  43. /// True if the control key was down when the mouse event was fired.
  44. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  45. pub ctrl_key: bool,
  46. /// True if the meta key was down when the mouse event was fired.
  47. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  48. pub meta_key: bool,
  49. /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
  50. #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")]
  51. pub offset_x: i32,
  52. /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node.
  53. #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")]
  54. pub offset_y: i32,
  55. /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible.
  56. ///
  57. /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300.
  58. #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")]
  59. pub page_x: i32,
  60. /// The Y (vertical) coordinate in pixels of the event relative to the whole document.
  61. ///
  62. /// See `page_x`.
  63. #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")]
  64. pub page_y: i32,
  65. /// The X coordinate of the mouse pointer in global (screen) coordinates.
  66. #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")]
  67. pub screen_x: i32,
  68. /// The Y coordinate of the mouse pointer in global (screen) coordinates.
  69. #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")]
  70. pub screen_y: i32,
  71. /// True if the shift key was down when the mouse event was fired.
  72. #[deprecated(since = "0.3.0", note = "use modifiers() instead")]
  73. pub shift_key: bool,
  74. }
  75. impl_event! {
  76. MouseData;
  77. /// Execute a callback when a button is clicked.
  78. ///
  79. /// ## Description
  80. ///
  81. /// An element receives a click event when a pointing device button (such as a mouse's primary mouse button)
  82. /// is both pressed and released while the pointer is located inside the element.
  83. ///
  84. /// - Bubbles: Yes
  85. /// - Cancelable: Yes
  86. /// - Interface(InteData): [`MouseEvent`]
  87. ///
  88. /// If the button is pressed on one element and the pointer is moved outside the element before the button
  89. /// is released, the event is fired on the most specific ancestor element that contained both elements.
  90. /// `click` fires after both the `mousedown` and `mouseup` events have fired, in that order.
  91. ///
  92. /// ## Example
  93. /// ```rust, ignore
  94. /// rsx!( button { "click me", onclick: move |_| tracing::info!("Clicked!`") } )
  95. /// ```
  96. ///
  97. /// ## Reference
  98. /// - <https://www.w3schools.com/tags/ev_onclick.asp>
  99. /// - <https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event>
  100. onclick
  101. /// oncontextmenu
  102. oncontextmenu
  103. #[deprecated(since = "0.5.0", note = "use ondoubleclick instead")]
  104. ondblclick
  105. /// onmousedown
  106. onmousedown
  107. /// onmouseenter
  108. onmouseenter
  109. /// onmouseleave
  110. onmouseleave
  111. /// onmousemove
  112. onmousemove
  113. /// onmouseout
  114. onmouseout
  115. /// onmouseover
  116. ///
  117. /// Triggered when the users's mouse hovers over an element.
  118. onmouseover
  119. /// onmouseup
  120. onmouseup
  121. }
  122. /// ondoubleclick
  123. #[inline]
  124. pub fn ondoubleclick<'a, E: crate::EventReturn<T>, T>(
  125. _cx: &'a ::dioxus_core::ScopeState,
  126. mut _f: impl FnMut(::dioxus_core::Event<MouseData>) -> E + 'a,
  127. ) -> ::dioxus_core::Attribute<'a> {
  128. ::dioxus_core::Attribute::new(
  129. "ondblclick",
  130. _cx.listener(move |e: ::dioxus_core::Event<MouseData>| {
  131. _f(e).spawn(_cx);
  132. }),
  133. None,
  134. false,
  135. )
  136. }
  137. impl MouseData {
  138. /// Construct MouseData with the specified properties
  139. ///
  140. /// Note: the current implementation truncates coordinates. In the future, when we change the internal representation, it may also support a fractional part.
  141. pub fn new(
  142. coordinates: Coordinates,
  143. trigger_button: Option<MouseButton>,
  144. held_buttons: MouseButtonSet,
  145. modifiers: Modifiers,
  146. ) -> Self {
  147. let alt_key = modifiers.contains(Modifiers::ALT);
  148. let ctrl_key = modifiers.contains(Modifiers::CONTROL);
  149. let meta_key = modifiers.contains(Modifiers::META);
  150. let shift_key = modifiers.contains(Modifiers::SHIFT);
  151. let [client_x, client_y]: [i32; 2] = coordinates.client().cast().into();
  152. let [offset_x, offset_y]: [i32; 2] = coordinates.element().cast().into();
  153. let [page_x, page_y]: [i32; 2] = coordinates.page().cast().into();
  154. let [screen_x, screen_y]: [i32; 2] = coordinates.screen().cast().into();
  155. #[allow(deprecated)]
  156. Self {
  157. alt_key,
  158. ctrl_key,
  159. meta_key,
  160. shift_key,
  161. button: trigger_button.map_or(0, |b| b.into_web_code()),
  162. buttons: encode_mouse_button_set(held_buttons),
  163. client_x,
  164. client_y,
  165. offset_x,
  166. offset_y,
  167. page_x,
  168. page_y,
  169. screen_x,
  170. screen_y,
  171. }
  172. }
  173. /// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page).
  174. ///
  175. /// For example, clicking in the top left corner of the viewport will always result in a mouse event with client coordinates (0., 0.), regardless of whether the page is scrolled horizontally.
  176. pub fn client_coordinates(&self) -> ClientPoint {
  177. #[allow(deprecated)]
  178. ClientPoint::new(self.client_x.into(), self.client_y.into())
  179. }
  180. /// The event's coordinates relative to the padding edge of the target element
  181. ///
  182. /// For example, clicking in the top left corner of an element will result in element coordinates (0., 0.)
  183. pub fn element_coordinates(&self) -> ElementPoint {
  184. #[allow(deprecated)]
  185. ElementPoint::new(self.offset_x.into(), self.offset_y.into())
  186. }
  187. /// The event's coordinates relative to the entire document. This includes any portion of the document not currently visible.
  188. ///
  189. /// For example, if the page is scrolled 200 pixels to the right and 300 pixels down, clicking in the top left corner of the viewport would result in page coordinates (200., 300.)
  190. pub fn page_coordinates(&self) -> PagePoint {
  191. #[allow(deprecated)]
  192. PagePoint::new(self.page_x.into(), self.page_y.into())
  193. }
  194. /// The event's coordinates relative to the entire screen. This takes into account the window's offset.
  195. pub fn screen_coordinates(&self) -> ScreenPoint {
  196. #[allow(deprecated)]
  197. ScreenPoint::new(self.screen_x.into(), self.screen_y.into())
  198. }
  199. pub fn coordinates(&self) -> Coordinates {
  200. Coordinates::new(
  201. self.screen_coordinates(),
  202. self.client_coordinates(),
  203. self.element_coordinates(),
  204. self.page_coordinates(),
  205. )
  206. }
  207. /// The set of modifier keys which were pressed when the event occurred
  208. pub fn modifiers(&self) -> Modifiers {
  209. let mut modifiers = Modifiers::empty();
  210. #[allow(deprecated)]
  211. {
  212. if self.alt_key {
  213. modifiers.insert(Modifiers::ALT);
  214. }
  215. if self.ctrl_key {
  216. modifiers.insert(Modifiers::CONTROL);
  217. }
  218. if self.meta_key {
  219. modifiers.insert(Modifiers::META);
  220. }
  221. if self.shift_key {
  222. modifiers.insert(Modifiers::SHIFT);
  223. }
  224. }
  225. modifiers
  226. }
  227. /// The set of mouse buttons which were held when the event occurred.
  228. pub fn held_buttons(&self) -> MouseButtonSet {
  229. #[allow(deprecated)]
  230. decode_mouse_button_set(self.buttons)
  231. }
  232. /// The mouse button that triggered the event
  233. ///
  234. // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
  235. /// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed.
  236. pub fn trigger_button(&self) -> Option<MouseButton> {
  237. #[allow(deprecated)]
  238. Some(MouseButton::from_web_code(self.button))
  239. }
  240. }
  241. impl Debug for MouseData {
  242. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  243. f.debug_struct("MouseData")
  244. .field("coordinates", &self.coordinates())
  245. .field("modifiers", &self.modifiers())
  246. .field("held_buttons", &self.held_buttons())
  247. .field("trigger_button", &self.trigger_button())
  248. .finish()
  249. }
  250. }