Skip to content

Commit

Permalink
Back out "Refactor conditional event emitting to the C++ layer" (#38973)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38973

Original commit changeset: 6c00c2fcfdfd

Original Phabricator Diff: D47852371

When #38674 and #38959 are combined, they cause Android apps on Fabric to crash. #38959 is the more urgent fix, so backing out #38674.

## Changelog:
[General] [Fixed] - Rolling back change that broke e2e tests until we can figure out why the e2e tests are broken

Reviewed By: NickGerleman

Differential Revision: D48288752

fbshipit-source-id: b52e28936bbebd21bd3b2f49f9a233f295ba6248
  • Loading branch information
rozele authored and facebook-github-bot committed Aug 12, 2023
1 parent 9f7dfcc commit ae88aef
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 202 deletions.
60 changes: 52 additions & 8 deletions packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,28 @@ static void UpdateActivePointerWithUITouch(
activePointer.modifierFlags = uiEvent.modifierFlags;
}

static BOOL IsViewListeningToEvent(RCTReactTaggedView *taggedView, ViewEvents::Offset eventType)
{
UIView *view = taggedView.view;
if (view && [view.class conformsToProtocol:@protocol(RCTComponentViewProtocol)]) {
auto props = ((id<RCTComponentViewProtocol>)view).props;
if (SharedViewProps viewProps = std::dynamic_pointer_cast<ViewProps const>(props)) {
return viewProps->events[eventType];
}
}
return NO;
}

static BOOL IsAnyViewInPathListeningToEvent(NSOrderedSet<RCTReactTaggedView *> *viewPath, ViewEvents::Offset eventType)
{
for (RCTReactTaggedView *taggedView in viewPath) {
if (IsViewListeningToEvent(taggedView, eventType)) {
return YES;
}
}
return NO;
}

/**
* Given an ActivePointer determine if it is still within the same event target tree as
* the one which initiated the pointer gesture.
Expand Down Expand Up @@ -612,7 +634,8 @@ - (void)_dispatchActivePointers:(std::vector<ActivePointer>)activePointers event
{
for (const auto &activePointer : activePointers) {
PointerEvent pointerEvent = CreatePointerEventFromActivePointer(activePointer, eventType, _rootComponentView);
[self handleIncomingPointerEvent:pointerEvent onView:activePointer.componentView];
NSOrderedSet<RCTReactTaggedView *> *eventPathViews = [self handleIncomingPointerEvent:pointerEvent
onView:activePointer.componentView];

SharedTouchEventEmitter eventEmitter = GetTouchEmitterFromView(
activePointer.componentView,
Expand All @@ -625,7 +648,12 @@ - (void)_dispatchActivePointers:(std::vector<ActivePointer>)activePointers event
break;
}
case RCTPointerEventTypeMove: {
eventEmitter->onPointerMove(pointerEvent);
BOOL hasMoveEventListeners =
IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMove) ||
IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMoveCapture);
if (hasMoveEventListeners) {
eventEmitter->onPointerMove(pointerEvent);
}
break;
}
case RCTPointerEventTypeEnd: {
Expand Down Expand Up @@ -764,9 +792,11 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer
PointerEvent event = CreatePointerEventFromIncompleteHoverData(
pointerId, pointerType, clientLocation, screenLocation, offsetLocation, modifierFlags);

[self handleIncomingPointerEvent:event onView:targetView];
NSOrderedSet<RCTReactTaggedView *> *eventPathViews = [self handleIncomingPointerEvent:event onView:targetView];
SharedTouchEventEmitter eventEmitter = GetTouchEmitterFromView(targetView, offsetLocation);
if (eventEmitter != nil) {
BOOL hasMoveEventListeners = IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMove) ||
IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMoveCapture);
if (eventEmitter != nil && hasMoveEventListeners) {
eventEmitter->onPointerMove(event);
}
}
Expand Down Expand Up @@ -801,9 +831,10 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer

// Out
if (prevTargetView != nil && prevTargetTaggedView.tag != targetTaggedView.tag) {
BOOL shouldEmitOutEvent = IsAnyViewInPathListeningToEvent(currentlyHoveredViews, ViewEvents::Offset::PointerOut);
SharedTouchEventEmitter eventEmitter =
GetTouchEmitterFromView(prevTargetView, [_rootComponentView convertPoint:clientLocation toView:prevTargetView]);
if (eventEmitter != nil) {
if (shouldEmitOutEvent && eventEmitter != nil) {
eventEmitter->onPointerOut(event);
}
}
Expand All @@ -816,14 +847,20 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer
// we reverse iterate (now from target to root), actually emitting the events.
NSMutableOrderedSet<UIView *> *viewsToEmitLeaveEventsTo = [NSMutableOrderedSet orderedSet];

BOOL hasParentLeaveListener = NO;
for (RCTReactTaggedView *taggedView in [currentlyHoveredViews reverseObjectEnumerator]) {
UIView *componentView = taggedView.view;

BOOL shouldEmitEvent = componentView != nil;
BOOL shouldEmitEvent = componentView != nil &&
(hasParentLeaveListener || IsViewListeningToEvent(taggedView, ViewEvents::Offset::PointerLeave));

if (shouldEmitEvent && ![eventPathViews containsObject:taggedView]) {
[viewsToEmitLeaveEventsTo addObject:componentView];
}

if (shouldEmitEvent && !hasParentLeaveListener) {
hasParentLeaveListener = YES;
}
}

for (UIView *componentView in [viewsToEmitLeaveEventsTo reverseObjectEnumerator]) {
Expand All @@ -836,9 +873,10 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer

// Over
if (targetView != nil && prevTargetTaggedView.tag != targetTaggedView.tag) {
BOOL shouldEmitOverEvent = IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerOver);
SharedTouchEventEmitter eventEmitter =
GetTouchEmitterFromView(targetView, [_rootComponentView convertPoint:clientLocation toView:targetView]);
if (eventEmitter != nil) {
if (shouldEmitOverEvent && eventEmitter != nil) {
eventEmitter->onPointerOver(event);
}
}
Expand All @@ -850,10 +888,12 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer
// or if one of its parents is listening in case those listeners care about the capturing phase. Adding the ability
// for native to distinguish between capturing listeners and not could be an optimization to further reduce the number
// of events we send to JS
BOOL hasParentEnterListener = NO;
for (RCTReactTaggedView *taggedView in [eventPathViews reverseObjectEnumerator]) {
UIView *componentView = taggedView.view;

BOOL shouldEmitEvent = componentView != nil;
BOOL shouldEmitEvent = componentView != nil &&
(hasParentEnterListener || IsViewListeningToEvent(taggedView, ViewEvents::Offset::PointerEnter));

if (shouldEmitEvent && ![currentlyHoveredViews containsObject:taggedView]) {
SharedTouchEventEmitter eventEmitter =
Expand All @@ -862,6 +902,10 @@ - (void)hovering:(UIHoverGestureRecognizer *)recognizer
eventEmitter->onPointerEnter(event);
}
}

if (shouldEmitEvent && !hasParentEnterListener) {
hasParentEnterListener = YES;
}
}

[_currentlyHoveredViewsPerPointer setObject:eventPathViews forKey:@(pointerId)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ struct ViewEvents {
ClickCapture = 31,
GotPointerCapture = 32,
LostPointerCapture = 33,
PointerDown = 34,
PointerDownCapture = 35,
PointerUp = 36,
PointerUpCapture = 37,
};

constexpr bool operator[](const Offset offset) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,32 +616,18 @@ static inline ViewEvents convertRawProp(
"onClickCapture",
sourceValue[Offset::ClickCapture],
defaultValue[Offset::ClickCapture]);
result[Offset::PointerDown] = convertRawProp(
result[Offset::GotPointerCapture] = convertRawProp(
context,
rawProps,
"onPointerDown",
sourceValue[Offset::PointerDown],
defaultValue[Offset::PointerDown]);
result[Offset::PointerDownCapture] = convertRawProp(
"onGotPointerCapture",
sourceValue[Offset::GotPointerCapture],
defaultValue[Offset::GotPointerCapture]);
result[Offset::LostPointerCapture] = convertRawProp(
context,
rawProps,
"onPointerDownCapture",
sourceValue[Offset::PointerDownCapture],
defaultValue[Offset::PointerDownCapture]);
result[Offset::PointerUp] = convertRawProp(
context,
rawProps,
"onPointerUp",
sourceValue[Offset::PointerUp],
defaultValue[Offset::PointerUp]);
result[Offset::PointerUpCapture] = convertRawProp(
context,
rawProps,
"onPointerUpCapture",
sourceValue[Offset::PointerUpCapture],
defaultValue[Offset::PointerUpCapture]);
// TODO: gotPointerCapture & lostPointerCapture (causes issues with
// RawPropsKey for some reason)
"onLostPointerCapture",
sourceValue[Offset::LostPointerCapture],
defaultValue[Offset::LostPointerCapture]);

// PanResponder callbacks
result[Offset::MoveShouldSetResponder] = convertRawProp(
Expand Down
Loading

0 comments on commit ae88aef

Please sign in to comment.