use std::cell::Cell;
use dom_struct::dom_struct;
use js::context::JSContext;
use js::rust::HandleObject;
use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
use crate::dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::DOMRectInit;
use crate::dom::bindings::codegen::Bindings::IntersectionObserverEntryBinding::{
IntersectionObserverEntryInit, IntersectionObserverEntryMethods,
};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::domrectreadonly::DOMRectReadOnly;
use crate::dom::element::Element;
use crate::dom::window::Window;
#[dom_struct]
pub(crate) struct IntersectionObserverEntry {
reflector_: Reflector,
time: Cell<Finite<f64>>,
root_bounds: Option<Dom<DOMRectReadOnly>>,
bounding_client_rect: Dom<DOMRectReadOnly>,
intersection_rect: Dom<DOMRectReadOnly>,
is_intersecting: Cell<bool>,
is_visible: Cell<bool>,
intersection_ratio: Cell<Finite<f64>>,
target: Dom<Element>,
}
impl IntersectionObserverEntry {
#[allow(clippy::too_many_arguments)]
fn new_inherited(
time: Finite<f64>,
root_bounds: Option<&DOMRectReadOnly>,
bounding_client_rect: &DOMRectReadOnly,
intersection_rect: &DOMRectReadOnly,
is_intersecting: bool,
is_visible: bool,
intersection_ratio: Finite<f64>,
target: &Element,
) -> Self {
Self {
reflector_: Reflector::new(),
target: Dom::from_ref(target),
time: Cell::new(time),
root_bounds: root_bounds.map(Dom::from_ref),
bounding_client_rect: Dom::from_ref(bounding_client_rect),
intersection_rect: Dom::from_ref(intersection_rect),
is_intersecting: Cell::new(is_intersecting),
is_visible: Cell::new(is_visible),
intersection_ratio: Cell::new(intersection_ratio),
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
time: Finite<f64>,
root_bounds: Option<&DOMRectReadOnly>,
bounding_client_rect: &DOMRectReadOnly,
intersection_rect: &DOMRectReadOnly,
is_intersecting: bool,
is_visible: bool,
intersection_ratio: Finite<f64>,
target: &Element,
) -> DomRoot<Self> {
let observer = Box::new(Self::new_inherited(
time,
root_bounds,
bounding_client_rect,
intersection_rect,
is_intersecting,
is_visible,
intersection_ratio,
target,
));
reflect_dom_object_with_proto_and_cx(observer, window, proto, cx)
}
fn new_from_dictionary(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
init: &IntersectionObserverEntryInit,
) -> DomRoot<Self> {
let mut domrectreadonly_from_dictionary = |dictionary: &DOMRectInit| {
DOMRectReadOnly::new_from_dictionary(cx, window.as_global_scope(), proto, dictionary)
};
let observer = Box::new(Self::new_inherited(
init.time,
Some(&*domrectreadonly_from_dictionary(&init.rootBounds)),
&domrectreadonly_from_dictionary(&init.boundingClientRect),
&domrectreadonly_from_dictionary(&init.intersectionRect),
init.isIntersecting,
init.isVisible,
init.intersectionRatio,
&init.target,
));
reflect_dom_object_with_proto_and_cx(observer, window, proto, cx)
}
}
impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObserverEntry {
fn Time(&self) -> Finite<f64> {
self.time.get()
}
fn GetRootBounds(&self) -> Option<DomRoot<DOMRectReadOnly>> {
self.root_bounds.as_ref().map(|rect| rect.as_rooted())
}
fn BoundingClientRect(&self) -> DomRoot<DOMRectReadOnly> {
self.bounding_client_rect.as_rooted()
}
fn IntersectionRect(&self) -> DomRoot<DOMRectReadOnly> {
self.intersection_rect.as_rooted()
}
fn IsIntersecting(&self) -> bool {
self.is_intersecting.get()
}
fn IsVisible(&self) -> bool {
self.is_visible.get()
}
fn IntersectionRatio(&self) -> Finite<f64> {
self.intersection_ratio.get()
}
fn Target(&self) -> DomRoot<Element> {
self.target.as_rooted()
}
fn Constructor(
cx: &mut JSContext,
window: &Window,
proto: Option<HandleObject>,
init: &IntersectionObserverEntryInit,
) -> DomRoot<IntersectionObserverEntry> {
Self::new_from_dictionary(cx, window, proto, init)
}
}