@@ -10,6 +10,8 @@ import {
1010// Import layout from utils directly to avoid circular references
1111import { layout } from "../../utils/utils" ;
1212
13+ import * as timer from "../../timer" ;
14+
1315export * from "./gestures-common" ;
1416
1517interface TapAndDoubleTapGestureListener {
@@ -27,6 +29,11 @@ function initializeTapAndDoubleTapGestureListener() {
2729 private _target : View ;
2830 private _type : number ;
2931
32+ private _lastUpTime : number = 0 ;
33+ private _tapTimeoutId : number ;
34+
35+ private static DoubleTapTimeout = android . view . ViewConfiguration . getDoubleTapTimeout ( ) ;
36+
3037 constructor ( observer : GesturesObserver , target : View , type : number ) {
3138 super ( ) ;
3239
@@ -37,28 +44,42 @@ function initializeTapAndDoubleTapGestureListener() {
3744 }
3845
3946 public onSingleTapUp ( motionEvent : android . view . MotionEvent ) : boolean {
40- if ( this . _type & GestureTypes . tap ) {
41- let args = _getArgs ( GestureTypes . tap , this . _target , motionEvent ) ;
42- _executeCallback ( this . _observer , args ) ;
47+ this . _handleSingleTap ( motionEvent ) ;
48+ this . _lastUpTime = Date . now ( ) ;
49+ return true ;
50+ }
51+
52+ public onDown ( motionEvent : android . view . MotionEvent ) : boolean {
53+ const tapTime = Date . now ( ) ;
54+ if ( ( tapTime - this . _lastUpTime ) <= TapAndDoubleTapGestureListenerImpl . DoubleTapTimeout ) {
55+ this . _handleDoubleTap ( motionEvent ) ;
4356 }
4457 return true ;
4558 }
4659
47- public onDoubleTap ( motionEvent : android . view . MotionEvent ) : boolean {
48- if ( this . _type & GestureTypes . doubleTap ) {
49- let args = _getArgs ( GestureTypes . doubleTap , this . _target , motionEvent ) ;
60+ public onLongPress ( motionEvent : android . view . MotionEvent ) : void {
61+ if ( this . _type & GestureTypes . longPress ) {
62+ const args = _getArgs ( GestureTypes . longPress , this . _target , motionEvent ) ;
5063 _executeCallback ( this . _observer , args ) ;
5164 }
52- return true ;
5365 }
5466
55- public onDown ( motionEvent : android . view . MotionEvent ) : boolean {
56- return true ;
67+ private _handleSingleTap ( motionEvent : android . view . MotionEvent ) : void {
68+ this . _tapTimeoutId = timer . setTimeout ( ( ) => {
69+ if ( this . _type & GestureTypes . tap ) {
70+ const args = _getArgs ( GestureTypes . tap , this . _target , motionEvent ) ;
71+ _executeCallback ( this . _observer , args ) ;
72+ }
73+ timer . clearTimeout ( this . _tapTimeoutId ) ;
74+ } , TapAndDoubleTapGestureListenerImpl . DoubleTapTimeout ) ;
5775 }
5876
59- public onLongPress ( motionEvent : android . view . MotionEvent ) : void {
60- if ( this . _type & GestureTypes . longPress ) {
61- let args = _getArgs ( GestureTypes . longPress , this . _target , motionEvent ) ;
77+ private _handleDoubleTap ( motionEvent : android . view . MotionEvent ) : void {
78+ if ( this . _tapTimeoutId ) {
79+ timer . clearTimeout ( this . _tapTimeoutId ) ;
80+ }
81+ if ( this . _type & GestureTypes . doubleTap ) {
82+ const args = _getArgs ( GestureTypes . doubleTap , this . _target , motionEvent ) ;
6283 _executeCallback ( this . _observer , args ) ;
6384 }
6485 }
@@ -94,7 +115,7 @@ function initializePinchGestureListener() {
94115 public onScaleBegin ( detector : android . view . ScaleGestureDetector ) : boolean {
95116 this . _scale = detector . getScaleFactor ( ) ;
96117
97- let args = new PinchGestureEventData (
118+ const args = new PinchGestureEventData (
98119 this . _target ,
99120 detector ,
100121 this . _scale ,
@@ -109,7 +130,7 @@ function initializePinchGestureListener() {
109130 public onScale ( detector : android . view . ScaleGestureDetector ) : boolean {
110131 this . _scale *= detector . getScaleFactor ( ) ;
111132
112- let args = new PinchGestureEventData (
133+ const args = new PinchGestureEventData (
113134 this . _target ,
114135 detector ,
115136 this . _scale ,
@@ -123,7 +144,7 @@ function initializePinchGestureListener() {
123144 public onScaleEnd ( detector : android . view . ScaleGestureDetector ) : void {
124145 this . _scale *= detector . getScaleFactor ( ) ;
125146
126- let args = new PinchGestureEventData (
147+ const args = new PinchGestureEventData (
127148 this . _target ,
128149 detector ,
129150 this . _scale ,
@@ -172,41 +193,28 @@ function initializeSwipeGestureListener() {
172193 let deltaX = currentEvent . getX ( ) - initialEvent . getX ( ) ;
173194
174195 if ( Math . abs ( deltaX ) > Math . abs ( deltaY ) ) {
175-
176196 if ( Math . abs ( deltaX ) > SWIPE_THRESHOLD
177197 && Math . abs ( velocityX ) > SWIPE_VELOCITY_THRESHOLD ) {
178-
179198 if ( deltaX > 0 ) {
180-
181199 args = _getSwipeArgs ( SwipeDirection . right , this . _target , initialEvent , currentEvent ) ;
182200 _executeCallback ( this . _observer , args ) ;
183-
184201 result = true ;
185202 } else {
186-
187203 args = _getSwipeArgs ( SwipeDirection . left , this . _target , initialEvent , currentEvent ) ;
188204 _executeCallback ( this . _observer , args ) ;
189-
190205 result = true ;
191206 }
192207 }
193-
194208 } else {
195-
196209 if ( Math . abs ( deltaY ) > SWIPE_THRESHOLD
197210 && Math . abs ( velocityY ) > SWIPE_VELOCITY_THRESHOLD ) {
198-
199211 if ( deltaY > 0 ) {
200-
201212 args = _getSwipeArgs ( SwipeDirection . down , this . _target , initialEvent , currentEvent ) ;
202213 _executeCallback ( this . _observer , args ) ;
203-
204214 result = true ;
205215 } else {
206-
207216 args = _getSwipeArgs ( SwipeDirection . up , this . _target , initialEvent , currentEvent ) ;
208217 _executeCallback ( this . _observer , args ) ;
209-
210218 result = true ;
211219 }
212220 }
@@ -228,7 +236,7 @@ const INVALID_POINTER_ID = -1;
228236const TO_DEGREES = ( 180 / Math . PI ) ;
229237
230238export function observe ( target : View , type : GestureTypes , callback : ( args : GestureEventData ) => void , context ?: any ) : GesturesObserver {
231- let observer = new GesturesObserver ( target , callback , context ) ;
239+ const observer = new GesturesObserver ( target , callback , context ) ;
232240 observer . observe ( type ) ;
233241 return observer ;
234242}
@@ -292,7 +300,7 @@ export class GesturesObserver extends GesturesObserverBase {
292300 private _attach ( target : View , type : GestureTypes ) {
293301 this . _detach ( ) ;
294302
295- if ( type & GestureTypes . tap || type & GestureTypes . doubleTap || type & GestureTypes . longPress ) {
303+ if ( ( type & GestureTypes . tap ) || ( type & GestureTypes . doubleTap ) || ( type & GestureTypes . longPress ) ) {
296304 initializeTapAndDoubleTapGestureListener ( ) ;
297305 this . _simpleGestureDetector = new androidx . core . view . GestureDetectorCompat ( target . _context , new TapAndDoubleTapGestureListener ( this , this . target , type ) ) ;
298306 }
@@ -465,7 +473,7 @@ class CustomPanGestureDetector {
465473 return true ;
466474 }
467475
468- private trackStop ( currentEvent : android . view . MotionEvent , cahceEvent : boolean ) {
476+ private trackStop ( currentEvent : android . view . MotionEvent , cacheEvent : boolean ) {
469477 if ( this . isTracking ) {
470478 let args = _getPanArgs ( this . deltaX , this . deltaY , this . target , GestureStateTypes . ended , null , currentEvent ) ;
471479 _executeCallback ( this . observer , args ) ;
@@ -475,10 +483,9 @@ class CustomPanGestureDetector {
475483 this . isTracking = false ;
476484 }
477485
478- if ( cahceEvent ) {
486+ if ( cacheEvent ) {
479487 this . lastEventCache = currentEvent ;
480- }
481- else {
488+ } else {
482489 this . lastEventCache = undefined ;
483490 }
484491 }
@@ -509,8 +516,7 @@ class CustomPanGestureDetector {
509516 x : event . getRawX ( ) / this . density ,
510517 y : event . getRawY ( ) / this . density
511518 } ;
512- }
513- else {
519+ } else {
514520 const offX = event . getRawX ( ) - event . getX ( ) ;
515521 const offY = event . getRawY ( ) - event . getY ( ) ;
516522 let res = { x : 0 , y : 0 } ;
@@ -559,8 +565,7 @@ class CustomRotateGestureDetector {
559565 if ( this . trackedPtrId1 === INVALID_POINTER_ID && pointerID !== this . trackedPtrId2 ) {
560566 this . trackedPtrId1 = pointerID ;
561567 assigned = true ;
562- }
563- else if ( this . trackedPtrId2 === INVALID_POINTER_ID && pointerID !== this . trackedPtrId1 ) {
568+ } else if ( this . trackedPtrId2 === INVALID_POINTER_ID && pointerID !== this . trackedPtrId1 ) {
564569 this . trackedPtrId2 = pointerID ;
565570 assigned = true ;
566571 }
@@ -585,8 +590,7 @@ class CustomRotateGestureDetector {
585590 case android . view . MotionEvent . ACTION_POINTER_UP :
586591 if ( pointerID === this . trackedPtrId1 ) {
587592 this . trackedPtrId1 = INVALID_POINTER_ID ;
588- }
589- else if ( pointerID === this . trackedPtrId2 ) {
593+ } else if ( pointerID === this . trackedPtrId2 ) {
590594 this . trackedPtrId2 = INVALID_POINTER_ID ;
591595 }
592596
0 commit comments