stdweb/webapi/events/
touch.rs

1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use webapi::event::{IEvent, IUiEvent, UiEvent, Event};
4use webapi::touch::Touch;
5
6/// The `ITouchEvent` interface represents events that occur due to the user
7/// interacting with a touch device (such as a phone).
8///
9/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent)
10// https://w3c.github.io/touch-events/#idl-def-touchevent
11pub trait ITouchEvent: IUiEvent {
12    /// Returns whether the Alt key was down when this event was fired.
13    ///
14    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/altKey)
15    // https://w3c.github.io/touch-events/#touchevent-interface
16    #[inline]
17    fn alt_key( &self ) -> bool {
18        js!(
19            return @{self.as_ref()}.altKey;
20        ).try_into().unwrap()
21    }
22
23    /// Indicates whether the Ctrl key was down when this event fired.
24    ///
25    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/ctrlKey)
26    // https://w3c.github.io/touch-events/#touchevent-interface
27    #[inline]
28    fn ctrl_key( &self ) -> bool {
29        js!(
30            return @{self.as_ref()}.ctrlKey;
31        ).try_into().unwrap()
32    }
33
34    /// Indicates whether the Meta key was down when this event fired.
35    ///
36    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/metaKey)
37    // https://w3c.github.io/touch-events/#touchevent-interface
38    #[inline]
39    fn meta_key( &self ) -> bool {
40        js!(
41            return @{self.as_ref()}.metaKey;
42        ).try_into().unwrap()
43    }
44
45    /// Indicates whether the Shift key was down when this event was fired.
46    ///
47    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/shiftKey)
48    // https://w3c.github.io/touch-events/#touchevent-interface
49    #[inline]
50    fn shift_key( &self ) -> bool {
51        js!(
52            return @{self.as_ref()}.shiftKey;
53        ).try_into().unwrap()
54    }
55
56    /// A list of Touches for every point of contact currently touching the surface.
57    ///
58    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches)
59    // https://w3c.github.io/touch-events/#touchevent-interface
60    #[inline]
61    fn touches( &self ) -> Vec<Touch> {
62        js!(
63            return Array.from( @{self.as_ref()}.touches );
64        ).try_into().unwrap()
65    }
66
67    /// A list of Touches for every point of contact that is touching the surface and started
68    /// on the element that is the target of the current event.
69    ///
70    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/targetTouches)
71    // https://w3c.github.io/touch-events/#touchevent-interface
72    #[inline]
73    fn target_touches( &self ) -> Vec<Touch> {
74        js!(
75            return Array.from( @{self.as_ref()}.targetTouches );
76        ).try_into().unwrap()
77    }
78
79    /// A list of Touches, one for each touch touch point that just became active with the current event.
80    ///
81    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches)
82    // https://w3c.github.io/touch-events/#touchevent-interface
83    #[inline]
84    fn changed_touches( &self ) -> Vec<Touch> {
85        js!(
86            return Array.from( @{self.as_ref()}.changedTouches );
87        ).try_into().unwrap()
88    }
89}
90
91
92/// A reference to a JavaScript object which implements the [ITouchEvent](trait.ITouchEvent.html)
93/// interface.
94///
95/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent)
96// https://w3c.github.io/touch-events/#idl-def-touchevent
97#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
98#[reference(instance_of = "TouchEvent")]
99#[reference(subclass_of(Event, UiEvent))]
100pub struct TouchEvent( Reference );
101
102impl IEvent for TouchEvent {}
103impl IUiEvent for TouchEvent {}
104impl ITouchEvent for TouchEvent {}
105
106/// The `TouchMove` is fired when one or more touch points are moved along the
107/// touch surface.
108///
109/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchmove)
110// https://w3c.github.io/touch-events/#event-touchmove
111#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
112#[reference(instance_of = "TouchEvent")]
113#[reference(event = "touchmove")]
114#[reference(subclass_of(Event, UiEvent, TouchEvent))]
115pub struct TouchMove( Reference );
116
117impl IEvent for TouchMove {}
118impl IUiEvent for TouchMove {}
119impl ITouchEvent for TouchMove {}
120
121/// The `TouchLeave` event is fired when a touch point is moved off the
122/// interactive area of an element.
123///
124/// Warning: This event was a proposal in an early version of the specification
125/// and has not been implemented. Do not rely on it.
126///
127/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchleave)
128#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
129#[reference(instance_of = "TouchEvent")]
130#[reference(event = "touchleave")]
131#[reference(subclass_of(Event, UiEvent, TouchEvent))]
132pub struct TouchLeave( Reference );
133
134impl IEvent for TouchLeave {}
135impl IUiEvent for TouchLeave {}
136impl ITouchEvent for TouchLeave {}
137
138/// The `TouchEnter` event is fired when a touch point is moved onto the
139/// interactive area of an element.
140///
141/// Warning: This event was a proposal in an early version of the specification
142/// and has not been implemented. Do not rely on it.
143///
144/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchenter)
145#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
146#[reference(instance_of = "TouchEvent")]
147#[reference(event = "touchenter")]
148#[reference(subclass_of(Event, UiEvent, TouchEvent))]
149pub struct TouchEnter( Reference );
150
151impl IEvent for TouchEnter {}
152impl IUiEvent for TouchEnter {}
153impl ITouchEvent for TouchEnter {}
154
155/// The `TouchEnd` event is fired when one or more touch points are removed
156/// from the touch surface.
157///
158/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchend)
159// https://w3c.github.io/touch-events/#event-touchend
160#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
161#[reference(instance_of = "TouchEvent")]
162#[reference(event = "touchend")]
163#[reference(subclass_of(Event, UiEvent, TouchEvent))]
164pub struct TouchEnd( Reference );
165
166impl IEvent for TouchEnd {}
167impl IUiEvent for TouchEnd {}
168impl ITouchEvent for TouchEnd {}
169
170/// The `TouchCancel` event is fired when one or more touch points have been
171/// disrupted in an implementation-specific manner (for example, too many touch
172/// points are created).
173///
174/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchcancel)
175// https://w3c.github.io/touch-events/#event-touchcancel
176#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
177#[reference(instance_of = "TouchEvent")]
178#[reference(event = "touchcancel")]
179#[reference(subclass_of(Event, UiEvent, TouchEvent))]
180pub struct TouchCancel( Reference );
181
182impl IEvent for TouchCancel {}
183impl IUiEvent for TouchCancel {}
184impl ITouchEvent for TouchCancel {}
185
186/// The `TouchStart` event is fired when one or more touch points are placed
187/// on the touch surface.
188///
189/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchstart)
190// https://w3c.github.io/touch-events/#event-touchstart
191#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
192#[reference(instance_of = "TouchEvent")]
193#[reference(event = "touchstart")]
194#[reference(subclass_of(Event, UiEvent, TouchEvent))]
195pub struct TouchStart( Reference );
196
197impl IEvent for TouchStart {}
198impl IUiEvent for TouchStart {}
199impl ITouchEvent for TouchStart {}
200
201
202#[cfg(all(test, feature = "web_test"))]
203mod tests {
204    use super::*;
205    use webapi::event::ConcreteEvent;
206
207    #[test]
208    fn test_touch_event() {
209        let event: TouchEvent = js!(
210            return new TouchEvent(
211                @{TouchMove::EVENT_TYPE},
212                {
213                    touches: [],
214                    targetTouches: [],
215                    changedTouches: [],
216                    ctrlKey: true,
217                    shiftKey: true,
218                    altKey: true,
219                    metaKey: true
220                }
221            );
222        ).try_into().unwrap();
223        assert_eq!( event.event_type(), TouchMove::EVENT_TYPE );
224        assert!( event.ctrl_key() );
225        assert!( event.alt_key() );
226        assert!( event.shift_key() );
227        assert!( event.meta_key() );
228        assert_eq!( event.touches(), vec![] );
229        assert_eq!( event.target_touches(), vec![] );
230        assert_eq!( event.changed_touches(), vec![] );
231    }
232
233    #[test]
234    fn test_touch_move_event() {
235        let event: TouchMove = js!(
236            return new TouchEvent( @{TouchMove::EVENT_TYPE} );
237        ).try_into().unwrap();
238        assert_eq!( event.event_type(), TouchMove::EVENT_TYPE );
239    }
240
241    #[test]
242    fn test_touch_leave_event() {
243        let event: TouchLeave = js!(
244            return new TouchEvent( @{TouchLeave::EVENT_TYPE} );
245        ).try_into().unwrap();
246        assert_eq!( event.event_type(), TouchLeave::EVENT_TYPE );
247    }
248
249
250    #[test]
251    fn test_touch_enter_event() {
252        let event: TouchEnter = js!(
253            return new TouchEvent( @{TouchEnter::EVENT_TYPE} );
254        ).try_into().unwrap();
255        assert_eq!( event.event_type(), TouchEnter::EVENT_TYPE );
256    }
257
258    #[test]
259    fn test_touch_end_event() {
260        let event: TouchEnd = js!(
261            return new TouchEvent( @{TouchEnd::EVENT_TYPE} );
262        ).try_into().unwrap();
263        assert_eq!( event.event_type(), TouchEnd::EVENT_TYPE );
264    }
265
266    #[test]
267    fn test_touch_cancel_event() {
268        let event: TouchCancel = js!(
269            return new TouchEvent( @{TouchCancel::EVENT_TYPE} );
270        ).try_into().unwrap();
271        assert_eq!( event.event_type(), TouchCancel::EVENT_TYPE );
272    }
273
274    #[test]
275    fn test_touch_start_event() {
276        let event: TouchStart = js!(
277            return new TouchEvent( @{TouchStart::EVENT_TYPE} );
278        ).try_into().unwrap();
279        assert_eq!( event.event_type(), TouchStart::EVENT_TYPE );
280    }
281}