dioxus_web/events/
mounted.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use dioxus_html::{
    geometry::euclid::{Point2D, Size2D},
    MountedData,
};
use wasm_bindgen::JsCast;

use super::{Synthetic, WebEventExt};

impl dioxus_html::RenderedElementBacking for Synthetic<web_sys::Element> {
    fn get_scroll_offset(
        &self,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<
                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsVector2D>,
            >,
        >,
    > {
        let left = self.event.scroll_left();
        let top = self.event.scroll_top();
        let result = Ok(dioxus_html::geometry::PixelsVector2D::new(
            left as f64,
            top as f64,
        ));
        Box::pin(async { result })
    }

    fn get_scroll_size(
        &self,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<
                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsSize>,
            >,
        >,
    > {
        let width = self.event.scroll_width();
        let height = self.event.scroll_height();
        let result = Ok(dioxus_html::geometry::PixelsSize::new(
            width as f64,
            height as f64,
        ));
        Box::pin(async { result })
    }

    fn get_client_rect(
        &self,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<
                Output = dioxus_html::MountedResult<dioxus_html::geometry::PixelsRect>,
            >,
        >,
    > {
        let rect = self.event.get_bounding_client_rect();
        let result = Ok(dioxus_html::geometry::PixelsRect::new(
            Point2D::new(rect.left(), rect.top()),
            Size2D::new(rect.width(), rect.height()),
        ));
        Box::pin(async { result })
    }

    fn as_any(&self) -> &dyn std::any::Any {
        &self.event
    }

    fn scroll_to(
        &self,
        behavior: dioxus_html::ScrollBehavior,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = dioxus_html::MountedResult<()>>>> {
        let options = web_sys::ScrollIntoViewOptions::new();
        match behavior {
            dioxus_html::ScrollBehavior::Instant => {
                options.set_behavior(web_sys::ScrollBehavior::Instant);
            }
            dioxus_html::ScrollBehavior::Smooth => {
                options.set_behavior(web_sys::ScrollBehavior::Smooth);
            }
        }
        self.event
            .scroll_into_view_with_scroll_into_view_options(&options);

        Box::pin(async { Ok(()) })
    }

    fn set_focus(
        &self,
        focus: bool,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = dioxus_html::MountedResult<()>>>> {
        #[derive(Debug)]
        struct FocusError(wasm_bindgen::JsValue);

        impl std::fmt::Display for FocusError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "failed to focus element {:?}", self.0)
            }
        }

        impl std::error::Error for FocusError {}

        let result = self
            .event
            .dyn_ref::<web_sys::HtmlElement>()
            .ok_or_else(|| {
                dioxus_html::MountedError::OperationFailed(Box::new(FocusError(
                    self.event.clone().into(),
                )))
            })
            .and_then(|e| {
                (if focus { e.focus() } else { e.blur() }).map_err(|err| {
                    dioxus_html::MountedError::OperationFailed(Box::new(FocusError(err)))
                })
            });
        Box::pin(async { result })
    }
}

impl WebEventExt for MountedData {
    type WebEvent = web_sys::Element;

    #[inline(always)]
    fn try_as_web_event(&self) -> Option<web_sys::Element> {
        self.downcast::<web_sys::Element>().cloned()
    }
}