From 62f0dee2353b14ce1524dc62de5e1d2f1883a089 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 22 Jan 2021 19:29:00 -0800 Subject: [PATCH] Migrate ScrollEvent to RCTModernEventEmitter Summary: Motivation: perf, simplicity, adhering to new SurfaceMountingManager APIs available to us. Backwards-compatible with events sent through old system or Fabric, to Fabric or non-Fabric Views. Changelog: [Changed][Android] Old Native method to create ScrollEvent has been deprecated and will be removed at some point in the (distant) future Reviewed By: mdvacca Differential Revision: D26027105 fbshipit-source-id: b9dba5b56c2bfed3b8fc4488c54b271b85ab5fa0 --- .../mounting/SurfaceMountingManager.java | 1 + .../react/uimanager/UIManagerHelper.java | 36 +++++++++++++++++++ .../views/scroll/ReactScrollViewHelper.java | 2 ++ .../react/views/scroll/ScrollEvent.java | 12 ++++++- .../textinput/ReactTextInputManager.java | 2 ++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java index 6581e8aeedb..1b73122f8cb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java @@ -43,6 +43,7 @@ import javax.annotation.Nullable; public class SurfaceMountingManager { public static final String TAG = SurfaceMountingManager.class.getSimpleName(); + private static final boolean SHOW_CHANGED_VIEW_HIERARCHIES = ReactBuildConfig.DEBUG && false; private static final long KEEPALIVE_MILLISECONDS = 1000; diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java index ccd5600f268..f68548d86ba 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java @@ -150,6 +150,42 @@ public class UIManagerHelper { return (ReactContext) context; } + /** + * @return Get the ThemedReactContext associated with a View, if possible, and then call + * getSurfaceId on it. See above (getReactContext) for additional context. + */ + public static int getSurfaceId(View view) { + int reactTag = view.getId(); + + // In non-Fabric we don't have (or use) SurfaceId + if (getUIManagerType(reactTag) == UIManagerType.DEFAULT) { + return -1; + } + + Context context = view.getContext(); + if (!(context instanceof ThemedReactContext) && context instanceof ContextWrapper) { + context = ((ContextWrapper) context).getBaseContext(); + } + + int surfaceId = getSurfaceId(context); + + // All Fabric-managed Views (should) have a ThemedReactContext attached. + if (surfaceId == -1) { + ReactSoftException.logSoftException( + "UIManagerHelper", + new IllegalStateException( + "Fabric View [" + reactTag + "] does not have SurfaceId associated with it")); + } + return surfaceId; + } + + public static int getSurfaceId(Context context) { + if (context instanceof ThemedReactContext) { + return ((ThemedReactContext) context).getSurfaceId(); + } + return -1; + } + /** * @return the default padding used by Android EditText's. This method returns the padding in an * array to avoid extra classloading during hot-path of RN Android. diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java index c10a38ebdb0..b46874aaf14 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java @@ -81,9 +81,11 @@ public class ReactScrollViewHelper { } ReactContext reactContext = (ReactContext) scrollView.getContext(); + int surfaceId = UIManagerHelper.getSurfaceId(reactContext); UIManagerHelper.getEventDispatcherForReactTag(reactContext, scrollView.getId()) .dispatchEvent( ScrollEvent.obtain( + surfaceId, scrollView.getId(), scrollEventType, scrollView.getScrollX(), diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.java index 91876fc2e3b..9abc523589d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.java @@ -15,6 +15,7 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.events.Event; import com.facebook.react.uimanager.events.RCTEventEmitter; +import com.facebook.react.uimanager.events.RCTModernEventEmitter; /** A event dispatched from a ScrollView scrolling. */ public class ScrollEvent extends Event { @@ -33,6 +34,7 @@ public class ScrollEvent extends Event { private @Nullable ScrollEventType mScrollEventType; public static ScrollEvent obtain( + int surfaceId, int viewTag, ScrollEventType scrollEventType, int scrollX, @@ -48,6 +50,7 @@ public class ScrollEvent extends Event { event = new ScrollEvent(); } event.init( + surfaceId, viewTag, scrollEventType, scrollX, @@ -69,6 +72,7 @@ public class ScrollEvent extends Event { private ScrollEvent() {} private void init( + int surfaceId, int viewTag, ScrollEventType scrollEventType, int scrollX, @@ -79,7 +83,7 @@ public class ScrollEvent extends Event { int contentHeight, int scrollViewWidth, int scrollViewHeight) { - super.init(viewTag); + super.init(surfaceId, viewTag); mScrollEventType = scrollEventType; mScrollX = scrollX; mScrollY = scrollY; @@ -116,6 +120,12 @@ public class ScrollEvent extends Event { rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData()); } + @Override + public void dispatchV2(RCTModernEventEmitter rctEventEmitter) { + rctEventEmitter.receiveEvent( + getSurfaceId(), getViewTag(), getEventName(), serializeEventData()); + } + private WritableMap serializeEventData() { WritableMap contentInset = Arguments.createMap(); contentInset.putDouble("top", 0); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index d8cd58f3801..3c32ac96017 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -1131,8 +1131,10 @@ public class ReactTextInputManager extends BaseViewManager