Skip to content

Commit

Permalink
Calculate entered/exited views during handling of all MotionEvents (f…
Browse files Browse the repository at this point in the history
…acebook#36666)

Summary:
Pull Request resolved: facebook#36666

Modify JSPointerDispatcher to dispatch out/over/leave/enter events on handing of any MotionEvent. This functionality is in parity with [how touch events are handled in iOS](https://www.internalfb.com/code/fbsource/[cdfd2520a4d43b3a46104ecbf437bb792460e3f7]/xplat/js/react-native-github/packages/react-native/React/Fabric/RCTSurfacePointerHandler.mm?lines=780).

handleMotionEvent updates the last hit path upon any MotionEvent, so we want to make sure that we handle any out/over/leave/enter dispatches in this method. This diff just extracts the dispatching of out/over/leave/enter from onMove() to handleMotionEvent().

Changelog:
[Internal] - Calculate entered/exited views on all touch/move events

Differential Revision: D44426524

fbshipit-source-id: fb2e7f1c6d1276cff7d09b9450969ad0e667526e
  • Loading branch information
genkikondo authored and facebook-github-bot committed Mar 27, 2023
1 parent ae0d714 commit b16808f
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ public void handleMotionEvent(
activeTargetTag = activeHitPath.get(0).getViewId();
}

handleHitStateDivergence(activeTargetTag, eventState, motionEvent, eventDispatcher);

// Dispatch pointer events from the MotionEvents. When we want to ignore an event, we need to
// exit early so we don't record anything about this MotionEvent.
switch (action) {
Expand Down Expand Up @@ -362,7 +364,7 @@ private static List<ViewTarget> filterByShouldDispatch(
return dispatchableViewTargets;
}

private void dispatchEventForViewTargets(
private static void dispatchEventForViewTargets(
String eventName,
PointerEventState eventState,
MotionEvent motionEvent,
Expand All @@ -375,20 +377,20 @@ private void dispatchEventForViewTargets(
}
}

private boolean qualifiedMove(float[] eventCoordinates, float[] lastEventCoordinates) {
private static boolean qualifiedMove(float[] eventCoordinates, float[] lastEventCoordinates) {
return (Math.abs(lastEventCoordinates[0] - eventCoordinates[0]) > ONMOVE_EPSILON
|| Math.abs(lastEventCoordinates[1] - eventCoordinates[1]) > ONMOVE_EPSILON);
}

private void onMove(
// Determines which views are being entered and exited based on comparison between the previous
// hit path and the current hit path, and dispatches out/over/leave/enter events.
private void handleHitStateDivergence(
int targetTag,
PointerEventState eventState,
MotionEvent motionEvent,
EventDispatcher eventDispatcher) {

int activePointerId = eventState.getActivePointerId();
List<ViewTarget> activeHitPath = eventState.getHitPathByPointerId().get(activePointerId);

List<ViewTarget> lastHitPath =
mLastHitPathByPointerId != null && mLastHitPathByPointerId.containsKey(activePointerId)
? mLastHitPathByPointerId.get(activePointerId)
Expand Down Expand Up @@ -483,6 +485,15 @@ private void onMove(
eventDispatcher);
}
}
}

private void onMove(
int targetTag,
PointerEventState eventState,
MotionEvent motionEvent,
EventDispatcher eventDispatcher) {
int activePointerId = eventState.getActivePointerId();
List<ViewTarget> activeHitPath = eventState.getHitPathByPointerId().get(activePointerId);

boolean listeningToMove =
isAnyoneListeningForBubblingEvent(activeHitPath, EVENT.MOVE, EVENT.MOVE_CAPTURE);
Expand Down Expand Up @@ -551,7 +562,7 @@ private void dispatchCancelEvent(
}
}

private void debugPrintHitPath(List<ViewTarget> hitPath) {
private static void debugPrintHitPath(List<ViewTarget> hitPath) {
StringBuilder builder = new StringBuilder("hitPath: ");
for (ViewTarget viewTarget : hitPath) {
builder.append(String.format("%d, ", viewTarget.getViewId()));
Expand Down

0 comments on commit b16808f

Please sign in to comment.