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
2 changes: 2 additions & 0 deletions benchmark/Benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ std::shared_ptr<YGNode> buildNodeFromJson(
for (json::iterator it = nodeState.begin(); it != nodeState.end(); it++) {
if (it.key() == "always-forms-containing-block") {
YGNodeSetAlwaysFormsContainingBlock(node.get(), it.value());
} else if (it.key() == "measure-func-follows-measure-modes") {
YGNodeSetMeasureFuncFollowsMeasureModes(node.get(), it.value());
} else if (it.key() == "measure-funcs") {
std::vector<SerializedMeasureFunc> vec{};
for (auto measureFuncJson : it.value()) {
Expand Down
5 changes: 5 additions & 0 deletions capture/NodeToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ static void serializeTreeImpl(
"always-forms-containing-block",
YGNodeGetAlwaysFormsContainingBlock(node),
YGNodeGetAlwaysFormsContainingBlock(defaultNode.get()));
appendBoolIfNotDefault(
j["node"],
"measure-func-follows-measure-modes",
YGNodeGetMeasureFuncFollowsMeasureModes(node),
YGNodeGetMeasureFuncFollowsMeasureModes(defaultNode.get()));
if (YGNodeHasMeasureFunc(node)) {
auto measureFuncIt = nodesToMeasureFuncs.find(node);
if (measureFuncIt == nodesToMeasureFuncs.end()) {
Expand Down
9 changes: 9 additions & 0 deletions java/com/facebook/yoga/YogaNative.kt
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
25 changes: 25 additions & 0 deletions java/com/facebook/yoga/YogaNode.kt
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
7 changes: 7 additions & 0 deletions java/com/facebook/yoga/YogaNodeJNIBase.kt
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
23 changes: 23 additions & 0 deletions java/jni/YGJNIVanilla.cpp
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
193 changes: 193 additions & 0 deletions java/tests/com/facebook/yoga/YogaNodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,199 @@ class YogaNodeTest {
)
}

@Test
fun testMeasureFunctionFollowsMeasureModesByDefault() {
assertTrue(createNode().measureFunctionFollowsMeasureModes)
}

@Test
fun testClonesKeepMeasureFunctionFollowsMeasureModes() {
val node = createNode()
node.setMeasureFunctionFollowsMeasureModes(false)
assertFalse(node.measureFunctionFollowsMeasureModes)
assertFalse(node.cloneWithoutChildren().measureFunctionFollowsMeasureModes)
assertFalse(node.cloneWithChildren().measureFunctionFollowsMeasureModes)
}

@Test
fun testResetRestoresMeasureFunctionFollowsMeasureModes() {
val node = createNode()
node.setMeasureFunctionFollowsMeasureModes(false)
node.reset()
assertTrue(node.measureFunctionFollowsMeasureModes)
}

@Test
fun testSettingMeasureFunctionFollowsMeasureModesDirtiesOnChange() {
val root = createNode()
val child = createNode()
child.setMeasureFunction(
YogaMeasureFunction { _, _, _, _, _ -> YogaMeasureOutput.make(10f, 10f) },
)
root.addChildAt(child, 0)
root.calculateLayout(100f, 100f)
assertFalse(root.isDirty())

child.setMeasureFunctionFollowsMeasureModes(true)
assertFalse(child.isDirty())
assertFalse(root.isDirty())

child.setMeasureFunctionFollowsMeasureModes(false)
assertTrue(child.isDirty())
assertTrue(root.isDirty())
}

@Test
fun testMeasureFunctionIsNotAskedAgainUnderTighterBoundItFitsByDefault() {
assertEquals(0, measuresAt80WideAfterAtMost100(followsMeasureModes = true))
}

@Test
fun testMeasureFunctionIsAskedAgainUnderTighterBoundWhenNotFollowingMeasureModes() {
assertEquals(2, measuresAt80WideAfterAtMost100(followsMeasureModes = false))
}

@Test
fun testMeasureFunctionIsNotAskedAgainAtExactlyItsMeasuredWidthByDefault() {
assertEquals(0, measuresStretchedTo10AfterAtMost100(followsMeasureModes = true))
}

@Test
fun testMeasureFunctionIsAskedAgainAtExactlyItsMeasuredWidthWhenNotFollowingMeasureModes() {
assertEquals(1, measuresStretchedTo10AfterAtMost100(followsMeasureModes = false))
}

@Test
fun testMeasurementsAreKeptUnderTheSameSpecsWhenNotFollowingMeasureModes() {
var measureCount = 0
val root = createNode()
root.alignItems = YogaAlign.FLEX_START
val child = createNode()
child.setMeasureFunction(
YogaMeasureFunction { _, _, _, _, _ ->
measureCount++
YogaMeasureOutput.make(10f, 10f)
},
)
child.setMeasureFunctionFollowsMeasureModes(false)
root.addChildAt(child, 0)
val sibling = createNode()
sibling.setWidth(10f)
sibling.setHeight(10f)
root.addChildAt(sibling, 1)
root.calculateLayout(100f, YogaConstants.UNDEFINED)
val firstLayoutCount = measureCount

sibling.setHeight(20f)
root.calculateLayout(100f, YogaConstants.UNDEFINED)

assertEquals(firstLayoutCount, measureCount)
assertEquals(30f, root.layoutHeight, 0f)
}

@Test
fun testMinContentProbeAsksMeasureFunctionAtMostZeroByDefault() {
val (probeCount, textWidth) = probesAtMostZeroWhenShrunk(followsMeasureModes = true)
assertTrue(probeCount > 0)
assertEquals(30f, textWidth, 0f)
}

@Test
fun testMinContentProbeSkipsMeasureFunctionWhenNotFollowingMeasureModes() {
val (probeCount, textWidth) = probesAtMostZeroWhenShrunk(followsMeasureModes = false)
assertEquals(0, probeCount)
assertEquals(10f, textWidth, 0f)
}

/**
* How many times a node is measured stretched to exactly 10 wide, after it measured 10 wide at
* most 100 wide, under the same height.
*/
private fun measuresStretchedTo10AfterAtMost100(followsMeasureModes: Boolean): Int {
var measureCount = 0
val root = createNode()
root.alignItems = YogaAlign.FLEX_START
val child = createNode()
child.setMeasureFunction(
YogaMeasureFunction { _, width, widthMode, _, _ ->
measureCount++
val measuredWidth = if (widthMode == YogaMeasureMode.EXACTLY) width else 10f
YogaMeasureOutput.make(measuredWidth, 10f)
},
)
child.setMeasureFunctionFollowsMeasureModes(followsMeasureModes)
root.addChildAt(child, 0)
root.calculateLayout(100f, 100f)
val firstLayoutCount = measureCount
root.alignItems = YogaAlign.STRETCH
root.calculateLayout(10f, 100f)
return measureCount - firstLayoutCount
}

/**
* How many times a node 10 wide is measured laid out at most 80 wide, after it was laid out at
* most 100 wide.
*/
private fun measuresAt80WideAfterAtMost100(followsMeasureModes: Boolean): Int {
var measureCount = 0
val root = createNode()
root.alignItems = YogaAlign.FLEX_START
val child = createNode()
child.setMeasureFunction(
YogaMeasureFunction { _, width, widthMode, _, _ ->
measureCount++
val measuredWidth = if (widthMode == YogaMeasureMode.UNDEFINED) 10f else minOf(10f, width)
YogaMeasureOutput.make(measuredWidth, 10f)
},
)
child.setMeasureFunctionFollowsMeasureModes(followsMeasureModes)
root.addChildAt(child, 0)
root.calculateLayout(100f, YogaConstants.UNDEFINED)
val firstLayoutCount = measureCount
root.calculateLayout(80f, YogaConstants.UNDEFINED)
return measureCount - firstLayoutCount
}

/**
* Shrinks a text 90 wide whose longest word is 30 wide into 10, beside a spacer 10 wide, with
* automatic minimum sizing on. Returns how many times its measure function is asked at most 0
* wide, and the width it's laid out at.
*/
private fun probesAtMostZeroWhenShrunk(followsMeasureModes: Boolean): Pair<Int, Float> {
var probeCount = 0
val config = YogaConfigFactory.create()
config.setErrata(YogaErrata.NONE)
val root = createNode(config)
root.flexDirection = YogaFlexDirection.ROW
root.setWidth(20f)
root.setHeight(50f)
val text = createNode(config)
text.setMeasureFunction(
YogaMeasureFunction { _, width, widthMode, _, _ ->
if (widthMode == YogaMeasureMode.AT_MOST && width == 0f) {
probeCount++
}
val measuredWidth =
when (widthMode) {
YogaMeasureMode.EXACTLY -> width
YogaMeasureMode.AT_MOST -> minOf(90f, maxOf(30f, width))
else -> 90f
}
YogaMeasureOutput.make(measuredWidth, 16f)
},
)
text.setMeasureFunctionFollowsMeasureModes(followsMeasureModes)
text.setFlexBasis(90f)
text.flexShrink = 1f
root.addChildAt(text, 0)
val spacer = createNode(config)
spacer.setWidth(10f)
spacer.flexShrink = 0f
root.addChildAt(spacer, 1)
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED)
return Pair(probeCount, text.layoutWidth)
}

/** No width of its own, so flexing gives it its third. */
private val fillsWidth = YogaMeasureFunction { _, _, _, _, _ -> YogaMeasureOutput.make(0f, 10f) }

Expand Down
17 changes: 17 additions & 0 deletions javascript/src/wrapAssembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type Node = {
getJustifyContent(): Justify;
getGap(gutter: Gutter): Value;
getMargin(edge: Edge): Value;
getMeasureFuncFollowsMeasureModes(): boolean;
getMaxHeight(): Value;
getMaxWidth(): Value;
getMinHeight(): Value;
Expand Down Expand Up @@ -205,6 +206,9 @@ export type Node = {
setMaxWidthStretch(): void;
setDirtiedFunc(dirtiedFunc: DirtiedFunction | null): void;
setMeasureFunc(measureFunc: MeasureFunction | null): void;
setMeasureFuncFollowsMeasureModes(
measureFuncFollowsMeasureModes: boolean,
): void;
setMinHeight(
minHeight:
| number
Expand Down Expand Up @@ -884,6 +888,10 @@ export default function wrapAssembly(lib: any): Yoga {
return !!lib._YGNodeIsReferenceBaseline(this._ptr);
}

getMeasureFuncFollowsMeasureModes(): boolean {
return !!lib._YGNodeGetMeasureFuncFollowsMeasureModes(this._ptr);
}

// --- Measure / Dirtied ---
setMeasureFunc(measureFunc: MeasureFunction | null): void {
if (measureFunc) {
Expand All @@ -899,6 +907,15 @@ export default function wrapAssembly(lib: any): Yoga {
lib._jswrap_YGNodeUnsetMeasureFunc(this._ptr);
}

setMeasureFuncFollowsMeasureModes(
measureFuncFollowsMeasureModes: boolean,
): void {
lib._YGNodeSetMeasureFuncFollowsMeasureModes(
this._ptr,
measureFuncFollowsMeasureModes ? 1 : 0,
);
}

setDirtiedFunc(dirtiedFunc: DirtiedFunction | null): void {
if (dirtiedFunc) {
const nodeWeakRef = new WeakRef(this);
Expand Down
Loading
Loading