script/dom/
domrectlist.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4use dom_struct::dom_struct;
5
6use crate::dom::bindings::cell::DomRefCell;
7use crate::dom::bindings::codegen::Bindings::DOMRectListBinding::DOMRectListMethods;
8use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
9use crate::dom::bindings::root::{Dom, DomRoot};
10use crate::dom::domrect::DOMRect;
11use crate::dom::window::Window;
12use crate::script_runtime::CanGc;
13
14#[dom_struct]
15pub(crate) struct DOMRectList {
16    reflector_: Reflector,
17    rects: DomRefCell<Vec<Dom<DOMRect>>>,
18}
19
20impl DOMRectList {
21    fn new_inherited(rects: Vec<DomRoot<DOMRect>>) -> DOMRectList {
22        DOMRectList {
23            reflector_: Reflector::new(),
24            rects: DomRefCell::new(
25                rects
26                    .into_iter()
27                    .map(|dom_root| dom_root.as_traced())
28                    .collect(),
29            ),
30        }
31    }
32
33    pub(crate) fn new(
34        window: &Window,
35        rects: Vec<DomRoot<DOMRect>>,
36        can_gc: CanGc,
37    ) -> DomRoot<DOMRectList> {
38        reflect_dom_object_with_proto(
39            Box::new(DOMRectList::new_inherited(rects)),
40            window,
41            None,
42            can_gc,
43        )
44    }
45
46    pub(crate) fn first(&self) -> Option<DomRoot<DOMRect>> {
47        self.rects.borrow().first().map(Dom::as_rooted)
48    }
49}
50
51impl DOMRectListMethods<crate::DomTypeHolder> for DOMRectList {
52    /// <https://drafts.fxtf.org/geometry/#DOMRectList>
53    fn Item(&self, index: u32) -> Option<DomRoot<DOMRect>> {
54        self.rects.borrow().get(index as usize).map(Dom::as_rooted)
55    }
56
57    /// <https://drafts.fxtf.org/geometry/#DOMRectList>
58    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<DOMRect>> {
59        self.Item(index)
60    }
61
62    /// <https://drafts.fxtf.org/geometry/#DOMRectList>
63    fn Length(&self) -> u32 {
64        self.rects.borrow().len() as u32
65    }
66}