Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTAssert.h>
#import <React/RCTConstants.h>
#import <React/RCTScrollEvent.h>
#import <React/RCTSurfaceTouchHandler+Internal.h>
#import <React/RCTUtils.h>

#import <react/featureflags/ReactNativeFeatureFlags.h>
Expand Down Expand Up @@ -620,24 +621,63 @@ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompo

/*
* Returns whether or not the scroll view interaction should be blocked because
* JavaScript was found to be the responder.
* JavaScript was found to be the responder. A scroll view that is the
* responder does not block the scroll views inside it.
*/
- (BOOL)_shouldDisableScrollInteraction
{
UIView *ancestorJSResponder = [self _ancestorJSResponder];
return ancestorJSResponder != nil && ![ancestorJSResponder isKindOfClass:[RCTScrollViewComponentView class]];
}

- (UIView *)_ancestorJSResponder
{
UIView *ancestorView = self.superview;

while (ancestorView) {
if ([ancestorView respondsToSelector:@selector(isJSResponder)]) {
BOOL isJSResponder = ((UIView<RCTComponentViewProtocol> *)ancestorView).isJSResponder;
if (isJSResponder) {
return YES;
return ancestorView;
}
}

ancestorView = ancestorView.superview;
}

return NO;
return nil;
}

/*
* Cancels the touches that began inside this scroll view when a scroll view
* above it is the JavaScript responder, so that the responder is terminated
* when this scroll view takes the gesture. The responder system does not ask
* a view inside the responder whether it wants to take over, so this scroll
* view's `onScrollShouldSetResponder` is not called. The responder reaches
* its view asynchronously: one granted after the gesture began is not
* terminated.
*/
- (void)_cancelTouchesUnderScrollViewJSResponder
{
#if !TARGET_OS_TV
UIView *ancestorView = [self _ancestorJSResponder];
if (![ancestorView isKindOfClass:[RCTScrollViewComponentView class]]) {
return;
}

// Cancel through the nearest touch handler above the responder, which is the one that sent it its touches.
// A modal and an input accessory view attach their own to views outside the surface's view.
while (ancestorView) {
for (UIGestureRecognizer *gestureRecognizer in ancestorView.gestureRecognizers) {
if ([gestureRecognizer isKindOfClass:[RCTSurfaceTouchHandler class]]) {
[(RCTSurfaceTouchHandler *)gestureRecognizer cancelTouchesInView:self];
return;
}
}

ancestorView = ancestorView.superview;
}
#endif
}

- (ScrollViewEventEmitter::Metrics)_scrollViewMetrics
Expand Down Expand Up @@ -845,6 +885,7 @@ - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self _cancelTouchesUnderScrollViewJSResponder];
[self _forceDispatchNextScrollEvent];

// A drag interrupts an animated programmatic scroll without `scrollViewDidEndScrollingAnimation`.
Expand Down Expand Up @@ -1019,6 +1060,13 @@ - (void)_handleFinishedScrolling:(UIScrollView *)scrollView

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view
{
#if !TARGET_OS_TV
// Cancel only for a pinch. UIKit also calls this method for a zoom that a command animates.
UIGestureRecognizerState pinchState = scrollView.pinchGestureRecognizer.state;
if (pinchState == UIGestureRecognizerStateBegan || pinchState == UIGestureRecognizerStateChanged) {
[self _cancelTouchesUnderScrollViewJSResponder];
}
#endif
[self _forceDispatchNextScrollEvent];

if (!_eventEmitter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTSurfacePointerHandler.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTSurfacePointerHandler (Internal)

/*
* Cancels the pointers of the touches that began inside `view`. JavaScript
* receives `pointercancel` for them and no further events for those touches,
* nor, until they have all ended, for a touch that begins inside `view`.
*/
- (void)cancelPointersInView:(UIView *)view;

@end

NS_ASSUME_NONNULL_END
78 changes: 77 additions & 1 deletion packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

#import "RCTSurfacePointerHandler.h"
#import "RCTSurfacePointerHandler+Internal.h"

#import <React/RCTIdentifierPool.h>
#import <React/RCTReactTaggedView.h>
Expand Down Expand Up @@ -442,6 +442,17 @@ @implementation RCTSurfacePointerHandler {
std::unordered_map<__unsafe_unretained UITouch *, ActivePointer, PointerHasher<__unsafe_unretained UITouch *>>
_activePointers;

/*
* The touches whose pointers `cancelPointersInView:` cancelled and that have not ended.
* UIKit keeps reporting them, and their events are dropped.
*/
std::unordered_set<__unsafe_unretained UITouch *, PointerHasher<__unsafe_unretained UITouch *>> _cancelledTouches;

/*
* The view whose touches were cancelled last.
*/
__weak UIView *_cancelledTouchesView;

/*
* We hold the view weakly to prevent a retain cycle.
*/
Expand Down Expand Up @@ -658,6 +669,7 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];

touches = [self _touchesWithoutTouchesBeganInCancelledView:touches];
[self _registerTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeStart];

Expand All @@ -672,6 +684,7 @@ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:NO];
[self _updateTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeMove];

Expand All @@ -682,6 +695,7 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:YES];
[self _updateTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeEnd];
[self _unregisterTouches:touches];
Expand All @@ -697,6 +711,7 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:YES];
[self _updateTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeCancel];
[self _unregisterTouches:touches];
Expand All @@ -712,6 +727,8 @@ - (void)reset
{
[super reset];

_cancelledTouches.clear();

if (!_activePointers.empty()) {
std::vector<ActivePointer> activePointers;
activePointers.reserve(_activePointers.size());
Expand All @@ -733,6 +750,65 @@ - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecog
return NO;
}

- (void)cancelPointersInView:(UIView *)view
{
NSMutableSet<UITouch *> *touches = [NSMutableSet new];
for (const auto &pair : _activePointers) {
if ([pair.second.initialComponentView isDescendantOfView:view]) {
[touches addObject:pair.first];
}
}

if (touches.count > 0) {
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeCancel];
[self _unregisterTouches:touches];
for (UITouch *touch in touches) {
_cancelledTouches.insert(touch);
}
_cancelledTouchesView = view;
}
}

/*
* A touch that begins inside the view whose touches were cancelled, while one of those touches is still down,
* is part of the gesture that the view took. JavaScript is not told about it. Returns the other touches.
*/
- (NSSet<UITouch *> *)_touchesWithoutTouchesBeganInCancelledView:(NSSet<UITouch *> *)touches
{
UIView *cancelledTouchesView = _cancelledTouchesView;
if (_cancelledTouches.empty() || cancelledTouchesView == nil) {
return touches;
}

NSMutableSet<UITouch *> *remainingTouches = [NSMutableSet setWithCapacity:touches.count];
for (UITouch *touch in touches) {
if ([touch.view isDescendantOfView:cancelledTouchesView]) {
_cancelledTouches.insert(touch);
} else {
[remainingTouches addObject:touch];
}
}
return remainingTouches;
}

- (NSSet<UITouch *> *)_touchesWithoutCancelledTouches:(NSSet<UITouch *> *)touches forgettingThem:(BOOL)forgettingThem
{
if (_cancelledTouches.empty()) {
return touches;
}

NSMutableSet<UITouch *> *remainingTouches = [NSMutableSet setWithCapacity:touches.count];
for (UITouch *touch in touches) {
auto iterator = _cancelledTouches.find(touch);
if (iterator == _cancelledTouches.end()) {
[remainingTouches addObject:touch];
} else if (forgettingThem) {
_cancelledTouches.erase(iterator);
}
}
return remainingTouches;
}

#pragma mark - Hover callbacks

- (void)penHovering:(UIHoverGestureRecognizer *)recognizer API_AVAILABLE(ios(13.0))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTSurfaceTouchHandler.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTSurfaceTouchHandler (Internal)

/*
* Cancels the touches that began inside `view`. JavaScript receives
* `touchcancel` for them, and `pointercancel` when W3C pointer events are on,
* and no further events for those touches. Until they have all ended,
* JavaScript receives no events either for a touch that begins inside
* `view`. Touches outside `view` are not affected. A native view that takes
* a gesture away from JavaScript calls this.
*/
- (void)cancelTouchesInView:(UIView *)view;

@end

NS_ASSUME_NONNULL_END
Loading
Loading