Remove usage of Gutters arrays and YGGutter as index (#1406)

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

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

Similar in vain to D49362819, we want to stop exposing pre-resolved CompactValue, and allow enum class usage without becoming annoying.

This also simplifies gap resolution a bit. I moved this to Style, to make it clear we aren't relying on any node state. I plan to do some similar cleanup for other resolution later.

Reviewed By: rshest

Differential Revision: D49530923

fbshipit-source-id: 47b06a7301fb283acc493dba159f496159d59580
This commit is contained in:
Nick Gerleman
2023-10-03 10:08:10 -07:00
committed by Facebook GitHub Bot
parent 07cabca526
commit 4ef28bce24
7 changed files with 39 additions and 63 deletions

View File

@@ -635,11 +635,11 @@ void YGNodeStyleSetGap(
const YGGutter gutter, const YGGutter gutter,
const float gapLength) { const float gapLength) {
auto length = CompactValue::ofMaybe<YGUnitPoint>(gapLength); auto length = CompactValue::ofMaybe<YGUnitPoint>(gapLength);
updateIndexedStyleProp<MSVC_HINT(gap)>(node, &Style::gap, gutter, length); updateIndexedStyleProp<&Style::gap, &Style::setGap>(node, gutter, length);
} }
float YGNodeStyleGetGap(const YGNodeConstRef node, const YGGutter gutter) { float YGNodeStyleGetGap(const YGNodeConstRef node, const YGGutter gutter) {
auto gapLength = resolveRef(node)->getStyle().gap()[gutter]; auto gapLength = resolveRef(node)->getStyle().gap(gutter);
if (gapLength.isUndefined() || gapLength.isAuto()) { if (gapLength.isUndefined() || gapLength.isAuto()) {
return YGUndefined; return YGUndefined;
} }

View File

@@ -1215,7 +1215,7 @@ static void justifyMainAxis(
node->getLeadingPaddingAndBorder(mainAxis, ownerWidth).unwrap(); node->getLeadingPaddingAndBorder(mainAxis, ownerWidth).unwrap();
const float trailingPaddingAndBorderMain = const float trailingPaddingAndBorderMain =
node->getTrailingPaddingAndBorder(mainAxis, ownerWidth).unwrap(); node->getTrailingPaddingAndBorder(mainAxis, ownerWidth).unwrap();
const float gap = node->getGapForAxis(mainAxis, ownerWidth); const float gap = node->getGapForAxis(mainAxis);
// If we are using "at most" rules in the main axis, make sure that // If we are using "at most" rules in the main axis, make sure that
// remainingFreeSpace is 0 when min main dimension is not given // remainingFreeSpace is 0 when min main dimension is not given
if (measureModeMainDim == MeasureMode::AtMost && if (measureModeMainDim == MeasureMode::AtMost &&
@@ -1666,8 +1666,8 @@ static void calculateLayoutImpl(
generationCount); generationCount);
if (childCount > 1) { if (childCount > 1) {
totalMainDim += node->getGapForAxis(mainAxis, availableInnerCrossDim) * totalMainDim +=
static_cast<float>(childCount - 1); node->getGapForAxis(mainAxis) * static_cast<float>(childCount - 1);
} }
const bool mainAxisOverflows = const bool mainAxisOverflows =
@@ -1690,8 +1690,7 @@ static void calculateLayoutImpl(
// Accumulated cross dimensions of all lines so far. // Accumulated cross dimensions of all lines so far.
float totalLineCrossDim = 0; float totalLineCrossDim = 0;
const float crossAxisGap = const float crossAxisGap = node->getGapForAxis(crossAxis);
node->getGapForAxis(crossAxis, availableInnerCrossDim);
// Max main dimension of all the lines. // Max main dimension of all the lines.
float maxLineMainDim = 0; float maxLineMainDim = 0;

View File

@@ -34,7 +34,7 @@ FlexLine calculateFlexLine(
const FlexDirection mainAxis = resolveDirection( const FlexDirection mainAxis = resolveDirection(
node->getStyle().flexDirection(), node->resolveDirection(ownerDirection)); node->getStyle().flexDirection(), node->resolveDirection(ownerDirection));
const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap; const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap;
const float gap = node->getGapForAxis(mainAxis, availableInnerWidth); const float gap = node->getGapForAxis(mainAxis);
// Add items to the current line until it's full or we run out of items. // Add items to the current line until it's full or we run out of items.
for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) { for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) {

View File

@@ -180,17 +180,11 @@ void nodeToString(
appendEdges(str, "padding", style.padding()); appendEdges(str, "padding", style.padding());
appendEdges(str, "border", style.border()); appendEdges(str, "border", style.border());
if (yoga::Node::computeColumnGap( if (!style.gap(YGGutterAll).isUndefined()) {
style.gap(), CompactValue::ofUndefined()) != appendNumberIfNotUndefined(str, "gap", style.gap(YGGutterAll));
yoga::Node::computeColumnGap( } else {
yoga::Node{}.getStyle().gap(), CompactValue::ofUndefined())) { appendNumberIfNotUndefined(str, "column-gap", style.gap(YGGutterColumn));
appendNumberIfNotUndefined( appendNumberIfNotUndefined(str, "row-gap", style.gap(YGGutterRow));
str, "column-gap", style.gap()[YGGutterColumn]);
}
if (yoga::Node::computeRowGap(style.gap(), CompactValue::ofUndefined()) !=
yoga::Node::computeRowGap(
yoga::Node{}.getStyle().gap(), CompactValue::ofUndefined())) {
appendNumberIfNotUndefined(str, "row-gap", style.gap()[YGGutterRow]);
} }
appendNumberIfNotAuto(str, "width", style.dimension(Dimension::Width)); appendNumberIfNotAuto(str, "width", style.dimension(Dimension::Width));

View File

@@ -89,30 +89,6 @@ CompactValue Node::computeEdgeValueForColumn(
} }
} }
CompactValue Node::computeRowGap(
const Style::Gutters& gutters,
CompactValue defaultValue) {
if (!gutters[YGGutterRow].isUndefined()) {
return gutters[YGGutterRow];
} else if (!gutters[YGGutterAll].isUndefined()) {
return gutters[YGGutterAll];
} else {
return defaultValue;
}
}
CompactValue Node::computeColumnGap(
const Style::Gutters& gutters,
CompactValue defaultValue) {
if (!gutters[YGGutterColumn].isUndefined()) {
return gutters[YGGutterColumn];
} else if (!gutters[YGGutterAll].isUndefined()) {
return gutters[YGGutterAll];
} else {
return defaultValue;
}
}
FloatOptional Node::getLeadingPosition( FloatOptional Node::getLeadingPosition(
const FlexDirection axis, const FlexDirection axis,
const float axisSize) const { const float axisSize) const {
@@ -202,13 +178,12 @@ FloatOptional Node::getMarginForAxis(
return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize); return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize);
} }
float Node::getGapForAxis(const FlexDirection axis, const float widthSize) float Node::getGapForAxis(const FlexDirection axis) const {
const { auto gap = isRow(axis) ? style_.resolveColumnGap() : style_.resolveRowGap();
auto gap = isRow(axis) // TODO: Validate percentage gap, and expose ability to set percentage to
? computeColumnGap(style_.gap(), CompactValue::ofUndefined()) // public API
: computeRowGap(style_.gap(), CompactValue::ofUndefined()); auto resolvedGap = yoga::resolveValue(gap, 0.0f /*ownerSize*/);
auto resolvedGap = yoga::resolveValue(gap, widthSize); return maxOrDefined(resolvedGap.unwrap(), 0.0f);
return maxOrDefined(resolvedGap.unwrap(), 0);
} }
YGSize Node::measure( YGSize Node::measure(

View File

@@ -191,14 +191,6 @@ class YG_EXPORT Node : public ::YGNode {
YGEdge edge, YGEdge edge,
CompactValue defaultValue); CompactValue defaultValue);
static CompactValue computeRowGap(
const Style::Gutters& gutters,
CompactValue defaultValue);
static CompactValue computeColumnGap(
const Style::Gutters& gutters,
CompactValue defaultValue);
// Methods related to positions, margin, padding and border // Methods related to positions, margin, padding and border
FloatOptional getLeadingPosition( FloatOptional getLeadingPosition(
const FlexDirection axis, const FlexDirection axis,
@@ -231,7 +223,7 @@ class YG_EXPORT Node : public ::YGNode {
FloatOptional getMarginForAxis( FloatOptional getMarginForAxis(
const FlexDirection axis, const FlexDirection axis,
const float widthSize) const; const float widthSize) const;
float getGapForAxis(const FlexDirection axis, const float widthSize) const; float getGapForAxis(const FlexDirection axis) const;
// Setters // Setters
void setContext(void* context) { void setContext(void* context) {

View File

@@ -274,11 +274,11 @@ class YG_EXPORT Style {
return {*this}; return {*this};
} }
const Gutters& gap() const { CompactValue gap(YGGutter gutter) const {
return gap_; return gap_[gutter];
} }
IdxRef<YGGutter, &Style::gap_> gap() { void setGap(YGGutter gutter, CompactValue value) {
return {*this}; gap_[gutter] = value;
} }
CompactValue dimension(Dimension axis) const { CompactValue dimension(Dimension axis) const {
@@ -310,6 +310,22 @@ class YG_EXPORT Style {
return {*this}; return {*this};
} }
CompactValue resolveColumnGap() const {
if (!gap_[YGGutterColumn].isUndefined()) {
return gap_[YGGutterColumn];
} else {
return gap_[YGGutterAll];
}
}
CompactValue resolveRowGap() const {
if (!gap_[YGGutterRow].isUndefined()) {
return gap_[YGGutterRow];
} else {
return gap_[YGGutterAll];
}
}
bool operator==(const Style& other) const { bool operator==(const Style& other) const {
return flags == other.flags && inexactEquals(flex_, other.flex_) && return flags == other.flags && inexactEquals(flex_, other.flex_) &&
inexactEquals(flexGrow_, other.flexGrow_) && inexactEquals(flexGrow_, other.flexGrow_) &&