Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public object YogaNative {

@JvmStatic public external fun jni_YGNodeIsReferenceBaselineJNI(nativePointer: Long): Boolean

@JvmStatic
public external fun jni_YGNodeSetMeasureFuncFollowsMeasureModesJNI(
nativePointer: Long,
followsMeasureModes: Boolean,
)

@JvmStatic
public external fun jni_YGNodeGetMeasureFuncFollowsMeasureModesJNI(nativePointer: Long): Boolean

@JvmStatic public external fun jni_YGNodeRemoveAllChildrenJNI(nativePointer: Long)

@JvmStatic public external fun jni_YGNodeRemoveChildJNI(nativePointer: Long, childPointer: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ public abstract class YogaNode : YogaProps {
*/
public open fun setNodeType(nodeType: YogaNodeType) {}

/**
* Sets whether this node's measure function sizes as [YogaMeasureFunction] describes for each
* [YogaMeasureMode]: [YogaMeasureMode.UNDEFINED] as max-content, [YogaMeasureMode.AT_MOST] as
* fit-content and [YogaMeasureMode.EXACTLY] as stretch-fit. True by default.
*
* When false, Yoga infers nothing about the node from a measurement under other constraints:
* - It reuses a measurement only under the same constraints.
* - For automatic minimum sizing (enabled by clearing
* [YogaErrata.MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO]), the measure function isn't called with
* [YogaMeasureMode.AT_MOST] 0. The node's min-content size is the one set by
* [setMinContentWidth] / [setMinContentHeight], if any; else its padding and border plus what
* its [setMinContentMeasureFunction] function returns, or plus 0 without one.
*
* Set it false for a measure function that sizes itself otherwise, for example one that fills the
* space it is given or picks a layout by it. Has no effect on the layout of a node without a
* measure function. Marks the node dirty when the value changes.
*
* Does nothing by default, so a subclass built against an earlier [YogaNode] keeps working.
*/
public open fun setMeasureFunctionFollowsMeasureModes(followsMeasureModes: Boolean) {}

/** Whether this node's measure function sizes as the measure modes describe. True by default. */
public open val measureFunctionFollowsMeasureModes: Boolean
get() = true

abstract override fun setMinContentMeasureFunction(measureFunction: YogaMeasureFunction?)

abstract override fun setMinContentWidth(minContentWidth: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ public abstract class YogaNodeJNIBase : YogaNode, Cloneable {
YogaNative.jni_YGNodeSetNodeTypeJNI(nativePointer, nodeType.intValue())
}

override fun setMeasureFunctionFollowsMeasureModes(followsMeasureModes: Boolean) {
YogaNative.jni_YGNodeSetMeasureFuncFollowsMeasureModesJNI(nativePointer, followsMeasureModes)
}

override val measureFunctionFollowsMeasureModes: Boolean
get() = YogaNative.jni_YGNodeGetMeasureFuncFollowsMeasureModesJNI(nativePointer)

override fun setMinContentMeasureFunction(measureFunction: YogaMeasureFunction?) {
this.minContentMeasureFunction = measureFunction
YogaNative.jni_YGNodeSetHasMinContentMeasureFuncJNI(nativePointer, measureFunction != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ static jboolean jni_YGNodeIsReferenceBaselineJNI(
YGNodeIsReferenceBaseline(_jlong2YGNodeRef(nativePointer)));
}

static void jni_YGNodeSetMeasureFuncFollowsMeasureModesJNI(
JNIEnv* /*env*/,
jobject /*obj*/,
jlong nativePointer,
jboolean followsMeasureModes) {
YGNodeSetMeasureFuncFollowsMeasureModes(
_jlong2YGNodeRef(nativePointer), static_cast<bool>(followsMeasureModes));
}

static jboolean jni_YGNodeGetMeasureFuncFollowsMeasureModesJNI(
JNIEnv* /*env*/,
jobject /*obj*/,
jlong nativePointer) {
return static_cast<jboolean>(
YGNodeGetMeasureFuncFollowsMeasureModes(_jlong2YGNodeRef(nativePointer)));
}

static void jni_YGNodeRemoveAllChildrenJNI(
JNIEnv* /*env*/,
jobject /*obj*/,
Expand Down Expand Up @@ -880,6 +897,12 @@ static JNINativeMethod methods[] = {
{"jni_YGNodeIsReferenceBaselineJNI",
"(J)Z",
(void*)jni_YGNodeIsReferenceBaselineJNI},
{"jni_YGNodeSetMeasureFuncFollowsMeasureModesJNI",
"(JZ)V",
(void*)jni_YGNodeSetMeasureFuncFollowsMeasureModesJNI},
{"jni_YGNodeGetMeasureFuncFollowsMeasureModesJNI",
"(J)Z",
(void*)jni_YGNodeGetMeasureFuncFollowsMeasureModesJNI},
{"jni_YGNodeRemoveAllChildrenJNI",
"(J)V",
(void*)jni_YGNodeRemoveAllChildrenJNI},
Expand Down
18 changes: 17 additions & 1 deletion packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ bool YGNodeHasMeasureFunc(YGNodeConstRef node) {
return resolveRef(node)->hasMeasureFunc();
}

void YGNodeSetMeasureFuncFollowsMeasureModes(
YGNodeRef nodeRef,
bool measureFuncFollowsMeasureModes) {
const auto node = resolveRef(nodeRef);
if (node->measureFuncFollowsMeasureModes() !=
measureFuncFollowsMeasureModes) {
node->setMeasureFuncFollowsMeasureModes(measureFuncFollowsMeasureModes);
node->markDirtyAndPropagate();
}
}

bool YGNodeGetMeasureFuncFollowsMeasureModes(YGNodeConstRef node) {
return resolveRef(node)->measureFuncFollowsMeasureModes();
}

void YGNodeSetMinContentMeasureFunc(
YGNodeRef node,
YGMinContentMeasureFunc minContentMeasureFunc) {
Expand Down Expand Up @@ -413,5 +428,6 @@ bool YGNodeCanUseCachedMeasurement(
lastComputedHeight,
marginRow,
marginColumn,
resolveRef(config));
resolveRef(config),
/*measureFuncFollowsMeasureModes=*/true);
}
37 changes: 35 additions & 2 deletions packages/react-native/ReactCommon/yoga/yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ YG_EXPORT void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc);
*/
YG_EXPORT bool YGNodeHasMeasureFunc(YGNodeConstRef node);

/**
* Sets whether the node's measure function sizes as `YGMeasureFunc` describes
* for each `YGMeasureMode`: `YGMeasureModeUndefined` as max-content,
* `YGMeasureModeAtMost` as fit-content and `YGMeasureModeExactly` as
* stretch-fit. True by default.
*
* When false, Yoga infers nothing about the node from a measurement under other
* constraints:
* - It reuses a measurement only under the same constraints.
* - For automatic minimum sizing (enabled by clearing
* `YGErrataMinSizeUndefinedInsteadOfAuto`), the measure function isn't
* called with `AtMost 0`. The node's min-content size is the one set by
* `YGNodeSetMinContentWidth` / `YGNodeSetMinContentHeight`, if any; else
* its padding and border plus what its `YGMinContentMeasureFunc` returns,
* or plus 0 without one.
*
* Set it false for a measure function that sizes itself otherwise, for example
* one that fills the space it is given or picks a layout by it. Has no effect
* on the layout of a node without a measure function. Marks the node dirty when
* the value changes.
*/
YG_EXPORT void YGNodeSetMeasureFuncFollowsMeasureModes(
YGNodeRef node,
bool measureFuncFollowsMeasureModes);

/**
* Whether the node's measure function sizes as the measure modes describe.
*/
YG_EXPORT bool YGNodeGetMeasureFuncFollowsMeasureModes(YGNodeConstRef node);

/**
* Optional companion to `YGMeasureFunc` invoked by the CSS Flexbox §4.5
* automatic minimum sizing path when probing this node for its min-content
Expand All @@ -233,7 +263,9 @@ YG_EXPORT bool YGNodeHasMeasureFunc(YGNodeConstRef node);
*
* When unset, the algorithm falls back to the regular `YGMeasureFunc`
* invoked with the same `AtMost 0` constraint — preserving today's
* behavior. Setting this is a perf opt-in for Primitives whose min-content
* behavior — unless the node's measure function doesn't follow the measure
* modes (`YGNodeSetMeasureFuncFollowsMeasureModes`), whose content then
* contributes 0. Setting this is a perf opt-in for Primitives whose min-content
* size is cheap to compute or differs from what the regular measure would
* return. Its results are kept, as a measure function's are, until the node is
* marked dirty.
Expand Down Expand Up @@ -273,7 +305,8 @@ YG_EXPORT bool YGNodeHasMinContentMeasureFunc(YGNodeConstRef node);
* Precedence inside the probe is:
* 1. Static value via `YGNodeSetMinContentWidth/Height` (this API)
* 2. Dynamic callback via `YGNodeSetMinContentMeasureFunc`
* 3. Regular `YGMeasureFunc` with `AtMost 0` (today's default)
* 3. Regular `YGMeasureFunc` with `AtMost 0` (today's default), if it
* follows the measure modes; else 0 for the content
* 4. Container recursion (sum on main axis, max on cross)
*
* Static is preferred for known-constant values because it skips the JNI
Expand Down
10 changes: 9 additions & 1 deletion packages/react-native/ReactCommon/yoga/yoga/algorithm/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ bool canUseCachedMeasurement(
const float lastComputedHeight,
const float marginRow,
const float marginColumn,
const yoga::Config* const config) {
const yoga::Config* const config,
const bool measureFuncFollowsMeasureModes) {
if ((yoga::isDefined(lastComputedHeight) && lastComputedHeight < 0) ||
((yoga::isDefined(lastComputedWidth)) && lastComputedWidth < 0)) {
return false;
Expand Down Expand Up @@ -84,6 +85,13 @@ bool canUseCachedMeasurement(
const bool hasSameHeightSpec = lastHeightMode == heightMode &&
yoga::inexactEquals(effectiveLastHeight, effectiveHeight);

// A measure function that doesn't follow the measure modes may size the node
// differently under any other spec, so nothing is inferred from a measurement
// under another: only the same spec reuses it.
if (!measureFuncFollowsMeasureModes) {
return hasSameWidthSpec && hasSameHeightSpec;
}

const bool widthIsCompatible =
hasSameWidthSpec ||
sizeIsExactAndMatchesOldMeasuredSize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bool canUseCachedMeasurement(
float lastComputedHeight,
float marginRow,
float marginColumn,
const yoga::Config* config);
const yoga::Config* config,
bool measureFuncFollowsMeasureModes);

} // namespace facebook::yoga
Original file line number Diff line number Diff line change
Expand Up @@ -991,19 +991,26 @@ static MinContentSize measureOrWalkMinContentMainSize(
// 2. Dynamic min-content callback if set (for Primitives whose
// min-content depends on state). Otherwise fall back to the regular
// measure function with `AtMost 0`, which text measurers naturally
// answer with longest-word width.
const YGSize size = node->hasMinContentMeasureFunc()
? node->measureMinContent(
wantRow ? 0.0f : YGUndefined,
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
wantRow ? YGUndefined : 0.0f,
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost)
: node->measure(
wantRow ? 0.0f : YGUndefined,
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
wantRow ? YGUndefined : 0.0f,
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost);
layout.minContent[axisIndex] = wantRow ? size.width : size.height;
// answer with longest-word width. A measure function that doesn't
// follow the measure modes isn't called: its answer at `AtMost 0` says
// nothing about its min-content, so its content contributes 0.
if (node->hasMinContentMeasureFunc()) {
const YGSize size = node->measureMinContent(
wantRow ? 0.0f : YGUndefined,
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
wantRow ? YGUndefined : 0.0f,
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost);
layout.minContent[axisIndex] = wantRow ? size.width : size.height;
} else if (node->measureFuncFollowsMeasureModes()) {
const YGSize size = node->measure(
wantRow ? 0.0f : YGUndefined,
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
wantRow ? YGUndefined : 0.0f,
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost);
layout.minContent[axisIndex] = wantRow ? size.width : size.height;
} else {
layout.minContent[axisIndex] = 0.0f;
}
layout.minContentGeneration[axisIndex] = generationCount;
return leafMinContentSize(node, requestedAxis, ownerDirection, ownerWidth);
}
Expand Down Expand Up @@ -1115,7 +1122,10 @@ static MinContentSize computeChildMinContentMainSize(
// * Leaf with measure function: invoke it with `AtMost 0` on the
// requested axis and `Undefined` on the other. Text measure-funcs
// respond with longest-word width; image/collection-like measures
// respond with 0 along their scroll axis.
// respond with 0 along their scroll axis. A measure function that
// doesn't follow the measure modes (see
// `YGNodeSetMeasureFuncFollowsMeasureModes`) isn't invoked: without a
// static or callback min-content, its content contributes 0.
// * Empty leaf: return 0.
// * Container: iterate in-flow children. For each, take its
// min-content along the container's own main axis (sum into
Expand Down Expand Up @@ -2983,6 +2993,8 @@ bool calculateLayoutInternal(
node->style().computeMarginForAxis(FlexDirection::Row, ownerWidth);
const float marginAxisColumn =
node->style().computeMarginForAxis(FlexDirection::Column, ownerWidth);
const bool measureFuncFollowsMeasureModes =
node->measureFuncFollowsMeasureModes();

// First, try to use the layout cache.
if (canUseCachedMeasurement(
Expand All @@ -2998,7 +3010,8 @@ bool calculateLayoutInternal(
layout->cachedLayout.computedHeight,
marginAxisRow,
marginAxisColumn,
node->getConfig()) &&
node->getConfig(),
measureFuncFollowsMeasureModes) &&
isCachedForOwnerSize(
node, layout->cachedLayout, ownerWidth, ownerHeight)) {
cachedResults = &layout->cachedLayout;
Expand All @@ -3018,7 +3031,8 @@ bool calculateLayoutInternal(
layout->cachedMeasurements[i].computedHeight,
marginAxisRow,
marginAxisColumn,
node->getConfig()) &&
node->getConfig(),
measureFuncFollowsMeasureModes) &&
isCachedForOwnerSize(
node, layout->cachedMeasurements[i], ownerWidth, ownerHeight)) {
cachedResults = &layout->cachedMeasurements[i];
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Node::Node(Node&& node) noexcept
isReferenceBaseline_(node.isReferenceBaseline_),
isDirty_(node.isDirty_),
alwaysFormsContainingBlock_(node.alwaysFormsContainingBlock_),
measureFuncFollowsMeasureModes_(node.measureFuncFollowsMeasureModes_),
nodeType_(node.nodeType_),
context_(node.context_),
measureFunc_(node.measureFunc_),
Expand Down
9 changes: 9 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class YG_EXPORT Node : public ::YGNode {
return measureFunc_ != nullptr;
}

bool measureFuncFollowsMeasureModes() const noexcept {
return measureFuncFollowsMeasureModes_;
}

YGSize measure(
float availableWidth,
MeasureMode widthMode,
Expand Down Expand Up @@ -238,6 +242,10 @@ class YG_EXPORT Node : public ::YGNode {

void setMeasureFunc(YGMeasureFunc measureFunc);

void setMeasureFuncFollowsMeasureModes(bool measureFuncFollowsMeasureModes) {
measureFuncFollowsMeasureModes_ = measureFuncFollowsMeasureModes;
}

void setMinContentMeasureFunc(YGMinContentMeasureFunc minContentMeasureFunc) {
minContentMeasureFunc_ = minContentMeasureFunc;
}
Expand Down Expand Up @@ -343,6 +351,7 @@ class YG_EXPORT Node : public ::YGNode {
bool isReferenceBaseline_ : 1 = false;
bool isDirty_ : 1 = true;
bool alwaysFormsContainingBlock_ : 1 = false;
bool measureFuncFollowsMeasureModes_ : 1 = true;
NodeType nodeType_ : bitCount<NodeType>() = NodeType::Default;
void* context_ = nullptr;
YGMeasureFunc measureFunc_ = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -6459,7 +6459,7 @@ template <typename EnumT>
concept facebook::yoga::HasOrdinality = (facebook::yoga::ordinalCount<EnumT>()> 0);
YGLogger facebook::yoga::getDefaultLogger();
bool facebook::yoga::calculateLayoutInternal(facebook::yoga::Node* node, float availableWidth, float availableHeight, facebook::yoga::Direction ownerDirection, facebook::yoga::SizingMode widthSizingMode, facebook::yoga::SizingMode heightSizingMode, float ownerWidth, float ownerHeight, bool performLayout, facebook::yoga::LayoutPassReason reason, facebook::yoga::LayoutData& layoutMarkerData, uint32_t depth, uint32_t generationCount);
bool facebook::yoga::canUseCachedMeasurement(facebook::yoga::SizingMode widthMode, float availableWidth, facebook::yoga::SizingMode heightMode, float availableHeight, facebook::yoga::SizingMode lastWidthMode, float lastAvailableWidth, facebook::yoga::SizingMode lastHeightMode, float lastAvailableHeight, float lastComputedWidth, float lastComputedHeight, float marginRow, float marginColumn, const facebook::yoga::Config* config);
bool facebook::yoga::canUseCachedMeasurement(facebook::yoga::SizingMode widthMode, float availableWidth, facebook::yoga::SizingMode heightMode, float availableHeight, facebook::yoga::SizingMode lastWidthMode, float lastAvailableWidth, facebook::yoga::SizingMode lastHeightMode, float lastAvailableHeight, float lastComputedWidth, float lastComputedHeight, float marginRow, float marginColumn, const facebook::yoga::Config* config, bool measureFuncFollowsMeasureModes);
bool facebook::yoga::configUpdateInvalidatesLayout(const facebook::yoga::Config& oldConfig, const facebook::yoga::Config& newConfig);
bool facebook::yoga::inexactEquals(const facebook::yoga::StyleLength& a, const facebook::yoga::StyleLength& b);
bool facebook::yoga::inexactEquals(const facebook::yoga::StyleSizeLength& a, const facebook::yoga::StyleSizeLength& b);
Expand Down Expand Up @@ -6657,6 +6657,7 @@ class facebook::yoga::Node : public YGNode {
public bool isLayoutDimensionDefined(facebook::yoga::FlexDirection axis);
public bool isNodeFlexible();
public bool isReferenceBaseline() const;
public bool measureFuncFollowsMeasureModes() const noexcept;
public bool removeChild(facebook::yoga::Node* child);
public const facebook::yoga::Config* getConfig() const;
public const facebook::yoga::LayoutResults& getLayout() const;
Expand Down Expand Up @@ -6719,6 +6720,7 @@ class facebook::yoga::Node : public YGNode {
public void setLayoutPosition(float position, facebook::yoga::PhysicalEdge edge);
public void setLineIndex(size_t lineIndex);
public void setMeasureFunc(YGMeasureFunc measureFunc);
public void setMeasureFuncFollowsMeasureModes(bool measureFuncFollowsMeasureModes);
public void setMinContentHeight(facebook::yoga::FloatOptional minContentHeight) noexcept;
public void setMinContentMeasureFunc(YGMinContentMeasureFunc minContentMeasureFunc);
public void setMinContentWidth(facebook::yoga::FloatOptional minContentWidth) noexcept;
Expand Down
Loading
Loading