dioxus_html/events/
mouse.rs

1use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
2use crate::input_data::{MouseButton, MouseButtonSet};
3use crate::prelude::*;
4use dioxus_core::Event;
5use keyboard_types::Modifiers;
6
7pub type MouseEvent = Event<MouseData>;
8
9/// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
10/// Data associated with a mouse event
11pub struct MouseData {
12    inner: Box<dyn HasMouseData>,
13}
14
15impl<E: HasMouseData + 'static> From<E> for MouseData {
16    fn from(e: E) -> Self {
17        Self { inner: Box::new(e) }
18    }
19}
20
21impl std::fmt::Debug for MouseData {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        f.debug_struct("MouseData")
24            .field("coordinates", &self.coordinates())
25            .field("modifiers", &self.modifiers())
26            .field("held_buttons", &self.held_buttons())
27            .field("trigger_button", &self.trigger_button())
28            .finish()
29    }
30}
31
32impl<E: HasMouseData> PartialEq<E> for MouseData {
33    fn eq(&self, other: &E) -> bool {
34        self.coordinates() == other.coordinates()
35            && self.modifiers() == other.modifiers()
36            && self.held_buttons() == other.held_buttons()
37            && self.trigger_button() == other.trigger_button()
38    }
39}
40
41/// A trait for any object that has the data for a mouse event
42pub trait HasMouseData: PointerInteraction {
43    /// return self as Any
44    fn as_any(&self) -> &dyn std::any::Any;
45}
46
47impl_event! {
48    MouseData;
49
50    /// Execute a callback when a button is clicked.
51    ///
52    /// ## Description
53    ///
54    /// An element receives a click event when a pointing device button (such as a mouse's primary mouse button)
55    /// is both pressed and released while the pointer is located inside the element.
56    ///
57    /// - Bubbles: Yes
58    /// - Cancelable: Yes
59    /// - Interface(InteData): [`MouseEvent`]
60    ///
61    /// If the button is pressed on one element and the pointer is moved outside the element before the button
62    /// is released, the event is fired on the most specific ancestor element that contained both elements.
63    /// `click` fires after both the `mousedown` and `mouseup` events have fired, in that order.
64    ///
65    /// ## Example
66    /// ```rust, ignore
67    /// rsx!( button { "click me", onclick: move |_| tracing::info!("Clicked!`") } )
68    /// ```
69    ///
70    /// ## Reference
71    /// - <https://www.w3schools.com/tags/ev_onclick.asp>
72    /// - <https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event>
73    onclick
74
75    /// oncontextmenu
76    oncontextmenu
77
78    #[deprecated(since = "0.5.0", note = "use ondoubleclick instead")]
79    ondblclick
80
81    ondoubleclick: "ondblclick"
82
83    /// onmousedown
84    onmousedown
85
86    /// onmouseenter
87    onmouseenter
88
89    /// onmouseleave
90    onmouseleave
91
92    /// onmousemove
93    onmousemove
94
95    /// onmouseout
96    onmouseout
97
98    /// onmouseover
99    ///
100    /// Triggered when the users's mouse hovers over an element.
101    onmouseover
102
103    /// onmouseup
104    onmouseup
105}
106
107impl MouseData {
108    /// Create a new instance of MouseData
109    pub fn new(inner: impl HasMouseData + 'static) -> Self {
110        Self {
111            inner: Box::new(inner),
112        }
113    }
114
115    /// Downcast this event to a concrete event type
116    #[inline(always)]
117    pub fn downcast<T: 'static>(&self) -> Option<&T> {
118        self.inner.as_any().downcast_ref::<T>()
119    }
120}
121
122impl InteractionLocation for MouseData {
123    fn client_coordinates(&self) -> ClientPoint {
124        self.inner.client_coordinates()
125    }
126
127    fn page_coordinates(&self) -> PagePoint {
128        self.inner.page_coordinates()
129    }
130
131    fn screen_coordinates(&self) -> ScreenPoint {
132        self.inner.screen_coordinates()
133    }
134}
135
136impl InteractionElementOffset for MouseData {
137    fn element_coordinates(&self) -> ElementPoint {
138        self.inner.element_coordinates()
139    }
140
141    fn coordinates(&self) -> Coordinates {
142        self.inner.coordinates()
143    }
144}
145
146impl ModifiersInteraction for MouseData {
147    /// The set of modifier keys which were pressed when the event occurred
148    fn modifiers(&self) -> Modifiers {
149        self.inner.modifiers()
150    }
151}
152
153impl PointerInteraction for MouseData {
154    /// The set of mouse buttons which were held when the event occurred.
155    fn held_buttons(&self) -> MouseButtonSet {
156        self.inner.held_buttons()
157    }
158
159    /// The mouse button that triggered the event
160    ///
161    // 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
162    /// 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.
163    fn trigger_button(&self) -> Option<MouseButton> {
164        self.inner.trigger_button()
165    }
166}
167
168impl PartialEq for MouseData {
169    fn eq(&self, other: &Self) -> bool {
170        self.coordinates() == other.coordinates()
171            && self.modifiers() == other.modifiers()
172            && self.held_buttons() == other.held_buttons()
173            && self.trigger_button() == other.trigger_button()
174    }
175}
176
177#[cfg(feature = "serialize")]
178/// A serialized version of [`MouseData`]
179#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
180pub struct SerializedMouseData {
181    /// Common data for all pointer/mouse events
182    #[serde(flatten)]
183    point_data: crate::point_interaction::SerializedPointInteraction,
184}
185
186#[cfg(feature = "serialize")]
187impl SerializedMouseData {
188    /// Create a new instance of SerializedMouseData
189    pub fn new(
190        trigger_button: Option<MouseButton>,
191        held_buttons: MouseButtonSet,
192        coordinates: Coordinates,
193        modifiers: Modifiers,
194    ) -> Self {
195        Self {
196            point_data: crate::point_interaction::SerializedPointInteraction::new(
197                trigger_button,
198                held_buttons,
199                coordinates,
200                modifiers,
201            ),
202        }
203    }
204}
205
206#[cfg(feature = "serialize")]
207impl From<&MouseData> for SerializedMouseData {
208    fn from(e: &MouseData) -> Self {
209        Self {
210            point_data: crate::point_interaction::SerializedPointInteraction::from(e),
211        }
212    }
213}
214
215#[cfg(feature = "serialize")]
216impl HasMouseData for SerializedMouseData {
217    fn as_any(&self) -> &dyn std::any::Any {
218        self
219    }
220}
221
222#[cfg(feature = "serialize")]
223impl InteractionLocation for SerializedMouseData {
224    fn client_coordinates(&self) -> ClientPoint {
225        self.point_data.client_coordinates()
226    }
227
228    fn page_coordinates(&self) -> PagePoint {
229        self.point_data.page_coordinates()
230    }
231
232    fn screen_coordinates(&self) -> ScreenPoint {
233        self.point_data.screen_coordinates()
234    }
235}
236
237#[cfg(feature = "serialize")]
238impl InteractionElementOffset for SerializedMouseData {
239    fn element_coordinates(&self) -> ElementPoint {
240        self.point_data.element_coordinates()
241    }
242}
243
244#[cfg(feature = "serialize")]
245impl ModifiersInteraction for SerializedMouseData {
246    fn modifiers(&self) -> Modifiers {
247        self.point_data.modifiers()
248    }
249}
250
251#[cfg(feature = "serialize")]
252impl PointerInteraction for SerializedMouseData {
253    fn held_buttons(&self) -> MouseButtonSet {
254        self.point_data.held_buttons()
255    }
256
257    fn trigger_button(&self) -> Option<MouseButton> {
258        self.point_data.trigger_button()
259    }
260}
261
262#[cfg(feature = "serialize")]
263impl serde::Serialize for MouseData {
264    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
265        SerializedMouseData::from(self).serialize(serializer)
266    }
267}
268
269#[cfg(feature = "serialize")]
270impl<'de> serde::Deserialize<'de> for MouseData {
271    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
272        let data = SerializedMouseData::deserialize(deserializer)?;
273        Ok(Self {
274            inner: Box::new(data),
275        })
276    }
277}