dioxus_web/events/
mouse.rs

1use dioxus_html::{
2    geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint},
3    input_data::{decode_mouse_button_set, MouseButton},
4    HasMouseData, InteractionElementOffset, InteractionLocation, Modifiers, ModifiersInteraction,
5    PointerInteraction,
6};
7use web_sys::MouseEvent;
8
9use super::{Synthetic, WebEventExt};
10
11impl InteractionLocation for Synthetic<MouseEvent> {
12    fn client_coordinates(&self) -> ClientPoint {
13        ClientPoint::new(self.event.client_x().into(), self.event.client_y().into())
14    }
15
16    fn page_coordinates(&self) -> PagePoint {
17        PagePoint::new(self.event.page_x().into(), self.event.page_y().into())
18    }
19
20    fn screen_coordinates(&self) -> ScreenPoint {
21        ScreenPoint::new(self.event.screen_x().into(), self.event.screen_y().into())
22    }
23}
24
25impl InteractionElementOffset for Synthetic<MouseEvent> {
26    fn element_coordinates(&self) -> ElementPoint {
27        ElementPoint::new(self.event.offset_x().into(), self.event.offset_y().into())
28    }
29}
30
31impl ModifiersInteraction for Synthetic<MouseEvent> {
32    fn modifiers(&self) -> Modifiers {
33        let mut modifiers = Modifiers::empty();
34
35        if self.event.alt_key() {
36            modifiers.insert(Modifiers::ALT);
37        }
38        if self.event.ctrl_key() {
39            modifiers.insert(Modifiers::CONTROL);
40        }
41        if self.event.meta_key() {
42            modifiers.insert(Modifiers::META);
43        }
44        if self.event.shift_key() {
45            modifiers.insert(Modifiers::SHIFT);
46        }
47
48        modifiers
49    }
50}
51
52impl PointerInteraction for Synthetic<MouseEvent> {
53    fn held_buttons(&self) -> dioxus_html::input_data::MouseButtonSet {
54        decode_mouse_button_set(self.event.buttons())
55    }
56
57    fn trigger_button(&self) -> Option<MouseButton> {
58        Some(MouseButton::from_web_code(self.event.button()))
59    }
60}
61
62impl HasMouseData for Synthetic<MouseEvent> {
63    fn as_any(&self) -> &dyn std::any::Any {
64        &self.event
65    }
66}
67
68impl WebEventExt for dioxus_html::MouseData {
69    type WebEvent = web_sys::MouseEvent;
70
71    #[inline(always)]
72    fn try_as_web_event(&self) -> Option<web_sys::MouseEvent> {
73        self.downcast::<web_sys::MouseEvent>().cloned()
74    }
75}