dioxus_web/events/
touch.rs1use dioxus_html::{
2 geometry::{ClientPoint, PagePoint, ScreenPoint},
3 prelude::{InteractionLocation, Modifiers, ModifiersInteraction},
4 HasTouchPointData, TouchPoint,
5};
6use web_sys::{Touch, TouchEvent};
7
8use super::{Synthetic, WebEventExt};
9
10impl ModifiersInteraction for Synthetic<TouchEvent> {
11 fn modifiers(&self) -> Modifiers {
12 let mut modifiers = Modifiers::empty();
13
14 if self.event.alt_key() {
15 modifiers.insert(Modifiers::ALT);
16 }
17 if self.event.ctrl_key() {
18 modifiers.insert(Modifiers::CONTROL);
19 }
20 if self.event.meta_key() {
21 modifiers.insert(Modifiers::META);
22 }
23 if self.event.shift_key() {
24 modifiers.insert(Modifiers::SHIFT);
25 }
26
27 modifiers
28 }
29}
30
31impl dioxus_html::events::HasTouchData for Synthetic<TouchEvent> {
32 fn touches(&self) -> Vec<TouchPoint> {
33 let touches = TouchEvent::touches(&self.event);
34 let mut result = Vec::with_capacity(touches.length() as usize);
35 for i in 0..touches.length() {
36 let touch = touches.get(i).unwrap();
37 result.push(TouchPoint::new(Synthetic::new(touch)));
38 }
39 result
40 }
41
42 fn touches_changed(&self) -> Vec<TouchPoint> {
43 let touches = self.event.changed_touches();
44 let mut result = Vec::with_capacity(touches.length() as usize);
45 for i in 0..touches.length() {
46 let touch = touches.get(i).unwrap();
47 result.push(TouchPoint::new(Synthetic::new(touch)));
48 }
49 result
50 }
51
52 fn target_touches(&self) -> Vec<TouchPoint> {
53 let touches = self.event.target_touches();
54 let mut result = Vec::with_capacity(touches.length() as usize);
55 for i in 0..touches.length() {
56 let touch = touches.get(i).unwrap();
57 result.push(TouchPoint::new(Synthetic::new(touch)));
58 }
59 result
60 }
61
62 fn as_any(&self) -> &dyn std::any::Any {
63 &self.event
64 }
65}
66
67impl HasTouchPointData for Synthetic<Touch> {
68 fn identifier(&self) -> i32 {
69 self.event.identifier()
70 }
71
72 fn radius(&self) -> ScreenPoint {
73 ScreenPoint::new(self.event.radius_x().into(), self.event.radius_y().into())
74 }
75
76 fn rotation(&self) -> f64 {
77 self.event.rotation_angle() as f64
78 }
79
80 fn force(&self) -> f64 {
81 self.event.force() as f64
82 }
83
84 fn as_any(&self) -> &dyn std::any::Any {
85 &self.event
86 }
87}
88
89impl InteractionLocation for Synthetic<Touch> {
90 fn client_coordinates(&self) -> ClientPoint {
91 ClientPoint::new(self.event.client_x().into(), self.event.client_y().into())
92 }
93
94 fn screen_coordinates(&self) -> ScreenPoint {
95 ScreenPoint::new(self.event.screen_x().into(), self.event.screen_y().into())
96 }
97
98 fn page_coordinates(&self) -> PagePoint {
99 PagePoint::new(self.event.page_x().into(), self.event.page_y().into())
100 }
101}
102
103impl WebEventExt for dioxus_html::TouchData {
104 type WebEvent = web_sys::TouchEvent;
105
106 #[inline(always)]
107 fn try_as_web_event(&self) -> Option<web_sys::TouchEvent> {
108 self.downcast::<web_sys::TouchEvent>().cloned()
109 }
110}