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