dioxus_web/events/
visible.rs

1use std::time::SystemTime;
2
3use dioxus_html::{
4    geometry::{
5        euclid::{Point2D, Size2D},
6        PixelsRect,
7    },
8    HasVisibleData, VisibleData, VisibleError, VisibleResult,
9};
10use wasm_bindgen::JsCast;
11use web_sys::{CustomEvent, DomRectReadOnly, Event, IntersectionObserverEntry};
12
13use super::{Synthetic, WebEventExt};
14
15impl From<Event> for Synthetic<IntersectionObserverEntry> {
16    #[inline]
17    fn from(e: Event) -> Self {
18        <Synthetic<IntersectionObserverEntry> as From<&Event>>::from(&e)
19    }
20}
21
22impl From<&Event> for Synthetic<IntersectionObserverEntry> {
23    #[inline]
24    fn from(e: &Event) -> Self {
25        let e: &CustomEvent = e.unchecked_ref();
26        let value = e.detail();
27        Self::new(value.unchecked_into::<IntersectionObserverEntry>())
28    }
29}
30fn dom_rect_ro_to_pixel_rect(dom_rect: &DomRectReadOnly) -> PixelsRect {
31    PixelsRect::new(
32        Point2D::new(dom_rect.x(), dom_rect.y()),
33        Size2D::new(dom_rect.width(), dom_rect.height()),
34    )
35}
36
37impl HasVisibleData for Synthetic<IntersectionObserverEntry> {
38    /// Get the bounds rectangle of the target element
39    fn get_bounding_client_rect(&self) -> VisibleResult<PixelsRect> {
40        Ok(dom_rect_ro_to_pixel_rect(
41            &self.event.bounding_client_rect(),
42        ))
43    }
44
45    /// Get the ratio of the intersectionRect to the boundingClientRect
46    fn get_intersection_ratio(&self) -> VisibleResult<f64> {
47        Ok(self.event.intersection_ratio())
48    }
49
50    /// Get the rect representing the target's visible area
51    fn get_intersection_rect(&self) -> VisibleResult<PixelsRect> {
52        Ok(dom_rect_ro_to_pixel_rect(&self.event.intersection_rect()))
53    }
54
55    /// Get if the target element intersects with the intersection observer's root
56    fn is_intersecting(&self) -> VisibleResult<bool> {
57        Ok(self.event.is_intersecting())
58    }
59
60    /// Get the rect for the intersection observer's root
61    fn get_root_bounds(&self) -> VisibleResult<PixelsRect> {
62        match self.event.root_bounds() {
63            Some(root_bounds) => Ok(dom_rect_ro_to_pixel_rect(&root_bounds)),
64            None => Err(VisibleError::NotSupported),
65        }
66    }
67
68    /// Get a timestamp indicating the time at which the intersection was recorded
69    fn get_time(&self) -> VisibleResult<SystemTime> {
70        let ms_since_epoch = self.event.time();
71        let rounded = ms_since_epoch.round() as u64;
72        let duration = std::time::Duration::from_millis(rounded);
73        Ok(SystemTime::UNIX_EPOCH + duration)
74    }
75
76    fn as_any(&self) -> &dyn std::any::Any {
77        self
78    }
79}
80
81impl WebEventExt for VisibleData {
82    type WebEvent = IntersectionObserverEntry;
83
84    #[inline(always)]
85    fn try_as_web_event(&self) -> Option<IntersectionObserverEntry> {
86        self.downcast::<CustomEvent>()
87            .and_then(|e| e.detail().dyn_into::<IntersectionObserverEntry>().ok())
88    }
89}