Skip to content

Commit 62f0dee

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
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
1 parent 708038d commit 62f0dee

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
public class SurfaceMountingManager {
4545
public static final String TAG = SurfaceMountingManager.class.getSimpleName();
46+
4647
private static final boolean SHOW_CHANGED_VIEW_HIERARCHIES = ReactBuildConfig.DEBUG && false;
4748
private static final long KEEPALIVE_MILLISECONDS = 1000;
4849

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java

+36
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,42 @@ public static ReactContext getReactContext(View view) {
150150
return (ReactContext) context;
151151
}
152152

153+
/**
154+
* @return Get the ThemedReactContext associated with a View, if possible, and then call
155+
* getSurfaceId on it. See above (getReactContext) for additional context.
156+
*/
157+
public static int getSurfaceId(View view) {
158+
int reactTag = view.getId();
159+
160+
// In non-Fabric we don't have (or use) SurfaceId
161+
if (getUIManagerType(reactTag) == UIManagerType.DEFAULT) {
162+
return -1;
163+
}
164+
165+
Context context = view.getContext();
166+
if (!(context instanceof ThemedReactContext) && context instanceof ContextWrapper) {
167+
context = ((ContextWrapper) context).getBaseContext();
168+
}
169+
170+
int surfaceId = getSurfaceId(context);
171+
172+
// All Fabric-managed Views (should) have a ThemedReactContext attached.
173+
if (surfaceId == -1) {
174+
ReactSoftException.logSoftException(
175+
"UIManagerHelper",
176+
new IllegalStateException(
177+
"Fabric View [" + reactTag + "] does not have SurfaceId associated with it"));
178+
}
179+
return surfaceId;
180+
}
181+
182+
public static int getSurfaceId(Context context) {
183+
if (context instanceof ThemedReactContext) {
184+
return ((ThemedReactContext) context).getSurfaceId();
185+
}
186+
return -1;
187+
}
188+
153189
/**
154190
* @return the default padding used by Android EditText's. This method returns the padding in an
155191
* array to avoid extra classloading during hot-path of RN Android.

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ private static void emitScrollEvent(
8181
}
8282

8383
ReactContext reactContext = (ReactContext) scrollView.getContext();
84+
int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
8485
UIManagerHelper.getEventDispatcherForReactTag(reactContext, scrollView.getId())
8586
.dispatchEvent(
8687
ScrollEvent.obtain(
88+
surfaceId,
8789
scrollView.getId(),
8890
scrollEventType,
8991
scrollView.getScrollX(),

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.facebook.react.uimanager.PixelUtil;
1616
import com.facebook.react.uimanager.events.Event;
1717
import com.facebook.react.uimanager.events.RCTEventEmitter;
18+
import com.facebook.react.uimanager.events.RCTModernEventEmitter;
1819

1920
/** A event dispatched from a ScrollView scrolling. */
2021
public class ScrollEvent extends Event<ScrollEvent> {
@@ -33,6 +34,7 @@ public class ScrollEvent extends Event<ScrollEvent> {
3334
private @Nullable ScrollEventType mScrollEventType;
3435

3536
public static ScrollEvent obtain(
37+
int surfaceId,
3638
int viewTag,
3739
ScrollEventType scrollEventType,
3840
int scrollX,
@@ -48,6 +50,7 @@ public static ScrollEvent obtain(
4850
event = new ScrollEvent();
4951
}
5052
event.init(
53+
surfaceId,
5154
viewTag,
5255
scrollEventType,
5356
scrollX,
@@ -69,6 +72,7 @@ public void onDispose() {
6972
private ScrollEvent() {}
7073

7174
private void init(
75+
int surfaceId,
7276
int viewTag,
7377
ScrollEventType scrollEventType,
7478
int scrollX,
@@ -79,7 +83,7 @@ private void init(
7983
int contentHeight,
8084
int scrollViewWidth,
8185
int scrollViewHeight) {
82-
super.init(viewTag);
86+
super.init(surfaceId, viewTag);
8387
mScrollEventType = scrollEventType;
8488
mScrollX = scrollX;
8589
mScrollY = scrollY;
@@ -116,6 +120,12 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
116120
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
117121
}
118122

123+
@Override
124+
public void dispatchV2(RCTModernEventEmitter rctEventEmitter) {
125+
rctEventEmitter.receiveEvent(
126+
getSurfaceId(), getViewTag(), getEventName(), serializeEventData());
127+
}
128+
119129
private WritableMap serializeEventData() {
120130
WritableMap contentInset = Arguments.createMap();
121131
contentInset.putDouble("top", 0);

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+2
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,10 @@ public ReactScrollWatcher(ReactEditText editText) {
11311131
@Override
11321132
public void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
11331133
if (mPreviousHoriz != horiz || mPreviousVert != vert) {
1134+
int surfaceId = UIManagerHelper.getSurfaceId(mReactEditText);
11341135
ScrollEvent event =
11351136
ScrollEvent.obtain(
1137+
surfaceId,
11361138
mReactEditText.getId(),
11371139
ScrollEventType.SCROLL,
11381140
horiz,

0 commit comments

Comments
 (0)