script/dom/
touchlist.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/. */
4
5use dom_struct::dom_struct;
6
7use crate::dom::bindings::codegen::Bindings::TouchListBinding::TouchListMethods;
8use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
9use crate::dom::bindings::root::{Dom, DomRoot};
10use crate::dom::touch::Touch;
11use crate::dom::window::Window;
12use crate::script_runtime::CanGc;
13
14#[dom_struct]
15pub(crate) struct TouchList {
16    reflector_: Reflector,
17    touches: Vec<Dom<Touch>>,
18}
19
20impl TouchList {
21    fn new_inherited(touches: &[&Touch]) -> TouchList {
22        TouchList {
23            reflector_: Reflector::new(),
24            touches: touches.iter().map(|touch| Dom::from_ref(*touch)).collect(),
25        }
26    }
27
28    pub(crate) fn new(window: &Window, touches: &[&Touch], can_gc: CanGc) -> DomRoot<TouchList> {
29        reflect_dom_object(Box::new(TouchList::new_inherited(touches)), window, can_gc)
30    }
31}
32
33impl TouchListMethods<crate::DomTypeHolder> for TouchList {
34    /// <https://w3c.github.io/touch-events/#widl-TouchList-length>
35    fn Length(&self) -> u32 {
36        self.touches.len() as u32
37    }
38
39    /// <https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index>
40    fn Item(&self, index: u32) -> Option<DomRoot<Touch>> {
41        self.touches
42            .get(index as usize)
43            .map(|js| DomRoot::from_ref(&**js))
44    }
45
46    /// <https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index>
47    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Touch>> {
48        self.Item(index)
49    }
50}