dioxus_web/events/
wheel.rs

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