script/dom/
touchevent.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8
9use crate::dom::bindings::codegen::Bindings::TouchEventBinding::TouchEventMethods;
10use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::reflector::reflect_dom_object;
13use crate::dom::bindings::root::{DomRoot, MutDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::event::{EventBubbles, EventCancelable};
16use crate::dom::touchlist::TouchList;
17use crate::dom::uievent::UIEvent;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct TouchEvent {
23    uievent: UIEvent,
24    touches: MutDom<TouchList>,
25    target_touches: MutDom<TouchList>,
26    changed_touches: MutDom<TouchList>,
27    alt_key: Cell<bool>,
28    meta_key: Cell<bool>,
29    ctrl_key: Cell<bool>,
30    shift_key: Cell<bool>,
31}
32
33impl TouchEvent {
34    fn new_inherited(
35        touches: &TouchList,
36        changed_touches: &TouchList,
37        target_touches: &TouchList,
38    ) -> TouchEvent {
39        TouchEvent {
40            uievent: UIEvent::new_inherited(),
41            touches: MutDom::new(touches),
42            target_touches: MutDom::new(target_touches),
43            changed_touches: MutDom::new(changed_touches),
44            ctrl_key: Cell::new(false),
45            shift_key: Cell::new(false),
46            alt_key: Cell::new(false),
47            meta_key: Cell::new(false),
48        }
49    }
50
51    pub(crate) fn new_uninitialized(
52        window: &Window,
53        touches: &TouchList,
54        changed_touches: &TouchList,
55        target_touches: &TouchList,
56        can_gc: CanGc,
57    ) -> DomRoot<TouchEvent> {
58        reflect_dom_object(
59            Box::new(TouchEvent::new_inherited(
60                touches,
61                changed_touches,
62                target_touches,
63            )),
64            window,
65            can_gc,
66        )
67    }
68
69    #[allow(clippy::too_many_arguments)]
70    pub(crate) fn new(
71        window: &Window,
72        type_: DOMString,
73        can_bubble: EventBubbles,
74        cancelable: EventCancelable,
75        view: Option<&Window>,
76        detail: i32,
77        touches: &TouchList,
78        changed_touches: &TouchList,
79        target_touches: &TouchList,
80        ctrl_key: bool,
81        alt_key: bool,
82        shift_key: bool,
83        meta_key: bool,
84        can_gc: CanGc,
85    ) -> DomRoot<TouchEvent> {
86        let ev =
87            TouchEvent::new_uninitialized(window, touches, changed_touches, target_touches, can_gc);
88        ev.upcast::<UIEvent>().InitUIEvent(
89            type_,
90            bool::from(can_bubble),
91            bool::from(cancelable),
92            view,
93            detail,
94        );
95        ev.ctrl_key.set(ctrl_key);
96        ev.alt_key.set(alt_key);
97        ev.shift_key.set(shift_key);
98        ev.meta_key.set(meta_key);
99        ev
100    }
101}
102
103impl TouchEventMethods<crate::DomTypeHolder> for TouchEvent {
104    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-ctrlKey>
105    fn CtrlKey(&self) -> bool {
106        self.ctrl_key.get()
107    }
108
109    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-shiftKey>
110    fn ShiftKey(&self) -> bool {
111        self.shift_key.get()
112    }
113
114    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-altKey>
115    fn AltKey(&self) -> bool {
116        self.alt_key.get()
117    }
118
119    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-metaKey>
120    fn MetaKey(&self) -> bool {
121        self.meta_key.get()
122    }
123
124    /// <https://w3c.github.io/touch-events/#widl-TouchEventInit-touches>
125    fn Touches(&self) -> DomRoot<TouchList> {
126        self.touches.get()
127    }
128
129    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-targetTouches>
130    fn TargetTouches(&self) -> DomRoot<TouchList> {
131        self.target_touches.get()
132    }
133
134    /// <https://w3c.github.io/touch-events/#widl-TouchEvent-changedTouches>
135    fn ChangedTouches(&self) -> DomRoot<TouchList> {
136        self.changed_touches.get()
137    }
138
139    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
140    fn IsTrusted(&self) -> bool {
141        self.uievent.IsTrusted()
142    }
143}