Skip to content
Open
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
118 changes: 118 additions & 0 deletions packages/react-native/React/Fabric/RCTCancelledTouches+Internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.
*/

#pragma once

#import <UIKit/UIKit.h>

#import <cstddef>
#import <unordered_set>

namespace facebook::react {

/*
* The touches that a touch or pointer handler cancelled for JavaScript and that have not ended.
* UIKit keeps reporting them. The handler passes the touches of each event through this class,
* which returns the ones to report to JavaScript.
*/
class RCTCancelledTouches {
public:
/*
* Remembers `touches`, which began inside `view`, as cancelled.
*/
void add(NSSet<UITouch *> *touches, UIView *view)
{
for (UITouch *touch in touches) {
touches_.insert(touch);
}
view_ = view;
}

/*
* Returns the touches of a `touchesBegan:` to report. A touch that begins inside the view whose
* touches were cancelled last, while a remembered touch is still down, is part of the gesture
* that the view took: it is remembered too and not returned.
*/
NSSet<UITouch *> *touchesToReportBegan(NSSet<UITouch *> *touches)
{
UIView *view = view_;
if (touches_.empty() || view == nil) {
return touches;
}

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

/*
* Returns the touches of a `touchesMoved:` to report: those that are not remembered.
*/
NSSet<UITouch *> *touchesToReportMoved(NSSet<UITouch *> *touches)
{
return touchesNotCancelled(touches, false);
}

/*
* Returns the touches of a `touchesEnded:` or a `touchesCancelled:` to report: those that are
* not remembered. Forgets the remembered ones, which UIKit reports for the last time.
*/
NSSet<UITouch *> *touchesToReportEndedOrCancelled(NSSet<UITouch *> *touches)
{
return touchesNotCancelled(touches, true);
}

/*
* Forgets the remembered touches. The view is kept: it matters only while a touch is remembered.
*/
void clear()
{
touches_.clear();
}

private:
// `std::hash` cannot hash an `__unsafe_unretained` pointer
struct TouchHasher {
std::size_t operator()(__unsafe_unretained UITouch *touch) const
{
return reinterpret_cast<std::size_t>(touch);
}
};

NSSet<UITouch *> *touchesNotCancelled(NSSet<UITouch *> *touches, bool forgetCancelledTouches)
{
if (touches_.empty()) {
return touches;
}

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

std::unordered_set<__unsafe_unretained UITouch *, TouchHasher> touches_;

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

} // namespace facebook::react
64 changes: 8 additions & 56 deletions packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <React/RCTUtils.h>
#import <React/RCTViewComponentView.h>

#import "RCTCancelledTouches+Internal.h"
#import "RCTConversions.h"
#import "RCTTouchableComponentViewProtocol.h"

Expand Down Expand Up @@ -443,15 +444,9 @@ @implementation RCTSurfacePointerHandler {
_activePointers;

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

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

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

touches = [self _touchesWithoutTouchesBeganInCancelledView:touches];
touches = _cancelledTouches.touchesToReportBegan(touches);
[self _registerTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeStart];

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

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:NO];
touches = _cancelledTouches.touchesToReportMoved(touches);
[self _updateTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeMove];

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

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

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:YES];
touches = _cancelledTouches.touchesToReportEndedOrCancelled(touches);
[self _updateTouches:touches withEvent:event];
[self _dispatchActivePointers:[self _activePointersFromTouches:touches] eventType:RCTPointerEventTypeCancel];
[self _unregisterTouches:touches];
Expand Down Expand Up @@ -762,51 +757,8 @@ - (void)cancelPointersInView:(UIView *)view
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);
}
_cancelledTouches.add(touches, view);
}
return remainingTouches;
}

#pragma mark - Hover callbacks
Expand Down
64 changes: 8 additions & 56 deletions packages/react-native/React/Fabric/RCTSurfaceTouchHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <React/RCTUtils.h>
#import <React/RCTViewComponentView.h>

#import "RCTCancelledTouches+Internal.h"
#import "RCTConversions.h"
#import "RCTSurfacePointerHandler+Internal.h"
#import "RCTTouchableComponentViewProtocol.h"
Expand Down Expand Up @@ -138,15 +139,9 @@ @implementation RCTSurfaceTouchHandler {
_activeTouches;

/*
* The touches that `cancelTouchesInView:` cancelled and that have not ended.
* UIKit keeps reporting them, and their events are dropped.
* The touches withheld from JavaScript after `cancelTouchesInView:`.
*/
std::unordered_set<__unsafe_unretained UITouch *, PointerHasher<__unsafe_unretained UITouch *>> _cancelledTouches;

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

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

touches = [self _touchesWithoutTouchesBeganInCancelledView:touches];
touches = _cancelledTouches.touchesToReportBegan(touches);
[self _registerTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches] eventType:RCTTouchEventTypeTouchStart];

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

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:NO];
touches = _cancelledTouches.touchesToReportMoved(touches);
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches] eventType:RCTTouchEventTypeTouchMove];

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

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:YES];
touches = _cancelledTouches.touchesToReportEndedOrCancelled(touches);
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches] eventType:RCTTouchEventTypeTouchEnd];
[self _unregisterTouches:touches];
Expand All @@ -361,7 +356,7 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];

touches = [self _touchesWithoutCancelledTouches:touches forgettingThem:YES];
touches = _cancelledTouches.touchesToReportEndedOrCancelled(touches);
[self _updateTouches:touches];
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches] eventType:RCTTouchEventTypeTouchCancel];
[self _unregisterTouches:touches];
Expand Down Expand Up @@ -440,55 +435,12 @@ - (void)cancelTouchesInView:(UIView *)view
if (touches.count > 0) {
[self _dispatchActiveTouches:[self _activeTouchesFromTouches:touches] eventType:RCTTouchEventTypeTouchCancel];
[self _unregisterTouches:touches];
for (UITouch *touch in touches) {
_cancelledTouches.insert(touch);
}
_cancelledTouchesView = view;
_cancelledTouches.add(touches, view);
}

[_pointerHandler cancelPointersInView: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;
}

- (void)_cancelTouches
{
[self setEnabled:NO];
Expand Down
Loading
Loading