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
This commit is contained in:
Joshua Gross 2021-01-22 19:29:00 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 708038d80e
Коммит 62f0dee235
5 изменённых файлов: 52 добавлений и 1 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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.

Просмотреть файл

@ -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(),

Просмотреть файл

@ -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<ScrollEvent> {
@ -33,6 +34,7 @@ public class ScrollEvent extends Event<ScrollEvent> {
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<ScrollEvent> {
event = new ScrollEvent();
}
event.init(
surfaceId,
viewTag,
scrollEventType,
scrollX,
@ -69,6 +72,7 @@ public class ScrollEvent extends Event<ScrollEvent> {
private ScrollEvent() {}
private void init(
int surfaceId,
int viewTag,
ScrollEventType scrollEventType,
int scrollX,
@ -79,7 +83,7 @@ public class ScrollEvent extends Event<ScrollEvent> {
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<ScrollEvent> {
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);

Просмотреть файл

@ -1131,8 +1131,10 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
@Override
public void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
if (mPreviousHoriz != horiz || mPreviousVert != vert) {
int surfaceId = UIManagerHelper.getSurfaceId(mReactEditText);
ScrollEvent event =
ScrollEvent.obtain(
surfaceId,
mReactEditText.getId(),
ScrollEventType.SCROLL,
horiz,