yoga::Node::getStyle() to yoga::Node::style() (#1555)

Summary:
X-link: https://github.com/facebook/react-native/pull/42314

Pull Request resolved: https://github.com/facebook/yoga/pull/1555

The next diff moves a bunch of methods to `yoga::Style`. This renames the function to be a tad bit shorter, for more readable callsites. It also makes it more consistent with style property getters.

Changelog: [Internal]

Reviewed By: rozele

Differential Revision: D52803393

fbshipit-source-id: 557df34a9f0fb0ee42ad23b1fda99c1e0eb1d4e3
This commit is contained in:
Nick Gerleman
2024-01-19 11:28:06 -08:00
committed by Facebook GitHub Bot
parent d6a3b71085
commit 35b9b5223e
10 changed files with 121 additions and 122 deletions

View File

@@ -72,7 +72,7 @@ static void justifyAbsoluteChild(
const Direction direction,
const FlexDirection mainAxis,
const float containingBlockWidth) {
const Justify parentJustifyContent = parent->getStyle().justifyContent();
const Justify parentJustifyContent = parent->style().justifyContent();
switch (parentJustifyContent) {
case Justify::FlexStart:
case Justify::SpaceBetween:
@@ -99,7 +99,7 @@ static void alignAbsoluteChild(
const FlexDirection crossAxis,
const float containingBlockWidth) {
Align itemAlign = resolveChildAlignment(parent, child);
const Wrap parentWrap = parent->getStyle().flexWrap();
const Wrap parentWrap = parent->style().flexWrap();
if (parentWrap == Wrap::WrapReverse) {
if (itemAlign == Align::FlexEnd) {
itemAlign = Align::FlexStart;
@@ -143,12 +143,12 @@ static void positionAbsoluteChildLegacy(
const float containingBlockHeight) {
const bool isAxisRow = isRow(axis);
const bool shouldCenter = isMainAxis
? parent->getStyle().justifyContent() == Justify::Center
? parent->style().justifyContent() == Justify::Center
: resolveChildAlignment(parent, child) == Align::Center;
const bool shouldFlexEnd = isMainAxis
? parent->getStyle().justifyContent() == Justify::FlexEnd
? parent->style().justifyContent() == Justify::FlexEnd
: ((resolveChildAlignment(parent, child) == Align::FlexEnd) ^
(parent->getStyle().flexWrap() == Wrap::WrapReverse));
(parent->style().flexWrap() == Wrap::WrapReverse));
if (child->isFlexEndPositionDefined(axis, direction) &&
!child->isFlexStartPositionDefined(axis, direction)) {
@@ -294,7 +294,7 @@ void layoutAbsoluteChild(
const uint32_t depth,
const uint32_t generationCount) {
const FlexDirection mainAxis =
resolveDirection(node->getStyle().flexDirection(), direction);
resolveDirection(node->style().flexDirection(), direction);
const FlexDirection crossAxis = resolveCrossDirection(mainAxis, direction);
const bool isMainAxisRow = isRow(mainAxis);
@@ -366,7 +366,7 @@ void layoutAbsoluteChild(
// Exactly one dimension needs to be defined for us to be able to do aspect
// ratio calculation. One dimension being the anchor and the other being
// flexible.
const auto& childStyle = child->getStyle();
const auto& childStyle = child->style();
if (yoga::isUndefined(childWidth) ^ yoga::isUndefined(childHeight)) {
if (childStyle.aspectRatio().isDefined()) {
if (yoga::isUndefined(childWidth)) {
@@ -467,13 +467,13 @@ void layoutAbsoluteDescendants(
float containingNodeAvailableInnerWidth,
float containingNodeAvailableInnerHeight) {
const FlexDirection mainAxis = resolveDirection(
currentNode->getStyle().flexDirection(), currentNodeDirection);
currentNode->style().flexDirection(), currentNodeDirection);
const FlexDirection crossAxis =
resolveCrossDirection(mainAxis, currentNodeDirection);
for (auto child : currentNode->getChildren()) {
if (child->getStyle().display() == Display::None) {
if (child->style().display() == Display::None) {
continue;
} else if (child->getStyle().positionType() == PositionType::Absolute) {
} else if (child->style().positionType() == PositionType::Absolute) {
const bool absoluteErrata =
currentNode->hasErrata(Errata::AbsolutePercentAgainstInnerSize);
const float containingBlockWidth = absoluteErrata
@@ -499,11 +499,11 @@ void layoutAbsoluteDescendants(
const bool isMainAxisRow = isRow(mainAxis);
const bool mainInsetsDefined = isMainAxisRow
? child->getStyle().horizontalInsetsDefined()
: child->getStyle().verticalInsetsDefined();
? child->style().horizontalInsetsDefined()
: child->style().verticalInsetsDefined();
const bool crossInsetsDefined = isMainAxisRow
? child->getStyle().verticalInsetsDefined()
: child->getStyle().horizontalInsetsDefined();
? child->style().verticalInsetsDefined()
: child->style().horizontalInsetsDefined();
const float childMainOffsetFromParent = mainInsetsDefined
? (child->getLayout().position(flexStartEdge(mainAxis)) -
@@ -526,7 +526,7 @@ void layoutAbsoluteDescendants(
setChildTrailingPosition(currentNode, child, crossAxis);
}
} else if (
child->getStyle().positionType() == PositionType::Static &&
child->style().positionType() == PositionType::Static &&
!child->alwaysFormsContainingBlock()) {
const Direction childDirection =
child->resolveDirection(currentNodeDirection);

View File

@@ -17,10 +17,10 @@ namespace facebook::yoga {
inline Align resolveChildAlignment(
const yoga::Node* node,
const yoga::Node* child) {
const Align align = child->getStyle().alignSelf() == Align::Auto
? node->getStyle().alignItems()
: child->getStyle().alignSelf();
if (align == Align::Baseline && isColumn(node->getStyle().flexDirection())) {
const Align align = child->style().alignSelf() == Align::Auto
? node->style().alignItems()
: child->style().alignSelf();
if (align == Align::Baseline && isColumn(node->style().flexDirection())) {
return Align::FlexStart;
}
return align;

View File

@@ -38,7 +38,7 @@ float calculateBaseline(const yoga::Node* node) {
if (child->getLineIndex() > 0) {
break;
}
if (child->getStyle().positionType() == PositionType::Absolute) {
if (child->style().positionType() == PositionType::Absolute) {
continue;
}
if (resolveChildAlignment(node, child) == Align::Baseline ||
@@ -61,17 +61,17 @@ float calculateBaseline(const yoga::Node* node) {
}
bool isBaselineLayout(const yoga::Node* node) {
if (isColumn(node->getStyle().flexDirection())) {
if (isColumn(node->style().flexDirection())) {
return false;
}
if (node->getStyle().alignItems() == Align::Baseline) {
if (node->style().alignItems() == Align::Baseline) {
return true;
}
const auto childCount = node->getChildCount();
for (size_t i = 0; i < childCount; i++) {
auto child = node->getChild(i);
if (child->getStyle().positionType() != PositionType::Absolute &&
child->getStyle().alignSelf() == Align::Baseline) {
if (child->style().positionType() != PositionType::Absolute &&
child->style().alignSelf() == Align::Baseline) {
return true;
}
}

View File

@@ -35,11 +35,11 @@ inline FloatOptional boundAxisWithinMinAndMax(
FloatOptional max;
if (isColumn(axis)) {
min = node->getStyle().minDimension(Dimension::Height).resolve(axisSize);
max = node->getStyle().maxDimension(Dimension::Height).resolve(axisSize);
min = node->style().minDimension(Dimension::Height).resolve(axisSize);
max = node->style().maxDimension(Dimension::Height).resolve(axisSize);
} else if (isRow(axis)) {
min = node->getStyle().minDimension(Dimension::Width).resolve(axisSize);
max = node->getStyle().maxDimension(Dimension::Width).resolve(axisSize);
min = node->style().minDimension(Dimension::Width).resolve(axisSize);
max = node->style().maxDimension(Dimension::Width).resolve(axisSize);
}
if (max >= FloatOptional{0} && value > max) {

View File

@@ -44,7 +44,7 @@ static void constrainMaxSizeForMode(
/*in_out*/ SizingMode* mode,
/*in_out*/ float* size) {
const FloatOptional maxSize =
node->getStyle().maxDimension(dimension(axis)).resolve(ownerAxisSize) +
node->style().maxDimension(dimension(axis)).resolve(ownerAxisSize) +
FloatOptional(node->getMarginForAxis(axis, ownerWidth));
switch (*mode) {
case SizingMode::StretchFit:
@@ -76,7 +76,7 @@ static void computeFlexBasisForChild(
const uint32_t depth,
const uint32_t generationCount) {
const FlexDirection mainAxis =
resolveDirection(node->getStyle().flexDirection(), direction);
resolveDirection(node->style().flexDirection(), direction);
const bool isMainAxisRow = isRow(mainAxis);
const float mainAxisSize = isMainAxisRow ? width : height;
const float mainAxisownerSize = isMainAxisRow ? ownerWidth : ownerHeight;
@@ -147,23 +147,23 @@ static void computeFlexBasisForChild(
// The W3C spec doesn't say anything about the 'overflow' property, but all
// major browsers appear to implement the following logic.
if ((!isMainAxisRow && node->getStyle().overflow() == Overflow::Scroll) ||
node->getStyle().overflow() != Overflow::Scroll) {
if ((!isMainAxisRow && node->style().overflow() == Overflow::Scroll) ||
node->style().overflow() != Overflow::Scroll) {
if (yoga::isUndefined(childWidth) && yoga::isDefined(width)) {
childWidth = width;
childWidthSizingMode = SizingMode::FitContent;
}
}
if ((isMainAxisRow && node->getStyle().overflow() == Overflow::Scroll) ||
node->getStyle().overflow() != Overflow::Scroll) {
if ((isMainAxisRow && node->style().overflow() == Overflow::Scroll) ||
node->style().overflow() != Overflow::Scroll) {
if (yoga::isUndefined(childHeight) && yoga::isDefined(height)) {
childHeight = height;
childHeightSizingMode = SizingMode::FitContent;
}
}
const auto& childStyle = child->getStyle();
const auto& childStyle = child->style();
if (childStyle.aspectRatio().isDefined()) {
if (!isMainAxisRow && childWidthSizingMode == SizingMode::StretchFit) {
childHeight = marginColumn +
@@ -457,13 +457,13 @@ static float calculateAvailableInnerDimension(
// We want to make sure our available height does not violate min and max
// constraints
const FloatOptional minDimensionOptional =
node->getStyle().minDimension(dimension).resolve(ownerDim);
node->style().minDimension(dimension).resolve(ownerDim);
const float minInnerDim = minDimensionOptional.isUndefined()
? 0.0f
: minDimensionOptional.unwrap() - paddingAndBorder;
const FloatOptional maxDimensionOptional =
node->getStyle().maxDimension(dimension).resolve(ownerDim);
node->style().maxDimension(dimension).resolve(ownerDim);
const float maxInnerDim = maxDimensionOptional.isUndefined()
? FLT_MAX
@@ -514,7 +514,7 @@ static float computeFlexBasisForChildren(
for (auto child : children) {
child->resolveDimension();
if (child->getStyle().display() == Display::None) {
if (child->style().display() == Display::None) {
zeroOutLayoutRecursively(child);
child->setHasNewLayout(true);
child->setDirty(false);
@@ -531,7 +531,7 @@ static float computeFlexBasisForChildren(
childDirection, mainDim, crossDim, availableInnerWidth);
}
if (child->getStyle().positionType() == PositionType::Absolute) {
if (child->style().positionType() == PositionType::Absolute) {
continue;
}
if (child == singleFlexChild) {
@@ -586,7 +586,7 @@ static float distributeFreeSpaceSecondPass(
float flexGrowFactor = 0;
float deltaFreeSpace = 0;
const bool isMainAxisRow = isRow(mainAxis);
const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap;
const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
for (auto currentLineChild : flexLine.itemsInFlow) {
childFlexBasis = boundAxisWithinMinAndMax(
@@ -652,7 +652,7 @@ static float distributeFreeSpaceSecondPass(
SizingMode childCrossSizingMode;
SizingMode childMainSizingMode = SizingMode::StretchFit;
const auto& childStyle = currentLineChild->getStyle();
const auto& childStyle = currentLineChild->style();
if (childStyle.aspectRatio().isDefined()) {
childCrossSize = isMainAxisRow
? (childMainSize - marginMain) / childStyle.aspectRatio().unwrap()
@@ -917,7 +917,7 @@ static void justifyMainAxis(
const float availableInnerCrossDim,
const float availableInnerWidth,
const bool performLayout) {
const auto& style = node->getStyle();
const auto& style = node->style();
const float leadingPaddingAndBorderMain =
node->getFlexStartPaddingAndBorder(mainAxis, direction, ownerWidth);
@@ -956,7 +956,7 @@ static void justifyMainAxis(
int numberOfAutoMarginsOnCurrentLine = 0;
for (size_t i = startOfLineIndex; i < flexLine.endOfLineIndex; i++) {
auto child = node->getChild(i);
if (child->getStyle().positionType() != PositionType::Absolute) {
if (child->style().positionType() != PositionType::Absolute) {
if (child->getFlexStartMarginValue(mainAxis).unit() == Unit::Auto) {
numberOfAutoMarginsOnCurrentLine++;
}
@@ -971,7 +971,7 @@ static void justifyMainAxis(
// each two elements.
float leadingMainDim = 0;
float betweenMainDim = gap;
const Justify justifyContent = node->getStyle().justifyContent();
const Justify justifyContent = node->style().justifyContent();
if (numberOfAutoMarginsOnCurrentLine == 0) {
switch (justifyContent) {
@@ -1013,7 +1013,7 @@ static void justifyMainAxis(
bool isNodeBaselineLayout = isBaselineLayout(node);
for (size_t i = startOfLineIndex; i < flexLine.endOfLineIndex; i++) {
const auto child = node->getChild(i);
const Style& childStyle = child->getStyle();
const Style& childStyle = child->style();
const LayoutResults& childLayout = child->getLayout();
if (childStyle.display() == Display::None) {
continue;
@@ -1301,10 +1301,10 @@ static void calculateLayoutImpl(
// STEP 1: CALCULATE VALUES FOR REMAINDER OF ALGORITHM
const FlexDirection mainAxis =
resolveDirection(node->getStyle().flexDirection(), direction);
resolveDirection(node->style().flexDirection(), direction);
const FlexDirection crossAxis = resolveCrossDirection(mainAxis, direction);
const bool isMainAxisRow = isRow(mainAxis);
const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap;
const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
const float mainAxisownerSize = isMainAxisRow ? ownerWidth : ownerHeight;
const float crossAxisownerSize = isMainAxisRow ? ownerHeight : ownerWidth;
@@ -1419,7 +1419,7 @@ static void calculateLayoutImpl(
// If we don't measure with exact main dimension we want to ensure we don't
// violate min and max
if (sizingModeMainDim != SizingMode::StretchFit) {
const auto& style = node->getStyle();
const auto& style = node->style();
const float minInnerWidth =
style.minDimension(Dimension::Width).resolve(ownerWidth).unwrap() -
paddingAndBorderAxisRow;
@@ -1560,10 +1560,10 @@ static void calculateLayoutImpl(
if (performLayout) {
for (size_t i = startOfLineIndex; i < endOfLineIndex; i++) {
const auto child = node->getChild(i);
if (child->getStyle().display() == Display::None) {
if (child->style().display() == Display::None) {
continue;
}
if (child->getStyle().positionType() == PositionType::Absolute) {
if (child->style().positionType() == PositionType::Absolute) {
// If the child is absolutely positioned and has a
// top/left/bottom/right set, override all the previously computed
// positions to set it correctly.
@@ -1609,7 +1609,7 @@ static void calculateLayoutImpl(
dimension(crossAxis), availableInnerCrossDim)) {
float childMainSize =
child->getLayout().measuredDimension(dimension(mainAxis));
const auto& childStyle = child->getStyle();
const auto& childStyle = child->style();
float childCrossSize = childStyle.aspectRatio().isDefined()
? child->getMarginForAxis(crossAxis, availableInnerWidth) +
(isMainAxisRow
@@ -1642,7 +1642,7 @@ static void calculateLayoutImpl(
const float childHeight =
!isMainAxisRow ? childMainSize : childCrossSize;
auto alignContent = node->getStyle().alignContent();
auto alignContent = node->style().alignContent();
auto crossAxisDoesNotGrow =
alignContent != Align::Stretch && isNodeFlexWrap;
const SizingMode childWidthSizingMode =
@@ -1729,7 +1729,7 @@ static void calculateLayoutImpl(
paddingAndBorderAxisCross;
const float remainingAlignContentDim = innerCrossDim - totalLineCrossDim;
switch (node->getStyle().alignContent()) {
switch (node->style().alignContent()) {
case Align::FlexEnd:
currentLead += remainingAlignContentDim;
break;
@@ -1784,10 +1784,10 @@ static void calculateLayoutImpl(
float maxDescentForCurrentLine = 0;
for (ii = startIndex; ii < childCount; ii++) {
const auto child = node->getChild(ii);
if (child->getStyle().display() == Display::None) {
if (child->style().display() == Display::None) {
continue;
}
if (child->getStyle().positionType() != PositionType::Absolute) {
if (child->style().positionType() != PositionType::Absolute) {
if (child->getLineIndex() != i) {
break;
}
@@ -1821,10 +1821,10 @@ static void calculateLayoutImpl(
if (performLayout) {
for (ii = startIndex; ii < endIndex; ii++) {
const auto child = node->getChild(ii);
if (child->getStyle().display() == Display::None) {
if (child->style().display() == Display::None) {
continue;
}
if (child->getStyle().positionType() != PositionType::Absolute) {
if (child->style().positionType() != PositionType::Absolute) {
switch (resolveChildAlignment(node, child)) {
case Align::FlexStart: {
child->setLayoutPosition(
@@ -1951,7 +1951,7 @@ static void calculateLayoutImpl(
// If the user didn't specify a width or height for the node, set the
// dimensions based on the children.
if (sizingModeMainDim == SizingMode::MaxContent ||
(node->getStyle().overflow() != Overflow::Scroll &&
(node->style().overflow() != Overflow::Scroll &&
sizingModeMainDim == SizingMode::FitContent)) {
// Clamp the size to the min/max size, if specified, and make sure it
// doesn't go below the padding and border amount.
@@ -1962,7 +1962,7 @@ static void calculateLayoutImpl(
} else if (
sizingModeMainDim == SizingMode::FitContent &&
node->getStyle().overflow() == Overflow::Scroll) {
node->style().overflow() == Overflow::Scroll) {
node->setLayoutMeasuredDimension(
yoga::maxOrDefined(
yoga::minOrDefined(
@@ -1978,7 +1978,7 @@ static void calculateLayoutImpl(
}
if (sizingModeCrossDim == SizingMode::MaxContent ||
(node->getStyle().overflow() != Overflow::Scroll &&
(node->style().overflow() != Overflow::Scroll &&
sizingModeCrossDim == SizingMode::FitContent)) {
// Clamp the size to the min/max size, if specified, and make sure it
// doesn't go below the padding and border amount.
@@ -1993,7 +1993,7 @@ static void calculateLayoutImpl(
} else if (
sizingModeCrossDim == SizingMode::FitContent &&
node->getStyle().overflow() == Overflow::Scroll) {
node->style().overflow() == Overflow::Scroll) {
node->setLayoutMeasuredDimension(
yoga::maxOrDefined(
yoga::minOrDefined(
@@ -2011,10 +2011,10 @@ static void calculateLayoutImpl(
// As we only wrapped in normal direction yet, we need to reverse the
// positions on wrap-reverse.
if (performLayout && node->getStyle().flexWrap() == Wrap::WrapReverse) {
if (performLayout && node->style().flexWrap() == Wrap::WrapReverse) {
for (size_t i = 0; i < childCount; i++) {
const auto child = node->getChild(i);
if (child->getStyle().positionType() != PositionType::Absolute) {
if (child->style().positionType() != PositionType::Absolute) {
child->setLayoutPosition(
node->getLayout().measuredDimension(dimension(crossAxis)) -
child->getLayout().position(flexStartEdge(crossAxis)) -
@@ -2028,7 +2028,7 @@ static void calculateLayoutImpl(
// STEP 10: SIZING AND POSITIONING ABSOLUTE CHILDREN
// Let the containing block layout its absolute descendants. By definition
// the containing block will not be static unless we are at the root.
if (node->getStyle().positionType() != PositionType::Static ||
if (node->style().positionType() != PositionType::Static ||
node->alwaysFormsContainingBlock() || depth == 1) {
layoutAbsoluteDescendants(
node,
@@ -2054,8 +2054,8 @@ static void calculateLayoutImpl(
// Absolute children will be handled by their containing block since we
// cannot guarantee that their positions are set when their parents are
// done with layout.
if (child->getStyle().display() == Display::None ||
child->getStyle().positionType() == PositionType::Absolute) {
if (child->style().display() == Display::None ||
child->style().positionType() == PositionType::Absolute) {
continue;
}
if (needsMainTrailingPos) {
@@ -2289,7 +2289,7 @@ void calculateLayout(
node->resolveDimension();
float width = YGUndefined;
SizingMode widthSizingMode = SizingMode::MaxContent;
const auto& style = node->getStyle();
const auto& style = node->style();
if (node->hasDefiniteLength(Dimension::Width, ownerWidth)) {
width =
(node->getResolvedDimension(dimension(FlexDirection::Row))

View File

@@ -32,15 +32,15 @@ FlexLine calculateFlexLine(
float sizeConsumedIncludingMinConstraint = 0;
const FlexDirection mainAxis = resolveDirection(
node->getStyle().flexDirection(), node->resolveDirection(ownerDirection));
const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap;
node->style().flexDirection(), node->resolveDirection(ownerDirection));
const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
const float gap = node->getGapForAxis(mainAxis);
// Add items to the current line until it's full or we run out of items.
for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) {
auto child = node->getChild(endOfLineIndex);
if (child->getStyle().display() == Display::None ||
child->getStyle().positionType() == PositionType::Absolute) {
if (child->style().display() == Display::None ||
child->style().positionType() == PositionType::Absolute) {
if (firstElementInLineIndex == endOfLineIndex) {
// We haven't found the first contributing element in the line yet.
firstElementInLineIndex++;