Skip to content

Commit

Permalink
Node::styleDefinesDimension() -> Node::hasDefiniteLength() (facebook#…
Browse files Browse the repository at this point in the history
…1526)

Summary:
X-link: facebook/react-native#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
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Dec 19, 2023
1 parent 3f8d44b commit 1f0863f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 36 deletions.
5 changes: 2 additions & 3 deletions yoga/algorithm/AbsoluteLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand All @@ -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() +
Expand Down
31 changes: 16 additions & 15 deletions yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ||
Expand Down Expand Up @@ -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 &&
Expand All @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) +
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 0 additions & 17 deletions yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1f0863f

Please sign in to comment.