dioxus_web/events/
drag.rs

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