diff --git a/enums.py b/enums.py index 83bd85271c..7e2bf19f2d 100755 --- a/enums.py +++ b/enums.py @@ -100,6 +100,11 @@ # default on new configs to preserve the pre-fix geometry. Clear this # bit to opt into the spec-correct single distribution. ("FlexFirstPassUsesRunningTotals", 1 << 4), + # Use min/max-clamped flex bases when distributing positive free + # space, instead of the flex bases of unfrozen items. Set by default + # on new configs to preserve the pre-fix geometry. Clear this bit to + # opt into CSS Flexbox §9.7 grow free-space accounting. + ("FlexGrowFreeSpaceUsesClampedBasis", 1 << 5), # Enable all incorrect behavior (preserve compatibility) ("All", 0x7FFFFFFF), # Enable all errata except for "StretchFlexBasis" (Defaults behavior diff --git a/java/com/facebook/yoga/YogaErrata.kt b/java/com/facebook/yoga/YogaErrata.kt index ff2bcaf1b6..d6cd4b2e6c 100644 --- a/java/com/facebook/yoga/YogaErrata.kt +++ b/java/com/facebook/yoga/YogaErrata.kt @@ -16,6 +16,7 @@ public enum class YogaErrata(public val intValue: Int) { ABSOLUTE_PERCENT_AGAINST_INNER_SIZE(4), MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO(8), FLEX_FIRST_PASS_USES_RUNNING_TOTALS(16), + FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS(32), ALL(2147483647), CLASSIC(2147483646); @@ -31,6 +32,7 @@ public enum class YogaErrata(public val intValue: Int) { 4 -> ABSOLUTE_PERCENT_AGAINST_INNER_SIZE 8 -> MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO 16 -> FLEX_FIRST_PASS_USES_RUNNING_TOTALS + 32 -> FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS 2147483647 -> ALL 2147483646 -> CLASSIC else -> throw IllegalArgumentException("Unknown enum value: $value") diff --git a/javascript/src/generated/YGEnums.ts b/javascript/src/generated/YGEnums.ts index 18f52e86a0..4ecc9b4a07 100644 --- a/javascript/src/generated/YGEnums.ts +++ b/javascript/src/generated/YGEnums.ts @@ -63,6 +63,7 @@ export enum Errata { AbsolutePercentAgainstInnerSize = 4, MinSizeUndefinedInsteadOfAuto = 8, FlexFirstPassUsesRunningTotals = 16, + FlexGrowFreeSpaceUsesClampedBasis = 32, All = 2147483647, Classic = 2147483646, } @@ -192,6 +193,7 @@ const constants = { ERRATA_ABSOLUTE_PERCENT_AGAINST_INNER_SIZE: Errata.AbsolutePercentAgainstInnerSize, ERRATA_MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO: Errata.MinSizeUndefinedInsteadOfAuto, ERRATA_FLEX_FIRST_PASS_USES_RUNNING_TOTALS: Errata.FlexFirstPassUsesRunningTotals, + ERRATA_FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS: Errata.FlexGrowFreeSpaceUsesClampedBasis, ERRATA_ALL: Errata.All, ERRATA_CLASSIC: Errata.Classic, EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: ExperimentalFeature.WebFlexBasis, diff --git a/tests/YGConfigTest.cpp b/tests/YGConfigTest.cpp index 05bdb8c123..913e1ac987 100644 --- a/tests/YGConfigTest.cpp +++ b/tests/YGConfigTest.cpp @@ -289,10 +289,12 @@ TEST(YogaTest, config_errata_bitmask_add_remove_operations) { // Default configs carry the errata that preserve legacy geometry // (MinSizeUndefinedInsteadOfAuto for CSS §4.5 auto-min, - // FlexFirstPassUsesRunningTotals for free-space distribution). Clear them - // for this test so we can assert exact equality at the end. + // FlexFirstPassUsesRunningTotals and FlexGrowFreeSpaceUsesClampedBasis for + // free-space distribution). Clear them for this test so we can assert exact + // equality at the end. config->removeErrata(yoga::Errata::MinSizeUndefinedInsteadOfAuto); config->removeErrata(yoga::Errata::FlexFirstPassUsesRunningTotals); + config->removeErrata(yoga::Errata::FlexGrowFreeSpaceUsesClampedBasis); // Initially no errata ASSERT_FALSE(config->hasErrata(yoga::Errata::StretchFlexBasis)); diff --git a/tests/YGFlexGrowMinSizeFreeSpaceTest.cpp b/tests/YGFlexGrowMinSizeFreeSpaceTest.cpp new file mode 100644 index 0000000000..54f73dedba --- /dev/null +++ b/tests/YGFlexGrowMinSizeFreeSpaceTest.cpp @@ -0,0 +1,270 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +class YGFlexGrowMinSizeFreeSpace : public testing::Test { + protected: + YGConfigRef config = YGConfigNew(); + YGNodeRef root = YGNodeNewWithConfig(config); + + void SetUp() override { + YGConfigSetErrata(config, YGErrataNone); + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); + YGNodeStyleSetHeight(root, 10); + } + + void TearDown() override { + YGNodeFreeRecursive(root); + YGConfigFree(config); + } + + YGNodeRef addChild(float grow, float basis = 0) { + YGNodeRef child = YGNodeNewWithConfig(config); + YGNodeStyleSetFlexGrow(child, grow); + YGNodeStyleSetFlexBasis(child, basis); + YGNodeInsertChild(root, child, YGNodeGetChildCount(root)); + return child; + } +}; + +TEST_F(YGFlexGrowMinSizeFreeSpace, min_width_below_proportional_share) { + YGNodeStyleSetWidth(root, 120); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 30); + YGNodeRef second = addChild(2); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, min_width_above_proportional_share) { + YGNodeStyleSetWidth(root, 300); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 200); + YGNodeRef second = addChild(1); + YGNodeRef third = addChild(1); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(third)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, errata_preserves_original_sizes) { + YGConfigSetErrata(config, YGErrataFlexGrowFreeSpaceUsesClampedBasis); + YGNodeStyleSetWidth(root, 300); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 200); + YGNodeRef second = addChild(1); + YGNodeRef third = addChild(1); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(233, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(34, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(33, YGNodeLayoutGetWidth(third)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, errata_enabled_by_default) { + YGConfigRef defaults = YGConfigNew(); + const YGErrata errata = YGConfigGetErrata(defaults); + YGConfigFree(defaults); + EXPECT_NE(errata & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0); + EXPECT_NE(YGErrataAll & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0); + EXPECT_NE(YGErrataClassic & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0); + YGConfigSetErrata(config, errata); + YGNodeStyleSetWidth(root, 120); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 30); + YGNodeRef second = addChild(2); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(second)); + + YGConfigSetErrata( + config, + static_cast( + errata & ~YGErrataFlexGrowFreeSpaceUsesClampedBasis)); + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, max_width_freezes_before_distribution) { + YGNodeStyleSetWidth(root, 300); + YGNodeRef first = addChild(1, 150); + YGNodeStyleSetMaxWidth(first, 50); + YGNodeRef second = addChild(1); + YGNodeStyleSetMinWidth(second, 100); + YGNodeRef third = addChild(1); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(125, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(125, YGNodeLayoutGetWidth(third)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, gap_is_removed_from_free_space) { + YGNodeStyleSetWidth(root, 140); + YGNodeStyleSetGap(root, YGGutterColumn, 20); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 30); + YGNodeRef second = addChild(2); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(60, YGNodeLayoutGetLeft(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, column_uses_unclamped_height) { + YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn); + YGNodeStyleSetWidth(root, 10); + YGNodeStyleSetHeight(root, 120); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinHeight(first, 30); + YGNodeRef second = addChild(2); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetHeight(first)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetHeight(second)); + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetTop(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, wrapping_uses_hypothetical_sizes) { + YGNodeStyleSetWidth(root, 120); + YGNodeStyleSetHeight(root, 20); + YGNodeStyleSetFlexWrap(root, YGWrapWrap); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 30); + YGNodeRef second = addChild(2); + YGNodeStyleSetMinWidth(second, 30); + YGNodeRef third = addChild(1); + YGNodeStyleSetMinWidth(third, 100); + for (YGNodeRef child : {first, second, third}) { + YGNodeStyleSetHeight(child, 10); + } + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third)); + EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(first)); + EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(second)); + EXPECT_FLOAT_EQ(10, YGNodeLayoutGetTop(third)); + + YGConfigSetErrata(config, YGErrataFlexGrowFreeSpaceUsesClampedBasis); + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(70, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third)); + EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(first)); + EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(second)); + EXPECT_FLOAT_EQ(10, YGNodeLayoutGetTop(third)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, zero_grow_uses_hypothetical_size) { + YGNodeStyleSetWidth(root, 200); + YGNodeRef first = addChild(0); + YGNodeStyleSetMinWidth(first, 50); + YGNodeRef second = addChild(1); + YGNodeRef third = addChild(2); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(third)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, nonzero_basis_and_margins) { + YGNodeStyleSetWidth(root, 200); + YGNodeRef first = addChild(1, 20); + YGNodeStyleSetMinWidth(first, 60); + YGNodeStyleSetMargin(first, YGEdgeRight, 20); + YGNodeRef second = addChild(2, 40); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(80, YGNodeLayoutGetLeft(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, mixed_min_max_freezes_only_max_violation) { + YGNodeStyleSetWidth(root, 300); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 160); + YGNodeRef second = addChild(1); + YGNodeStyleSetMaxWidth(second, 100); + + for (YGErrata errata : + {YGErrataNone, YGErrataFlexFirstPassUsesRunningTotals}) { + YGConfigSetErrata(config, errata); + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(second)); + } +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, mixed_violations_cancel) { + YGNodeStyleSetWidth(root, 300); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 180); + YGNodeRef second = addChild(1); + YGNodeStyleSetMaxWidth(second, 120); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(180, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, fractional_grow_after_min_freezes) { + YGConfigSetPointScaleFactor(config, 0); + YGNodeStyleSetWidth(root, 200); + YGNodeRef first = addChild(0.25f); + YGNodeStyleSetMinWidth(first, 100); + YGNodeRef second = addChild(0.25f); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second)); +} + +TEST_F(YGFlexGrowMinSizeFreeSpace, successive_min_violations) { + YGNodeStyleSetWidth(root, 600); + YGNodeRef first = addChild(1); + YGNodeStyleSetMinWidth(first, 200); + YGNodeRef second = addChild(1); + YGNodeStyleSetMinWidth(second, 180); + YGNodeRef third = addChild(1); + YGNodeStyleSetMinWidth(third, 120); + YGNodeRef fourth = addChild(1); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first)); + EXPECT_FLOAT_EQ(180, YGNodeLayoutGetWidth(second)); + EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third)); + EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(fourth)); +} diff --git a/yoga/YGEnums.cpp b/yoga/YGEnums.cpp index 017d6ee45a..30f6a545bb 100644 --- a/yoga/YGEnums.cpp +++ b/yoga/YGEnums.cpp @@ -121,6 +121,8 @@ const char* YGErrataToString(const YGErrata value) { return "min-size-undefined-instead-of-auto"; case YGErrataFlexFirstPassUsesRunningTotals: return "flex-first-pass-uses-running-totals"; + case YGErrataFlexGrowFreeSpaceUsesClampedBasis: + return "flex-grow-free-space-uses-clamped-basis"; case YGErrataAll: return "all"; case YGErrataClassic: diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index c672870023..88cdf18115 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -69,6 +69,7 @@ YG_ENUM_DECL( YGErrataAbsolutePercentAgainstInnerSize = 4, YGErrataMinSizeUndefinedInsteadOfAuto = 8, YGErrataFlexFirstPassUsesRunningTotals = 16, + YGErrataFlexGrowFreeSpaceUsesClampedBasis = 32, YGErrataAll = 2147483647, YGErrataClassic = 2147483646) YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata) diff --git a/yoga/algorithm/CalculateLayout.cpp b/yoga/algorithm/CalculateLayout.cpp index 928b41e785..cb4a6202a3 100644 --- a/yoga/algorithm/CalculateLayout.cpp +++ b/yoga/algorithm/CalculateLayout.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -1138,13 +1139,115 @@ static float boundAxisWithAutoMin( return bounded; } -// It distributes the free space to the flexible items and ensures that the size -// of the flex items abide the min and max constraints. At the end of this -// function the child nodes would have proper size. Prior using this function -// please ensure that distributeFreeSpaceFirstPass is called. +// Resolve growing items using CSS Flexbox §9.7. Undefined sizes are unfrozen; +// once frozen, an item's target size is retained for the layout pass. +static std::vector distributeFreeSpaceForGrow( + const FlexLine& flexLine, + const Direction direction, + const FlexDirection mainAxis, + const float ownerWidth, + const float mainAxisOwnerSize, + const float availableInnerMainDim, + const float availableInnerWidth, + const float availableInnerHeight, + const uint32_t generationCount) { + std::vector mainSizes(flexLine.itemsInFlow.size(), YGUndefined); + float remainingFreeSpace = flexLine.layout.remainingFreeSpace; + size_t unfrozenCount = 0; + + for (size_t i = 0; i < flexLine.itemsInFlow.size(); i++) { + auto child = flexLine.itemsInFlow[i]; + const float flexBasis = child->getLayout().computedFlexBasis.unwrap(); + const float hypotheticalMainSize = boundAxisWithinMinAndMax( + child, + direction, + mainAxis, + child->getLayout().computedFlexBasis, + mainAxisOwnerSize, + ownerWidth) + .unwrap(); + if (child->resolveFlexGrow() == 0 || flexBasis > hypotheticalMainSize) { + mainSizes[i] = hypotheticalMainSize; + } else { + // Line breaking uses hypothetical sizes; unfrozen items grow from their + // flex bases. Margins and gaps are already excluded from free space. + remainingFreeSpace += hypotheticalMainSize - flexBasis; + unfrozenCount++; + } + } + const float initialFreeSpace = remainingFreeSpace; + + while (unfrozenCount > 0) { + float totalFlexGrowFactors = 0; + for (size_t i = 0; i < flexLine.itemsInFlow.size(); i++) { + if (yoga::isUndefined(mainSizes[i])) { + totalFlexGrowFactors += flexLine.itemsInFlow[i]->resolveFlexGrow(); + } + } + const float freeSpace = totalFlexGrowFactors < 1 + ? yoga::minOrDefined( + remainingFreeSpace, initialFreeSpace * totalFlexGrowFactors) + : remainingFreeSpace; + const float growUnit = freeSpace / totalFlexGrowFactors; + float totalViolation = 0; + + for (size_t i = 0; i < flexLine.itemsInFlow.size(); i++) { + if (yoga::isUndefined(mainSizes[i])) { + auto child = flexLine.itemsInFlow[i]; + const float mainSize = child->getLayout().computedFlexBasis.unwrap() + + growUnit * child->resolveFlexGrow(); + totalViolation += boundAxisWithAutoMin( + child, + mainAxis, + direction, + mainSize, + availableInnerMainDim, + mainAxisOwnerSize, + availableInnerWidth, + availableInnerHeight, + generationCount) - + mainSize; + } + } + + for (size_t i = 0; i < flexLine.itemsInFlow.size(); i++) { + if (yoga::isUndefined(mainSizes[i])) { + auto child = flexLine.itemsInFlow[i]; + const float flexBasis = child->getLayout().computedFlexBasis.unwrap(); + const float mainSize = flexBasis + growUnit * child->resolveFlexGrow(); + const float boundedSize = boundAxisWithAutoMin( + child, + mainAxis, + direction, + mainSize, + availableInnerMainDim, + mainAxisOwnerSize, + availableInnerWidth, + availableInnerHeight, + generationCount); + const float violation = boundedSize - mainSize; + // Freeze only violations with the same sign as the total. Freezing + // both min and max violations can exhaust the factors prematurely. + if (yoga::isUndefined(totalViolation) || totalViolation == 0 || + (totalViolation > 0 && violation > 0) || + (totalViolation < 0 && violation < 0)) { + mainSizes[i] = boundedSize; + remainingFreeSpace -= boundedSize - flexBasis; + unfrozenCount--; + } + } + } + } + + return mainSizes; +} + +// Distribute free space and lay out each item. Corrected grow sizes are already +// resolved; the legacy path requires distributeFreeSpaceFirstPass first. static float distributeFreeSpaceSecondPass( FlexLine& flexLine, yoga::Node* const node, + const std::vector& flexGrowMainSizes, const FlexDirection mainAxis, const FlexDirection crossAxis, const Direction direction, @@ -1167,7 +1270,8 @@ static float distributeFreeSpaceSecondPass( const bool isMainAxisRow = isRow(mainAxis); const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap; - for (auto currentLineChild : flexLine.itemsInFlow) { + for (size_t i = 0; i < flexLine.itemsInFlow.size(); i++) { + auto currentLineChild = flexLine.itemsInFlow[i]; childFlexBasis = boundAxisWithinMinAndMax( currentLineChild, direction, @@ -1178,7 +1282,10 @@ static float distributeFreeSpaceSecondPass( .unwrap(); float updatedMainSize = childFlexBasis; - if (yoga::isDefined(flexLine.layout.remainingFreeSpace) && + if (!flexGrowMainSizes.empty()) { + updatedMainSize = flexGrowMainSizes[i]; + } else if ( + yoga::isDefined(flexLine.layout.remainingFreeSpace) && flexLine.layout.remainingFreeSpace < 0) { flexShrinkScaledFactor = -currentLineChild->resolveFlexShrink() * childFlexBasis; @@ -1473,8 +1580,8 @@ static void distributeFreeSpaceFirstPass( flexLine.layout.remainingFreeSpace -= deltaFreeSpace; } -// Do two passes over the flex items to figure out how to distribute the -// remaining space. +// Growing items use the CSS freeze loop unless the legacy errata is enabled. +// Otherwise, do two passes to figure out how to distribute the remaining space. // // The first pass finds the items whose min/max constraints trigger, freezes // them at those sizes, and excludes those sizes from the remaining space. @@ -1484,8 +1591,8 @@ static void distributeFreeSpaceFirstPass( // the first pass. For the other items, it sets their sizes by forcing their // min/max constraints to trigger again. // -// This two pass approach for resolving min/max constraints deviates from the -// spec. The spec +// This legacy two pass approach for resolving min/max constraints deviates from +// the spec. The spec // (https://www.w3.org/TR/CSS-flexbox-1/#resolve-flexible-lengths) describes a // process that needs to be repeated a variable number of times. The algorithm // implemented here won't handle all cases but it was simpler to implement and @@ -1527,23 +1634,39 @@ static void resolveFlexibleLength( childLayout.setComputedAutoMinMainSizeResolved(autoMinResolved); } - // First pass: detect the flex items whose min/max constraints trigger - distributeFreeSpaceFirstPass( - node, - flexLine, - direction, - mainAxis, - ownerWidth, - mainAxisOwnerSize, - availableInnerMainDim, - availableInnerWidth, - availableInnerHeight, - generationCount); + std::vector flexGrowMainSizes; + if (originalFreeSpace > 0 && + !node->hasErrata(Errata::FlexGrowFreeSpaceUsesClampedBasis)) { + flexGrowMainSizes = distributeFreeSpaceForGrow( + flexLine, + direction, + mainAxis, + ownerWidth, + mainAxisOwnerSize, + availableInnerMainDim, + availableInnerWidth, + availableInnerHeight, + generationCount); + } else { + // First pass: detect the flex items whose min/max constraints trigger + distributeFreeSpaceFirstPass( + node, + flexLine, + direction, + mainAxis, + ownerWidth, + mainAxisOwnerSize, + availableInnerMainDim, + availableInnerWidth, + availableInnerHeight, + generationCount); + } - // Second pass: resolve the sizes of the flexible items + // Lay out the flexible items using their resolved main sizes. const float distributedFreeSpace = distributeFreeSpaceSecondPass( flexLine, node, + flexGrowMainSizes, mainAxis, crossAxis, direction, diff --git a/yoga/config/Config.h b/yoga/config/Config.h index 95f63e87a4..219a13531f 100644 --- a/yoga/config/Config.h +++ b/yoga/config/Config.h @@ -88,7 +88,8 @@ class YG_EXPORT Config : public ::YGConfig { uint32_t version_ = 0; ExperimentalFeatureSet experimentalFeatures_{}; Errata errata_ = Errata::MinSizeUndefinedInsteadOfAuto | - Errata::FlexFirstPassUsesRunningTotals; + Errata::FlexFirstPassUsesRunningTotals | + Errata::FlexGrowFreeSpaceUsesClampedBasis; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; }; diff --git a/yoga/enums/Errata.h b/yoga/enums/Errata.h index 09e6f83a3e..2ffae88393 100644 --- a/yoga/enums/Errata.h +++ b/yoga/enums/Errata.h @@ -22,6 +22,7 @@ enum class Errata : uint32_t { AbsolutePercentAgainstInnerSize = YGErrataAbsolutePercentAgainstInnerSize, MinSizeUndefinedInsteadOfAuto = YGErrataMinSizeUndefinedInsteadOfAuto, FlexFirstPassUsesRunningTotals = YGErrataFlexFirstPassUsesRunningTotals, + FlexGrowFreeSpaceUsesClampedBasis = YGErrataFlexGrowFreeSpaceUsesClampedBasis, All = YGErrataAll, Classic = YGErrataClassic, };