From b616c850a4cbb2ee0f8c3520cc27257db2279cf6 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Sat, 25 Nov 2023 13:35:24 -0800 Subject: [PATCH] YGEdge -> yoga::Edge (#1461) Summary: X-link: https://github.com/facebook/react-native/pull/41391 Converts usages of `YGEdge` within internal APIs to `yoga::Edge` scoped enum. With the exception of YGUnit which is in its own state of transition, this is the last public yoga enum to need to be moved to scoped enum form for usages internal to the Yoga public API. Changelog: [internal] Reviewed By: rshest Differential Revision: D51152779 --- yoga/YGNodeLayout.cpp | 38 ++++---- yoga/YGNodeStyle.cpp | 24 ++--- yoga/algorithm/Baseline.cpp | 2 +- yoga/algorithm/CalculateLayout.cpp | 55 ++++++------ yoga/algorithm/FlexDirection.h | 53 +++++------ yoga/algorithm/PixelGrid.cpp | 8 +- yoga/debug/NodeToString.cpp | 8 +- yoga/node/LayoutResults.cpp | 8 +- yoga/node/LayoutResults.h | 71 ++++++++++++--- yoga/node/Node.cpp | 138 +++++++++++++---------------- yoga/node/Node.h | 17 ++-- yoga/style/Style.h | 32 +++---- 12 files changed, 243 insertions(+), 211 deletions(-) diff --git a/yoga/YGNodeLayout.cpp b/yoga/YGNodeLayout.cpp index 39f67f9dc9..d2477f72cf 100644 --- a/yoga/YGNodeLayout.cpp +++ b/yoga/YGNodeLayout.cpp @@ -7,6 +7,7 @@ #include #include +#include #include using namespace facebook; @@ -15,50 +16,48 @@ using namespace facebook::yoga; namespace { template -float getResolvedLayoutProperty( - const YGNodeConstRef nodeRef, - const YGEdge edge) { +float getResolvedLayoutProperty(const YGNodeConstRef nodeRef, const Edge edge) { const auto node = resolveRef(nodeRef); yoga::assertFatalWithNode( node, - edge <= YGEdgeEnd, + edge <= Edge::End, "Cannot get layout properties of multi-edge shorthands"); - if (edge == YGEdgeStart) { + if (edge == Edge::Start) { if (node->getLayout().direction() == Direction::RTL) { - return (node->getLayout().*LayoutMember)[YGEdgeRight]; + return (node->getLayout().*LayoutMember)(Edge::Right); } else { - return (node->getLayout().*LayoutMember)[YGEdgeLeft]; + return (node->getLayout().*LayoutMember)(Edge::Left); } } - if (edge == YGEdgeEnd) { + if (edge == Edge::End) { if (node->getLayout().direction() == Direction::RTL) { - return (node->getLayout().*LayoutMember)[YGEdgeLeft]; + return (node->getLayout().*LayoutMember)(Edge::Left); } else { - return (node->getLayout().*LayoutMember)[YGEdgeRight]; + return (node->getLayout().*LayoutMember)(Edge::Right); } } - return (node->getLayout().*LayoutMember)[edge]; + return (node->getLayout().*LayoutMember)(edge); } } // namespace float YGNodeLayoutGetLeft(const YGNodeConstRef node) { - return resolveRef(node)->getLayout().position[YGEdgeLeft]; + return resolveRef(node)->getLayout().position(Edge::Left); } float YGNodeLayoutGetTop(const YGNodeConstRef node) { - return resolveRef(node)->getLayout().position[YGEdgeTop]; + return resolveRef(node)->getLayout().position(Edge::Top); } float YGNodeLayoutGetRight(const YGNodeConstRef node) { - return resolveRef(node)->getLayout().position[YGEdgeRight]; + return resolveRef(node)->getLayout().position(Edge::Right); } float YGNodeLayoutGetBottom(const YGNodeConstRef node) { - return resolveRef(node)->getLayout().position[YGEdgeBottom]; + return resolveRef(node)->getLayout().position(Edge::Bottom); } float YGNodeLayoutGetWidth(const YGNodeConstRef node) { @@ -78,13 +77,16 @@ bool YGNodeLayoutGetHadOverflow(const YGNodeConstRef node) { } float YGNodeLayoutGetMargin(YGNodeConstRef node, YGEdge edge) { - return getResolvedLayoutProperty<&LayoutResults::margin>(node, edge); + return getResolvedLayoutProperty<&LayoutResults::margin>( + node, scopedEnum(edge)); } float YGNodeLayoutGetBorder(YGNodeConstRef node, YGEdge edge) { - return getResolvedLayoutProperty<&LayoutResults::border>(node, edge); + return getResolvedLayoutProperty<&LayoutResults::border>( + node, scopedEnum(edge)); } float YGNodeLayoutGetPadding(YGNodeConstRef node, YGEdge edge) { - return getResolvedLayoutProperty<&LayoutResults::padding>(node, edge); + return getResolvedLayoutProperty<&LayoutResults::padding>( + node, scopedEnum(edge)); } diff --git a/yoga/YGNodeStyle.cpp b/yoga/YGNodeStyle.cpp index 6767e5e8e3..c39372a33a 100644 --- a/yoga/YGNodeStyle.cpp +++ b/yoga/YGNodeStyle.cpp @@ -223,49 +223,49 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) { void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) { updateIndexedStyleProp<&Style::position, &Style::setPosition>( - node, edge, value::points(points)); + node, scopedEnum(edge), value::points(points)); } void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) { updateIndexedStyleProp<&Style::position, &Style::setPosition>( - node, edge, value::percent(percent)); + node, scopedEnum(edge), value::percent(percent)); } YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) { - return resolveRef(node)->getStyle().position(edge); + return resolveRef(node)->getStyle().position(scopedEnum(edge)); } void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) { updateIndexedStyleProp<&Style::margin, &Style::setMargin>( - node, edge, value::points(points)); + node, scopedEnum(edge), value::points(points)); } void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) { updateIndexedStyleProp<&Style::margin, &Style::setMargin>( - node, edge, value::percent(percent)); + node, scopedEnum(edge), value::percent(percent)); } void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) { updateIndexedStyleProp<&Style::margin, &Style::setMargin>( - node, edge, value::ofAuto()); + node, scopedEnum(edge), value::ofAuto()); } YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) { - return resolveRef(node)->getStyle().margin(edge); + return resolveRef(node)->getStyle().margin(scopedEnum(edge)); } void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) { updateIndexedStyleProp<&Style::padding, &Style::setPadding>( - node, edge, value::points(points)); + node, scopedEnum(edge), value::points(points)); } void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) { updateIndexedStyleProp<&Style::padding, &Style::setPadding>( - node, edge, value::percent(percent)); + node, scopedEnum(edge), value::percent(percent)); } YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) { - return resolveRef(node)->getStyle().padding(edge); + return resolveRef(node)->getStyle().padding(scopedEnum(edge)); } void YGNodeStyleSetBorder( @@ -273,11 +273,11 @@ void YGNodeStyleSetBorder( const YGEdge edge, const float border) { updateIndexedStyleProp<&Style::border, &Style::setBorder>( - node, edge, value::points(border)); + node, scopedEnum(edge), value::points(border)); } float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) { - auto border = resolveRef(node)->getStyle().border(edge); + auto border = resolveRef(node)->getStyle().border(scopedEnum(edge)); if (border.isUndefined() || border.isAuto()) { return YGUndefined; } diff --git a/yoga/algorithm/Baseline.cpp b/yoga/algorithm/Baseline.cpp index aea316c035..a86b9b28a4 100644 --- a/yoga/algorithm/Baseline.cpp +++ b/yoga/algorithm/Baseline.cpp @@ -57,7 +57,7 @@ float calculateBaseline(const yoga::Node* node) { } const float baseline = calculateBaseline(baselineChild); - return baseline + baselineChild->getLayout().position[YGEdgeTop]; + return baseline + baselineChild->getLayout().position(Edge::Top); } bool isBaselineLayout(const yoga::Node* node) { diff --git a/yoga/algorithm/CalculateLayout.cpp b/yoga/algorithm/CalculateLayout.cpp index f066dbdfc4..fe97b4e116 100644 --- a/yoga/algorithm/CalculateLayout.cpp +++ b/yoga/algorithm/CalculateLayout.cpp @@ -89,7 +89,7 @@ static void setChildTrailingPosition( const float size = child->getLayout().measuredDimension(dimension(axis)); child->setLayoutPosition( node->getLayout().measuredDimension(dimension(axis)) - size - - child->getLayout().position[flexStartEdge(axis)], + child->getLayout().position(flexStartEdge(axis)), flexEndEdge(axis)); } @@ -553,12 +553,13 @@ static void measureNodeWithMeasureFunc( availableHeight = YGUndefined; } - const auto& padding = node->getLayout().padding; - const auto& border = node->getLayout().border; - const float paddingAndBorderAxisRow = padding[YGEdgeLeft] + - padding[YGEdgeRight] + border[YGEdgeLeft] + border[YGEdgeRight]; - const float paddingAndBorderAxisColumn = padding[YGEdgeTop] + - padding[YGEdgeBottom] + border[YGEdgeTop] + border[YGEdgeBottom]; + const auto& layout = node->getLayout(); + const float paddingAndBorderAxisRow = layout.padding(Edge::Left) + + layout.padding(Edge::Right) + layout.border(Edge::Left) + + layout.border(Edge::Right); + const float paddingAndBorderAxisColumn = layout.padding(Edge::Top) + + layout.padding(Edge::Bottom) + layout.border(Edge::Top) + + layout.border(Edge::Bottom); // We want to make sure we don't call measure with negative size const float innerWidth = yoga::isUndefined(availableWidth) @@ -643,14 +644,13 @@ static void measureNodeWithoutChildren( const SizingMode heightSizingMode, const float ownerWidth, const float ownerHeight) { - const auto& padding = node->getLayout().padding; - const auto& border = node->getLayout().border; + const auto& layout = node->getLayout(); float width = availableWidth; if (widthSizingMode == SizingMode::MaxContent || widthSizingMode == SizingMode::FitContent) { - width = padding[YGEdgeLeft] + padding[YGEdgeRight] + border[YGEdgeLeft] + - border[YGEdgeRight]; + width = layout.padding(Edge::Left) + layout.padding(Edge::Right) + + layout.border(Edge::Left) + layout.border(Edge::Right); } node->setLayoutMeasuredDimension( boundAxis(node, FlexDirection::Row, width, ownerWidth, ownerWidth), @@ -659,8 +659,8 @@ static void measureNodeWithoutChildren( float height = availableHeight; if (heightSizingMode == SizingMode::MaxContent || heightSizingMode == SizingMode::FitContent) { - height = padding[YGEdgeTop] + padding[YGEdgeBottom] + border[YGEdgeTop] + - border[YGEdgeBottom]; + height = layout.padding(Edge::Top) + layout.padding(Edge::Bottom) + + layout.border(Edge::Top) + layout.border(Edge::Bottom); } node->setLayoutMeasuredDimension( boundAxis(node, FlexDirection::Column, height, ownerHeight, ownerWidth), @@ -1330,7 +1330,7 @@ static void justifyMainAxis( if (performLayout) { child->setLayoutPosition( - childLayout.position[flexStartEdge(mainAxis)] + + childLayout.position(flexStartEdge(mainAxis)) + flexLine.layout.mainDim, flexStartEdge(mainAxis)); } @@ -1386,7 +1386,7 @@ static void justifyMainAxis( } } else if (performLayout) { child->setLayoutPosition( - childLayout.position[flexStartEdge(mainAxis)] + + childLayout.position(flexStartEdge(mainAxis)) + node->getInlineStartBorder(mainAxis, direction) + leadingMainDim, flexStartEdge(mainAxis)); @@ -1497,9 +1497,8 @@ static void calculateLayoutImpl( const FlexDirection flexColumnDirection = resolveDirection(FlexDirection::Column, direction); - const YGEdge startEdge = - direction == Direction::LTR ? YGEdgeLeft : YGEdgeRight; - const YGEdge endEdge = direction == Direction::LTR ? YGEdgeRight : YGEdgeLeft; + const Edge startEdge = direction == Direction::LTR ? Edge::Left : Edge::Right; + const Edge endEdge = direction == Direction::LTR ? Edge::Right : Edge::Left; const float marginRowLeading = node->getInlineStartMargin(flexRowDirection, direction, ownerWidth); @@ -1509,10 +1508,10 @@ static void calculateLayoutImpl( node->setLayoutMargin(marginRowTrailing, endEdge); const float marginColumnLeading = node->getInlineStartMargin(flexColumnDirection, direction, ownerWidth); - node->setLayoutMargin(marginColumnLeading, YGEdgeTop); + node->setLayoutMargin(marginColumnLeading, Edge::Top); const float marginColumnTrailing = node->getInlineEndMargin(flexColumnDirection, direction, ownerWidth); - node->setLayoutMargin(marginColumnTrailing, YGEdgeBottom); + node->setLayoutMargin(marginColumnTrailing, Edge::Bottom); const float marginAxisRow = marginRowLeading + marginRowTrailing; const float marginAxisColumn = marginColumnLeading + marginColumnTrailing; @@ -1522,9 +1521,9 @@ static void calculateLayoutImpl( node->setLayoutBorder( node->getInlineEndBorder(flexRowDirection, direction), endEdge); node->setLayoutBorder( - node->getInlineStartBorder(flexColumnDirection, direction), YGEdgeTop); + node->getInlineStartBorder(flexColumnDirection, direction), Edge::Top); node->setLayoutBorder( - node->getInlineEndBorder(flexColumnDirection, direction), YGEdgeBottom); + node->getInlineEndBorder(flexColumnDirection, direction), Edge::Bottom); node->setLayoutPadding( node->getInlineStartPadding(flexRowDirection, direction, ownerWidth), @@ -1534,10 +1533,10 @@ static void calculateLayoutImpl( endEdge); node->setLayoutPadding( node->getInlineStartPadding(flexColumnDirection, direction, ownerWidth), - YGEdgeTop); + Edge::Top); node->setLayoutPadding( node->getInlineEndPadding(flexColumnDirection, direction, ownerWidth), - YGEdgeBottom); + Edge::Bottom); if (node->hasMeasureFunc()) { measureNodeWithMeasureFunc( @@ -1868,7 +1867,7 @@ static void calculateLayoutImpl( // default to border + margin if (!isChildLeadingPosDefined || yoga::isUndefined( - child->getLayout().position[flexStartEdge(crossAxis)])) { + child->getLayout().position(flexStartEdge(crossAxis)))) { child->setLayoutPosition( node->getInlineStartBorder(crossAxis, direction) + child->getInlineStartMargin( @@ -1981,7 +1980,7 @@ static void calculateLayoutImpl( } // And we apply the position child->setLayoutPosition( - child->getLayout().position[flexStartEdge(crossAxis)] + + child->getLayout().position(flexStartEdge(crossAxis)) + totalLineCrossDim + leadingCrossDim, flexStartEdge(crossAxis)); } @@ -2190,7 +2189,7 @@ static void calculateLayoutImpl( FlexDirection::Column, direction, availableInnerCrossDim), - YGEdgeTop); + Edge::Top); break; } @@ -2296,7 +2295,7 @@ static void calculateLayoutImpl( if (child->getStyle().positionType() != PositionType::Absolute) { child->setLayoutPosition( node->getLayout().measuredDimension(dimension(crossAxis)) - - child->getLayout().position[flexStartEdge(crossAxis)] - + child->getLayout().position(flexStartEdge(crossAxis)) - child->getLayout().measuredDimension(dimension(crossAxis)), flexStartEdge(crossAxis)); } diff --git a/yoga/algorithm/FlexDirection.h b/yoga/algorithm/FlexDirection.h index a8ee7586ae..5bc5880a4d 100644 --- a/yoga/algorithm/FlexDirection.h +++ b/yoga/algorithm/FlexDirection.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace facebook::yoga { @@ -48,80 +49,80 @@ inline FlexDirection resolveCrossDirection( : FlexDirection::Column; } -inline YGEdge flexStartEdge(const FlexDirection flexDirection) { +inline Edge flexStartEdge(const FlexDirection flexDirection) { switch (flexDirection) { case FlexDirection::Column: - return YGEdgeTop; + return Edge::Top; case FlexDirection::ColumnReverse: - return YGEdgeBottom; + return Edge::Bottom; case FlexDirection::Row: - return YGEdgeLeft; + return Edge::Left; case FlexDirection::RowReverse: - return YGEdgeRight; + return Edge::Right; } fatalWithMessage("Invalid FlexDirection"); } -inline YGEdge flexEndEdge(const FlexDirection flexDirection) { +inline Edge flexEndEdge(const FlexDirection flexDirection) { switch (flexDirection) { case FlexDirection::Column: - return YGEdgeBottom; + return Edge::Bottom; case FlexDirection::ColumnReverse: - return YGEdgeTop; + return Edge::Top; case FlexDirection::Row: - return YGEdgeRight; + return Edge::Right; case FlexDirection::RowReverse: - return YGEdgeLeft; + return Edge::Left; } fatalWithMessage("Invalid FlexDirection"); } -inline YGEdge inlineStartEdge( +inline Edge inlineStartEdge( const FlexDirection flexDirection, const Direction direction) { if (isRow(flexDirection)) { - return direction == Direction::RTL ? YGEdgeRight : YGEdgeLeft; + return direction == Direction::RTL ? Edge::Right : Edge::Left; } - return YGEdgeTop; + return Edge::Top; } -inline YGEdge inlineEndEdge( +inline Edge inlineEndEdge( const FlexDirection flexDirection, const Direction direction) { if (isRow(flexDirection)) { - return direction == Direction::RTL ? YGEdgeLeft : YGEdgeRight; + return direction == Direction::RTL ? Edge::Left : Edge::Right; } - return YGEdgeBottom; + return Edge::Bottom; } /** - * The physical edges that YGEdgeStart and YGEdgeEnd correspond to (e.g. + * The physical edges that Edge::Start and Edge::End correspond to (e.g. * left/right) are soley dependent on the direction. However, there are cases * where we want the flex start/end edge (i.e. which edge is the start/end * for laying out flex items), which can be distinct from the corresponding * inline edge. In these cases we need to know which "relative edge" - * (YGEdgeStart/YGEdgeEnd) corresponds to the said flex start/end edge as these + * (Edge::Start/Edge::End) corresponds to the said flex start/end edge as these * relative edges can be used instead of physical ones when defining certain * attributes like border or padding. */ -inline YGEdge flexStartRelativeEdge( +inline Edge flexStartRelativeEdge( FlexDirection flexDirection, Direction direction) { - const YGEdge leadLayoutEdge = inlineStartEdge(flexDirection, direction); - const YGEdge leadFlexItemEdge = flexStartEdge(flexDirection); - return leadLayoutEdge == leadFlexItemEdge ? YGEdgeStart : YGEdgeEnd; + const Edge leadLayoutEdge = inlineStartEdge(flexDirection, direction); + const Edge leadFlexItemEdge = flexStartEdge(flexDirection); + return leadLayoutEdge == leadFlexItemEdge ? Edge::Start : Edge::End; } -inline YGEdge flexEndRelativeEdge( +inline Edge flexEndRelativeEdge( FlexDirection flexDirection, Direction direction) { - const YGEdge trailLayoutEdge = inlineEndEdge(flexDirection, direction); - const YGEdge trailFlexItemEdge = flexEndEdge(flexDirection); - return trailLayoutEdge == trailFlexItemEdge ? YGEdgeEnd : YGEdgeStart; + const Edge trailLayoutEdge = inlineEndEdge(flexDirection, direction); + const Edge trailFlexItemEdge = flexEndEdge(flexDirection); + return trailLayoutEdge == trailFlexItemEdge ? Edge::End : Edge::Start; } inline Dimension dimension(const FlexDirection flexDirection) { diff --git a/yoga/algorithm/PixelGrid.cpp b/yoga/algorithm/PixelGrid.cpp index 5e44c2cc8d..b84cd7db09 100644 --- a/yoga/algorithm/PixelGrid.cpp +++ b/yoga/algorithm/PixelGrid.cpp @@ -68,8 +68,8 @@ void roundLayoutResultsToPixelGrid( const double absoluteTop) { const auto pointScaleFactor = node->getConfig()->getPointScaleFactor(); - const double nodeLeft = node->getLayout().position[YGEdgeLeft]; - const double nodeTop = node->getLayout().position[YGEdgeTop]; + const double nodeLeft = node->getLayout().position(Edge::Left); + const double nodeTop = node->getLayout().position(Edge::Top); const double nodeWidth = node->getLayout().dimension(Dimension::Width); const double nodeHeight = node->getLayout().dimension(Dimension::Height); @@ -87,11 +87,11 @@ void roundLayoutResultsToPixelGrid( node->setLayoutPosition( roundValueToPixelGrid(nodeLeft, pointScaleFactor, false, textRounding), - YGEdgeLeft); + Edge::Left); node->setLayoutPosition( roundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding), - YGEdgeTop); + Edge::Top); // We multiply dimension by scale factor and if the result is close to the // whole number, we don't have any fraction To verify if the result is close diff --git a/yoga/debug/NodeToString.cpp b/yoga/debug/NodeToString.cpp index ef55ea1d21..18e8aeec65 100644 --- a/yoga/debug/NodeToString.cpp +++ b/yoga/debug/NodeToString.cpp @@ -9,8 +9,6 @@ #include -#include - #include #include #include @@ -85,7 +83,7 @@ static void appendEdges(std::string& base, const std::string& key, const Style& style) { for (auto edge : ordinals()) { std::string str = key + "-" + toString(edge); - appendNumberIfNotZero(base, str, (style.*Field)(unscopedEnum(edge))); + appendNumberIfNotZero(base, str, (style.*Field)(edge)); } } @@ -104,9 +102,9 @@ void nodeToString( appendFormattedString( str, "height: %g; ", node->getLayout().dimension(Dimension::Height)); appendFormattedString( - str, "top: %g; ", node->getLayout().position[YGEdgeTop]); + str, "top: %g; ", node->getLayout().position(Edge::Top)); appendFormattedString( - str, "left: %g;", node->getLayout().position[YGEdgeLeft]); + str, "left: %g;", node->getLayout().position(Edge::Left)); appendFormattedString(str, "\" "); } diff --git a/yoga/node/LayoutResults.cpp b/yoga/node/LayoutResults.cpp index d050bfdc73..fd70870e65 100644 --- a/yoga/node/LayoutResults.cpp +++ b/yoga/node/LayoutResults.cpp @@ -13,11 +13,11 @@ namespace facebook::yoga { bool LayoutResults::operator==(LayoutResults layout) const { - bool isEqual = yoga::inexactEquals(position, layout.position) && + bool isEqual = yoga::inexactEquals(position_, layout.position_) && yoga::inexactEquals(dimensions_, layout.dimensions_) && - yoga::inexactEquals(margin, layout.margin) && - yoga::inexactEquals(border, layout.border) && - yoga::inexactEquals(padding, layout.padding) && + yoga::inexactEquals(margin_, layout.margin_) && + yoga::inexactEquals(border_, layout.border_) && + yoga::inexactEquals(padding_, layout.padding_) && direction() == layout.direction() && hadOverflow() == layout.hadOverflow() && lastOwnerDirection == layout.lastOwnerDirection && diff --git a/yoga/node/LayoutResults.h b/yoga/node/LayoutResults.h index 8b324e6454..f7cf8b8cf4 100644 --- a/yoga/node/LayoutResults.h +++ b/yoga/node/LayoutResults.h @@ -10,8 +10,10 @@ #include #include +#include #include #include +#include #include #include @@ -22,19 +24,6 @@ struct LayoutResults { // 98% of analyzed layouts require less than 8 entries. static constexpr int32_t MaxCachedMeasurements = 8; - std::array position = {}; - std::array margin = {}; - std::array border = {}; - std::array padding = {}; - - private: - Direction direction_ : bitCount() = Direction::Inherit; - bool hadOverflow_ : 1 = false; - - std::array dimensions_ = {{YGUndefined, YGUndefined}}; - std::array measuredDimensions_ = {{YGUndefined, YGUndefined}}; - - public: uint32_t computedFlexBasisGeneration = 0; FloatOptional computedFlexBasis = {}; @@ -80,10 +69,66 @@ struct LayoutResults { measuredDimensions_[yoga::to_underlying(axis)] = dimension; } + float position(Edge cardinalEdge) const { + assertCardinalEdge(cardinalEdge); + return position_[yoga::to_underlying(cardinalEdge)]; + } + + void setPosition(Edge cardinalEdge, float dimension) { + assertCardinalEdge(cardinalEdge); + position_[yoga::to_underlying(cardinalEdge)] = dimension; + } + + float margin(Edge cardinalEdge) const { + assertCardinalEdge(cardinalEdge); + return margin_[yoga::to_underlying(cardinalEdge)]; + } + + void setMargin(Edge cardinalEdge, float dimension) { + assertCardinalEdge(cardinalEdge); + margin_[yoga::to_underlying(cardinalEdge)] = dimension; + } + + float border(Edge cardinalEdge) const { + assertCardinalEdge(cardinalEdge); + return border_[yoga::to_underlying(cardinalEdge)]; + } + + void setBorder(Edge cardinalEdge, float dimension) { + assertCardinalEdge(cardinalEdge); + border_[yoga::to_underlying(cardinalEdge)] = dimension; + } + + float padding(Edge cardinalEdge) const { + assertCardinalEdge(cardinalEdge); + return padding_[yoga::to_underlying(cardinalEdge)]; + } + + void setPadding(Edge cardinalEdge, float dimension) { + assertCardinalEdge(cardinalEdge); + padding_[yoga::to_underlying(cardinalEdge)] = dimension; + } + bool operator==(LayoutResults layout) const; bool operator!=(LayoutResults layout) const { return !(*this == layout); } + + private: + static inline void assertCardinalEdge(Edge edge) { + assertFatal( + static_cast(edge) <= 3, "Edge must be top/left/bottom/right"); + } + + Direction direction_ : bitCount() = Direction::Inherit; + bool hadOverflow_ : 1 = false; + + std::array dimensions_ = {{YGUndefined, YGUndefined}}; + std::array measuredDimensions_ = {{YGUndefined, YGUndefined}}; + std::array position_ = {}; + std::array margin_ = {}; + std::array border_ = {}; + std::array padding_ = {}; }; } // namespace facebook::yoga diff --git a/yoga/node/Node.cpp b/yoga/node/Node.cpp index 0781b8e63a..faf812e8cf 100644 --- a/yoga/node/Node.cpp +++ b/yoga/node/Node.cpp @@ -58,31 +58,31 @@ void Node::print() { // TODO: Edge value resolution should be moved to `yoga::Style` template -Style::Length Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const { +Style::Length Node::computeEdgeValueForRow(Edge rowEdge, Edge edge) const { if ((style_.*Field)(rowEdge).isDefined()) { return (style_.*Field)(rowEdge); } else if ((style_.*Field)(edge).isDefined()) { return (style_.*Field)(edge); - } else if ((style_.*Field)(YGEdgeHorizontal).isDefined()) { - return (style_.*Field)(YGEdgeHorizontal); + } else if ((style_.*Field)(Edge::Horizontal).isDefined()) { + return (style_.*Field)(Edge::Horizontal); } else { - return (style_.*Field)(YGEdgeAll); + return (style_.*Field)(Edge::All); } } // TODO: Edge value resolution should be moved to `yoga::Style` template -Style::Length Node::computeEdgeValueForColumn(YGEdge edge) const { +Style::Length Node::computeEdgeValueForColumn(Edge edge) const { if ((style_.*Field)(edge).isDefined()) { return (style_.*Field)(edge); - } else if ((style_.*Field)(YGEdgeVertical).isDefined()) { - return (style_.*Field)(YGEdgeVertical); + } else if ((style_.*Field)(Edge::Vertical).isDefined()) { + return (style_.*Field)(Edge::Vertical); } else { - return (style_.*Field)(YGEdgeAll); + return (style_.*Field)(Edge::All); } } -YGEdge Node::getInlineStartEdgeUsingErrata( +Edge Node::getInlineStartEdgeUsingErrata( FlexDirection flexDirection, Direction direction) const { return hasErrata(Errata::StartingEndingEdgeFromFlexDirection) @@ -90,7 +90,7 @@ YGEdge Node::getInlineStartEdgeUsingErrata( : inlineStartEdge(flexDirection, direction); } -YGEdge Node::getInlineEndEdgeUsingErrata( +Edge Node::getInlineEndEdgeUsingErrata( FlexDirection flexDirection, Direction direction) const { return hasErrata(Errata::StartingEndingEdgeFromFlexDirection) @@ -99,9 +99,9 @@ YGEdge Node::getInlineEndEdgeUsingErrata( } bool Node::isFlexStartPositionDefined(FlexDirection axis) const { - const YGEdge startEdge = flexStartEdge(axis); + const Edge startEdge = flexStartEdge(axis); auto leadingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::position>(startEdge); return leadingPosition.isDefined(); @@ -109,18 +109,18 @@ bool Node::isFlexStartPositionDefined(FlexDirection axis) const { bool Node::isInlineStartPositionDefined(FlexDirection axis, Direction direction) const { - const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); + const Edge startEdge = getInlineStartEdgeUsingErrata(axis, direction); auto leadingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::position>(startEdge); return leadingPosition.isDefined(); } bool Node::isFlexEndPositionDefined(FlexDirection axis) const { - const YGEdge endEdge = flexEndEdge(axis); + const Edge endEdge = flexEndEdge(axis); auto trailingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::position>(endEdge); return !trailingPosition.isUndefined(); @@ -128,18 +128,18 @@ bool Node::isFlexEndPositionDefined(FlexDirection axis) const { bool Node::isInlineEndPositionDefined(FlexDirection axis, Direction direction) const { - const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); + const Edge endEdge = getInlineEndEdgeUsingErrata(axis, direction); auto trailingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::position>(endEdge); return trailingPosition.isDefined(); } float Node::getFlexStartPosition(FlexDirection axis, float axisSize) const { - const YGEdge startEdge = flexStartEdge(axis); + const Edge startEdge = flexStartEdge(axis); auto leadingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::position>(startEdge); return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f); @@ -149,18 +149,18 @@ float Node::getInlineStartPosition( FlexDirection axis, Direction direction, float axisSize) const { - const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); + const Edge startEdge = getInlineStartEdgeUsingErrata(axis, direction); auto leadingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::position>(startEdge); return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f); } float Node::getFlexEndPosition(FlexDirection axis, float axisSize) const { - const YGEdge endEdge = flexEndEdge(axis); + const Edge endEdge = flexEndEdge(axis); auto trailingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::position>(endEdge); return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f); @@ -170,18 +170,18 @@ float Node::getInlineEndPosition( FlexDirection axis, Direction direction, float axisSize) const { - const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); + const Edge endEdge = getInlineEndEdgeUsingErrata(axis, direction); auto trailingPosition = isRow(axis) - ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::position>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::position>(endEdge); return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f); } float Node::getFlexStartMargin(FlexDirection axis, float widthSize) const { - const YGEdge startEdge = flexStartEdge(axis); + const Edge startEdge = flexStartEdge(axis); auto leadingMargin = isRow(axis) - ? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::margin>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::margin>(startEdge); return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f); @@ -191,18 +191,18 @@ float Node::getInlineStartMargin( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); + const Edge startEdge = getInlineStartEdgeUsingErrata(axis, direction); auto leadingMargin = isRow(axis) - ? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::margin>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::margin>(startEdge); return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f); } float Node::getFlexEndMargin(FlexDirection axis, float widthSize) const { - const YGEdge endEdge = flexEndEdge(axis); + const Edge endEdge = flexEndEdge(axis); auto trailingMargin = isRow(axis) - ? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::margin>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::margin>(endEdge); return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f); @@ -212,9 +212,9 @@ float Node::getInlineEndMargin( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); + const Edge endEdge = getInlineEndEdgeUsingErrata(axis, direction); auto trailingMargin = isRow(axis) - ? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::margin>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::margin>(endEdge); return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f); @@ -222,17 +222,16 @@ float Node::getInlineEndMargin( float Node::getInlineStartBorder(FlexDirection axis, Direction direction) const { - const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); + const Edge startEdge = getInlineStartEdgeUsingErrata(axis, direction); YGValue leadingBorder = isRow(axis) - ? computeEdgeValueForRow<&Style::border>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::border>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::border>(startEdge); return maxOrDefined(leadingBorder.value, 0.0f); } float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const { - const YGEdge leadRelativeFlexItemEdge = - flexStartRelativeEdge(axis, direction); + const Edge leadRelativeFlexItemEdge = flexStartRelativeEdge(axis, direction); YGValue leadingBorder = isRow(axis) ? computeEdgeValueForRow<&Style::border>( leadRelativeFlexItemEdge, flexStartEdge(axis)) @@ -242,16 +241,16 @@ float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const { } float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const { - const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); + const Edge endEdge = getInlineEndEdgeUsingErrata(axis, direction); YGValue trailingBorder = isRow(axis) - ? computeEdgeValueForRow<&Style::border>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::border>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::border>(endEdge); return maxOrDefined(trailingBorder.value, 0.0f); } float Node::getFlexEndBorder(FlexDirection axis, Direction direction) const { - const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); + const Edge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); YGValue trailingBorder = isRow(axis) ? computeEdgeValueForRow<&Style::border>( trailRelativeFlexItemEdge, flexEndEdge(axis)) @@ -264,9 +263,9 @@ float Node::getInlineStartPadding( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); + const Edge startEdge = getInlineStartEdgeUsingErrata(axis, direction); auto leadingPadding = isRow(axis) - ? computeEdgeValueForRow<&Style::padding>(YGEdgeStart, startEdge) + ? computeEdgeValueForRow<&Style::padding>(Edge::Start, startEdge) : computeEdgeValueForColumn<&Style::padding>(startEdge); return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f); @@ -276,8 +275,7 @@ float Node::getFlexStartPadding( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge leadRelativeFlexItemEdge = - flexStartRelativeEdge(axis, direction); + const Edge leadRelativeFlexItemEdge = flexStartRelativeEdge(axis, direction); auto leadingPadding = isRow(axis) ? computeEdgeValueForRow<&Style::padding>( leadRelativeFlexItemEdge, flexStartEdge(axis)) @@ -290,9 +288,9 @@ float Node::getInlineEndPadding( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); + const Edge endEdge = getInlineEndEdgeUsingErrata(axis, direction); auto trailingPadding = isRow(axis) - ? computeEdgeValueForRow<&Style::padding>(YGEdgeEnd, endEdge) + ? computeEdgeValueForRow<&Style::padding>(Edge::End, endEdge) : computeEdgeValueForColumn<&Style::padding>(endEdge); return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f); @@ -302,7 +300,7 @@ float Node::getFlexEndPadding( FlexDirection axis, Direction direction, float widthSize) const { - const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); + const Edge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); auto trailingPadding = isRow(axis) ? computeEdgeValueForRow<&Style::padding>( trailRelativeFlexItemEdge, flexEndEdge(axis)) @@ -446,25 +444,16 @@ void Node::setLayoutDirection(Direction direction) { layout_.setDirection(direction); } -void Node::setLayoutMargin(float margin, YGEdge edge) { - assertFatal( - edge < static_cast(layout_.margin.size()), - "Edge must be top/left/bottom/right"); - layout_.margin[edge] = margin; +void Node::setLayoutMargin(float margin, Edge edge) { + layout_.setMargin(edge, margin); } -void Node::setLayoutBorder(float border, YGEdge edge) { - assertFatal( - edge < static_cast(layout_.border.size()), - "Edge must be top/left/bottom/right"); - layout_.border[edge] = border; +void Node::setLayoutBorder(float border, Edge edge) { + layout_.setBorder(edge, border); } -void Node::setLayoutPadding(float padding, YGEdge edge) { - assertFatal( - edge < static_cast(layout_.padding.size()), - "Edge must be top/left/bottom/right"); - layout_.padding[edge] = padding; +void Node::setLayoutPadding(float padding, Edge edge) { + layout_.setPadding(edge, padding); } void Node::setLayoutLastOwnerDirection(Direction direction) { @@ -475,11 +464,8 @@ void Node::setLayoutComputedFlexBasis(const FloatOptional computedFlexBasis) { layout_.computedFlexBasis = computedFlexBasis; } -void Node::setLayoutPosition(float position, YGEdge edge) { - assertFatal( - edge < static_cast(layout_.position.size()), - "Edge must be top/left/bottom/right"); - layout_.position[edge] = position; +void Node::setLayoutPosition(float position, Edge edge) { + layout_.setPosition(edge, position); } void Node::setLayoutComputedFlexBasisGeneration( @@ -536,13 +522,13 @@ void Node::setPosition( const float relativePositionCross = relativePosition(crossAxis, directionRespectingRoot, crossSize); - const YGEdge mainAxisLeadingEdge = + const Edge mainAxisLeadingEdge = getInlineStartEdgeUsingErrata(mainAxis, direction); - const YGEdge mainAxisTrailingEdge = + const Edge mainAxisTrailingEdge = getInlineEndEdgeUsingErrata(mainAxis, direction); - const YGEdge crossAxisLeadingEdge = + const Edge crossAxisLeadingEdge = getInlineStartEdgeUsingErrata(crossAxis, direction); - const YGEdge crossAxisTrailingEdge = + const Edge crossAxisTrailingEdge = getInlineEndEdgeUsingErrata(crossAxis, direction); setLayoutPosition( @@ -564,16 +550,16 @@ void Node::setPosition( } YGValue Node::getFlexStartMarginValue(FlexDirection axis) const { - if (isRow(axis) && style_.margin(YGEdgeStart).isDefined()) { - return style_.margin(YGEdgeStart); + if (isRow(axis) && style_.margin(Edge::Start).isDefined()) { + return style_.margin(Edge::Start); } else { return style_.margin(flexStartEdge(axis)); } } YGValue Node::marginTrailingValue(FlexDirection axis) const { - if (isRow(axis) && style_.margin(YGEdgeEnd).isDefined()) { - return style_.margin(YGEdgeEnd); + if (isRow(axis) && style_.margin(Edge::End).isDefined()) { + return style_.margin(Edge::End); } else { return style_.margin(flexEndEdge(axis)); } diff --git a/yoga/node/Node.h b/yoga/node/Node.h index 1c9acbbe86..d01a1ed4ca 100644 --- a/yoga/node/Node.h +++ b/yoga/node/Node.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -52,10 +53,10 @@ class YG_EXPORT Node : public ::YGNode { Direction direction, const float axisSize) const; - YGEdge getInlineStartEdgeUsingErrata( + Edge getInlineStartEdgeUsingErrata( FlexDirection flexDirection, Direction direction) const; - YGEdge getInlineEndEdgeUsingErrata( + Edge getInlineEndEdgeUsingErrata( FlexDirection flexDirection, Direction direction) const; @@ -65,10 +66,10 @@ class YG_EXPORT Node : public ::YGNode { } template - Style::Length computeEdgeValueForColumn(YGEdge edge) const; + Style::Length computeEdgeValueForColumn(Edge edge) const; template - Style::Length computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const; + Style::Length computeEdgeValueForRow(Edge rowEdge, Edge edge) const; // DANGER DANGER DANGER! // If the node assigned to has children, we'd either have to deallocate @@ -328,10 +329,10 @@ class YG_EXPORT Node : public ::YGNode { void setLayoutHadOverflow(bool hadOverflow); void setLayoutDimension(float LengthValue, Dimension dimension); void setLayoutDirection(Direction direction); - void setLayoutMargin(float margin, YGEdge edge); - void setLayoutBorder(float border, YGEdge edge); - void setLayoutPadding(float padding, YGEdge edge); - void setLayoutPosition(float position, YGEdge edge); + void setLayoutMargin(float margin, Edge edge); + void setLayoutBorder(float border, Edge edge); + void setLayoutPadding(float padding, Edge edge); + void setLayoutPosition(float position, Edge edge); void setPosition( const Direction direction, const float mainSize, diff --git a/yoga/style/Style.h b/yoga/style/Style.h index 835632edd5..e40f1c4fb6 100644 --- a/yoga/style/Style.h +++ b/yoga/style/Style.h @@ -227,32 +227,32 @@ class YG_EXPORT Style { return {*this}; } - Style::Length margin(YGEdge edge) const { - return margin_[edge]; + Style::Length margin(Edge edge) const { + return margin_[yoga::to_underlying(edge)]; } - void setMargin(YGEdge edge, Style::Length value) { - margin_[edge] = value; + void setMargin(Edge edge, Style::Length value) { + margin_[yoga::to_underlying(edge)] = value; } - Style::Length position(YGEdge edge) const { - return position_[edge]; + Style::Length position(Edge edge) const { + return position_[yoga::to_underlying(edge)]; } - void setPosition(YGEdge edge, Style::Length value) { - position_[edge] = value; + void setPosition(Edge edge, Style::Length value) { + position_[yoga::to_underlying(edge)] = value; } - Style::Length padding(YGEdge edge) const { - return padding_[edge]; + Style::Length padding(Edge edge) const { + return padding_[yoga::to_underlying(edge)]; } - void setPadding(YGEdge edge, Style::Length value) { - padding_[edge] = value; + void setPadding(Edge edge, Style::Length value) { + padding_[yoga::to_underlying(edge)] = value; } - Style::Length border(YGEdge edge) const { - return border_[edge]; + Style::Length border(Edge edge) const { + return border_[yoga::to_underlying(edge)]; } - void setBorder(YGEdge edge, Style::Length value) { - border_[edge] = value; + void setBorder(Edge edge, Style::Length value) { + border_[yoga::to_underlying(edge)] = value; } Style::Length gap(Gutter gutter) const {