Align ReadableMap and MapBuffer measure methods in ViewManager

Summary:
Aligns two codepaths for measure, making sure we can use both MapBuffer and ReadableMap for measuring components.

Changelog: [Internal] - Align measure interface for MapBuffer experiment

Reviewed By: javache, mdvacca

Differential Revision: D34960317

fbshipit-source-id: a39eb84a0abb4414717463f2f1741e470be3531f
This commit is contained in:
Andrei Shikov
2022-03-18 00:36:07 -07:00
committed by Facebook GitHub Bot
parent 5f2835b14d
commit d63bf4add3
5 changed files with 92 additions and 27 deletions
@@ -540,8 +540,9 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
private long measureMapBuffer(
int surfaceId,
String componentName,
ReadableMapBuffer attributedString,
ReadableMapBuffer paragraphAttributes,
ReadableMapBuffer localData,
ReadableMapBuffer props,
@Nullable ReadableMapBuffer state,
float minWidth,
float maxWidth,
float minHeight,
@@ -561,11 +562,12 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
}
// TODO: replace ReadableNativeMap -> ReadableMapBuffer
return mMountingManager.measureTextMapBuffer(
return mMountingManager.measureMapBuffer(
context,
componentName,
attributedString,
paragraphAttributes,
localData,
props,
state,
getYogaSize(minWidth, maxWidth),
getYogaMeasureMode(minWidth, maxWidth),
getYogaSize(minHeight, maxHeight),
@@ -10,7 +10,6 @@ package com.facebook.react.fabric.mounting;
import static com.facebook.infer.annotation.ThreadConfined.ANY;
import static com.facebook.infer.annotation.ThreadConfined.UI;
import android.text.Spannable;
import android.view.View;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
@@ -32,8 +31,6 @@ import com.facebook.react.touch.JSResponderHandler;
import com.facebook.react.uimanager.RootViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewManagerRegistry;
import com.facebook.react.views.text.ReactTextViewManagerCallback;
import com.facebook.react.views.text.TextLayoutManagerMapBuffer;
import com.facebook.yoga.YogaMeasureMode;
import java.util.Map;
import java.util.Queue;
@@ -386,8 +383,8 @@ public class MountingManager {
*
* @param context
* @param componentName
* @param attributedString
* @param paragraphAttributes
* @param localData
* @param props
* @param width
* @param widthMode
* @param height
@@ -396,30 +393,30 @@ public class MountingManager {
* @return
*/
@AnyThread
public long measureTextMapBuffer(
public long measureMapBuffer(
@NonNull ReactContext context,
@NonNull String componentName,
@NonNull ReadableMapBuffer attributedString,
@NonNull ReadableMapBuffer paragraphAttributes,
@NonNull ReadableMapBuffer localData,
@NonNull ReadableMapBuffer props,
@Nullable ReadableMapBuffer state,
float width,
@NonNull YogaMeasureMode widthMode,
float height,
@NonNull YogaMeasureMode heightMode,
@Nullable float[] attachmentsPositions) {
return TextLayoutManagerMapBuffer.measureText(
context,
attributedString,
paragraphAttributes,
width,
widthMode,
height,
heightMode,
new ReactTextViewManagerCallback() {
@Override
public void onPostProcessSpannable(Spannable text) {}
},
attachmentsPositions);
return mViewManagerRegistry
.get(componentName)
.measure(
context,
localData,
props,
state,
width,
widthMode,
height,
heightMode,
attachmentsPositions);
}
public void initializeViewManager(String componentName) {
@@ -15,6 +15,7 @@ import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.mapbuffer.ReadableMapBuffer;
import com.facebook.react.touch.JSResponderHandler;
import com.facebook.react.touch.ReactInterceptingViewGroup;
import com.facebook.react.uimanager.annotations.ReactProp;
@@ -323,6 +324,47 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
return 0;
}
/**
* THIS MEASURE METHOD IS EXPERIMENTAL, MOST LIKELY YOU ARE LOOKING TO USE THE OTHER OVERLOAD
* INSTEAD: {@link #measure(Context, ReadableMap, ReadableMap, ReadableMap, float,
* YogaMeasureMode, float, YogaMeasureMode, float[])}
*
* <p>Subclasses can override this method to implement custom measure functions for the
* ViewManager
*
* @param context {@link com.facebook.react.bridge.ReactContext} used for the view.
* @param localData {@link ReadableMapBuffer} containing "local data" defined in C++
* @param props {@link ReadableMapBuffer} containing JS props
* @param state {@link ReadableMapBuffer} containing state defined in C++
* @param width width of the view (usually zero)
* @param widthMode widthMode used during calculation of layout
* @param height height of the view (usually zero)
* @param heightMode widthMode used during calculation of layout
* @param attachmentsPositions {@link int[]} array containing 2x times the amount of attachments
* of the view. An attachment represents the position of an inline view that needs to be
* rendered inside a component and it requires the content of the parent view in order to be
* positioned. This array is meant to be used by the platform to RETURN the position of each
* attachment, as a result of the calculation of layout. (e.g. this array is used to measure
* inlineViews that are rendered inside Text components). On most of the components this array
* will be contain a null value.
* <p>Even values will represent the TOP of each attachment, Odd values represent the LEFT of
* each attachment.
* @return result of calculation of layout for the arguments received as a parameter.
*/
public long measure(
Context context,
ReadableMapBuffer localData,
ReadableMapBuffer props,
// TODO(T114731225): review whether state parameter is needed
@Nullable ReadableMapBuffer state,
float width,
YogaMeasureMode widthMode,
float height,
YogaMeasureMode heightMode,
@Nullable float[] attachmentsPositions) {
return 0;
}
/**
* Subclasses can override this method to set padding for the given View in Fabric. Since not all
* components support setting padding, the default implementation of this method does nothing.
@@ -175,7 +175,6 @@ public class ReactTextViewManager
float height,
YogaMeasureMode heightMode,
@Nullable float[] attachmentsPositions) {
return TextLayoutManager.measureText(
context,
localData,
@@ -188,6 +187,29 @@ public class ReactTextViewManager
attachmentsPositions);
}
@Override
public long measure(
Context context,
ReadableMapBuffer localData,
ReadableMapBuffer props,
@Nullable ReadableMapBuffer state,
float width,
YogaMeasureMode widthMode,
float height,
YogaMeasureMode heightMode,
@Nullable float[] attachmentsPositions) {
return TextLayoutManagerMapBuffer.measureText(
context,
localData,
props,
width,
widthMode,
height,
heightMode,
mReactTextViewManagerCallback,
attachmentsPositions);
}
@Override
public void setPadding(ReactTextView view, int left, int top, int right, int bottom) {
view.setPadding(left, top, right, bottom);