CompactValue -> Style::Length (#1458)

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

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

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

fbshipit-source-id: 00f55e72bfb8aa291b53308f8a62ac8797be490f
This commit is contained in:
Nick Gerleman
2023-11-25 20:41:22 -08:00
committed by Facebook GitHub Bot
parent a822f2635e
commit aca02406ef
8 changed files with 140 additions and 121 deletions

View File

@@ -28,18 +28,29 @@
#include <yoga/enums/Wrap.h>
#include <yoga/numeric/FloatOptional.h>
#include <yoga/style/CompactValue.h>
#include <yoga/style/ValueFactories.h>
namespace facebook::yoga {
class YG_EXPORT Style {
template <typename Enum>
using Values = std::array<CompactValue, ordinalCount<Enum>()>;
using Dimensions = Values<Dimension>;
using Edges = Values<YGEdge>;
using Gutters = Values<Gutter>;
public:
/**
* Style::Length represents a CSS Value which may be one of:
* 1. Undefined
* 2. A keyword (e.g. auto)
* 3. A CSS <length-percentage> value:
* a. <length> value (e.g. 10px)
* b. <percentage> value of a reference <length>
* 4. (soon) A math function which returns a <length-percentage> value
*
* References:
* 1. https://www.w3.org/TR/css-values-4/#lengths
* 2. https://www.w3.org/TR/css-values-4/#percentage-value
* 3. https://www.w3.org/TR/css-values-4/#mixed-percentages
* 4. https://www.w3.org/TR/css-values-4/#math
*/
using Length = CompactValue;
static constexpr float DefaultFlexGrow = 0.0f;
static constexpr float DefaultFlexShrink = 0.0f;
static constexpr float WebDefaultFlexShrink = 1.0f;
@@ -76,6 +87,10 @@ class YG_EXPORT Style {
~Style() = default;
private:
using Dimensions = std::array<Style::Length, ordinalCount<Dimension>()>;
using Edges = std::array<Style::Length, ordinalCount<Edge>()>;
using Gutters = std::array<Style::Length, ordinalCount<Gutter>()>;
static constexpr uint8_t directionOffset = 0;
static constexpr uint8_t flexdirectionOffset =
directionOffset + minimumBitCount<Direction>();
@@ -101,13 +116,13 @@ class YG_EXPORT Style {
FloatOptional flex_ = {};
FloatOptional flexGrow_ = {};
FloatOptional flexShrink_ = {};
CompactValue flexBasis_ = CompactValue::ofAuto();
Style::Length flexBasis_ = value::ofAuto();
Edges margin_ = {};
Edges position_ = {};
Edges padding_ = {};
Edges border_ = {};
Gutters gap_ = {};
Dimensions dimensions_{CompactValue::ofAuto(), CompactValue::ofAuto()};
Dimensions dimensions_{value::ofAuto(), value::ofAuto()};
Dimensions minDimensions_ = {};
Dimensions maxDimensions_ = {};
// Yoga specific properties, not compatible with flexbox specification
@@ -205,66 +220,66 @@ class YG_EXPORT Style {
return {*this};
}
CompactValue flexBasis() const {
Style::Length flexBasis() const {
return flexBasis_;
}
Ref<CompactValue, &Style::flexBasis_> flexBasis() {
Ref<Style::Length, &Style::flexBasis_> flexBasis() {
return {*this};
}
CompactValue margin(YGEdge edge) const {
Style::Length margin(YGEdge edge) const {
return margin_[edge];
}
void setMargin(YGEdge edge, CompactValue value) {
void setMargin(YGEdge edge, Style::Length value) {
margin_[edge] = value;
}
CompactValue position(YGEdge edge) const {
Style::Length position(YGEdge edge) const {
return position_[edge];
}
void setPosition(YGEdge edge, CompactValue value) {
void setPosition(YGEdge edge, Style::Length value) {
position_[edge] = value;
}
CompactValue padding(YGEdge edge) const {
Style::Length padding(YGEdge edge) const {
return padding_[edge];
}
void setPadding(YGEdge edge, CompactValue value) {
void setPadding(YGEdge edge, Style::Length value) {
padding_[edge] = value;
}
CompactValue border(YGEdge edge) const {
Style::Length border(YGEdge edge) const {
return border_[edge];
}
void setBorder(YGEdge edge, CompactValue value) {
void setBorder(YGEdge edge, Style::Length value) {
border_[edge] = value;
}
CompactValue gap(Gutter gutter) const {
Style::Length gap(Gutter gutter) const {
return gap_[yoga::to_underlying(gutter)];
}
void setGap(Gutter gutter, CompactValue value) {
void setGap(Gutter gutter, Style::Length value) {
gap_[yoga::to_underlying(gutter)] = value;
}
CompactValue dimension(Dimension axis) const {
Style::Length dimension(Dimension axis) const {
return dimensions_[yoga::to_underlying(axis)];
}
void setDimension(Dimension axis, CompactValue value) {
void setDimension(Dimension axis, Style::Length value) {
dimensions_[yoga::to_underlying(axis)] = value;
}
CompactValue minDimension(Dimension axis) const {
Style::Length minDimension(Dimension axis) const {
return minDimensions_[yoga::to_underlying(axis)];
}
void setMinDimension(Dimension axis, CompactValue value) {
void setMinDimension(Dimension axis, Style::Length value) {
minDimensions_[yoga::to_underlying(axis)] = value;
}
CompactValue maxDimension(Dimension axis) const {
Style::Length maxDimension(Dimension axis) const {
return maxDimensions_[yoga::to_underlying(axis)];
}
void setMaxDimension(Dimension axis, CompactValue value) {
void setMaxDimension(Dimension axis, Style::Length value) {
maxDimensions_[yoga::to_underlying(axis)] = value;
}
@@ -276,7 +291,7 @@ class YG_EXPORT Style {
return {*this};
}
CompactValue resolveColumnGap() const {
Length resolveColumnGap() const {
if (gap_[yoga::to_underlying(Gutter::Column)].isDefined()) {
return gap_[yoga::to_underlying(Gutter::Column)];
} else {
@@ -284,7 +299,7 @@ class YG_EXPORT Style {
}
}
CompactValue resolveRowGap() const {
Style::Length resolveRowGap() const {
if (gap_[yoga::to_underlying(Gutter::Row)].isDefined()) {
return gap_[yoga::to_underlying(Gutter::Row)];
} else {