use dom_struct::dom_struct;
use js::jsval::JSVal;
use crate::dom::bindings::codegen::Bindings::ResizeObserverEntryBinding::ResizeObserverEntryMethods;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::domrectreadonly::DOMRectReadOnly;
use crate::dom::element::Element;
use crate::dom::resizeobserversize::ResizeObserverSize;
use crate::dom::window::Window;
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
#[dom_struct]
pub struct ResizeObserverEntry {
reflector_: Reflector,
target: Dom<Element>,
content_rect: Dom<DOMRectReadOnly>,
border_box_size: Vec<Dom<ResizeObserverSize>>,
content_box_size: Vec<Dom<ResizeObserverSize>>,
device_pixel_content_box_size: Vec<Dom<ResizeObserverSize>>,
}
impl ResizeObserverEntry {
fn new_inherited(
target: &Element,
content_rect: &DOMRectReadOnly,
border_box_size: &[&ResizeObserverSize],
content_box_size: &[&ResizeObserverSize],
device_pixel_content_box_size: &[&ResizeObserverSize],
) -> ResizeObserverEntry {
ResizeObserverEntry {
reflector_: Reflector::new(),
target: Dom::from_ref(target),
content_rect: Dom::from_ref(content_rect),
border_box_size: border_box_size
.iter()
.map(|size| Dom::from_ref(*size))
.collect(),
content_box_size: content_box_size
.iter()
.map(|size| Dom::from_ref(*size))
.collect(),
device_pixel_content_box_size: device_pixel_content_box_size
.iter()
.map(|size| Dom::from_ref(*size))
.collect(),
}
}
pub fn new(
window: &Window,
target: &Element,
content_rect: &DOMRectReadOnly,
border_box_size: &[&ResizeObserverSize],
content_box_size: &[&ResizeObserverSize],
device_pixel_content_box_size: &[&ResizeObserverSize],
) -> DomRoot<ResizeObserverEntry> {
let entry = Box::new(ResizeObserverEntry::new_inherited(
target,
content_rect,
border_box_size,
content_box_size,
device_pixel_content_box_size,
));
reflect_dom_object_with_proto(entry, window, None, CanGc::note())
}
}
impl ResizeObserverEntryMethods for ResizeObserverEntry {
fn Target(&self) -> DomRoot<Element> {
DomRoot::from_ref(&*self.target)
}
fn ContentRect(&self) -> DomRoot<DOMRectReadOnly> {
DomRoot::from_ref(&*self.content_rect)
}
fn BorderBoxSize(&self, cx: SafeJSContext) -> JSVal {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.border_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx)
}
fn ContentBoxSize(&self, cx: SafeJSContext) -> JSVal {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.content_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx)
}
fn DevicePixelContentBoxSize(&self, cx: SafeJSContext) -> JSVal {
let sizes: Vec<DomRoot<ResizeObserverSize>> = self
.device_pixel_content_box_size
.iter()
.map(|size| DomRoot::from_ref(&**size))
.collect();
to_frozen_array(sizes.as_slice(), cx)
}
}