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