Skip to content

Commit

Permalink
Support RootView in UIManagerHelper.getSurfaceId
Browse files Browse the repository at this point in the history
Summary:
The `Event.getSurfaceIdForView` method I added recently is actually a duplicate of `UIManagerHelper.getSurfaceId`, except that the latter doesn't support RootViews very well.

Changelog:
[Android][Changed] - Improved UIManagerHelper.getSurfaceId and removed Event.getSurfaceIdForView

Reviewed By: JoshuaGross, mdvacca

Differential Revision: D32102175

fbshipit-source-id: 01741df6b646037a4575e9ca302ea248af9fd6f3
  • Loading branch information
javache authored and facebook-github-bot committed Nov 9, 2021
1 parent 74b91c5 commit 954fc04
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.events.TouchEvent;
import com.facebook.react.uimanager.events.TouchEventCoalescingKeyHelper;
Expand Down Expand Up @@ -75,7 +74,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
mTargetTag = findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.START,
ev,
Expand All @@ -100,7 +99,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.END,
ev,
Expand All @@ -115,7 +114,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
findTargetTagAndSetCoordinates(ev);
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.MOVE,
ev,
Expand All @@ -127,7 +126,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
// New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.START,
ev,
Expand All @@ -139,7 +138,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
// Exactly one of the pointers goes up
eventDispatcher.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.END,
ev,
Expand Down Expand Up @@ -188,7 +187,7 @@ private void dispatchCancelEvent(MotionEvent androidEvent, EventDispatcher event
Assertions.assertNotNull(eventDispatcher)
.dispatchEvent(
TouchEvent.obtain(
Event.getSurfaceIdForView(mRootViewGroup),
UIManagerHelper.getSurfaceId(mRootViewGroup),
mTargetTag,
TouchEventType.CANCEL,
androidEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,18 @@ public static ReactContext getReactContext(View view) {
}

/**
* @return Get the ThemedReactContext associated with a View, if possible, and then call
* getSurfaceId on it. See above (getReactContext) for additional context.
* @return Gets the surfaceId for the {@link ThemedReactContext} associated with a View, if
* possible, and then call getSurfaceId on it. See above (getReactContext) for additional
* context.
* <p>For RootViews, the root's rootViewTag is returned
* <p>Returns -1 for non-Fabric views
*/
public static int getSurfaceId(View view) {
if (view instanceof ReactRoot) {
ReactRoot rootView = (ReactRoot) view;
return rootView.getUIManagerType() == UIManagerType.FABRIC ? rootView.getRootViewTag() : -1;
}

int reactTag = view.getId();

// In non-Fabric we don't have (or use) SurfaceId
Expand All @@ -177,9 +185,8 @@ public static int getSurfaceId(View view) {
}

int surfaceId = getSurfaceId(context);

// All Fabric-managed Views (should) have a ThemedReactContext attached.
if (surfaceId == -1) {
// All Fabric-managed Views (should) have a ThemedReactContext attached.
ReactSoftExceptionLogger.logSoftException(
TAG,
new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

package com.facebook.react.uimanager.events;

import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.SystemClock;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.ReactRoot;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.common.UIManagerType;

/**
Expand Down Expand Up @@ -43,23 +40,6 @@ public abstract class Event<T extends Event> {
private long mTimestampMs;
private int mUniqueID = sUniqueID++;

/**
* This surfaceId should be a valid SurfaceId in Fabric, and should ALWAYS return -1 in
* non-Fabric.
*/
public static int getSurfaceIdForView(@Nullable View view) {
if (view != null
&& view instanceof ReactRoot
&& ((ReactRoot) view).getUIManagerType() == UIManagerType.FABRIC) {
if (view.getContext() instanceof ThemedReactContext) {
ThemedReactContext context = (ThemedReactContext) view.getContext();
return context.getSurfaceId();
}
return ((ReactRoot) view).getRootViewTag();
}
return -1;
}

protected Event() {}

@Deprecated
Expand Down

0 comments on commit 954fc04

Please sign in to comment.