forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTapGestureHandler.js
More file actions
160 lines (143 loc) · 3.94 KB
/
TapGestureHandler.js
File metadata and controls
160 lines (143 loc) · 3.94 KB
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
import Hammer from 'hammerjs';
import DiscreteGestureHandler from './DiscreteGestureHandler';
import { isnan } from './utils';
class TapGestureHandler extends DiscreteGestureHandler {
get name() {
return 'tap';
}
get NativeGestureClass() {
return Hammer.Tap;
}
get maxDelayMs() {
return isnan(this.config.maxDelayMs) ? 300 : this.config.maxDelayMs;
}
simulateCancelEvent(inputData) {
if (this.isGestureRunning) {
this.cancelEvent(inputData);
}
}
onGestureActivated(ev) {
if (this.isGestureRunning) {
this.onSuccessfulTap(ev);
}
}
onSuccessfulTap = ev => {
if (this._getPendingGestures().length) {
this._shouldFireEndEvent = ev;
return;
}
if (ev.eventType === Hammer.INPUT_END) {
this.sendEvent({ ...ev, eventType: Hammer.INPUT_MOVE });
}
// When handler gets activated it will turn into State.END immediately.
this.sendEvent({ ...ev, isFinal: true });
this.onGestureEnded(ev);
};
onRawEvent(ev) {
super.onRawEvent(ev);
// Attempt to create a touch-down event by checking if a valid tap hasn't started yet, then validating the input.
if (
!this.hasGestureFailed &&
!this.isGestureRunning &&
// Prevent multi-pointer events from misfiring.
!ev.isFinal
) {
// Tap Gesture start event
const gesture = this.hammer.get(this.name);
if (gesture.options.enable(gesture, ev)) {
clearTimeout(this._multiTapTimer);
this.onStart(ev);
this.sendEvent(ev);
}
}
if (ev.isFinal && ev.maxPointers > 1) {
setTimeout(() => {
// Handle case where one finger presses slightly
// after the first finger on a multi-tap event
if (this.isGestureRunning) {
this.cancelEvent(ev);
}
});
}
if (this.hasGestureFailed) {
return;
}
// Hammer doesn't send a `cancel` event for taps.
// Manually fail the event.
if (ev.isFinal) {
// Handle case where one finger presses slightly
// after the first finger on a multi-tap event
if (ev.maxPointers > 1) {
setTimeout(() => {
if (this.isGestureRunning) {
this.cancelEvent(ev);
}
});
}
// Clear last timer
clearTimeout(this._timer);
// Create time out for multi-taps.
this._timer = setTimeout(() => {
this.hasGestureFailed = true;
this.cancelEvent(ev);
}, this.maxDelayMs);
} else if (!this.hasGestureFailed && !this.isGestureRunning) {
// Tap Gesture start event
const gesture = this.hammer.get(this.name);
if (gesture.options.enable(gesture, ev)) {
clearTimeout(this._multiTapTimer);
this.onStart(ev);
this.sendEvent(ev);
}
}
}
getHammerConfig() {
return {
...super.getHammerConfig(),
event: this.name,
taps: isnan(this.config.numberOfTaps) ? 1 : this.config.numberOfTaps,
interval: this.maxDelayMs,
time:
isnan(this.config.maxDurationMs) || this.config.maxDurationMs == null
? 250
: this.config.maxDurationMs,
};
}
updateGestureConfig({
shouldCancelWhenOutside = true,
maxDeltaX = Number.NaN,
maxDeltaY = Number.NaN,
numberOfTaps = 1,
minDurationMs = 525,
maxDelayMs = Number.NaN,
maxDurationMs = Number.NaN,
maxDist = 2,
minPointers = 1,
maxPointers = 1,
...props
}) {
return super.updateGestureConfig({
shouldCancelWhenOutside,
numberOfTaps,
maxDeltaX,
maxDeltaY,
minDurationMs,
maxDelayMs,
maxDist,
minPointers,
maxPointers,
...props,
});
}
onGestureEnded(...props) {
clearTimeout(this._timer);
super.onGestureEnded(...props);
}
onWaitingEnded(gesture) {
if (this._shouldFireEndEvent) {
this.onSuccessfulTap(this._shouldFireEndEvent);
this._shouldFireEndEvent = null;
}
}
}
export default TapGestureHandler;