forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanGestureHandler.js
More file actions
213 lines (186 loc) · 6.21 KB
/
PanGestureHandler.js
File metadata and controls
213 lines (186 loc) · 6.21 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
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
import Hammer from 'hammerjs';
import {
MULTI_FINGER_PAN_MAX_PINCH_THRESHOLD,
MULTI_FINGER_PAN_MAX_ROTATION_THRESHOLD,
} from './constants';
import DraggingGestureHandler from './DraggingGestureHandler';
import { isnan, TEST_MIN_IF_NOT_NAN, VEC_LEN_SQ } from './utils';
class PanGestureHandler extends DraggingGestureHandler {
get name() {
return 'pan';
}
get NativeGestureClass() {
return Hammer.Pan;
}
getHammerConfig() {
return {
...super.getHammerConfig(),
direction: this.getDirection(),
};
}
getDirection() {
const config = this.getConfig();
const {
activeOffsetXStart,
activeOffsetXEnd,
activeOffsetYStart,
activeOffsetYEnd,
minDist,
} = config;
let directions = [];
let horizontalDirections = [];
if (!isnan(minDist)) {
return Hammer.DIRECTION_ALL;
}
if (!isnan(activeOffsetXStart)) horizontalDirections.push(Hammer.DIRECTION_LEFT);
if (!isnan(activeOffsetXEnd)) horizontalDirections.push(Hammer.DIRECTION_RIGHT);
if (horizontalDirections.length === 2) horizontalDirections = [Hammer.DIRECTION_HORIZONTAL];
directions = directions.concat(horizontalDirections);
let verticalDirections = [];
if (!isnan(activeOffsetYStart)) verticalDirections.push(Hammer.DIRECTION_UP);
if (!isnan(activeOffsetYEnd)) verticalDirections.push(Hammer.DIRECTION_DOWN);
if (verticalDirections.length === 2) verticalDirections = [Hammer.DIRECTION_VERTICAL];
directions = directions.concat(verticalDirections);
if (!directions.length) {
return Hammer.DIRECTION_NONE;
}
if (
directions[0] === Hammer.DIRECTION_HORIZONTAL &&
directions[1] === Hammer.DIRECTION_VERTICAL
) {
return Hammer.DIRECTION_ALL;
}
if (horizontalDirections.length && verticalDirections.length) {
return Hammer.DIRECTION_ALL;
}
return directions[0];
}
getConfig() {
if (!this._hasCustomActivationCriteria) {
// Default config
// If no params have been defined then this config should emulate the native gesture as closely as possible.
return {
minDistSq: 10,
};
}
return this.config;
}
shouldFailUnderCustomCriteria({ deltaX, deltaY }, criteria) {
return (
(!isnan(criteria.failOffsetXStart) && deltaX < criteria.failOffsetXStart) ||
(!isnan(criteria.failOffsetXEnd) && deltaX > criteria.failOffsetXEnd) ||
(!isnan(criteria.failOffsetYStart) && deltaY < criteria.failOffsetYStart) ||
(!isnan(criteria.failOffsetYEnd) && deltaY > criteria.failOffsetYEnd)
);
}
shouldActivateUnderCustomCriteria({ deltaX, deltaY, velocity }, criteria) {
return (
(!isnan(criteria.activeOffsetXStart) && deltaX < criteria.activeOffsetXStart) ||
(!isnan(criteria.activeOffsetXEnd) && deltaX > criteria.activeOffsetXEnd) ||
(!isnan(criteria.activeOffsetYStart) && deltaY < criteria.activeOffsetYStart) ||
(!isnan(criteria.activeOffsetYEnd) && deltaY > criteria.activeOffsetYEnd) ||
TEST_MIN_IF_NOT_NAN(VEC_LEN_SQ({ x: deltaX, y: deltaY }), criteria.minDistSq) ||
TEST_MIN_IF_NOT_NAN(velocity.x, criteria.minVelocityX) ||
TEST_MIN_IF_NOT_NAN(velocity.y, criteria.minVelocityY) ||
TEST_MIN_IF_NOT_NAN(VEC_LEN_SQ(velocity), criteria.minVelocitySq)
);
}
shouldMultiFingerPanFail({ pointerLength, scale, deltaRotation }) {
if (pointerLength <= 1) {
return false;
}
// Test if the pan had too much pinching or rotating.
const deltaScale = Math.abs(scale - 1);
const absDeltaRotation = Math.abs(deltaRotation);
if (deltaScale > MULTI_FINGER_PAN_MAX_PINCH_THRESHOLD) {
// > If the threshold doesn't seem right.
// You can log the value which it failed at here:
return true;
}
if (absDeltaRotation > MULTI_FINGER_PAN_MAX_ROTATION_THRESHOLD) {
// > If the threshold doesn't seem right.
// You can log the value which it failed at here:
return true;
}
return false;
}
updateHasCustomActivationCriteria(criteria) {
return (
!isnan(criteria.minDistSq) ||
!isnan(criteria.minVelocityX) ||
!isnan(criteria.minVelocityY) ||
!isnan(criteria.minVelocitySq) ||
!isnan(criteria.activeOffsetXStart) ||
!isnan(criteria.activeOffsetXEnd) ||
!isnan(criteria.activeOffsetYStart) ||
!isnan(criteria.activeOffsetYEnd)
);
}
isGestureEnabledForEvent(props, recognizer, inputData) {
if (this.shouldFailUnderCustomCriteria(inputData, props)) {
return { failed: true };
}
const velocity = { x: inputData.velocityX, y: inputData.velocityY };
if (
this._hasCustomActivationCriteria &&
this.shouldActivateUnderCustomCriteria(
{ deltaX: inputData.deltaX, deltaY: inputData.deltaY, velocity },
props
)
) {
if (
this.shouldMultiFingerPanFail({
pointerLength: inputData.maxPointers,
scale: inputData.scale,
deltaRotation: inputData.deltaRotation,
})
) {
return {
failed: true,
};
}
return { success: true };
}
return { success: false };
}
}
function validateConfig(config = {}) {
const isNum = v => isnan(v) || typeof v === 'number';
const isBool = v => typeof v === 'boolean';
const valid = {
enabled: isBool,
minDistSq: isNum,
minVelocityX: isNum,
minVelocityY: isNum,
// TODO: Bacon: remove `minVelocity`
minVelocity: isNum,
minVelocitySq: isNum,
activeOffsetXStart: isNum,
activeOffsetXEnd: isNum,
failOffsetXStart: isNum,
failOffsetXEnd: isNum,
activeOffsetYStart: isNum,
activeOffsetYEnd: isNum,
failOffsetYStart: isNum,
failOffsetYEnd: isNum,
hasCustomActivationCriteria: isBool,
minPointers: isNum,
maxPointers: isNum,
};
const keys = Object.keys(valid);
let invalidKeys = [];
for (const key of Object.keys(config)) {
if (keys.includes(key)) {
if (valid[key](config[key])) {
console.warn('Invalid type: ' + key + ': ' + config[key]);
}
} else {
invalidKeys.push(key);
}
}
if (invalidKeys.length) {
throw new Error('Invalid config props found: ' + invalidKeys.join(', '));
}
return config;
}
export default PanGestureHandler;