Skip to content

Commit

Permalink
YGEdge -> yoga::Edge (facebook#1461)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#41391

Pull Request resolved: facebook#1461

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

fbshipit-source-id: de91bb48688a12dd42fe12ba2251436d47f90cec
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 22, 2023
1 parent 11e211a commit 02c06f7
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 211 deletions.
38 changes: 20 additions & 18 deletions yoga/YGNodeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <yoga/Yoga.h>
#include <yoga/debug/AssertFatal.h>
#include <yoga/enums/Edge.h>
#include <yoga/node/Node.h>

using namespace facebook;
Expand All @@ -15,50 +16,48 @@ using namespace facebook::yoga;
namespace {

template <auto LayoutMember>
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) {
Expand All @@ -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));
}
24 changes: 12 additions & 12 deletions yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,61 +223,61 @@ 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(
const YGNodeRef node,
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;
}
Expand Down
2 changes: 1 addition & 1 deletion yoga/algorithm/Baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
55 changes: 27 additions & 28 deletions yoga/algorithm/CalculateLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -1330,7 +1330,7 @@ static void justifyMainAxis(

if (performLayout) {
child->setLayoutPosition(
childLayout.position[flexStartEdge(mainAxis)] +
childLayout.position(flexStartEdge(mainAxis)) +
flexLine.layout.mainDim,
flexStartEdge(mainAxis));
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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),
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -2190,7 +2189,7 @@ static void calculateLayoutImpl(
FlexDirection::Column,
direction,
availableInnerCrossDim),
YGEdgeTop);
Edge::Top);

break;
}
Expand Down Expand Up @@ -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));
}
Expand Down
Loading

0 comments on commit 02c06f7

Please sign in to comment.