dioxus_html/events/
pointer.rs

1use dioxus_core::Event;
2use keyboard_types::Modifiers;
3
4use crate::{geometry::*, input_data::*, prelude::*};
5
6/// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent)
7pub type PointerEvent = Event<PointerData>;
8
9pub struct PointerData {
10    inner: Box<dyn HasPointerData>,
11}
12
13impl PointerData {
14    /// Create a new PointerData
15    pub fn new(data: impl HasPointerData + 'static) -> Self {
16        Self::from(data)
17    }
18
19    /// Downcast this event to a concrete event type
20    #[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
72/// A trait for any object that has the data for a pointer event
73pub trait HasPointerData: PointerInteraction {
74    /// Gets the unique identifier of the pointer causing the event.
75    fn pointer_id(&self) -> i32;
76
77    /// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
78    fn width(&self) -> i32;
79
80    /// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
81    fn height(&self) -> i32;
82
83    /// Gets the normalized pressure of the pointer input in the range of 0 to 1,
84    fn pressure(&self) -> f32;
85
86    /// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
87    fn tangential_pressure(&self) -> f32;
88
89    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
90    fn tilt_x(&self) -> i32;
91
92    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
93    fn tilt_y(&self) -> i32;
94
95    /// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
96    fn twist(&self) -> i32;
97
98    /// Gets the device type that caused the event (mouse, pen, touch, etc.).
99    fn pointer_type(&self) -> String;
100
101    /// Gets if the pointer represents the primary pointer of this pointer type.
102    fn is_primary(&self) -> bool;
103
104    /// return self as Any
105    fn as_any(&self) -> &dyn std::any::Any;
106}
107
108impl_event![
109    PointerData;
110    /// pointerdown
111    onpointerdown
112
113    /// pointermove
114    onpointermove
115
116    /// pointerup
117    onpointerup
118
119    /// pointercancel
120    onpointercancel
121
122    /// gotpointercapture
123    ongotpointercapture
124
125    /// lostpointercapture
126    onlostpointercapture
127
128    /// pointerenter
129    onpointerenter
130
131    /// pointerleave
132    onpointerleave
133
134    /// pointerover
135    onpointerover
136
137    /// pointerout
138    onpointerout
139];
140
141impl PointerData {
142    /// Gets the unique identifier of the pointer causing the event.
143    pub fn pointer_id(&self) -> i32 {
144        self.inner.pointer_id()
145    }
146
147    /// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
148    pub fn width(&self) -> i32 {
149        self.inner.width()
150    }
151
152    /// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
153    pub fn height(&self) -> i32 {
154        self.inner.height()
155    }
156
157    /// Gets the normalized pressure of the pointer input in the range of 0 to 1,
158    pub fn pressure(&self) -> f32 {
159        self.inner.pressure()
160    }
161
162    /// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
163    pub fn tangential_pressure(&self) -> f32 {
164        self.inner.tangential_pressure()
165    }
166
167    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
168    pub fn tilt_x(&self) -> i32 {
169        self.inner.tilt_x()
170    }
171
172    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
173    pub fn tilt_y(&self) -> i32 {
174        self.inner.tilt_y()
175    }
176
177    /// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
178    pub fn twist(&self) -> i32 {
179        self.inner.twist()
180    }
181
182    /// Gets the device type that caused the event (mouse, pen, touch, etc.).
183    pub fn pointer_type(&self) -> String {
184        self.inner.pointer_type()
185    }
186
187    /// Gets if the pointer represents the primary pointer of this pointer type.
188    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/// A serialized version of PointerData
231#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
232pub struct SerializedPointerData {
233    /// Common data for all pointer/mouse events
234    #[serde(flatten)]
235    point_data: crate::point_interaction::SerializedPointInteraction,
236
237    /// The unique identifier of the pointer causing the event.
238    pointer_id: i32,
239
240    /// The width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
241    width: i32,
242
243    /// The height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
244    height: i32,
245
246    /// The normalized pressure of the pointer input in the range of 0 to 1,
247    pressure: f32,
248
249    /// The normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
250    tangential_pressure: f32,
251
252    /// The plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
253    tilt_x: i32,
254
255    /// The plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
256    tilt_y: i32,
257
258    /// The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
259    twist: i32,
260
261    /// Indicates the device type that caused the event (mouse, pen, touch, etc.).
262    pointer_type: String,
263
264    /// Indicates if the pointer represents the primary pointer of this pointer type.
265    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}