Do not expose aggregate style edges (#1477)

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

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

A reland of the main change in D50998164, moving away from exposing compound edge arrays directly.

Changelog: [Internal]

Reviewed By: joevilches

Differential Revision: D51512611

fbshipit-source-id: 2d4ceb89c9d76317feb9074aa271358a3abc0ee1
This commit is contained in:
Nick Gerleman
2023-11-22 22:43:41 -08:00
committed by Facebook GitHub Bot
parent 0ebac779aa
commit a822f2635e
6 changed files with 119 additions and 198 deletions

View File

@@ -35,19 +35,6 @@ void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) {
[prop](Style& s, T x) { (s.*prop)() = x; }); [prop](Style& s, T x) { (s.*prop)() = x; });
} }
template <typename Ref, typename Idx>
void updateIndexedStyleProp(
YGNodeRef node,
Ref (Style::*prop)(),
Idx idx,
CompactValue value) {
updateStyle(
resolveRef(node),
value,
[idx, prop](Style& s, CompactValue x) { return (s.*prop)()[idx] != x; },
[idx, prop](Style& s, CompactValue x) { (s.*prop)()[idx] = x; });
}
template <auto GetterT, auto SetterT, typename IdxT> template <auto GetterT, auto SetterT, typename IdxT>
void updateIndexedStyleProp(YGNodeRef node, IdxT idx, CompactValue value) { void updateIndexedStyleProp(YGNodeRef node, IdxT idx, CompactValue value) {
updateStyle( updateStyle(
@@ -237,53 +224,53 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) { void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points); auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(position)>( updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, &Style::position, edge, value); node, edge, value);
} }
void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) { void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent); auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(position)>( updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, &Style::position, edge, value); node, edge, value);
} }
YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) { YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().position()[edge]; return resolveRef(node)->getStyle().position(edge);
} }
void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) { void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points); auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value); updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
} }
void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) { void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent); auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value); updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
} }
void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) { void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<MSVC_HINT(margin)>( updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, &Style::margin, edge, CompactValue::ofAuto()); node, edge, CompactValue::ofAuto());
} }
YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) { YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().margin()[edge]; return resolveRef(node)->getStyle().margin(edge);
} }
void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) { void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points); auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(padding)>( updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, &Style::padding, edge, value); node, edge, value);
} }
void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) { void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent); auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(padding)>( updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, &Style::padding, edge, value); node, edge, value);
} }
YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) { YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().padding()[edge]; return resolveRef(node)->getStyle().padding(edge);
} }
void YGNodeStyleSetBorder( void YGNodeStyleSetBorder(
@@ -291,11 +278,11 @@ void YGNodeStyleSetBorder(
const YGEdge edge, const YGEdge edge,
const float border) { const float border) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(border); auto value = CompactValue::ofMaybe<YGUnitPoint>(border);
updateIndexedStyleProp<MSVC_HINT(border)>(node, &Style::border, edge, value); updateIndexedStyleProp<&Style::border, &Style::setBorder>(node, edge, value);
} }
float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) { float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) {
auto border = resolveRef(node)->getStyle().border()[edge]; auto border = resolveRef(node)->getStyle().border(edge);
if (border.isUndefined() || border.isAuto()) { if (border.isUndefined() || border.isAuto()) {
return YGUndefined; return YGUndefined;
} }

View File

@@ -11,6 +11,7 @@
#include <yoga/debug/AssertFatal.h> #include <yoga/debug/AssertFatal.h>
#include <yoga/enums/Dimension.h> #include <yoga/enums/Dimension.h>
#include <yoga/enums/Direction.h>
#include <yoga/enums/FlexDirection.h> #include <yoga/enums/FlexDirection.h>
namespace facebook::yoga { namespace facebook::yoga {

View File

@@ -23,12 +23,6 @@ static void indent(std::string& base, uint32_t level) {
} }
} }
static bool areFourValuesEqual(const Style::Edges& four) {
return yoga::inexactEquals(four[0], four[1]) &&
yoga::inexactEquals(four[0], four[2]) &&
yoga::inexactEquals(four[0], four[3]);
}
static void appendFormattedString(std::string& str, const char* fmt, ...) { static void appendFormattedString(std::string& str, const char* fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
@@ -86,31 +80,13 @@ static void appendNumberIfNotZero(
} }
} }
static void appendEdges( template <auto Field>
std::string& base, static void
const std::string& key, appendEdges(std::string& base, const std::string& key, const Style& style) {
const Style::Edges& edges) { for (auto edge : ordinals<Edge>()) {
if (areFourValuesEqual(edges)) { std::string str = key + "-" + toString(edge);
auto edgeValue = yoga::Node::computeEdgeValueForColumn(edges, YGEdgeLeft); appendNumberIfNotZero(base, str, (style.*Field)(unscopedEnum(edge)));
appendNumberIfNotUndefined(base, key, edgeValue);
} else {
for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
std::string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
appendNumberIfNotZero(base, str, edges[static_cast<size_t>(edge)]);
} }
}
}
static void appendEdgeIfNotUndefined(
std::string& base,
const std::string& str,
const Style::Edges& edges,
const YGEdge edge) {
// TODO: this doesn't take RTL / YGEdgeStart / YGEdgeEnd into account
auto value = (edge == YGEdgeLeft || edge == YGEdgeRight)
? yoga::Node::computeEdgeValueForRow(edges, edge, edge)
: yoga::Node::computeEdgeValueForColumn(edges, edge);
appendNumberIfNotUndefined(base, str, value);
} }
void nodeToString( void nodeToString(
@@ -173,9 +149,9 @@ void nodeToString(
if (style.display() != yoga::Node{}.getStyle().display()) { if (style.display() != yoga::Node{}.getStyle().display()) {
appendFormattedString(str, "display: %s; ", toString(style.display())); appendFormattedString(str, "display: %s; ", toString(style.display()));
} }
appendEdges(str, "margin", style.margin()); appendEdges<&Style::margin>(str, "margin", style);
appendEdges(str, "padding", style.padding()); appendEdges<&Style::padding>(str, "padding", style);
appendEdges(str, "border", style.border()); appendEdges<&Style::border>(str, "border", style);
if (style.gap(Gutter::All).isDefined()) { if (style.gap(Gutter::All).isDefined()) {
appendNumberIfNotUndefined(str, "gap", style.gap(Gutter::All)); appendNumberIfNotUndefined(str, "gap", style.gap(Gutter::All));
@@ -200,10 +176,7 @@ void nodeToString(
str, "position: %s; ", toString(style.positionType())); str, "position: %s; ", toString(style.positionType()));
} }
appendEdgeIfNotUndefined(str, "left", style.position(), YGEdgeLeft); appendEdges<&Style::position>(str, "position", style);
appendEdgeIfNotUndefined(str, "right", style.position(), YGEdgeRight);
appendEdgeIfNotUndefined(str, "top", style.position(), YGEdgeTop);
appendEdgeIfNotUndefined(str, "bottom", style.position(), YGEdgeBottom);
appendFormattedString(str, "\" "); appendFormattedString(str, "\" ");
if (node->hasMeasureFunc()) { if (node->hasMeasureFunc()) {

View File

@@ -56,30 +56,29 @@ void Node::print() {
} }
} }
CompactValue Node::computeEdgeValueForRow( // TODO: Edge value resolution should be moved to `yoga::Style`
const Style::Edges& edges, template <auto Field>
YGEdge rowEdge, CompactValue Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const {
YGEdge edge) { if ((style_.*Field)(rowEdge).isDefined()) {
if (edges[rowEdge].isDefined()) { return (style_.*Field)(rowEdge);
return edges[rowEdge]; } else if ((style_.*Field)(edge).isDefined()) {
} else if (edges[edge].isDefined()) { return (style_.*Field)(edge);
return edges[edge]; } else if ((style_.*Field)(YGEdgeHorizontal).isDefined()) {
} else if (edges[YGEdgeHorizontal].isDefined()) { return (style_.*Field)(YGEdgeHorizontal);
return edges[YGEdgeHorizontal];
} else { } else {
return edges[YGEdgeAll]; return (style_.*Field)(YGEdgeAll);
} }
} }
CompactValue Node::computeEdgeValueForColumn( // TODO: Edge value resolution should be moved to `yoga::Style`
const Style::Edges& edges, template <auto Field>
YGEdge edge) { CompactValue Node::computeEdgeValueForColumn(YGEdge edge) const {
if (edges[edge].isDefined()) { if ((style_.*Field)(edge).isDefined()) {
return edges[edge]; return (style_.*Field)(edge);
} else if (edges[YGEdgeVertical].isDefined()) { } else if ((style_.*Field)(YGEdgeVertical).isDefined()) {
return edges[YGEdgeVertical]; return (style_.*Field)(YGEdgeVertical);
} else { } else {
return edges[YGEdgeAll]; return (style_.*Field)(YGEdgeAll);
} }
} }
@@ -102,8 +101,8 @@ YGEdge Node::getInlineEndEdgeUsingErrata(
bool Node::isFlexStartPositionDefined(FlexDirection axis) const { bool Node::isFlexStartPositionDefined(FlexDirection axis) const {
const YGEdge startEdge = flexStartEdge(axis); const YGEdge startEdge = flexStartEdge(axis);
auto leadingPosition = isRow(axis) auto leadingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.position(), startEdge); : computeEdgeValueForColumn<&Style::position>(startEdge);
return leadingPosition.isDefined(); return leadingPosition.isDefined();
} }
@@ -112,8 +111,8 @@ bool Node::isInlineStartPositionDefined(FlexDirection axis, Direction direction)
const { const {
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
auto leadingPosition = isRow(axis) auto leadingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.position(), startEdge); : computeEdgeValueForColumn<&Style::position>(startEdge);
return leadingPosition.isDefined(); return leadingPosition.isDefined();
} }
@@ -121,8 +120,8 @@ bool Node::isInlineStartPositionDefined(FlexDirection axis, Direction direction)
bool Node::isFlexEndPositionDefined(FlexDirection axis) const { bool Node::isFlexEndPositionDefined(FlexDirection axis) const {
const YGEdge endEdge = flexEndEdge(axis); const YGEdge endEdge = flexEndEdge(axis);
auto trailingPosition = isRow(axis) auto trailingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.position(), endEdge); : computeEdgeValueForColumn<&Style::position>(endEdge);
return !trailingPosition.isUndefined(); return !trailingPosition.isUndefined();
} }
@@ -131,8 +130,8 @@ bool Node::isInlineEndPositionDefined(FlexDirection axis, Direction direction)
const { const {
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
auto trailingPosition = isRow(axis) auto trailingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.position(), endEdge); : computeEdgeValueForColumn<&Style::position>(endEdge);
return trailingPosition.isDefined(); return trailingPosition.isDefined();
} }
@@ -140,8 +139,8 @@ bool Node::isInlineEndPositionDefined(FlexDirection axis, Direction direction)
float Node::getFlexStartPosition(FlexDirection axis, float axisSize) const { float Node::getFlexStartPosition(FlexDirection axis, float axisSize) const {
const YGEdge startEdge = flexStartEdge(axis); const YGEdge startEdge = flexStartEdge(axis);
auto leadingPosition = isRow(axis) auto leadingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.position(), startEdge); : computeEdgeValueForColumn<&Style::position>(startEdge);
return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f); return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f);
} }
@@ -152,8 +151,8 @@ float Node::getInlineStartPosition(
float axisSize) const { float axisSize) const {
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
auto leadingPosition = isRow(axis) auto leadingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.position(), startEdge); : computeEdgeValueForColumn<&Style::position>(startEdge);
return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f); return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f);
} }
@@ -161,8 +160,8 @@ float Node::getInlineStartPosition(
float Node::getFlexEndPosition(FlexDirection axis, float axisSize) const { float Node::getFlexEndPosition(FlexDirection axis, float axisSize) const {
const YGEdge endEdge = flexEndEdge(axis); const YGEdge endEdge = flexEndEdge(axis);
auto trailingPosition = isRow(axis) auto trailingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.position(), endEdge); : computeEdgeValueForColumn<&Style::position>(endEdge);
return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f); return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f);
} }
@@ -173,8 +172,8 @@ float Node::getInlineEndPosition(
float axisSize) const { float axisSize) const {
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
auto trailingPosition = isRow(axis) auto trailingPosition = isRow(axis)
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.position(), endEdge); : computeEdgeValueForColumn<&Style::position>(endEdge);
return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f); return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f);
} }
@@ -182,8 +181,8 @@ float Node::getInlineEndPosition(
float Node::getFlexStartMargin(FlexDirection axis, float widthSize) const { float Node::getFlexStartMargin(FlexDirection axis, float widthSize) const {
const YGEdge startEdge = flexStartEdge(axis); const YGEdge startEdge = flexStartEdge(axis);
auto leadingMargin = isRow(axis) auto leadingMargin = isRow(axis)
? computeEdgeValueForRow(style_.margin(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.margin(), startEdge); : computeEdgeValueForColumn<&Style::margin>(startEdge);
return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f); return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f);
} }
@@ -194,8 +193,8 @@ float Node::getInlineStartMargin(
float widthSize) const { float widthSize) const {
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
auto leadingMargin = isRow(axis) auto leadingMargin = isRow(axis)
? computeEdgeValueForRow(style_.margin(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.margin(), startEdge); : computeEdgeValueForColumn<&Style::margin>(startEdge);
return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f); return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f);
} }
@@ -203,8 +202,8 @@ float Node::getInlineStartMargin(
float Node::getFlexEndMargin(FlexDirection axis, float widthSize) const { float Node::getFlexEndMargin(FlexDirection axis, float widthSize) const {
const YGEdge endEdge = flexEndEdge(axis); const YGEdge endEdge = flexEndEdge(axis);
auto trailingMargin = isRow(axis) auto trailingMargin = isRow(axis)
? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.margin(), endEdge); : computeEdgeValueForColumn<&Style::margin>(endEdge);
return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f); return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f);
} }
@@ -215,8 +214,8 @@ float Node::getInlineEndMargin(
float widthSize) const { float widthSize) const {
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
auto trailingMargin = isRow(axis) auto trailingMargin = isRow(axis)
? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.margin(), endEdge); : computeEdgeValueForColumn<&Style::margin>(endEdge);
return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f); return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f);
} }
@@ -225,8 +224,8 @@ float Node::getInlineStartBorder(FlexDirection axis, Direction direction)
const { const {
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
YGValue leadingBorder = isRow(axis) YGValue leadingBorder = isRow(axis)
? computeEdgeValueForRow(style_.border(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::border>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.border(), startEdge); : computeEdgeValueForColumn<&Style::border>(startEdge);
return maxOrDefined(leadingBorder.value, 0.0f); return maxOrDefined(leadingBorder.value, 0.0f);
} }
@@ -235,9 +234,9 @@ float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const {
const YGEdge leadRelativeFlexItemEdge = const YGEdge leadRelativeFlexItemEdge =
flexStartRelativeEdge(axis, direction); flexStartRelativeEdge(axis, direction);
YGValue leadingBorder = isRow(axis) YGValue leadingBorder = isRow(axis)
? computeEdgeValueForRow( ? computeEdgeValueForRow<&Style::border>(
style_.border(), leadRelativeFlexItemEdge, flexStartEdge(axis)) leadRelativeFlexItemEdge, flexStartEdge(axis))
: computeEdgeValueForColumn(style_.border(), flexStartEdge(axis)); : computeEdgeValueForColumn<&Style::border>(flexStartEdge(axis));
return maxOrDefined(leadingBorder.value, 0.0f); return maxOrDefined(leadingBorder.value, 0.0f);
} }
@@ -245,8 +244,8 @@ float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const {
float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const { float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const {
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
YGValue trailingBorder = isRow(axis) YGValue trailingBorder = isRow(axis)
? computeEdgeValueForRow(style_.border(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::border>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.border(), endEdge); : computeEdgeValueForColumn<&Style::border>(endEdge);
return maxOrDefined(trailingBorder.value, 0.0f); return maxOrDefined(trailingBorder.value, 0.0f);
} }
@@ -254,9 +253,9 @@ float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const {
float Node::getFlexEndBorder(FlexDirection axis, Direction direction) const { float Node::getFlexEndBorder(FlexDirection axis, Direction direction) const {
const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction);
YGValue trailingBorder = isRow(axis) YGValue trailingBorder = isRow(axis)
? computeEdgeValueForRow( ? computeEdgeValueForRow<&Style::border>(
style_.border(), trailRelativeFlexItemEdge, flexEndEdge(axis)) trailRelativeFlexItemEdge, flexEndEdge(axis))
: computeEdgeValueForColumn(style_.border(), flexEndEdge(axis)); : computeEdgeValueForColumn<&Style::border>(flexEndEdge(axis));
return maxOrDefined(trailingBorder.value, 0.0f); return maxOrDefined(trailingBorder.value, 0.0f);
} }
@@ -267,8 +266,8 @@ float Node::getInlineStartPadding(
float widthSize) const { float widthSize) const {
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction); const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
auto leadingPadding = isRow(axis) auto leadingPadding = isRow(axis)
? computeEdgeValueForRow(style_.padding(), YGEdgeStart, startEdge) ? computeEdgeValueForRow<&Style::padding>(YGEdgeStart, startEdge)
: computeEdgeValueForColumn(style_.padding(), startEdge); : computeEdgeValueForColumn<&Style::padding>(startEdge);
return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f); return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f);
} }
@@ -280,9 +279,9 @@ float Node::getFlexStartPadding(
const YGEdge leadRelativeFlexItemEdge = const YGEdge leadRelativeFlexItemEdge =
flexStartRelativeEdge(axis, direction); flexStartRelativeEdge(axis, direction);
auto leadingPadding = isRow(axis) auto leadingPadding = isRow(axis)
? computeEdgeValueForRow( ? computeEdgeValueForRow<&Style::padding>(
style_.padding(), leadRelativeFlexItemEdge, flexStartEdge(axis)) leadRelativeFlexItemEdge, flexStartEdge(axis))
: computeEdgeValueForColumn(style_.padding(), flexStartEdge(axis)); : computeEdgeValueForColumn<&Style::padding>(flexStartEdge(axis));
return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f); return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f);
} }
@@ -293,8 +292,8 @@ float Node::getInlineEndPadding(
float widthSize) const { float widthSize) const {
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction); const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
auto trailingPadding = isRow(axis) auto trailingPadding = isRow(axis)
? computeEdgeValueForRow(style_.padding(), YGEdgeEnd, endEdge) ? computeEdgeValueForRow<&Style::padding>(YGEdgeEnd, endEdge)
: computeEdgeValueForColumn(style_.padding(), endEdge); : computeEdgeValueForColumn<&Style::padding>(endEdge);
return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f); return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f);
} }
@@ -305,9 +304,9 @@ float Node::getFlexEndPadding(
float widthSize) const { float widthSize) const {
const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction); const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction);
auto trailingPadding = isRow(axis) auto trailingPadding = isRow(axis)
? computeEdgeValueForRow( ? computeEdgeValueForRow<&Style::padding>(
style_.padding(), trailRelativeFlexItemEdge, flexEndEdge(axis)) trailRelativeFlexItemEdge, flexEndEdge(axis))
: computeEdgeValueForColumn(style_.padding(), flexEndEdge(axis)); : computeEdgeValueForColumn<&Style::padding>(flexEndEdge(axis));
return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f); return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f);
} }
@@ -565,18 +564,18 @@ void Node::setPosition(
} }
YGValue Node::getFlexStartMarginValue(FlexDirection axis) const { YGValue Node::getFlexStartMarginValue(FlexDirection axis) const {
if (isRow(axis) && style_.margin()[YGEdgeStart].isDefined()) { if (isRow(axis) && style_.margin(YGEdgeStart).isDefined()) {
return style_.margin()[YGEdgeStart]; return style_.margin(YGEdgeStart);
} else { } else {
return style_.margin()[flexStartEdge(axis)]; return style_.margin(flexStartEdge(axis));
} }
} }
YGValue Node::marginTrailingValue(FlexDirection axis) const { YGValue Node::marginTrailingValue(FlexDirection axis) const {
if (isRow(axis) && style_.margin()[YGEdgeEnd].isDefined()) { if (isRow(axis) && style_.margin(YGEdgeEnd).isDefined()) {
return style_.margin()[YGEdgeEnd]; return style_.margin(YGEdgeEnd);
} else { } else {
return style_.margin()[flexEndEdge(axis)]; return style_.margin(flexEndEdge(axis));
} }
} }

View File

@@ -65,6 +65,12 @@ class YG_EXPORT Node : public ::YGNode {
style_.alignContent() = Align::Stretch; style_.alignContent() = Align::Stretch;
} }
template <auto Field>
CompactValue computeEdgeValueForColumn(YGEdge edge) const;
template <auto Field>
CompactValue computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const;
// DANGER DANGER DANGER! // DANGER DANGER DANGER!
// If the node assigned to has children, we'd either have to deallocate // If the node assigned to has children, we'd either have to deallocate
// them (potentially incorrect) or ignore them (danger of leaks). Only ever // them (potentially incorrect) or ignore them (danger of leaks). Only ever
@@ -189,15 +195,6 @@ class YG_EXPORT Node : public ::YGNode {
return resolvedDimensions_[static_cast<size_t>(dimension)]; return resolvedDimensions_[static_cast<size_t>(dimension)];
} }
static CompactValue computeEdgeValueForColumn(
const Style::Edges& edges,
YGEdge edge);
static CompactValue computeEdgeValueForRow(
const Style::Edges& edges,
YGEdge rowEdge,
YGEdge edge);
// Methods related to positions, margin, padding and border // Methods related to positions, margin, padding and border
bool isFlexStartPositionDefined(FlexDirection axis) const; bool isFlexStartPositionDefined(FlexDirection axis) const;
bool isInlineStartPositionDefined(FlexDirection axis, Direction direction) bool isInlineStartPositionDefined(FlexDirection axis, Direction direction)

View File

@@ -35,11 +35,11 @@ class YG_EXPORT Style {
template <typename Enum> template <typename Enum>
using Values = std::array<CompactValue, ordinalCount<Enum>()>; using Values = std::array<CompactValue, ordinalCount<Enum>()>;
public:
using Dimensions = Values<Dimension>; using Dimensions = Values<Dimension>;
using Edges = Values<YGEdge>; using Edges = Values<YGEdge>;
using Gutters = Values<Gutter>; using Gutters = Values<Gutter>;
public:
static constexpr float DefaultFlexGrow = 0.0f; static constexpr float DefaultFlexGrow = 0.0f;
static constexpr float DefaultFlexShrink = 0.0f; static constexpr float DefaultFlexShrink = 0.0f;
static constexpr float WebDefaultFlexShrink = 1.0f; static constexpr float WebDefaultFlexShrink = 1.0f;
@@ -69,39 +69,6 @@ class YG_EXPORT Style {
} }
}; };
template <typename Idx, Values<Idx> Style::*Prop>
struct IdxRef {
struct Ref {
Style& style;
Idx idx;
operator CompactValue() const {
return (style.*Prop)[idx];
}
operator YGValue() const {
return (style.*Prop)[idx];
}
Ref& operator=(CompactValue value) {
(style.*Prop)[idx] = value;
return *this;
}
};
Style& style;
IdxRef<Idx, Prop>& operator=(const Values<Idx>& values) {
style.*Prop = values;
return *this;
}
operator const Values<Idx>&() const {
return style.*Prop;
}
Ref operator[](Idx idx) {
return {style, idx};
}
CompactValue operator[](Idx idx) const {
return (style.*Prop)[idx];
}
};
Style() { Style() {
alignContent() = Align::FlexStart; alignContent() = Align::FlexStart;
alignItems() = Align::Stretch; alignItems() = Align::Stretch;
@@ -147,9 +114,6 @@ class YG_EXPORT Style {
FloatOptional aspectRatio_ = {}; FloatOptional aspectRatio_ = {};
public: public:
// for library users needing a type
using ValueRepr = std::remove_reference<decltype(margin_[0])>::type;
Direction direction() const { Direction direction() const {
return getEnumData<Direction>(flags, directionOffset); return getEnumData<Direction>(flags, directionOffset);
} }
@@ -248,32 +212,32 @@ class YG_EXPORT Style {
return {*this}; return {*this};
} }
const Edges& margin() const { CompactValue margin(YGEdge edge) const {
return margin_; return margin_[edge];
} }
IdxRef<YGEdge, &Style::margin_> margin() { void setMargin(YGEdge edge, CompactValue value) {
return {*this}; margin_[edge] = value;
} }
const Edges& position() const { CompactValue position(YGEdge edge) const {
return position_; return position_[edge];
} }
IdxRef<YGEdge, &Style::position_> position() { void setPosition(YGEdge edge, CompactValue value) {
return {*this}; position_[edge] = value;
} }
const Edges& padding() const { CompactValue padding(YGEdge edge) const {
return padding_; return padding_[edge];
} }
IdxRef<YGEdge, &Style::padding_> padding() { void setPadding(YGEdge edge, CompactValue value) {
return {*this}; padding_[edge] = value;
} }
const Edges& border() const { CompactValue border(YGEdge edge) const {
return border_; return border_[edge];
} }
IdxRef<YGEdge, &Style::border_> border() { void setBorder(YGEdge edge, CompactValue value) {
return {*this}; border_[edge] = value;
} }
CompactValue gap(Gutter gutter) const { CompactValue gap(Gutter gutter) const {