1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::event::{IEvent, IUiEvent, UiEvent, Event};
use webapi::touch::Touch;

/// The `ITouchEvent` interface represents events that occur due to the user
/// interacting with a touch device (such as a phone).
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent)
// https://w3c.github.io/touch-events/#idl-def-touchevent
pub trait ITouchEvent: IUiEvent {
    /// Returns whether the Alt key was down when this event was fired.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/altKey)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn alt_key( &self ) -> bool {
        js!(
            return @{self.as_ref()}.altKey;
        ).try_into().unwrap()
    }

    /// Indicates whether the Ctrl key was down when this event fired.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/ctrlKey)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn ctrl_key( &self ) -> bool {
        js!(
            return @{self.as_ref()}.ctrlKey;
        ).try_into().unwrap()
    }

    /// Indicates whether the Meta key was down when this event fired.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/metaKey)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn meta_key( &self ) -> bool {
        js!(
            return @{self.as_ref()}.metaKey;
        ).try_into().unwrap()
    }

    /// Indicates whether the Shift key was down when this event was fired.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/shiftKey)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn shift_key( &self ) -> bool {
        js!(
            return @{self.as_ref()}.shiftKey;
        ).try_into().unwrap()
    }

    /// A list of Touches for every point of contact currently touching the surface.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn touches( &self ) -> Vec<Touch> {
        js!(
            return Array.from( @{self.as_ref()}.touches );
        ).try_into().unwrap()
    }

    /// A list of Touches for every point of contact that is touching the surface and started
    /// on the element that is the target of the current event.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/targetTouches)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn target_touches( &self ) -> Vec<Touch> {
        js!(
            return Array.from( @{self.as_ref()}.targetTouches );
        ).try_into().unwrap()
    }

    /// A list of Touches, one for each touch touch point that just became active with the current event.
    ///
    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches)
    // https://w3c.github.io/touch-events/#touchevent-interface
    #[inline]
    fn changed_touches( &self ) -> Vec<Touch> {
        js!(
            return Array.from( @{self.as_ref()}.changedTouches );
        ).try_into().unwrap()
    }
}


/// A reference to a JavaScript object which implements the [ITouchEvent](trait.ITouchEvent.html)
/// interface.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent)
// https://w3c.github.io/touch-events/#idl-def-touchevent
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(subclass_of(Event, UiEvent))]
pub struct TouchEvent( Reference );

impl IEvent for TouchEvent {}
impl IUiEvent for TouchEvent {}
impl ITouchEvent for TouchEvent {}

/// The `TouchMove` is fired when one or more touch points are moved along the
/// touch surface.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchmove)
// https://w3c.github.io/touch-events/#event-touchmove
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchmove")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchMove( Reference );

impl IEvent for TouchMove {}
impl IUiEvent for TouchMove {}
impl ITouchEvent for TouchMove {}

/// The `TouchLeave` event is fired when a touch point is moved off the
/// interactive area of an element.
///
/// Warning: This event was a proposal in an early version of the specification
/// and has not been implemented. Do not rely on it.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchleave)
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchleave")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchLeave( Reference );

impl IEvent for TouchLeave {}
impl IUiEvent for TouchLeave {}
impl ITouchEvent for TouchLeave {}

/// The `TouchEnter` event is fired when a touch point is moved onto the
/// interactive area of an element.
///
/// Warning: This event was a proposal in an early version of the specification
/// and has not been implemented. Do not rely on it.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchenter)
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchenter")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchEnter( Reference );

impl IEvent for TouchEnter {}
impl IUiEvent for TouchEnter {}
impl ITouchEvent for TouchEnter {}

/// The `TouchEnd` event is fired when one or more touch points are removed
/// from the touch surface.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchend)
// https://w3c.github.io/touch-events/#event-touchend
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchend")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchEnd( Reference );

impl IEvent for TouchEnd {}
impl IUiEvent for TouchEnd {}
impl ITouchEvent for TouchEnd {}

/// The `TouchCancel` event is fired when one or more touch points have been
/// disrupted in an implementation-specific manner (for example, too many touch
/// points are created).
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchcancel)
// https://w3c.github.io/touch-events/#event-touchcancel
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchcancel")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchCancel( Reference );

impl IEvent for TouchCancel {}
impl IUiEvent for TouchCancel {}
impl ITouchEvent for TouchCancel {}

/// The `TouchStart` event is fired when one or more touch points are placed
/// on the touch surface.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/touchstart)
// https://w3c.github.io/touch-events/#event-touchstart
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "TouchEvent")]
#[reference(event = "touchstart")]
#[reference(subclass_of(Event, UiEvent, TouchEvent))]
pub struct TouchStart( Reference );

impl IEvent for TouchStart {}
impl IUiEvent for TouchStart {}
impl ITouchEvent for TouchStart {}


#[cfg(all(test, feature = "web_test"))]
mod tests {
    use super::*;
    use webapi::event::ConcreteEvent;

    #[test]
    fn test_touch_event() {
        let event: TouchEvent = js!(
            return new TouchEvent(
                @{TouchMove::EVENT_TYPE},
                {
                    touches: [],
                    targetTouches: [],
                    changedTouches: [],
                    ctrlKey: true,
                    shiftKey: true,
                    altKey: true,
                    metaKey: true
                }
            );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchMove::EVENT_TYPE );
        assert!( event.ctrl_key() );
        assert!( event.alt_key() );
        assert!( event.shift_key() );
        assert!( event.meta_key() );
        assert_eq!( event.touches(), vec![] );
        assert_eq!( event.target_touches(), vec![] );
        assert_eq!( event.changed_touches(), vec![] );
    }

    #[test]
    fn test_touch_move_event() {
        let event: TouchMove = js!(
            return new TouchEvent( @{TouchMove::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchMove::EVENT_TYPE );
    }

    #[test]
    fn test_touch_leave_event() {
        let event: TouchLeave = js!(
            return new TouchEvent( @{TouchLeave::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchLeave::EVENT_TYPE );
    }


    #[test]
    fn test_touch_enter_event() {
        let event: TouchEnter = js!(
            return new TouchEvent( @{TouchEnter::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchEnter::EVENT_TYPE );
    }

    #[test]
    fn test_touch_end_event() {
        let event: TouchEnd = js!(
            return new TouchEvent( @{TouchEnd::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchEnd::EVENT_TYPE );
    }

    #[test]
    fn test_touch_cancel_event() {
        let event: TouchCancel = js!(
            return new TouchEvent( @{TouchCancel::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchCancel::EVENT_TYPE );
    }

    #[test]
    fn test_touch_start_event() {
        let event: TouchStart = js!(
            return new TouchEvent( @{TouchStart::EVENT_TYPE} );
        ).try_into().unwrap();
        assert_eq!( event.event_type(), TouchStart::EVENT_TYPE );
    }
}