Skip to main content

dioxus_html/events/
mouse.rs

1use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
2use crate::input_data::{MouseButton, MouseButtonSet};
3use crate::*;
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 MouseData {
48    /// Create a new instance of MouseData
49    pub fn new(inner: impl HasMouseData + 'static) -> Self {
50        Self {
51            inner: Box::new(inner),
52        }
53    }
54
55    /// Downcast this event to a concrete event type
56    #[inline(always)]
57    pub fn downcast<T: 'static>(&self) -> Option<&T> {
58        self.inner.as_any().downcast_ref::<T>()
59    }
60}
61
62impl InteractionLocation for MouseData {
63    fn client_coordinates(&self) -> ClientPoint {
64        self.inner.client_coordinates()
65    }
66
67    fn page_coordinates(&self) -> PagePoint {
68        self.inner.page_coordinates()
69    }
70
71    fn screen_coordinates(&self) -> ScreenPoint {
72        self.inner.screen_coordinates()
73    }
74}
75
76impl InteractionElementOffset for MouseData {
77    fn element_coordinates(&self) -> ElementPoint {
78        self.inner.element_coordinates()
79    }
80
81    fn coordinates(&self) -> Coordinates {
82        self.inner.coordinates()
83    }
84}
85
86impl ModifiersInteraction for MouseData {
87    /// The set of modifier keys which were pressed when the event occurred
88    fn modifiers(&self) -> Modifiers {
89        self.inner.modifiers()
90    }
91}
92
93impl PointerInteraction for MouseData {
94    /// The set of mouse buttons which were held when the event occurred.
95    fn held_buttons(&self) -> MouseButtonSet {
96        self.inner.held_buttons()
97    }
98
99    /// The mouse button that triggered the event
100    ///
101    // 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
102    /// 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.
103    fn trigger_button(&self) -> Option<MouseButton> {
104        self.inner.trigger_button()
105    }
106}
107
108impl PartialEq for MouseData {
109    fn eq(&self, other: &Self) -> bool {
110        self.coordinates() == other.coordinates()
111            && self.modifiers() == other.modifiers()
112            && self.held_buttons() == other.held_buttons()
113            && self.trigger_button() == other.trigger_button()
114    }
115}
116
117#[cfg(feature = "serialize")]
118/// A serialized version of [`MouseData`]
119#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
120pub struct SerializedMouseData {
121    /// Common data for all pointer/mouse events
122    #[serde(flatten)]
123    point_data: crate::point_interaction::SerializedPointInteraction,
124}
125
126#[cfg(feature = "serialize")]
127impl SerializedMouseData {
128    /// Create a new instance of SerializedMouseData
129    pub fn new(
130        trigger_button: Option<MouseButton>,
131        held_buttons: MouseButtonSet,
132        coordinates: Coordinates,
133        modifiers: Modifiers,
134    ) -> Self {
135        Self {
136            point_data: crate::point_interaction::SerializedPointInteraction::new(
137                trigger_button,
138                held_buttons,
139                coordinates,
140                modifiers,
141            ),
142        }
143    }
144}
145
146#[cfg(feature = "serialize")]
147impl From<&MouseData> for SerializedMouseData {
148    fn from(e: &MouseData) -> Self {
149        Self {
150            point_data: crate::point_interaction::SerializedPointInteraction::from(e),
151        }
152    }
153}
154
155#[cfg(feature = "serialize")]
156impl HasMouseData for SerializedMouseData {
157    fn as_any(&self) -> &dyn std::any::Any {
158        self
159    }
160}
161
162#[cfg(feature = "serialize")]
163impl InteractionLocation for SerializedMouseData {
164    fn client_coordinates(&self) -> ClientPoint {
165        self.point_data.client_coordinates()
166    }
167
168    fn page_coordinates(&self) -> PagePoint {
169        self.point_data.page_coordinates()
170    }
171
172    fn screen_coordinates(&self) -> ScreenPoint {
173        self.point_data.screen_coordinates()
174    }
175}
176
177#[cfg(feature = "serialize")]
178impl InteractionElementOffset for SerializedMouseData {
179    fn element_coordinates(&self) -> ElementPoint {
180        self.point_data.element_coordinates()
181    }
182}
183
184#[cfg(feature = "serialize")]
185impl ModifiersInteraction for SerializedMouseData {
186    fn modifiers(&self) -> Modifiers {
187        self.point_data.modifiers()
188    }
189}
190
191#[cfg(feature = "serialize")]
192impl PointerInteraction for SerializedMouseData {
193    fn held_buttons(&self) -> MouseButtonSet {
194        self.point_data.held_buttons()
195    }
196
197    fn trigger_button(&self) -> Option<MouseButton> {
198        self.point_data.trigger_button()
199    }
200}
201
202#[cfg(feature = "serialize")]
203impl serde::Serialize for MouseData {
204    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
205        SerializedMouseData::from(self).serialize(serializer)
206    }
207}
208
209#[cfg(feature = "serialize")]
210impl<'de> serde::Deserialize<'de> for MouseData {
211    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
212        let data = SerializedMouseData::deserialize(deserializer)?;
213        Ok(Self {
214            inner: Box::new(data),
215        })
216    }
217}