From 1f0863fabc8b0b5cbb56477df3f65fb8b522301d Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 19 Dec 2023 11:43:58 -0800 Subject: [PATCH] Node::styleDefinesDimension() -> Node::hasDefiniteLength() (#1526) Summary: X-link: https://github.com/facebook/react-native/pull/41995 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 --- yoga/algorithm/AbsoluteLayout.cpp | 5 ++--- yoga/algorithm/CalculateLayout.cpp | 31 +++++++++++++++--------------- yoga/node/Node.cpp | 17 ---------------- yoga/node/Node.h | 9 ++++++++- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/yoga/algorithm/AbsoluteLayout.cpp b/yoga/algorithm/AbsoluteLayout.cpp index abeab5c160..1da6c290bf 100644 --- a/yoga/algorithm/AbsoluteLayout.cpp +++ b/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/yoga/algorithm/CalculateLayout.cpp b/yoga/algorithm/CalculateLayout.cpp index 9d5c1b58e3..09ef4ea385 100644 --- a/yoga/algorithm/CalculateLayout.cpp +++ b/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/yoga/node/Node.cpp b/yoga/node/Node.cpp index e09c8e8295..1c83840875 100644 --- a/yoga/node/Node.cpp +++ b/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/yoga/node/Node.h b/yoga/node/Node.h index 19a5266894..acd2ec39ce 100644 --- a/yoga/node/Node.h +++ b/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);