dioxus_html/events/
touch.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use dioxus_core::Event;
use keyboard_types::Modifiers;

use crate::geometry::*;
use crate::prelude::{InteractionLocation, ModifiersInteraction};

pub type TouchEvent = Event<TouchData>;
pub struct TouchData {
    inner: Box<dyn HasTouchData>,
}

impl<E: HasTouchData> From<E> for TouchData {
    fn from(e: E) -> Self {
        Self { inner: Box::new(e) }
    }
}

impl std::fmt::Debug for TouchData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TouchData")
            .field("modifiers", &self.modifiers())
            .field("touches", &self.touches())
            .field("touches_changed", &self.touches_changed())
            .field("target_touches", &self.target_touches())
            .finish()
    }
}

impl PartialEq for TouchData {
    fn eq(&self, other: &Self) -> bool {
        self.modifiers() == other.modifiers()
    }
}

impl TouchData {
    /// Create a new TouchData
    pub fn new(inner: impl HasTouchData + 'static) -> Self {
        Self {
            inner: Box::new(inner),
        }
    }

    /// Get the pointers that are currently down
    pub fn touches(&self) -> Vec<TouchPoint> {
        self.inner.touches()
    }

    /// Get the touches that have changed since the last event
    pub fn touches_changed(&self) -> Vec<TouchPoint> {
        self.inner.touches_changed()
    }

    /// Get the touches that started and stayed on the element that triggered this event
    pub fn target_touches(&self) -> Vec<TouchPoint> {
        self.inner.target_touches()
    }

    /// Downcast this event to a concrete event type
    #[inline(always)]
    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        self.inner.as_any().downcast_ref::<T>()
    }
}

impl ModifiersInteraction for TouchData {
    fn modifiers(&self) -> Modifiers {
        self.inner.modifiers()
    }
}

#[cfg(feature = "serialize")]
/// A serialized version of TouchData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedTouchData {
    alt_key: bool,
    ctrl_key: bool,
    meta_key: bool,
    shift_key: bool,
    touches: Vec<SerializedTouchPoint>,
    changed_touches: Vec<SerializedTouchPoint>,
    target_touches: Vec<SerializedTouchPoint>,
}

#[cfg(feature = "serialize")]
impl From<&TouchData> for SerializedTouchData {
    fn from(data: &TouchData) -> Self {
        let modifiers = data.modifiers();
        Self {
            alt_key: modifiers.contains(Modifiers::ALT),
            ctrl_key: modifiers.contains(Modifiers::CONTROL),
            meta_key: modifiers.contains(Modifiers::META),
            shift_key: modifiers.contains(Modifiers::SHIFT),
            touches: data.touches().iter().map(|t| t.into()).collect(),
            changed_touches: data.touches_changed().iter().map(|t| t.into()).collect(),
            target_touches: data.target_touches().iter().map(|t| t.into()).collect(),
        }
    }
}

#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedTouchData {
    fn modifiers(&self) -> Modifiers {
        let mut modifiers = Modifiers::default();
        if self.alt_key {
            modifiers.insert(Modifiers::ALT);
        }
        if self.ctrl_key {
            modifiers.insert(Modifiers::CONTROL);
        }
        if self.meta_key {
            modifiers.insert(Modifiers::META);
        }
        if self.shift_key {
            modifiers.insert(Modifiers::SHIFT);
        }
        modifiers
    }
}

#[cfg(feature = "serialize")]
impl HasTouchData for SerializedTouchData {
    fn touches(&self) -> Vec<TouchPoint> {
        self.touches.clone().into_iter().map(Into::into).collect()
    }

    fn touches_changed(&self) -> Vec<TouchPoint> {
        self.changed_touches
            .clone()
            .into_iter()
            .map(Into::into)
            .collect()
    }

    fn target_touches(&self) -> Vec<TouchPoint> {
        self.target_touches
            .clone()
            .into_iter()
            .map(Into::into)
            .collect()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(feature = "serialize")]
impl serde::Serialize for TouchData {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        SerializedTouchData::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for TouchData {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let data = SerializedTouchData::deserialize(deserializer)?;
        Ok(Self {
            inner: Box::new(data),
        })
    }
}

pub trait HasTouchData: ModifiersInteraction + std::any::Any {
    /// Get the touches that are currently down
    fn touches(&self) -> Vec<TouchPoint>;

    /// Get the touches that have changed since the last event
    fn touches_changed(&self) -> Vec<TouchPoint>;

    /// Get the touches that started and stayed on the element that triggered this event
    fn target_touches(&self) -> Vec<TouchPoint>;

    /// return self as Any
    fn as_any(&self) -> &dyn std::any::Any;
}

pub struct TouchPoint {
    inner: Box<dyn HasTouchPointData>,
}

impl<E: HasTouchPointData> From<E> for TouchPoint {
    fn from(e: E) -> Self {
        Self { inner: Box::new(e) }
    }
}

impl TouchPoint {
    /// Create a new TouchPoint
    pub fn new(inner: impl HasTouchPointData + 'static) -> Self {
        Self {
            inner: Box::new(inner),
        }
    }

    /// A unique identifier for this touch point that will be the same for the duration of the touch
    fn identifier(&self) -> i32 {
        self.inner.identifier()
    }

    /// the pressure of the touch
    fn force(&self) -> f64 {
        self.inner.force()
    }

    /// the radius of the touch
    fn radius(&self) -> ScreenPoint {
        self.inner.radius()
    }

    /// the rotation of the touch in degrees between 0 and 90
    fn rotation(&self) -> f64 {
        self.inner.rotation()
    }

    /// Downcast this event to a concrete event type
    #[inline(always)]
    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        self.inner.as_any().downcast_ref::<T>()
    }
}

impl std::fmt::Debug for TouchPoint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TouchPoint")
            .field("client_coordinates", &self.client_coordinates())
            .field("page_coordinates", &self.page_coordinates())
            .field("screen_coordinates", &self.screen_coordinates())
            .field("identifier", &self.identifier())
            .field("force", &self.force())
            .field("radius", &self.radius())
            .field("rotation", &self.rotation())
            .finish()
    }
}

impl InteractionLocation for TouchPoint {
    fn client_coordinates(&self) -> ClientPoint {
        self.inner.client_coordinates()
    }

    fn page_coordinates(&self) -> PagePoint {
        self.inner.page_coordinates()
    }

    fn screen_coordinates(&self) -> ScreenPoint {
        self.inner.screen_coordinates()
    }
}

/// A trait for touch point data
pub trait HasTouchPointData: InteractionLocation + std::any::Any {
    /// A unique identifier for this touch point that will be the same for the duration of the touch
    fn identifier(&self) -> i32;

    /// the pressure of the touch
    fn force(&self) -> f64;

    /// the radius of the touch
    fn radius(&self) -> ScreenPoint;

    /// the rotation of the touch in degrees between 0 and 90
    fn rotation(&self) -> f64;

    /// return self as Any
    fn as_any(&self) -> &dyn std::any::Any;
}

#[cfg(feature = "serialize")]
/// A serialized version of TouchPoint
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
struct SerializedTouchPoint {
    identifier: i32,
    client_x: f64,
    client_y: f64,
    page_x: f64,
    page_y: f64,
    screen_x: f64,
    screen_y: f64,
    force: f64,
    radius_x: f64,
    radius_y: f64,
    rotation_angle: f64,
}

#[cfg(feature = "serialize")]
impl From<&TouchPoint> for SerializedTouchPoint {
    fn from(point: &TouchPoint) -> Self {
        let client_coordinates = point.client_coordinates();

        let page_coordinates = point.page_coordinates();
        let screen_coordinates = point.screen_coordinates();
        Self {
            identifier: point.identifier(),
            client_x: client_coordinates.x,
            client_y: client_coordinates.y,
            page_x: page_coordinates.x,
            page_y: page_coordinates.y,
            screen_x: screen_coordinates.x,
            screen_y: screen_coordinates.y,
            force: point.force(),
            radius_x: point.radius().x,
            radius_y: point.radius().y,
            rotation_angle: point.rotation(),
        }
    }
}

#[cfg(feature = "serialize")]
impl HasTouchPointData for SerializedTouchPoint {
    /// A unique identifier for this touch point that will be the same for the duration of the touch
    fn identifier(&self) -> i32 {
        self.identifier
    }

    /// the pressure of the touch
    fn force(&self) -> f64 {
        self.force
    }

    /// the radius of the touch
    fn radius(&self) -> ScreenPoint {
        ScreenPoint::new(self.radius_x, self.radius_y)
    }

    /// the rotation of the touch in degrees between 0 and 90
    fn rotation(&self) -> f64 {
        self.rotation_angle
    }

    /// return self as Any
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(feature = "serialize")]
impl InteractionLocation for SerializedTouchPoint {
    /// Gets the coordinates of the event relative to the browser viewport.
    fn client_coordinates(&self) -> ClientPoint {
        ClientPoint::new(self.client_x, self.client_y)
    }

    /// Gets the coordinates of the event relative to the screen.
    fn screen_coordinates(&self) -> ScreenPoint {
        ScreenPoint::new(self.screen_x, self.screen_y)
    }

    /// Gets the coordinates of the event relative to the page.
    fn page_coordinates(&self) -> PagePoint {
        PagePoint::new(self.page_x, self.page_y)
    }
}

impl_event! {
    TouchData;
    /// touchstart
    ontouchstart

    /// touchmove
    ontouchmove

    /// touchend
    ontouchend

    /// touchcancel
    ontouchcancel
}