Skip to content
Merged
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 @@ -11,7 +11,8 @@ package com.facebook.yoga

public enum class YogaExperimentalFeature(public val intValue: Int) {
WEB_FLEX_BASIS(0),
FIX_FLEX_BASIS_FIT_CONTENT(1);
FIX_FLEX_BASIS_FIT_CONTENT(1),
MIN_CONTENT_HEIGHT_AT_GIVEN_WIDTH(2);

public fun intValue(): Int = intValue

Expand All @@ -21,6 +22,7 @@ public enum class YogaExperimentalFeature(public val intValue: Int) {
when (value) {
0 -> WEB_FLEX_BASIS
1 -> FIX_FLEX_BASIS_FIT_CONTENT
2 -> MIN_CONTENT_HEIGHT_AT_GIVEN_WIDTH
else -> throw IllegalArgumentException("Unknown enum value: $value")
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ const char* YGExperimentalFeatureToString(const YGExperimentalFeature value) {
return "web-flex-basis";
case YGExperimentalFeatureFixFlexBasisFitContent:
return "fix-flex-basis-fit-content";
case YGExperimentalFeatureMinContentHeightAtGivenWidth:
return "min-content-height-at-given-width";
}
return "unknown";
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/ReactCommon/yoga/yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
YG_ENUM_DECL(
YGExperimentalFeature,
YGExperimentalFeatureWebFlexBasis,
YGExperimentalFeatureFixFlexBasisFitContent)
YGExperimentalFeatureFixFlexBasisFitContent,
YGExperimentalFeatureMinContentHeightAtGivenWidth)

YG_ENUM_DECL(
YGFlexDirection,
Expand Down
17 changes: 10 additions & 7 deletions packages/react-native/ReactCommon/yoga/yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,20 @@ YG_EXPORT bool YGNodeHasMeasureFunc(YGNodeConstRef node);
* size along the main axis.
*
* The signature mirrors `YGMeasureFunc`. The algorithm calls this with
* `AtMost 0` along the probed axis and `Undefined` on the other. A
* `AtMost 0` along the probed axis and `Undefined` on the other. With
* `YGExperimentalFeatureMinContentHeightAtGivenWidth`, it probes a height
* with the node's width as an `AtMost` width and an `Undefined` height. A
* min-content-aware Primitive can return its true minimum (e.g., `Image`
* and `Collection` along the scroll axis can return 0; text returns its
* longest-word width; etc.) without doing a full measure pass.
*
* 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
* 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.
* When unset, the algorithm falls back to the regular `YGMeasureFunc` with
* the same arguments. 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. With
* `YGExperimentalFeatureMinContentHeightAtGivenWidth`, a height result is also
* measured again when the width it was measured at changes.
*/
typedef YGSize (*YGMinContentMeasureFunc)(
YGNodeConstRef node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ struct MinContentSize {
};

// The min-content of a leaf with a measure function: its memoized measurement,
// which leaves out its padding and border, with them added.
// which leaves out its padding and border, with them added. A height measured
// at the leaf's width depends on the owner's width as well.
static MinContentSize leafMinContentSize(
const yoga::Node* const node,
const FlexDirection requestedAxis,
Expand All @@ -927,11 +928,12 @@ static MinContentSize leafMinContentSize(
requestedAxis, leafDirection, ownerWidth) +
node->style().computeFlexEndPaddingAndBorder(
requestedAxis, leafDirection, ownerWidth);
const LayoutResults& layout = node->getLayout();
const Dimension axisDimension = dimension(requestedAxis);
return {
node->getLayout()
.minContent[yoga::to_underlying(dimension(requestedAxis))] +
paddingAndBorder,
node->style().hasPercentPadding()};
layout.minContent[yoga::to_underlying(axisDimension)] + paddingAndBorder,
node->style().hasPercentPadding() ||
layout.minContentResolvesAgainstOwnerWidth(axisDimension)};
}

// The min-content of `node` that reading it gives, without writing to it: a
Expand All @@ -955,31 +957,26 @@ static std::optional<MinContentSize> readMinContentMainSize(
return MinContentSize{staticMin.unwrap(), false};
}

const Dimension axisDimension = dimension(requestedAxis);
if (node->hasMeasureFunc()) {
if (!hasMemoizedMinContent(
node, axisDimension, ownerDirection, generationCount)) {
return std::nullopt;
}
return leafMinContentSize(node, requestedAxis, ownerDirection, ownerWidth);
}

if (node->getChildCount() == 0) {
if (!node->hasMeasureFunc() && node->getChildCount() == 0) {
return MinContentSize{0.0f, false};
}

const Dimension axisDimension = dimension(requestedAxis);
const LayoutResults& layout = node->getLayout();
const auto axisIndex = yoga::to_underlying(axisDimension);
if (hasMemoizedMinContent(
node, axisDimension, ownerDirection, generationCount) &&
(!layout.minContentResolvesAgainstOwnerWidth(axisDimension) ||
yoga::inexactEquals(
if (!hasMemoizedMinContent(
node, axisDimension, ownerDirection, generationCount) ||
(layout.minContentResolvesAgainstOwnerWidth(axisDimension) &&
!yoga::inexactEquals(
layout.minContentOwnerWidth[axisIndex], ownerWidth))) {
return MinContentSize{
layout.minContent[axisIndex],
layout.minContentResolvesAgainstOwnerWidth(axisDimension)};
return std::nullopt;
}
return std::nullopt;
if (node->hasMeasureFunc()) {
return leafMinContentSize(node, requestedAxis, ownerDirection, ownerWidth);
}
return MinContentSize{
layout.minContent[axisIndex],
layout.minContentResolvesAgainstOwnerWidth(axisDimension)};
}

static MinContentSize computeChildMinContentContribution(
Expand All @@ -991,6 +988,52 @@ static MinContentSize computeChildMinContentContribution(
float ownerHeight,
uint32_t generationCount);

// The content-box width of `node` under an owner whose content box is
// `ownerWidth` wide: the width it states, else the owner's less its margins,
// within the minimum and maximum it states. This is its width as a stretched
// item of a column; one that is not stretched may be narrower, which the walk
// does not model.
static float contentWidthUnderOwner(
const yoga::Node* const node,
const Direction ownerDirection,
const float ownerWidth) {
const Style& style = node->style();
const Direction direction = node->resolveDirection(ownerDirection);
const float paddingAndBorder = style.computePaddingAndBorderForDimension(
direction, Dimension::Width, ownerWidth);
const FloatOptional stated =
style.dimension(Dimension::Width).resolve(ownerWidth);
FloatOptional borderBoxWidth{
ownerWidth - style.computeMarginForAxis(FlexDirection::Row, ownerWidth)};
if (stated.isDefined()) {
borderBoxWidth = style.boxSizing() == BoxSizing::ContentBox
? stated + FloatOptional{paddingAndBorder}
: stated;
}
borderBoxWidth = boundAxisWithinMinAndMax(
node,
direction,
FlexDirection::Row,
borderBoxWidth,
ownerWidth,
ownerWidth);
if (borderBoxWidth.isUndefined()) {
// With no width to fill, the box is still no wider than its maximum.
borderBoxWidth = boundAxisWithinMinAndMax(
node,
direction,
FlexDirection::Row,
style.resolvedMaxDimension(
direction, Dimension::Width, ownerWidth, ownerWidth),
ownerWidth,
ownerWidth);
}
if (borderBoxWidth.isUndefined()) {
return YGUndefined;
}
return yoga::maxOrDefined(borderBoxWidth.unwrap() - paddingAndBorder, 0.0f);
}

// Measures or walks `node`, which the tree being laid out owns, and memoizes
// the result.
static MinContentSize measureOrWalkMinContentMainSize(
Expand All @@ -1007,20 +1050,31 @@ static MinContentSize measureOrWalkMinContentMainSize(
if (node->hasMeasureFunc()) {
// 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.
// measure function. A width is probed with `AtMost 0`, which text
// measurers naturally answer with longest-word width. A height is probed
// the same way unless `MinContentHeightAtGivenWidth` is on: a text
// measurer answers `AtMost 0` with no height at all, where a min-content
// height is the height the content takes at the box's width (css-sizing-3
// §2.1), so the leaf is measured at its width with no limit on its height.
const bool heightAtGivenWidth = !wantRow &&
node->getConfig()->isExperimentalFeatureEnabled(
ExperimentalFeature::MinContentHeightAtGivenWidth);
float width = wantRow ? 0.0f : YGUndefined;
if (heightAtGivenWidth) {
width = contentWidthUnderOwner(node, ownerDirection, ownerWidth);
}
const MeasureMode widthMode =
yoga::isDefined(width) ? MeasureMode::AtMost : MeasureMode::Undefined;
const float height = wantRow || heightAtGivenWidth ? YGUndefined : 0.0f;
const MeasureMode heightMode =
yoga::isDefined(height) ? MeasureMode::AtMost : MeasureMode::Undefined;
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);
? node->measureMinContent(width, widthMode, height, heightMode)
: node->measure(width, widthMode, height, heightMode);
layout.minContent[axisIndex] = wantRow ? size.width : size.height;
layout.minContentOwnerWidth[axisIndex] = ownerWidth;
layout.setMinContentResolvesAgainstOwnerWidth(
dimension(requestedAxis), heightAtGivenWidth);
layout.minContentGeneration[axisIndex] = generationCount;
return leafMinContentSize(node, requestedAxis, ownerDirection, ownerWidth);
}
Expand All @@ -1034,6 +1088,20 @@ static MinContentSize measureOrWalkMinContentMainSize(
const bool childrenAddUp = isRow(node->style().flexDirection()) == wantRow &&
(node->style().flexWrap() == Wrap::NoWrap || !wantRow);

// A height measured at a leaf's width needs the width of each box above the
// leaf. A column's children are walked against its content width, which a
// stretched child fills. A row's children share its width by their content,
// which the walk does not know, so they are walked against no width and
// their text is as tall as it is unwrapped, the least it can be.
float childOwnerWidth = ownerWidth;
if (!wantRow &&
node->getConfig()->isExperimentalFeatureEnabled(
ExperimentalFeature::MinContentHeightAtGivenWidth)) {
childOwnerWidth = childrenAddUp
? contentWidthUnderOwner(node, ownerDirection, ownerWidth)
: YGUndefined;
}

float minContent = 0.0f;
size_t itemCount = 0;
bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding();
Expand All @@ -1051,11 +1119,11 @@ static MinContentSize measureOrWalkMinContentMainSize(
i,
requestedAxis,
direction,
ownerWidth,
childOwnerWidth,
ownerHeight,
generationCount);
const float childSize =
child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth);
const float childSize = child.size +
childStyle.computeMarginForAxis(requestedAxis, childOwnerWidth);
minContent = childrenAddUp ? minContent + childSize
: std::max(minContent, childSize);
resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ namespace facebook::yoga {
enum class ExperimentalFeature : uint8_t {
WebFlexBasis = YGExperimentalFeatureWebFlexBasis,
FixFlexBasisFitContent = YGExperimentalFeatureFixFlexBasisFitContent,
MinContentHeightAtGivenWidth = YGExperimentalFeatureMinContentHeightAtGivenWidth,
};

template <>
constexpr int32_t ordinalCount<ExperimentalFeature>() {
return 2;
return 3;
}

constexpr ExperimentalFeature scopedEnum(YGExperimentalFeature unscoped) {
Expand Down
Loading