dioxus_html/events/
pointer.rs1use dioxus_core::Event;
2use keyboard_types::Modifiers;
3
4use crate::{geometry::*, input_data::*, prelude::*};
5
6pub type PointerEvent = Event<PointerData>;
8
9pub struct PointerData {
10 inner: Box<dyn HasPointerData>,
11}
12
13impl PointerData {
14 pub fn new(data: impl HasPointerData + 'static) -> Self {
16 Self::from(data)
17 }
18
19 #[inline(always)]
21 pub fn downcast<T: 'static>(&self) -> Option<&T> {
22 self.inner.as_any().downcast_ref::<T>()
23 }
24}
25
26impl<E: HasPointerData + 'static> From<E> for PointerData {
27 fn from(e: E) -> Self {
28 Self { inner: Box::new(e) }
29 }
30}
31
32impl std::fmt::Debug for PointerData {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 f.debug_struct("PointerData")
35 .field("pointer_id", &self.pointer_id())
36 .field("width", &self.width())
37 .field("height", &self.height())
38 .field("pressure", &self.pressure())
39 .field("tangential_pressure", &self.tangential_pressure())
40 .field("tilt_x", &self.tilt_x())
41 .field("tilt_y", &self.tilt_y())
42 .field("twist", &self.twist())
43 .field("pointer_type", &self.pointer_type())
44 .field("is_primary", &self.is_primary())
45 .field("coordinates", &self.coordinates())
46 .field("modifiers", &self.modifiers())
47 .field("held_buttons", &self.held_buttons())
48 .field("trigger_button", &self.trigger_button())
49 .finish()
50 }
51}
52
53impl PartialEq for PointerData {
54 fn eq(&self, other: &Self) -> bool {
55 self.pointer_id() == other.pointer_id()
56 && self.width() == other.width()
57 && self.height() == other.height()
58 && self.pressure() == other.pressure()
59 && self.tangential_pressure() == other.tangential_pressure()
60 && self.tilt_x() == other.tilt_x()
61 && self.tilt_y() == other.tilt_y()
62 && self.twist() == other.twist()
63 && self.pointer_type() == other.pointer_type()
64 && self.is_primary() == other.is_primary()
65 && self.coordinates() == other.coordinates()
66 && self.modifiers() == other.modifiers()
67 && self.held_buttons() == other.held_buttons()
68 && self.trigger_button() == other.trigger_button()
69 }
70}
71
72pub trait HasPointerData: PointerInteraction {
74 fn pointer_id(&self) -> i32;
76
77 fn width(&self) -> i32;
79
80 fn height(&self) -> i32;
82
83 fn pressure(&self) -> f32;
85
86 fn tangential_pressure(&self) -> f32;
88
89 fn tilt_x(&self) -> i32;
91
92 fn tilt_y(&self) -> i32;
94
95 fn twist(&self) -> i32;
97
98 fn pointer_type(&self) -> String;
100
101 fn is_primary(&self) -> bool;
103
104 fn as_any(&self) -> &dyn std::any::Any;
106}
107
108impl_event![
109 PointerData;
110 onpointerdown
112
113 onpointermove
115
116 onpointerup
118
119 onpointercancel
121
122 ongotpointercapture
124
125 onlostpointercapture
127
128 onpointerenter
130
131 onpointerleave
133
134 onpointerover
136
137 onpointerout
139];
140
141impl PointerData {
142 pub fn pointer_id(&self) -> i32 {
144 self.inner.pointer_id()
145 }
146
147 pub fn width(&self) -> i32 {
149 self.inner.width()
150 }
151
152 pub fn height(&self) -> i32 {
154 self.inner.height()
155 }
156
157 pub fn pressure(&self) -> f32 {
159 self.inner.pressure()
160 }
161
162 pub fn tangential_pressure(&self) -> f32 {
164 self.inner.tangential_pressure()
165 }
166
167 pub fn tilt_x(&self) -> i32 {
169 self.inner.tilt_x()
170 }
171
172 pub fn tilt_y(&self) -> i32 {
174 self.inner.tilt_y()
175 }
176
177 pub fn twist(&self) -> i32 {
179 self.inner.twist()
180 }
181
182 pub fn pointer_type(&self) -> String {
184 self.inner.pointer_type()
185 }
186
187 pub fn is_primary(&self) -> bool {
189 self.inner.is_primary()
190 }
191}
192
193impl InteractionLocation for PointerData {
194 fn client_coordinates(&self) -> ClientPoint {
195 self.inner.client_coordinates()
196 }
197
198 fn screen_coordinates(&self) -> ScreenPoint {
199 self.inner.screen_coordinates()
200 }
201
202 fn page_coordinates(&self) -> PagePoint {
203 self.inner.page_coordinates()
204 }
205}
206
207impl InteractionElementOffset for PointerData {
208 fn element_coordinates(&self) -> ElementPoint {
209 self.inner.element_coordinates()
210 }
211}
212
213impl ModifiersInteraction for PointerData {
214 fn modifiers(&self) -> Modifiers {
215 self.inner.modifiers()
216 }
217}
218
219impl PointerInteraction for PointerData {
220 fn held_buttons(&self) -> MouseButtonSet {
221 self.inner.held_buttons()
222 }
223
224 fn trigger_button(&self) -> Option<MouseButton> {
225 self.inner.trigger_button()
226 }
227}
228
229#[cfg(feature = "serialize")]
230#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
232pub struct SerializedPointerData {
233 #[serde(flatten)]
235 point_data: crate::point_interaction::SerializedPointInteraction,
236
237 pointer_id: i32,
239
240 width: i32,
242
243 height: i32,
245
246 pressure: f32,
248
249 tangential_pressure: f32,
251
252 tilt_x: i32,
254
255 tilt_y: i32,
257
258 twist: i32,
260
261 pointer_type: String,
263
264 is_primary: bool,
266}
267
268#[cfg(feature = "serialize")]
269impl HasPointerData for SerializedPointerData {
270 fn pointer_id(&self) -> i32 {
271 self.pointer_id
272 }
273
274 fn width(&self) -> i32 {
275 self.width
276 }
277
278 fn height(&self) -> i32 {
279 self.height
280 }
281
282 fn pressure(&self) -> f32 {
283 self.pressure
284 }
285
286 fn tangential_pressure(&self) -> f32 {
287 self.tangential_pressure
288 }
289
290 fn tilt_x(&self) -> i32 {
291 self.tilt_x
292 }
293
294 fn tilt_y(&self) -> i32 {
295 self.tilt_y
296 }
297
298 fn twist(&self) -> i32 {
299 self.twist
300 }
301
302 fn pointer_type(&self) -> String {
303 self.pointer_type.clone()
304 }
305
306 fn is_primary(&self) -> bool {
307 self.is_primary
308 }
309
310 fn as_any(&self) -> &dyn std::any::Any {
311 self
312 }
313}
314
315#[cfg(feature = "serialize")]
316impl InteractionLocation for SerializedPointerData {
317 fn client_coordinates(&self) -> ClientPoint {
318 self.point_data.client_coordinates()
319 }
320
321 fn screen_coordinates(&self) -> ScreenPoint {
322 self.point_data.screen_coordinates()
323 }
324
325 fn page_coordinates(&self) -> PagePoint {
326 self.point_data.page_coordinates()
327 }
328}
329
330#[cfg(feature = "serialize")]
331impl InteractionElementOffset for SerializedPointerData {
332 fn element_coordinates(&self) -> ElementPoint {
333 self.point_data.element_coordinates()
334 }
335}
336
337#[cfg(feature = "serialize")]
338impl ModifiersInteraction for SerializedPointerData {
339 fn modifiers(&self) -> Modifiers {
340 self.point_data.modifiers()
341 }
342}
343
344#[cfg(feature = "serialize")]
345impl PointerInteraction for SerializedPointerData {
346 fn held_buttons(&self) -> MouseButtonSet {
347 self.point_data.held_buttons()
348 }
349
350 fn trigger_button(&self) -> Option<MouseButton> {
351 self.point_data.trigger_button()
352 }
353}
354
355#[cfg(feature = "serialize")]
356impl From<&PointerData> for SerializedPointerData {
357 fn from(data: &PointerData) -> Self {
358 Self {
359 point_data: data.into(),
360 pointer_id: data.pointer_id(),
361 width: data.width(),
362 height: data.height(),
363 pressure: data.pressure(),
364 tangential_pressure: data.tangential_pressure(),
365 tilt_x: data.tilt_x(),
366 tilt_y: data.tilt_y(),
367 twist: data.twist(),
368 pointer_type: data.pointer_type().to_string(),
369 is_primary: data.is_primary(),
370 }
371 }
372}
373
374#[cfg(feature = "serialize")]
375impl serde::Serialize for PointerData {
376 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
377 SerializedPointerData::from(self).serialize(serializer)
378 }
379}
380
381#[cfg(feature = "serialize")]
382impl<'de> serde::Deserialize<'de> for PointerData {
383 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
384 let data = SerializedPointerData::deserialize(deserializer)?;
385 Ok(Self {
386 inner: Box::new(data),
387 })
388 }
389}