Skip to main content

script/dom/touch/
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;
6use js::context::JSContext;
7use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
8
9use crate::dom::bindings::codegen::Bindings::TouchListBinding::TouchListMethods;
10use crate::dom::bindings::root::{Dom, DomRoot};
11use crate::dom::touch::Touch;
12use crate::dom::window::Window;
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(
29        cx: &mut JSContext,
30        window: &Window,
31        touches: &[&Touch],
32    ) -> DomRoot<TouchList> {
33        reflect_dom_object_with_cx(Box::new(TouchList::new_inherited(touches)), window, cx)
34    }
35}
36
37impl TouchListMethods<crate::DomTypeHolder> for TouchList {
38    /// <https://w3c.github.io/touch-events/#widl-TouchList-length>
39    fn Length(&self) -> u32 {
40        self.touches.len() as u32
41    }
42
43    /// <https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index>
44    fn Item(&self, index: u32) -> Option<DomRoot<Touch>> {
45        self.touches
46            .get(index as usize)
47            .map(|js| DomRoot::from_ref(&**js))
48    }
49
50    /// <https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index>
51    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Touch>> {
52        self.Item(index)
53    }
54}