2015-02-20 04:10:52 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
|
|
|
|
#import <UIKit/UIGestureRecognizerSubclass.h>
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUIManager.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "UIView+ReactKit.h"
|
|
|
|
|
|
|
|
// TODO: this class behaves a lot like a module, and could be implemented as a
|
|
|
|
// module if we were to assume that modules and RootViews had a 1:1 relationship
|
|
|
|
|
2015-02-27 12:05:35 +00:00
|
|
|
@interface RCTTouchEvent : NSObject
|
|
|
|
|
|
|
|
@property (nonatomic, assign, readonly) NSUInteger id;
|
|
|
|
@property (nonatomic, copy, readonly) NSString *eventName;
|
|
|
|
@property (nonatomic, copy, readonly) NSArray *touches;
|
|
|
|
@property (nonatomic, copy, readonly) NSArray *changedIndexes;
|
|
|
|
@property (nonatomic, assign, readonly) CFTimeInterval originatingTime;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation RCTTouchEvent
|
|
|
|
|
|
|
|
+ (instancetype)touchWithEventName:(NSString *)eventName touches:(NSArray *)touches changedIndexes:(NSArray *)changedIndexes originatingTime:(CFTimeInterval)originatingTime
|
|
|
|
{
|
|
|
|
RCTTouchEvent *touchEvent = [[self alloc] init];
|
|
|
|
touchEvent->_id = [self newID];
|
|
|
|
touchEvent->_eventName = [eventName copy];
|
|
|
|
touchEvent->_touches = [touches copy];
|
|
|
|
touchEvent->_changedIndexes = [changedIndexes copy];
|
|
|
|
touchEvent->_originatingTime = originatingTime;
|
|
|
|
return touchEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSUInteger)newID
|
|
|
|
{
|
|
|
|
static NSUInteger id = 0;
|
|
|
|
return ++id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTTouchHandler
|
|
|
|
{
|
|
|
|
__weak RCTBridge *_bridge;
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Arrays managed in parallel tracking native touch object along with the
|
|
|
|
* native view that was touched, and the react touch data dictionary.
|
|
|
|
* This must be kept track of because `UIKit` destroys the touch targets
|
|
|
|
* if touches are canceled and we have no other way to recover this information.
|
|
|
|
*/
|
|
|
|
NSMutableOrderedSet *_nativeTouches;
|
|
|
|
NSMutableArray *_reactTouches;
|
|
|
|
NSMutableArray *_touchViews;
|
2015-02-27 12:05:35 +00:00
|
|
|
|
|
|
|
BOOL _recordingInteractionTiming;
|
|
|
|
CFTimeInterval _mostRecentEnqueueJS;
|
|
|
|
CADisplayLink *_displayLink;
|
|
|
|
NSMutableArray *_pendingTouches;
|
|
|
|
NSMutableArray *_bridgeInteractionTiming;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if ((self = [super initWithTarget:nil action:NULL])) {
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTAssert(bridge != nil, @"Expect an event dispatcher");
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
_bridge = bridge;
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
_nativeTouches = [[NSMutableOrderedSet alloc] init];
|
|
|
|
_reactTouches = [[NSMutableArray alloc] init];
|
|
|
|
_touchViews = [[NSMutableArray alloc] init];
|
2015-02-27 12:05:35 +00:00
|
|
|
|
|
|
|
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_update:)];
|
|
|
|
_pendingTouches = [[NSMutableArray alloc] init];
|
|
|
|
_bridgeInteractionTiming = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// `cancelsTouchesInView` is needed in order to be used as a top level event delegated recognizer. Otherwise, lower
|
|
|
|
// level components not build using RCT, will fail to recognize gestures.
|
|
|
|
self.cancelsTouchesInView = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-02-27 12:05:35 +00:00
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return _displayLink != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[_displayLink invalidate];
|
|
|
|
_displayLink = nil;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
typedef NS_ENUM(NSInteger, RCTTouchEventType) {
|
|
|
|
RCTTouchEventTypeStart,
|
|
|
|
RCTTouchEventTypeMove,
|
|
|
|
RCTTouchEventTypeEnd,
|
|
|
|
RCTTouchEventTypeCancel
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma mark - Bookkeeping for touch indices
|
|
|
|
|
|
|
|
- (void)_recordNewTouches:(NSSet *)touches
|
|
|
|
{
|
|
|
|
for (UITouch *touch in touches) {
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTAssert(![_nativeTouches containsObject:touch],
|
|
|
|
@"Touch is already recorded. This is a critical bug.");
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Find closest React-managed touchable view
|
|
|
|
UIView *targetView = touch.view;
|
|
|
|
while (targetView) {
|
|
|
|
if (targetView.reactTag && targetView.userInteractionEnabled) { // TODO: implement respondsToTouch: mechanism
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetView = targetView.superview;
|
|
|
|
}
|
2015-02-27 02:08:56 +00:00
|
|
|
|
|
|
|
if (!targetView.reactTag || !targetView.userInteractionEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
// Get new, unique touch id
|
|
|
|
const NSUInteger RCTMaxTouches = 11; // This is the maximum supported by iDevices
|
|
|
|
NSInteger touchID = ([_reactTouches.lastObject[@"target"] integerValue] + 1) % RCTMaxTouches;
|
|
|
|
for (NSDictionary *reactTouch in _reactTouches) {
|
|
|
|
NSInteger usedID = [reactTouch[@"target"] integerValue];
|
|
|
|
if (usedID == touchID) {
|
|
|
|
// ID has already been used, try next value
|
|
|
|
touchID ++;
|
|
|
|
} else if (usedID > touchID) {
|
|
|
|
// If usedID > touchID, touchID must be unique, so we can stop looking
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Create touch
|
|
|
|
NSMutableDictionary *reactTouch = [[NSMutableDictionary alloc] initWithCapacity:9];
|
|
|
|
reactTouch[@"target"] = [targetView reactTagAtPoint:[touch locationInView:targetView]];
|
|
|
|
reactTouch[@"identifier"] = @(touchID);
|
|
|
|
reactTouch[@"touches"] = [NSNull null]; // We hijack this touchObj to serve both as an event
|
|
|
|
reactTouch[@"changedTouches"] = [NSNull null]; // and as a Touch object, so making this JIT friendly.
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Add to arrays
|
|
|
|
[_touchViews addObject:targetView];
|
|
|
|
[_nativeTouches addObject:touch];
|
|
|
|
[_reactTouches addObject:reactTouch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_recordRemovedTouches:(NSSet *)touches
|
|
|
|
{
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
NSUInteger index = [_nativeTouches indexOfObject:touch];
|
2015-02-27 02:08:56 +00:00
|
|
|
if(index == NSNotFound) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_touchViews removeObjectAtIndex:index];
|
|
|
|
[_nativeTouches removeObjectAtIndex:index];
|
|
|
|
[_reactTouches removeObjectAtIndex:index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_updateReactTouchAtIndex:(NSInteger)touchIndex
|
|
|
|
{
|
|
|
|
UITouch *nativeTouch = _nativeTouches[touchIndex];
|
|
|
|
CGPoint windowLocation = [nativeTouch locationInView:nativeTouch.window];
|
|
|
|
CGPoint rootViewLocation = [nativeTouch.window convertPoint:windowLocation toView:self.view];
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
UIView *touchView = _touchViews[touchIndex];
|
|
|
|
CGPoint touchViewLocation = [nativeTouch.window convertPoint:windowLocation toView:touchView];
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSMutableDictionary *reactTouch = _reactTouches[touchIndex];
|
|
|
|
reactTouch[@"pageX"] = @(rootViewLocation.x);
|
|
|
|
reactTouch[@"pageY"] = @(rootViewLocation.y);
|
|
|
|
reactTouch[@"locationX"] = @(touchViewLocation.x);
|
|
|
|
reactTouch[@"locationY"] = @(touchViewLocation.y);
|
|
|
|
reactTouch[@"timestamp"] = @(nativeTouch.timestamp * 1000); // in ms, for JS
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:06:57 +00:00
|
|
|
+ (NSArray *)JSMethods
|
|
|
|
{
|
|
|
|
return @[@"RCTEventEmitter.receiveTouches"];
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Constructs information about touch events to send across the serialized
|
|
|
|
* boundary. This data should be compliant with W3C `Touch` objects. This data
|
|
|
|
* alone isn't sufficient to construct W3C `Event` objects. To construct that,
|
|
|
|
* there must be a simple receiver on the other side of the bridge that
|
|
|
|
* organizes the touch objects into `Event`s.
|
|
|
|
*
|
|
|
|
* We send the data as an array of `Touch`es, the type of action
|
|
|
|
* (start/end/move/cancel) and the indices that represent "changed" `Touch`es
|
|
|
|
* from that array.
|
|
|
|
*/
|
2015-02-27 12:05:35 +00:00
|
|
|
- (void)_updateAndDispatchTouches:(NSSet *)touches eventName:(NSString *)eventName originatingTime:(CFTimeInterval)originatingTime
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
// Update touches
|
2015-02-27 12:05:35 +00:00
|
|
|
CFTimeInterval enqueueTime = CACurrentMediaTime();
|
2015-02-20 04:10:52 +00:00
|
|
|
NSMutableArray *changedIndexes = [[NSMutableArray alloc] init];
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
NSInteger index = [_nativeTouches indexOfObject:touch];
|
2015-02-27 02:08:56 +00:00
|
|
|
if (index == NSNotFound) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[self _updateReactTouchAtIndex:index];
|
|
|
|
[changedIndexes addObject:@(index)];
|
|
|
|
}
|
2015-02-27 02:08:56 +00:00
|
|
|
|
|
|
|
if (changedIndexes.count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
// Deep copy the touches because they will be accessed from another thread
|
|
|
|
// TODO: would it be safer to do this in the bridge or executor, rather than trusting caller?
|
|
|
|
NSMutableArray *reactTouches = [[NSMutableArray alloc] initWithCapacity:_reactTouches.count];
|
|
|
|
for (NSDictionary *touch in _reactTouches) {
|
|
|
|
[reactTouches addObject:[touch copy]];
|
|
|
|
}
|
2015-02-27 12:05:35 +00:00
|
|
|
|
|
|
|
RCTTouchEvent *touch = [RCTTouchEvent touchWithEventName:eventName
|
|
|
|
touches:reactTouches
|
|
|
|
changedIndexes:changedIndexes
|
|
|
|
originatingTime:originatingTime];
|
|
|
|
[_pendingTouches addObject:touch];
|
|
|
|
|
|
|
|
if (_recordingInteractionTiming) {
|
|
|
|
[_bridgeInteractionTiming addObject:@{
|
|
|
|
@"timeSeconds": @(touch.originatingTime),
|
|
|
|
@"operation": @"taskOriginated",
|
|
|
|
@"taskID": @(touch.id),
|
|
|
|
}];
|
|
|
|
[_bridgeInteractionTiming addObject:@{
|
|
|
|
@"timeSeconds": @(enqueueTime),
|
|
|
|
@"operation": @"taskEnqueuedPending",
|
|
|
|
@"taskID": @(touch.id),
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_update:(CADisplayLink *)sender
|
|
|
|
{
|
2015-02-20 04:10:52 +00:00
|
|
|
// Dispatch touch event
|
2015-02-27 12:05:35 +00:00
|
|
|
NSUInteger pendingCount = _pendingTouches.count;
|
|
|
|
for (RCTTouchEvent *touch in _pendingTouches) {
|
|
|
|
_mostRecentEnqueueJS = CACurrentMediaTime();
|
|
|
|
[_bridge enqueueJSCall:@"RCTEventEmitter.receiveTouches"
|
|
|
|
args:@[touch.eventName, touch.touches, touch.changedIndexes]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_recordingInteractionTiming) {
|
|
|
|
for (RCTTouchEvent *touch in _pendingTouches) {
|
|
|
|
[_bridgeInteractionTiming addObject:@{
|
|
|
|
@"timeSeconds": @(sender.timestamp),
|
|
|
|
@"operation": @"frameAlignedDispatch",
|
|
|
|
@"taskID": @(touch.id),
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingCount > 0 || sender.timestamp - _mostRecentEnqueueJS < 0.1) {
|
|
|
|
[_bridgeInteractionTiming addObject:@{
|
|
|
|
@"timeSeconds": @(sender.timestamp),
|
|
|
|
@"operation": @"mainThreadDisplayLink",
|
|
|
|
@"taskID": @([RCTTouchEvent newID]),
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[_pendingTouches removeAllObjects];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startOrResetInteractionTiming
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
|
|
|
[_bridgeInteractionTiming removeAllObjects];
|
|
|
|
_recordingInteractionTiming = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)endAndResetInteractionTiming
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
|
|
|
_recordingInteractionTiming = NO;
|
|
|
|
NSArray *_prevInteractionTimingData = _bridgeInteractionTiming;
|
|
|
|
_bridgeInteractionTiming = [[NSMutableArray alloc] init];
|
|
|
|
return @{ @"interactionTiming": _prevInteractionTimingData };
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Gesture Recognizer Delegate Callbacks
|
|
|
|
|
|
|
|
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
[super touchesBegan:touches withEvent:event];
|
|
|
|
self.state = UIGestureRecognizerStateBegan;
|
2015-02-27 12:05:35 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// "start" has to record new touches before extracting the event.
|
|
|
|
// "end"/"cancel" needs to remove the touch *after* extracting the event.
|
|
|
|
[self _recordNewTouches:touches];
|
2015-02-27 12:05:35 +00:00
|
|
|
[self _updateAndDispatchTouches:touches eventName:@"topTouchStart" originatingTime:event.timestamp];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
[super touchesMoved:touches withEvent:event];
|
|
|
|
if (self.state == UIGestureRecognizerStateFailed) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-27 12:05:35 +00:00
|
|
|
[self _updateAndDispatchTouches:touches eventName:@"topTouchMove" originatingTime:event.timestamp];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
[super touchesEnded:touches withEvent:event];
|
2015-02-27 12:05:35 +00:00
|
|
|
[self _updateAndDispatchTouches:touches eventName:@"topTouchEnd" originatingTime:event.timestamp];
|
2015-02-20 04:10:52 +00:00
|
|
|
[self _recordRemovedTouches:touches];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
[super touchesCancelled:touches withEvent:event];
|
2015-02-27 12:05:35 +00:00
|
|
|
[self _updateAndDispatchTouches:touches eventName:@"topTouchCancel" originatingTime:event.timestamp];
|
2015-02-20 04:10:52 +00:00
|
|
|
[self _recordRemovedTouches:touches];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|