From 52af3e4695c4e44fef371f6b5a1201216d1cf60e Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 19 Dec 2023 13:38:40 -0800 Subject: [PATCH] Node::styleDefinesDimension() -> Node::hasDefiniteLength() (#41995) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41995 X-link: https://github.com/facebook/yoga/pull/1526 This function has made quite the journey from something that originally made more sense. This renames, refactors, and adds documentation for what it actually does. This should eventually make its way into `yoga::Style` once computed style is moved into that structure. bypass-github-export-checks Reviewed By: joevilches Differential Revision: D52105718 fbshipit-source-id: 6492224dd2e10cef3c5fc6a139323ad189a0925c --- .../yoga/yoga/algorithm/AbsoluteLayout.cpp | 5 ++- .../yoga/yoga/algorithm/CalculateLayout.cpp | 31 ++++++++++--------- .../ReactCommon/yoga/yoga/node/Node.cpp | 17 ---------- .../ReactCommon/yoga/yoga/node/Node.h | 9 +++++- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.cpp index abeab5c1603..1da6c290bf5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.cpp @@ -321,7 +321,7 @@ void layoutAbsoluteChild( auto marginColumn = child->getMarginForAxis(FlexDirection::Column, containingBlockWidth); - if (child->styleDefinesDimension(FlexDirection::Row, containingBlockWidth)) { + if (child->hasDefiniteLength(Dimension::Width, containingBlockWidth)) { childWidth = child->getResolvedDimension(Dimension::Width) .resolve(containingBlockWidth) .unwrap() + @@ -348,8 +348,7 @@ void layoutAbsoluteChild( } } - if (child->styleDefinesDimension( - FlexDirection::Column, containingBlockHeight)) { + if (child->hasDefiniteLength(Dimension::Height, containingBlockHeight)) { childHeight = child->getResolvedDimension(Dimension::Height) .resolve(containingBlockHeight) .unwrap() + diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 9d5c1b58e35..09ef4ea3856 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -103,9 +103,9 @@ static void computeFlexBasisForChild( const FloatOptional resolvedFlexBasis = child->resolveFlexBasisPtr().resolve(mainAxisownerSize); const bool isRowStyleDimDefined = - child->styleDefinesDimension(FlexDirection::Row, ownerWidth); + child->hasDefiniteLength(Dimension::Width, ownerWidth); const bool isColumnStyleDimDefined = - child->styleDefinesDimension(FlexDirection::Column, ownerHeight); + child->hasDefiniteLength(Dimension::Height, ownerHeight); if (resolvedFlexBasis.isDefined() && yoga::isDefined(mainAxisSize)) { if (child->getLayout().computedFlexBasis.isUndefined() || @@ -676,8 +676,8 @@ static float distributeFreeSpaceSecondPass( childCrossSize += marginCross; } else if ( !std::isnan(availableInnerCrossDim) && - !currentLineChild->styleDefinesDimension( - crossAxis, availableInnerCrossDim) && + !currentLineChild->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim) && sizingModeCrossDim == SizingMode::StretchFit && !(isNodeFlexWrap && mainAxisOverflows) && resolveChildAlignment(node, currentLineChild) == Align::Stretch && @@ -686,8 +686,8 @@ static float distributeFreeSpaceSecondPass( currentLineChild->marginTrailingValue(crossAxis).unit() != Unit::Auto) { childCrossSize = availableInnerCrossDim; childCrossSizingMode = SizingMode::StretchFit; - } else if (!currentLineChild->styleDefinesDimension( - crossAxis, availableInnerCrossDim)) { + } else if (!currentLineChild->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim)) { childCrossSize = availableInnerCrossDim; childCrossSizingMode = yoga::isUndefined(childCrossSize) ? SizingMode::MaxContent @@ -723,8 +723,9 @@ static float distributeFreeSpaceSecondPass( &childCrossSizingMode, &childCrossSize); - const bool requiresStretchLayout = !currentLineChild->styleDefinesDimension( - crossAxis, availableInnerCrossDim) && + const bool requiresStretchLayout = + !currentLineChild->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim) && resolveChildAlignment(node, currentLineChild) == Align::Stretch && currentLineChild->getFlexStartMarginValue(crossAxis).unit() != Unit::Auto && @@ -1622,8 +1623,8 @@ static void calculateLayoutImpl( child->marginTrailingValue(crossAxis).unit() != Unit::Auto) { // If the child defines a definite size for its cross axis, there's // no need to stretch. - if (!child->styleDefinesDimension( - crossAxis, availableInnerCrossDim)) { + if (!child->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim)) { float childMainSize = child->getLayout().measuredDimension(dimension(mainAxis)); const auto& childStyle = child->getStyle(); @@ -1735,7 +1736,7 @@ static void calculateLayoutImpl( const float unclampedCrossDim = sizingModeCrossDim == SizingMode::StretchFit ? availableInnerCrossDim + paddingAndBorderAxisCross - : node->styleDefinesDimension(crossAxis, crossAxisownerSize) + : node->hasDefiniteLength(dimension(crossAxis), crossAxisownerSize) ? node->getResolvedDimension(dimension(crossAxis)) .resolve(crossAxisownerSize) .unwrap() @@ -1879,8 +1880,8 @@ static void calculateLayoutImpl( // Remeasure child with the line height as it as been only // measured with the owners height yet. - if (!child->styleDefinesDimension( - crossAxis, availableInnerCrossDim)) { + if (!child->hasDefiniteLength( + dimension(crossAxis), availableInnerCrossDim)) { const float childWidth = isMainAxisRow ? (child->getLayout().measuredDimension( Dimension::Width) + @@ -2427,7 +2428,7 @@ void calculateLayout( float width = YGUndefined; SizingMode widthSizingMode = SizingMode::MaxContent; const auto& style = node->getStyle(); - if (node->styleDefinesDimension(FlexDirection::Row, ownerWidth)) { + if (node->hasDefiniteLength(Dimension::Width, ownerWidth)) { width = (node->getResolvedDimension(dimension(FlexDirection::Row)) .resolve(ownerWidth) @@ -2447,7 +2448,7 @@ void calculateLayout( float height = YGUndefined; SizingMode heightSizingMode = SizingMode::MaxContent; - if (node->styleDefinesDimension(FlexDirection::Column, ownerHeight)) { + if (node->hasDefiniteLength(Dimension::Height, ownerHeight)) { height = (node->getResolvedDimension(dimension(FlexDirection::Column)) .resolve(ownerHeight) diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index e09c8e8295c..1c83840875d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -420,23 +420,6 @@ bool Node::isLayoutDimensionDefined(const FlexDirection axis) { return yoga::isDefined(value) && value >= 0.0f; } -bool Node::styleDefinesDimension( - const FlexDirection axis, - const float ownerSize) { - auto resolvedDimension = getResolvedDimension(dimension(axis)); - if (!resolvedDimension.isDefined()) { - return false; - } - - return !( - resolvedDimension.isAuto() || - (resolvedDimension.unit() == Unit::Point && - resolvedDimension.value().unwrap() < 0.0f) || - (resolvedDimension.unit() == Unit::Percent && - (resolvedDimension.value().unwrap() < 0.0f || - yoga::isUndefined(ownerSize)))); -} - // Setters void Node::setMeasureFunc(YGMeasureFunc measureFunc) { diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index 19a52668942..acd2ec39cea 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -130,7 +130,14 @@ class YG_EXPORT Node : public ::YGNode { bool isLayoutDimensionDefined(const FlexDirection axis); - bool styleDefinesDimension(const FlexDirection axis, const float ownerSize); + /** + * Whether the node has a "definite length" along the given axis. + * https://www.w3.org/TR/css-sizing-3/#definite + */ + inline bool hasDefiniteLength(Dimension dimension, float ownerSize) { + auto usedValue = getResolvedDimension(dimension).resolve(ownerSize); + return usedValue.isDefined() && usedValue.unwrap() >= 0.0f; + } bool hasErrata(Errata errata) const { return config_->hasErrata(errata);