dioxus_web/events/
mounted.rs

1use dioxus_html::{
2    geometry::euclid::{Point2D, Size2D},
3    MountedData,
4};
5use wasm_bindgen::JsCast;
6
7use super::{Synthetic, WebEventExt};
8
9impl dioxus_html::RenderedElementBacking for Synthetic<web_sys::Element> {
10    fn get_scroll_offset(
11        &self,
12    ) -> std::pin::Pin<
13        Box<
14            dyn std::future::Future<
15                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsVector2D>,
16            >,
17        >,
18    > {
19        let left = self.event.scroll_left();
20        let top = self.event.scroll_top();
21        let result = Ok(dioxus_html::geometry::PixelsVector2D::new(
22            left as f64,
23            top as f64,
24        ));
25        Box::pin(async { result })
26    }
27
28    fn get_scroll_size(
29        &self,
30    ) -> std::pin::Pin<
31        Box<
32            dyn std::future::Future<
33                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsSize>,
34            >,
35        >,
36    > {
37        let width = self.event.scroll_width();
38        let height = self.event.scroll_height();
39        let result = Ok(dioxus_html::geometry::PixelsSize::new(
40            width as f64,
41            height as f64,
42        ));
43        Box::pin(async { result })
44    }
45
46    fn get_client_rect(
47        &self,
48    ) -> std::pin::Pin<
49        Box<
50            dyn std::future::Future<
51                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsRect>,
52            >,
53        >,
54    > {
55        let rect = self.event.get_bounding_client_rect();
56        let result = Ok(dioxus_html::geometry::PixelsRect::new(
57            Point2D::new(rect.left(), rect.top()),
58            Size2D::new(rect.width(), rect.height()),
59        ));
60        Box::pin(async { result })
61    }
62
63    fn as_any(&self) -> &dyn std::any::Any {
64        &self.event
65    }
66
67    fn scroll_to(
68        &self,
69        behavior: dioxus_html::ScrollBehavior,
70    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = dioxus_html::MountedResult<()>>>> {
71        let options = web_sys::ScrollIntoViewOptions::new();
72        match behavior {
73            dioxus_html::ScrollBehavior::Instant => {
74                options.set_behavior(web_sys::ScrollBehavior::Instant);
75            }
76            dioxus_html::ScrollBehavior::Smooth => {
77                options.set_behavior(web_sys::ScrollBehavior::Smooth);
78            }
79        }
80        self.event
81            .scroll_into_view_with_scroll_into_view_options(&options);
82
83        Box::pin(async { Ok(()) })
84    }
85
86    fn set_focus(
87        &self,
88        focus: bool,
89    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = dioxus_html::MountedResult<()>>>> {
90        #[derive(Debug)]
91        struct FocusError(wasm_bindgen::JsValue);
92
93        impl std::fmt::Display for FocusError {
94            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95                write!(f, "failed to focus element {:?}", self.0)
96            }
97        }
98
99        impl std::error::Error for FocusError {}
100
101        let result = self
102            .event
103            .dyn_ref::<web_sys::HtmlElement>()
104            .ok_or_else(|| {
105                dioxus_html::MountedError::OperationFailed(Box::new(FocusError(
106                    self.event.clone().into(),
107                )))
108            })
109            .and_then(|e| {
110                (if focus { e.focus() } else { e.blur() }).map_err(|err| {
111                    dioxus_html::MountedError::OperationFailed(Box::new(FocusError(err)))
112                })
113            });
114        Box::pin(async { result })
115    }
116}
117
118impl WebEventExt for MountedData {
119    type WebEvent = web_sys::Element;
120
121    #[inline(always)]
122    fn try_as_web_event(&self) -> Option<web_sys::Element> {
123        self.downcast::<web_sys::Element>().cloned()
124    }
125}