dioxus_web/events/
pointer.rs

1use dioxus_html::{
2    geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint},
3    input_data::{decode_mouse_button_set, MouseButton},
4    HasPointerData, InteractionElementOffset, InteractionLocation, Modifiers, ModifiersInteraction,
5    PointerInteraction,
6};
7use web_sys::PointerEvent;
8
9use super::{Synthetic, WebEventExt};
10
11impl HasPointerData for Synthetic<PointerEvent> {
12    fn pointer_id(&self) -> i32 {
13        self.event.pointer_id()
14    }
15
16    fn width(&self) -> f64 {
17        self.event.width() as _
18    }
19
20    fn height(&self) -> f64 {
21        self.event.height() as _
22    }
23
24    fn pressure(&self) -> f32 {
25        self.event.pressure()
26    }
27
28    fn tangential_pressure(&self) -> f32 {
29        self.event.tangential_pressure()
30    }
31
32    fn tilt_x(&self) -> i32 {
33        self.event.tilt_x()
34    }
35
36    fn tilt_y(&self) -> i32 {
37        self.event.tilt_y()
38    }
39
40    fn twist(&self) -> i32 {
41        self.event.twist()
42    }
43
44    fn pointer_type(&self) -> String {
45        self.event.pointer_type()
46    }
47
48    fn is_primary(&self) -> bool {
49        self.event.is_primary()
50    }
51
52    fn as_any(&self) -> &dyn std::any::Any {
53        &self.event
54    }
55}
56
57impl InteractionLocation for Synthetic<PointerEvent> {
58    fn client_coordinates(&self) -> ClientPoint {
59        ClientPoint::new(self.event.client_x().into(), self.event.client_y().into())
60    }
61
62    fn screen_coordinates(&self) -> ScreenPoint {
63        ScreenPoint::new(self.event.screen_x().into(), self.event.screen_y().into())
64    }
65
66    fn page_coordinates(&self) -> PagePoint {
67        PagePoint::new(self.event.page_x().into(), self.event.page_y().into())
68    }
69}
70
71impl InteractionElementOffset for Synthetic<PointerEvent> {
72    fn element_coordinates(&self) -> ElementPoint {
73        ElementPoint::new(self.event.offset_x().into(), self.event.offset_y().into())
74    }
75}
76
77impl ModifiersInteraction for Synthetic<PointerEvent> {
78    fn modifiers(&self) -> Modifiers {
79        let mut modifiers = Modifiers::empty();
80
81        if self.event.alt_key() {
82            modifiers.insert(Modifiers::ALT);
83        }
84        if self.event.ctrl_key() {
85            modifiers.insert(Modifiers::CONTROL);
86        }
87        if self.event.meta_key() {
88            modifiers.insert(Modifiers::META);
89        }
90        if self.event.shift_key() {
91            modifiers.insert(Modifiers::SHIFT);
92        }
93
94        modifiers
95    }
96}
97
98impl PointerInteraction for Synthetic<PointerEvent> {
99    fn held_buttons(&self) -> dioxus_html::input_data::MouseButtonSet {
100        decode_mouse_button_set(self.event.buttons())
101    }
102
103    fn trigger_button(&self) -> Option<MouseButton> {
104        Some(MouseButton::from_web_code(self.event.button()))
105    }
106}
107
108impl WebEventExt for dioxus_html::PointerData {
109    type WebEvent = web_sys::PointerEvent;
110
111    #[inline(always)]
112    fn try_as_web_event(&self) -> Option<web_sys::PointerEvent> {
113        self.downcast::<web_sys::PointerEvent>().cloned()
114    }
115}