RCTSurfaceTouchHandler.mm
10.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSurfaceTouchHandler.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#import <React/RCTUtils.h>
#import <React/RCTViewComponentView.h>
#import "RCTConversions.h"
#import "RCTTouchableComponentViewProtocol.h"
using namespace facebook::react;
template <size_t size>
class IdentifierPool {
public:
void enqueue(int index) {
usage[index] = false;
}
int dequeue() {
while (true) {
if (!usage[lastIndex]) {
usage[lastIndex] = true;
return lastIndex;
}
lastIndex = (lastIndex + 1) % size;
}
}
void reset() {
for (int i = 0; i < size; i++) {
usage[i] = false;
}
}
private:
bool usage[size];
int lastIndex;
};
typedef NS_ENUM(NSInteger, RCTTouchEventType) {
RCTTouchEventTypeTouchStart,
RCTTouchEventTypeTouchMove,
RCTTouchEventTypeTouchEnd,
RCTTouchEventTypeTouchCancel,
};
struct ActiveTouch {
Touch touch;
SharedTouchEventEmitter eventEmitter;
struct Hasher {
size_t operator()(const ActiveTouch &activeTouch) const {
return std::hash<decltype(activeTouch.touch.identifier)>()(activeTouch.touch.identifier);
}
};
struct Comparator {
bool operator()(const ActiveTouch &lhs, const ActiveTouch &rhs) const {
return lhs.touch.identifier == rhs.touch.identifier;
}
};
};
static void UpdateActiveTouchWithUITouch(ActiveTouch &activeTouch, UITouch *uiTouch, UIView *rootComponentView) {
CGPoint offsetPoint = [uiTouch locationInView:uiTouch.view];
CGPoint screenPoint = [uiTouch locationInView:uiTouch.window];
CGPoint pagePoint = [uiTouch locationInView:rootComponentView];
activeTouch.touch.offsetPoint = RCTPointFromCGPoint(offsetPoint);
activeTouch.touch.screenPoint = RCTPointFromCGPoint(screenPoint);
activeTouch.touch.pagePoint = RCTPointFromCGPoint(pagePoint);
activeTouch.touch.timestamp = uiTouch.timestamp;
if (RCTForceTouchAvailable()) {
activeTouch.touch.force = uiTouch.force / uiTouch.maximumPossibleForce;
}
}
static ActiveTouch CreateTouchWithUITouch(UITouch *uiTouch, UIView *rootComponentView) {
UIView *componentView = uiTouch.view;
ActiveTouch activeTouch = {};
if ([componentView respondsToSelector:@selector(touchEventEmitterAtPoint:)]) {
activeTouch.eventEmitter = [(id<RCTTouchableComponentViewProtocol>)componentView touchEventEmitterAtPoint:[uiTouch locationInView:componentView]];
activeTouch.touch.target = (Tag)componentView.tag;
}
UpdateActiveTouchWithUITouch(activeTouch, uiTouch, rootComponentView);
return activeTouch;
}
static BOOL AllTouchesAreCancelledOrEnded(NSSet<UITouch *> *touches) {
for (UITouch *touch in touches) {
if (touch.phase == UITouchPhaseBegan ||
touch.phase == UITouchPhaseMoved ||
touch.phase == UITouchPhaseStationary) {
return NO;
}
}
return YES;
}
static BOOL AnyTouchesChanged(NSSet<UITouch *> *touches) {
for (UITouch *touch in touches) {
if (touch.phase == UITouchPhaseBegan ||
touch.phase == UITouchPhaseMoved) {
return YES;
}
}
return NO;
}
/**
* Surprisingly, `__unsafe_unretained id` pointers are not regular pointers
* and `std::hash<>` cannot hash them.
* This is quite trivial but decent implementation of hasher function
* inspired by this research: https://stackoverflow.com/a/21062520/496389.
*/
template<typename PointerT>
struct PointerHasher {
constexpr std::size_t operator()(const PointerT &value) const {
return reinterpret_cast<size_t>(value);
}
};
@interface RCTSurfaceTouchHandler () <UIGestureRecognizerDelegate>
@end
@implementation RCTSurfaceTouchHandler {
std::unordered_map<
__unsafe_unretained UITouch *,
ActiveTouch,
PointerHasher<__unsafe_unretained UITouch *>
> _activeTouches;
UIView *_rootComponentView;
IdentifierPool<11> _identifierPool;
}
- (instancetype)init
{
if (self = [super initWithTarget:nil action:nil]) {
// `cancelsTouchesInView` and `delaysTouches*` are needed in order
// to be used as a top level event delegated recognizer.
// Otherwise, lower-level components not built using React Native,
// will fail to recognize gestures.
self.cancelsTouchesInView = NO;
self.delaysTouchesBegan = NO; // This is default value.
self.delaysTouchesEnded = NO;
self.delegate = self;
}
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithTarget:(id)target action:(SEL)action)
- (void)attachToView:(UIView *)view
{
RCTAssert(self.view == nil, @"RCTTouchHandler already has attached view.");
[view addGestureRecognizer:self];
_rootComponentView = view;
}
- (void)detachFromView:(UIView *)view
{
RCTAssertParam(view);
RCTAssert(self.view == view, @"RCTTouchHandler attached to another view.");
[view removeGestureRecognizer:self];
_rootComponentView = nil;
}
- (void)_registerTouches:(NSSet<UITouch *> *)touches
{
for (UITouch *touch in touches) {
auto activeTouch = CreateTouchWithUITouch(touch, _rootComponentView);
activeTouch.touch.identifier = _identifierPool.dequeue();
_activeTouches.emplace(touch, activeTouch);
}
}
- (void)_updateTouches:(NSSet<UITouch *> *)touches
{
for (UITouch *touch in touches) {
UpdateActiveTouchWithUITouch(_activeTouches.at(touch), touch, _rootComponentView);
}
}
- (void)_unregisterTouches:(NSSet<UITouch *> *)touches
{
for (UITouch *touch in touches) {
const auto &activeTouch = _activeTouches.at(touch);
_identifierPool.enqueue(activeTouch.touch.identifier);
_activeTouches.erase(touch);
}
}
- (std::vector<ActiveTouch>)_activeTouchesFromTouches:(NSSet<UITouch *> *)touches
{
std::vector<ActiveTouch> activeTouches;
activeTouches.reserve(touches.count);
for (UITouch *touch in touches) {
activeTouches.push_back(_activeTouches.at(touch));
}
return activeTouches;
}
- (void)_dispatchActiveTouches:(std::vector<ActiveTouch>)activeTouches eventType:(RCTTouchEventType)eventType
{
TouchEvent event = {};
std::unordered_set<ActiveTouch, ActiveTouch::Hasher, ActiveTouch::Comparator> changedActiveTouches = {};
std::unordered_set<SharedTouchEventEmitter> uniqueEventEmitter = {};
BOOL isEndishEventType = eventType == RCTTouchEventTypeTouchEnd || eventType == RCTTouchEventTypeTouchCancel;
for (const auto &activeTouch : activeTouches) {
if (!activeTouch.eventEmitter) {
continue;
}
changedActiveTouches.insert(activeTouch);
event.changedTouches.insert(activeTouch.touch);
uniqueEventEmitter.insert(activeTouch.eventEmitter);
}
for (const auto &pair : _activeTouches) {
if (!pair.second.eventEmitter) {
continue;
}
if (
isEndishEventType &&
event.changedTouches.find(pair.second.touch) != event.changedTouches.end()
) {
continue;
}
event.touches.insert(pair.second.touch);
}
for (const auto &eventEmitter : uniqueEventEmitter) {
event.targetTouches.clear();
for (const auto &pair : _activeTouches) {
if (pair.second.eventEmitter == eventEmitter) {
event.targetTouches.insert(pair.second.touch);
}
}
switch (eventType) {
case RCTTouchEventTypeTouchStart:
eventEmitter->onTouchStart(event);
break;
case RCTTouchEventTypeTouchMove:
eventEmitter->onTouchMove(event);
break;
case RCTTouchEventTypeTouchEnd:
eventEmitter->onTouchEnd(event);
break;
case RCTTouchEventTypeTouchCancel:
eventEmitter->onTouchCancel(event);
break;
}
}
}
#pragma mark - `UIResponder`-ish touch-delivery methods
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self _registerTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches]
eventType:RCTTouchEventTypeTouchStart];
if (self.state == UIGestureRecognizerStatePossible) {
self.state = UIGestureRecognizerStateBegan;
} else if (self.state == UIGestureRecognizerStateBegan) {
self.state = UIGestureRecognizerStateChanged;
}
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches]
eventType:RCTTouchEventTypeTouchMove];
self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches]
eventType:RCTTouchEventTypeTouchEnd];
[self _unregisterTouches:touches];
if (AllTouchesAreCancelledOrEnded(event.allTouches)) {
self.state = UIGestureRecognizerStateEnded;
} else if (AnyTouchesChanged(event.allTouches)) {
self.state = UIGestureRecognizerStateChanged;
}
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches]
eventType:RCTTouchEventTypeTouchCancel];
[self _unregisterTouches:touches];
if (AllTouchesAreCancelledOrEnded(event.allTouches)) {
self.state = UIGestureRecognizerStateCancelled;
} else if (AnyTouchesChanged(event.allTouches)) {
self.state = UIGestureRecognizerStateChanged;
}
}
- (void)reset
{
[super reset];
if (_activeTouches.size() != 0) {
std::vector<ActiveTouch> activeTouches;
activeTouches.reserve(_activeTouches.size());
for (auto const &pair : _activeTouches) {
activeTouches.push_back(pair.second);
}
[self _dispatchActiveTouches:activeTouches
eventType:RCTTouchEventTypeTouchCancel];
// Force-unregistering all the touches.
_activeTouches.clear();
_identifierPool.reset();
}
}
- (BOOL)canPreventGestureRecognizer:(__unused UIGestureRecognizer *)preventedGestureRecognizer
{
return NO;
}
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
// We fail in favour of other external gesture recognizers.
// iOS will ask `delegate`'s opinion about this gesture recognizer little bit later.
return ![preventingGestureRecognizer.view isDescendantOfView:self.view];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(__unused UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Same condition for `failure of` as for `be prevented by`.
return [self canBePreventedByGestureRecognizer:otherGestureRecognizer];
}
@end