Skip to content

Commit

Permalink
CompactValue -> Style::Length (facebook#1458)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#41392


We're moving `CompactValue` to be an internal detail of `yoga::Style`, where users outside of the style will be dealing with a resolved/non-compact representation.

This change renames usages of `CompactValue` to `Style::Length`, which will be Yoga's representation for CSS input lengths. Right now one is just a type alias of the other, but this will let us change the internals of CompactValue with the rest of the world looking the same.

A few factory functions are added to `yoga::value` for creating CSS values. There are some shenanigans around how we want to represent CSS pixels (one YGUnitPoint), when we also end up adding CSS points (slightly larger than one YGUnitPoint). For now, I reused `point` until making other changes.

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D51000389
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 25, 2023
1 parent a822f26 commit 425885e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 121 deletions.
13 changes: 2 additions & 11 deletions tests/CompactValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,11 @@ TEST(YogaTest, dedicated_unit_factories) {
ASSERT_EQ(
CompactValue::of<YGUnitPercent>(123.456f),
CompactValue(YGValue{123.456f, YGUnitPercent}));
}

TEST(YogaTest, dedicated_unit_maybe_factories) {
ASSERT_EQ(
CompactValue::ofMaybe<YGUnitPoint>(-9876.5f),
CompactValue(YGValue{-9876.5f, YGUnitPoint}));
ASSERT_EQ(
CompactValue::ofMaybe<YGUnitPoint>(YGUndefined),
CompactValue::of<YGUnitPoint>(YGUndefined),
CompactValue(YGValueUndefined));
ASSERT_EQ(
CompactValue::ofMaybe<YGUnitPercent>(123.456f),
CompactValue(YGValue{123.456f, YGUnitPercent}));
ASSERT_EQ(
CompactValue::ofMaybe<YGUnitPercent>(YGUndefined),
CompactValue::of<YGUnitPercent>(YGUndefined),
CompactValue(YGValueUndefined));
}

Expand Down
96 changes: 39 additions & 57 deletions yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ void updateStyle(
}
}

template <typename Ref, typename T>
void updateStyle(YGNodeRef node, Ref (Style::*prop)(), T value) {
template <typename Ref, typename ValueT>
void updateStyle(YGNodeRef node, Ref (Style::*prop)(), ValueT value) {
updateStyle(
resolveRef(node),
value,
[prop](Style& s, T x) { return (s.*prop)() != x; },
[prop](Style& s, T x) { (s.*prop)() = x; });
[prop](Style& s, ValueT x) { return (s.*prop)() != x; },
[prop](Style& s, ValueT x) { (s.*prop)() = x; });
}

template <auto GetterT, auto SetterT, typename IdxT>
void updateIndexedStyleProp(YGNodeRef node, IdxT idx, CompactValue value) {
template <auto GetterT, auto SetterT, typename IdxT, typename ValueT>
void updateIndexedStyleProp(YGNodeRef node, IdxT idx, ValueT value) {
updateStyle(
resolveRef(node),
value,
[idx](Style& s, CompactValue x) { return (s.*GetterT)(idx) != x; },
[idx](Style& s, CompactValue x) { (s.*SetterT)(idx, x); });
[idx](Style& s, ValueT x) { return (s.*GetterT)(idx) != x; },
[idx](Style& s, ValueT x) { (s.*SetterT)(idx, x); });
}

} // namespace
Expand Down Expand Up @@ -198,20 +198,19 @@ float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) {
}

void YGNodeStyleSetFlexBasis(const YGNodeRef node, const float flexBasis) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(flexBasis);
updateStyle<MSVC_HINT(flexBasis)>(node, &Style::flexBasis, value);
updateStyle<MSVC_HINT(flexBasis)>(
node, &Style::flexBasis, value::points(flexBasis));
}

void YGNodeStyleSetFlexBasisPercent(
const YGNodeRef node,
const float flexBasisPercent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(flexBasisPercent);
updateStyle<MSVC_HINT(flexBasis)>(node, &Style::flexBasis, value);
updateStyle<MSVC_HINT(flexBasis)>(
node, &Style::flexBasis, value::percent(flexBasisPercent));
}

void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) {
updateStyle<MSVC_HINT(flexBasis)>(
node, &Style::flexBasis, CompactValue::ofAuto());
updateStyle<MSVC_HINT(flexBasis)>(node, &Style::flexBasis, value::ofAuto());
}

YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
Expand All @@ -223,50 +222,46 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
}

void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
node, edge, value::points(points));
}

void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
node, edge, value::percent(percent));
}

YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().position(edge);
}

void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, edge, value::points(points));
}

void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, edge, value::percent(percent));
}

void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, edge, CompactValue::ofAuto());
node, edge, value::ofAuto());
}

YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().margin(edge);
}

void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
node, edge, value::points(points));
}

void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
node, edge, value::percent(percent));
}

YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) {
Expand All @@ -277,8 +272,8 @@ void YGNodeStyleSetBorder(
const YGNodeRef node,
const YGEdge edge,
const float border) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(border);
updateIndexedStyleProp<&Style::border, &Style::setBorder>(node, edge, value);
updateIndexedStyleProp<&Style::border, &Style::setBorder>(
node, edge, value::points(border));
}

float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) {
Expand All @@ -294,9 +289,8 @@ void YGNodeStyleSetGap(
const YGNodeRef node,
const YGGutter gutter,
const float gapLength) {
auto length = CompactValue::ofMaybe<YGUnitPoint>(gapLength);
updateIndexedStyleProp<&Style::gap, &Style::setGap>(
node, scopedEnum(gutter), length);
node, scopedEnum(gutter), value::points(gapLength));
}

float YGNodeStyleGetGap(const YGNodeConstRef node, const YGGutter gutter) {
Expand All @@ -319,109 +313,97 @@ float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) {
}

void YGNodeStyleSetWidth(YGNodeRef node, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::points(points));
}

void YGNodeStyleSetWidthPercent(YGNodeRef node, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::percent(percent));
}

void YGNodeStyleSetWidthAuto(YGNodeRef node) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Width, CompactValue::ofAuto());
node, Dimension::Width, value::ofAuto());
}

YGValue YGNodeStyleGetWidth(YGNodeConstRef node) {
return resolveRef(node)->getStyle().dimension(Dimension::Width);
}

void YGNodeStyleSetHeight(YGNodeRef node, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::points(points));
}

void YGNodeStyleSetHeightPercent(YGNodeRef node, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::percent(percent));
}

void YGNodeStyleSetHeightAuto(YGNodeRef node) {
updateIndexedStyleProp<&Style::dimension, &Style::setDimension>(
node, Dimension::Height, CompactValue::ofAuto());
node, Dimension::Height, value::ofAuto());
}

YGValue YGNodeStyleGetHeight(YGNodeConstRef node) {
return resolveRef(node)->getStyle().dimension(Dimension::Height);
}

void YGNodeStyleSetMinWidth(const YGNodeRef node, const float minWidth) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(minWidth);
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::points(minWidth));
}

void YGNodeStyleSetMinWidthPercent(const YGNodeRef node, const float minWidth) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(minWidth);
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::percent(minWidth));
}

YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) {
return resolveRef(node)->getStyle().minDimension(Dimension::Width);
}

void YGNodeStyleSetMinHeight(const YGNodeRef node, const float minHeight) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(minHeight);
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::points(minHeight));
}

void YGNodeStyleSetMinHeightPercent(
const YGNodeRef node,
const float minHeight) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(minHeight);
updateIndexedStyleProp<&Style::minDimension, &Style::setMinDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::percent(minHeight));
}

YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) {
return resolveRef(node)->getStyle().minDimension(Dimension::Height);
}

void YGNodeStyleSetMaxWidth(const YGNodeRef node, const float maxWidth) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(maxWidth);
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::points(maxWidth));
}

void YGNodeStyleSetMaxWidthPercent(const YGNodeRef node, const float maxWidth) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(maxWidth);
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Width, value);
node, Dimension::Width, value::percent(maxWidth));
}

YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) {
return resolveRef(node)->getStyle().maxDimension(Dimension::Width);
}

void YGNodeStyleSetMaxHeight(const YGNodeRef node, const float maxHeight) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(maxHeight);
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::points(maxHeight));
}

void YGNodeStyleSetMaxHeightPercent(
const YGNodeRef node,
const float maxHeight) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(maxHeight);
updateIndexedStyleProp<&Style::maxDimension, &Style::setMaxDimension>(
node, Dimension::Height, value);
node, Dimension::Height, value::percent(maxHeight));
}

YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {
Expand Down
4 changes: 2 additions & 2 deletions yoga/algorithm/ResolveValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <yoga/Yoga.h>

#include <yoga/numeric/FloatOptional.h>
#include <yoga/style/CompactValue.h>
#include <yoga/style/Style.h>

namespace facebook::yoga {

Expand All @@ -25,7 +25,7 @@ inline FloatOptional resolveValue(const YGValue value, const float ownerSize) {
}
}

inline FloatOptional resolveValue(CompactValue value, float ownerSize) {
inline FloatOptional resolveValue(Style::Length value, float ownerSize) {
return resolveValue((YGValue)value, ownerSize);
}

Expand Down
8 changes: 4 additions & 4 deletions yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void Node::print() {

// TODO: Edge value resolution should be moved to `yoga::Style`
template <auto Field>
CompactValue Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const {
Style::Length Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const {
if ((style_.*Field)(rowEdge).isDefined()) {
return (style_.*Field)(rowEdge);
} else if ((style_.*Field)(edge).isDefined()) {
Expand All @@ -72,7 +72,7 @@ CompactValue Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const {

// TODO: Edge value resolution should be moved to `yoga::Style`
template <auto Field>
CompactValue Node::computeEdgeValueForColumn(YGEdge edge) const {
Style::Length Node::computeEdgeValueForColumn(YGEdge edge) const {
if ((style_.*Field)(edge).isDefined()) {
return (style_.*Field)(edge);
} else if ((style_.*Field)(YGEdgeVertical).isDefined()) {
Expand Down Expand Up @@ -497,8 +497,8 @@ void Node::setLayoutHadOverflow(bool hadOverflow) {
layout_.setHadOverflow(hadOverflow);
}

void Node::setLayoutDimension(float dimensionValue, Dimension dimension) {
layout_.setDimension(dimension, dimensionValue);
void Node::setLayoutDimension(float LengthValue, Dimension dimension) {
layout_.setDimension(dimension, LengthValue);
}

// If both left and right are defined, then use left. Otherwise return +left or
Expand Down
7 changes: 3 additions & 4 deletions yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <yoga/enums/MeasureMode.h>
#include <yoga/enums/NodeType.h>
#include <yoga/node/LayoutResults.h>
#include <yoga/style/CompactValue.h>
#include <yoga/style/Style.h>

// Tag struct used to form the opaque YGNodeRef for the public C API
Expand Down Expand Up @@ -66,10 +65,10 @@ class YG_EXPORT Node : public ::YGNode {
}

template <auto Field>
CompactValue computeEdgeValueForColumn(YGEdge edge) const;
Style::Length computeEdgeValueForColumn(YGEdge edge) const;

template <auto Field>
CompactValue computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const;
Style::Length computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const;

// DANGER DANGER DANGER!
// If the node assigned to has children, we'd either have to deallocate
Expand Down Expand Up @@ -327,7 +326,7 @@ class YG_EXPORT Node : public ::YGNode {
uint32_t computedFlexBasisGeneration);
void setLayoutMeasuredDimension(float measuredDimension, Dimension dimension);
void setLayoutHadOverflow(bool hadOverflow);
void setLayoutDimension(float dimensionValue, Dimension dimension);
void setLayoutDimension(float LengthValue, Dimension dimension);
void setLayoutDirection(Direction direction);
void setLayoutMargin(float margin, YGEdge edge);
void setLayoutBorder(float border, YGEdge edge);
Expand Down
Loading

0 comments on commit 425885e

Please sign in to comment.