Skip to content

Commit

Permalink
Apply pointer entering/leaving tracking to touch-initiated pointer ev…
Browse files Browse the repository at this point in the history
…ents

Summary:
Changelog: [iOS][Internal] - Apply pointer entering/leaving tracking to touch-initiated pointer events

This diff takes the refactoring done in the previous diff in this stack and applies it to the pointer events which are converted from discrete touch events. In addition this adds a check when an ActiveTouch is created to see if the pointer in question was hovering before the touch started or not, which is stored and leveraged once the touch is lifted to determine if we should treat the pointer as leaving the screen entirely or not.

Reviewed By: lunaleaps

Differential Revision: D38762330

fbshipit-source-id: 08452783e01bf5944c57b94a292805b2d69d4384
  • Loading branch information
vincentriemer authored and facebook-github-bot committed Aug 18, 2022
1 parent 4c408de commit 7ccb292
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions React/Fabric/RCTSurfaceTouchHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ typedef NS_ENUM(NSInteger, RCTTouchEventType) {
*/
int button;

/*
* Informs the event system that when the touch is released it should be treated as the
* pointer leaving the screen entirely.
*/
bool shouldLeaveWhenReleased;

/*
* A component view on which the touch was begun.
*/
Expand Down Expand Up @@ -550,10 +556,16 @@ - (void)_registerTouches:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
_primaryTouchPointerId = activeTouch.touch.identifier;
activeTouch.isPrimary = true;
}

activeTouch.button = 0;
}

// If the pointer has not been marked as hovering over views before the touch started, we register
// that the activeTouch should not maintain its hovered state once the pointer has been lifted.
auto currentlyHoveredViews = [_currentlyHoveredViewsPerPointer objectForKey:@(activeTouch.touch.identifier)];
if (currentlyHoveredViews == nil || [currentlyHoveredViews count] == 0) {
activeTouch.shouldLeaveWhenReleased = YES;
}

_activeTouches.emplace(touch, activeTouch);
}
}
Expand Down Expand Up @@ -639,20 +651,39 @@ - (void)_dispatchActiveTouches:(std::vector<ActiveTouch>)activeTouches eventType
// emit w3c pointer events
if (RCTGetDispatchW3CPointerEvents()) {
PointerEvent pointerEvent = CreatePointerEventFromActiveTouch(activeTouch, eventType);
switch (eventType) {
case RCTTouchEventTypeTouchStart:
activeTouch.eventEmitter->onPointerDown(pointerEvent);
break;
case RCTTouchEventTypeTouchMove:
activeTouch.eventEmitter->onPointerMove(pointerEvent);
break;
case RCTTouchEventTypeTouchEnd:
activeTouch.eventEmitter->onPointerUp(pointerEvent);
break;
case RCTTouchEventTypeTouchCancel:
activeTouch.eventEmitter->onPointerCancel(pointerEvent);
break;

UIView *targetView = nil;
bool shouldLeave = (eventType == RCTTouchEventTypeTouchEnd && activeTouch.shouldLeaveWhenReleased) ||
eventType == RCTTouchEventTypeTouchCancel;
if (!shouldLeave) {
CGPoint clientLocation = CGPointMake(pointerEvent.clientPoint.x, pointerEvent.clientPoint.y);
targetView = FindClosestFabricManagedTouchableView([_rootComponentView hitTest:clientLocation withEvent:nil]);
}

PointerHandler handler = ^(NSOrderedSet<RCTReactTaggedView *> *eventPathViews) {
switch (eventType) {
case RCTTouchEventTypeTouchStart:
activeTouch.eventEmitter->onPointerDown(pointerEvent);
break;
case RCTTouchEventTypeTouchMove: {
bool hasMoveEventListeners =
IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMove) ||
IsAnyViewInPathListeningToEvent(eventPathViews, ViewEvents::Offset::PointerMoveCapture);
if (hasMoveEventListeners) {
activeTouch.eventEmitter->onPointerMove(pointerEvent);
}
break;
}
case RCTTouchEventTypeTouchEnd:
activeTouch.eventEmitter->onPointerUp(pointerEvent);
break;
case RCTTouchEventTypeTouchCancel:
activeTouch.eventEmitter->onPointerCancel(pointerEvent);
break;
}
};

[self handleIncomingPointerEvent:pointerEvent onView:targetView withHandler:handler];
}
}

Expand Down

0 comments on commit 7ccb292

Please sign in to comment.