Refactor EventEmitters to take optional surfaceId, migrate TouchEvent

Summary:
Refactor EventEmitters to take an optional SurfaceId that Fabric will use, and non-Fabric will not.

Migrating touches is a good proof-of-concept for how this could be used generally, and as it turns out, TouchEvent's API is more flexible than most other event APIs (because it uses a dictionary to pass data around, so we can just stuff SurfaceId into it - not efficient, but flexible).

All new APIs are backwards-compatible and designed to work with old-style events, with Fabric and non-Fabric. Native Views that migrate to the new API will be backwards-compatible and get an efficiency boost in Fabric.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D26025135

fbshipit-source-id: 5b418951e9d0a3882f2d67398f2aaadac8a3a556
This commit is contained in:
Joshua Gross
2021-01-22 19:29:00 -08:00
committed by Facebook GitHub Bot
parent 2fbbdbb2ce
commit 708038d80e
9 changed files with 116 additions and 32 deletions
@@ -50,6 +50,17 @@ public class JSTouchDispatcher {
mTargetTag = -1;
}
private int getSurfaceId() {
if (mRootViewGroup instanceof ReactRoot) {
return ((ReactRoot) mRootViewGroup).getRootViewTag();
}
if (mRootViewGroup != null && mRootViewGroup.getContext() instanceof ThemedReactContext) {
ThemedReactContext context = (ThemedReactContext) mRootViewGroup.getContext();
return context.getSurfaceId();
}
return -1;
}
/**
* Main catalyst view is responsible for collecting and sending touch events to JS. This method
* reacts for an incoming android native touch events ({@link MotionEvent}) and calls into {@link
@@ -70,9 +81,11 @@ public class JSTouchDispatcher {
// this gesture
mChildIsHandlingNativeGesture = false;
mGestureStartTime = ev.getEventTime();
mTargetTag = findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.START,
ev,
@@ -97,6 +110,7 @@ public class JSTouchDispatcher {
findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.END,
ev,
@@ -111,6 +125,7 @@ public class JSTouchDispatcher {
findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.MOVE,
ev,
@@ -122,6 +137,7 @@ public class JSTouchDispatcher {
// New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.START,
ev,
@@ -133,6 +149,7 @@ public class JSTouchDispatcher {
// Exactly onw of the pointers goes up
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.END,
ev,
@@ -181,6 +198,7 @@ public class JSTouchDispatcher {
Assertions.assertNotNull(eventDispatcher)
.dispatchEvent(
TouchEvent.obtain(
getSurfaceId(),
mTargetTag,
TouchEventType.CANCEL,
androidEvent,