diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index db21f29fbff..03032cf0535 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -982,11 +982,11 @@ static std::optional readMinContentMainSize( return std::nullopt; } -static MinContentSize computeChildMinContentMainSize( +static MinContentSize computeChildMinContentContribution( yoga::Node* node, size_t index, - FlexDirection requestedAxis, - Direction ownerDirection, + FlexDirection axis, + Direction direction, float ownerWidth, float ownerHeight, uint32_t generationCount); @@ -1042,7 +1042,7 @@ static MinContentSize measureOrWalkMinContentMainSize( continue; } - const MinContentSize childMain = computeChildMinContentMainSize( + const MinContentSize childMain = computeChildMinContentContribution( node, i, nodeMainAxis, @@ -1050,7 +1050,7 @@ static MinContentSize measureOrWalkMinContentMainSize( ownerWidth, ownerHeight, generationCount); - const MinContentSize childCross = computeChildMinContentMainSize( + const MinContentSize childCross = computeChildMinContentContribution( node, i, nodeCrossAxis, @@ -1118,6 +1118,72 @@ static MinContentSize computeChildMinContentMainSize( generationCount); } +// The size `style` states on `dimension`, as a border-box size. A percentage +// resolves against the size the min-content walk is finding, so it behaves as +// auto there (css-sizing-3 §5.2.1). +static FloatOptional statedSize( + const Style& style, + const Direction direction, + const Dimension dimension, + const float ownerWidth) { + const FloatOptional size = style.dimension(dimension).resolve(YGUndefined); + if (size.isUndefined() || style.boxSizing() == BoxSizing::BorderBox) { + return size; + } + return size + + FloatOptional{style.computePaddingAndBorderForDimension( + direction, dimension, ownerWidth)}; +} + +// The min-content contribution along `axis` of the child of `node` at `index`, +// without its margins (css-sizing-3 §5.2): the size the child states on that +// axis, else its content's min-content size or, where that is larger, the size +// its aspect ratio transfers from the size it states on the other axis +// (css-sizing-4), within the minimum and maximum it states. A child that states +// its size on `axis` is not measured, walked or cloned for it. +static MinContentSize computeChildMinContentContribution( + yoga::Node* const node, + const size_t index, + const FlexDirection axis, + const Direction direction, + const float ownerWidth, + const float ownerHeight, + const uint32_t generationCount) { + // A clone made below has the same style. + const Style& style = node->getChild(index)->style(); + MinContentSize contribution{0.0f, false}; + const FloatOptional stated = + statedSize(style, direction, dimension(axis), ownerWidth); + if (stated.isDefined()) { + contribution = {stated.unwrap(), style.hasPercentPadding()}; + } else { + contribution = computeChildMinContentMainSize( + node, index, axis, direction, ownerWidth, ownerHeight, generationCount); + const FloatOptional aspectRatio = style.aspectRatio(); + const FloatOptional statedAcross = statedSize( + style, + direction, + isRow(axis) ? Dimension::Height : Dimension::Width, + ownerWidth); + if (aspectRatio.isDefined() && statedAcross.isDefined()) { + contribution.size = std::max( + contribution.size, + isRow(axis) ? statedAcross.unwrap() * aspectRatio.unwrap() + : statedAcross.unwrap() / aspectRatio.unwrap()); + } + } + + return { + boundAxis( + node->getChild(index), + axis, + direction, + contribution.size, + YGUndefined, + ownerWidth), + contribution.resolvesAgainstOwnerWidth || style.hasPercentPadding()}; +} + // Returns the min-content size of `node` along `requestedAxis`, used by CSS // Flexbox §4.5 automatic minimum sizing. // @@ -1135,8 +1201,8 @@ static MinContentSize computeChildMinContentMainSize( // respond with 0 along their scroll axis. // * Empty leaf: return 0. // * Container: iterate in-flow children. For each, take its -// min-content along the container's own main axis (sum into -// `mainTotal`) and along its cross axis (max into `crossMax`), +// min-content contribution along the container's own main axis (sum +// into `mainTotal`) and along its cross axis (max into `crossMax`), // plus the child's margins. Add the container's own padding and // border on both ends of each axis. Project onto `requestedAxis`. //