dioxus_html/events/
mouse.rs1use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
2use crate::input_data::{MouseButton, MouseButtonSet};
3use crate::*;
4use dioxus_core::Event;
5use keyboard_types::Modifiers;
6use std::option::Option;
7
8pub type MouseEvent = Event<MouseData>;
9
10pub struct MouseData {
13 inner: Box<dyn HasMouseData>,
14}
15
16impl<E: HasMouseData + 'static> From<E> for MouseData {
17 fn from(e: E) -> Self {
18 Self { inner: Box::new(e) }
19 }
20}
21
22impl std::fmt::Debug for MouseData {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 f.debug_struct("MouseData")
25 .field("coordinates", &self.coordinates())
26 .field("modifiers", &self.modifiers())
27 .field("held_buttons", &self.held_buttons())
28 .field("trigger_button", &self.trigger_button())
29 .finish()
30 }
31}
32
33impl<E: HasMouseData> PartialEq<E> for MouseData {
34 fn eq(&self, other: &E) -> bool {
35 self.coordinates() == other.coordinates()
36 && self.modifiers() == other.modifiers()
37 && self.held_buttons() == other.held_buttons()
38 && self.trigger_button() == other.trigger_button()
39 }
40}
41
42pub trait HasMouseData: PointerInteraction {
44 fn as_any(&self) -> &dyn std::any::Any;
46}
47
48impl MouseData {
49 pub fn new(inner: impl HasMouseData + 'static) -> Self {
51 Self {
52 inner: Box::new(inner),
53 }
54 }
55
56 #[inline(always)]
58 pub fn downcast<T: 'static>(&self) -> Option<&T> {
59 self.inner.as_any().downcast_ref::<T>()
60 }
61}
62
63impl InteractionLocation for MouseData {
64 fn client_coordinates(&self) -> ClientPoint {
65 self.inner.client_coordinates()
66 }
67
68 fn page_coordinates(&self) -> PagePoint {
69 self.inner.page_coordinates()
70 }
71
72 fn screen_coordinates(&self) -> ScreenPoint {
73 self.inner.screen_coordinates()
74 }
75}
76
77impl InteractionElementOffset for MouseData {
78 fn element_coordinates(&self) -> ElementPoint {
79 self.inner.element_coordinates()
80 }
81
82 fn coordinates(&self) -> Coordinates {
83 self.inner.coordinates()
84 }
85}
86
87impl ModifiersInteraction for MouseData {
88 fn modifiers(&self) -> Modifiers {
90 self.inner.modifiers()
91 }
92}
93
94impl PointerInteraction for MouseData {
95 fn held_buttons(&self) -> MouseButtonSet {
97 self.inner.held_buttons()
98 }
99
100 fn trigger_button(&self) -> Option<MouseButton> {
105 self.inner.trigger_button()
106 }
107}
108
109impl PartialEq for MouseData {
110 fn eq(&self, other: &Self) -> bool {
111 self.coordinates() == other.coordinates()
112 && self.modifiers() == other.modifiers()
113 && self.held_buttons() == other.held_buttons()
114 && self.trigger_button() == other.trigger_button()
115 }
116}
117
118#[cfg(feature = "serialize")]
119#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
121pub struct SerializedMouseData {
122 #[serde(flatten)]
124 point_data: crate::point_interaction::SerializedPointInteraction,
125}
126
127#[cfg(feature = "serialize")]
128impl SerializedMouseData {
129 pub fn new(
131 trigger_button: Option<MouseButton>,
132 held_buttons: MouseButtonSet,
133 coordinates: Coordinates,
134 modifiers: Modifiers,
135 ) -> Self {
136 Self {
137 point_data: crate::point_interaction::SerializedPointInteraction::new(
138 trigger_button,
139 held_buttons,
140 coordinates,
141 modifiers,
142 ),
143 }
144 }
145}
146
147#[cfg(feature = "serialize")]
148impl From<&MouseData> for SerializedMouseData {
149 fn from(e: &MouseData) -> Self {
150 Self {
151 point_data: crate::point_interaction::SerializedPointInteraction::from(e),
152 }
153 }
154}
155
156#[cfg(feature = "serialize")]
157impl HasMouseData for SerializedMouseData {
158 fn as_any(&self) -> &dyn std::any::Any {
159 self
160 }
161}
162
163#[cfg(feature = "serialize")]
164impl InteractionLocation for SerializedMouseData {
165 fn client_coordinates(&self) -> ClientPoint {
166 self.point_data.client_coordinates()
167 }
168
169 fn page_coordinates(&self) -> PagePoint {
170 self.point_data.page_coordinates()
171 }
172
173 fn screen_coordinates(&self) -> ScreenPoint {
174 self.point_data.screen_coordinates()
175 }
176}
177
178#[cfg(feature = "serialize")]
179impl InteractionElementOffset for SerializedMouseData {
180 fn element_coordinates(&self) -> ElementPoint {
181 self.point_data.element_coordinates()
182 }
183}
184
185#[cfg(feature = "serialize")]
186impl ModifiersInteraction for SerializedMouseData {
187 fn modifiers(&self) -> Modifiers {
188 self.point_data.modifiers()
189 }
190}
191
192#[cfg(feature = "serialize")]
193impl PointerInteraction for SerializedMouseData {
194 fn held_buttons(&self) -> MouseButtonSet {
195 self.point_data.held_buttons()
196 }
197
198 fn trigger_button(&self) -> Option<MouseButton> {
199 self.point_data.trigger_button()
200 }
201}
202
203#[cfg(feature = "serialize")]
204impl serde::Serialize for MouseData {
205 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
206 SerializedMouseData::from(self).serialize(serializer)
207 }
208}
209
210#[cfg(feature = "serialize")]
211impl<'de> serde::Deserialize<'de> for MouseData {
212 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
213 let data = SerializedMouseData::deserialize(deserializer)?;
214 Ok(Self {
215 inner: Box::new(data),
216 })
217 }
218}