Remove usage of Dimension arrays and YGDimension as index (#1402)

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

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

This change hides away most usages of YGDimension as an index. We do this for a couple reasons:

1. Right now the style interface may return a full array of resolved edge or dimension values, as a CompactValue. As we abstract away from CompactValue, and move towards ValuePool, this will no longer be the internal interface, and cheap to return. We instead change the interface to return a single value at once, which lets us resolve values lazily.

2. As we move internal usage to scoped enums, enums are not implicitly convertible to intergers (broadly a good thing). Hiding the enum as index prevents the need for callers to cast or convert to underlying.

Instead of making a new version of `IdxRef` for this, I converted to a more traditional setter. I will be making similar changes later for other styles, when I hide CompactValue from the public interface.

To review I would recommend filtering to changes in `xplat`, or viewing this in a single one of the OSS PRs exported. Everything apart from the below 20 files is a mirror.

{F1096792573}

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D49362819

fbshipit-source-id: 30d730d78e62f36597d43f477120f65694e51ea3
This commit is contained in:
Nick Gerleman
2023-09-20 16:19:59 -07:00
committed by Facebook GitHub Bot
parent 81754d8cb2
commit 83705c2942
14 changed files with 220 additions and 259 deletions

View File

@@ -14,7 +14,7 @@ namespace facebook::yoga {
bool LayoutResults::operator==(LayoutResults layout) const {
bool isEqual = yoga::inexactEquals(position, layout.position) &&
yoga::inexactEquals(dimensions, layout.dimensions) &&
yoga::inexactEquals(dimensions_, layout.dimensions_) &&
yoga::inexactEquals(margin, layout.margin) &&
yoga::inexactEquals(border, layout.border) &&
yoga::inexactEquals(padding, layout.padding) &&
@@ -30,15 +30,15 @@ bool LayoutResults::operator==(LayoutResults layout) const {
isEqual = isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i];
}
if (!yoga::isUndefined(measuredDimensions[0]) ||
!yoga::isUndefined(layout.measuredDimensions[0])) {
if (!yoga::isUndefined(measuredDimensions_[0]) ||
!yoga::isUndefined(layout.measuredDimensions_[0])) {
isEqual =
isEqual && (measuredDimensions[0] == layout.measuredDimensions[0]);
isEqual && (measuredDimensions_[0] == layout.measuredDimensions_[0]);
}
if (!yoga::isUndefined(measuredDimensions[1]) ||
!yoga::isUndefined(layout.measuredDimensions[1])) {
if (!yoga::isUndefined(measuredDimensions_[1]) ||
!yoga::isUndefined(layout.measuredDimensions_[1])) {
isEqual =
isEqual && (measuredDimensions[1] == layout.measuredDimensions[1]);
isEqual && (measuredDimensions_[1] == layout.measuredDimensions_[1]);
}
return isEqual;

View File

@@ -22,7 +22,6 @@ struct LayoutResults {
static constexpr int32_t MaxCachedMeasurements = 8;
std::array<float, 4> position = {};
std::array<float, 2> dimensions = {{YGUndefined, YGUndefined}};
std::array<float, 4> margin = {};
std::array<float, 4> border = {};
std::array<float, 4> padding = {};
@@ -31,6 +30,9 @@ struct LayoutResults {
Direction direction_ : bitCount<Direction>() = Direction::Inherit;
bool hadOverflow_ : 1 = false;
std::array<float, 2> dimensions_ = {{YGUndefined, YGUndefined}};
std::array<float, 2> measuredDimensions_ = {{YGUndefined, YGUndefined}};
public:
uint32_t computedFlexBasisGeneration = 0;
FloatOptional computedFlexBasis = {};
@@ -42,7 +44,6 @@ struct LayoutResults {
uint32_t nextCachedMeasurementsIndex = 0;
std::array<CachedMeasurement, MaxCachedMeasurements> cachedMeasurements = {};
std::array<float, 2> measuredDimensions = {{YGUndefined, YGUndefined}};
CachedMeasurement cachedLayout{};
@@ -57,10 +58,27 @@ struct LayoutResults {
bool hadOverflow() const {
return hadOverflow_;
}
void setHadOverflow(bool hadOverflow) {
hadOverflow_ = hadOverflow;
}
float dimension(YGDimension axis) const {
return dimensions_[axis];
}
void setDimension(YGDimension axis, float dimension) {
dimensions_[axis] = dimension;
}
float measuredDimension(YGDimension axis) const {
return measuredDimensions_[axis];
}
void setMeasuredDimension(YGDimension axis, float dimension) {
measuredDimensions_[axis] = dimension;
}
bool operator==(LayoutResults layout) const;
bool operator!=(LayoutResults layout) const {
return !(*this == layout);

View File

@@ -340,8 +340,7 @@ void Node::setLayoutComputedFlexBasisGeneration(
void Node::setLayoutMeasuredDimension(
float measuredDimension,
YGDimension dimension) {
layout_.measuredDimensions[static_cast<size_t>(dimension)] =
measuredDimension;
layout_.setMeasuredDimension(dimension, measuredDimension);
}
void Node::setLayoutHadOverflow(bool hadOverflow) {
@@ -349,7 +348,7 @@ void Node::setLayoutHadOverflow(bool hadOverflow) {
}
void Node::setLayoutDimension(float dimensionValue, YGDimension dimension) {
layout_.dimensions[static_cast<size_t>(dimension)] = dimensionValue;
layout_.setDimension(dimension, dimensionValue);
}
// If both left and right are defined, then use left. Otherwise return +left or
@@ -437,12 +436,11 @@ void Node::resolveDimension() {
using namespace yoga;
const Style& style = getStyle();
for (auto dim : {YGDimensionWidth, YGDimensionHeight}) {
if (!style.maxDimensions()[dim].isUndefined() &&
yoga::inexactEquals(
style.maxDimensions()[dim], style.minDimensions()[dim])) {
resolvedDimensions_[dim] = style.maxDimensions()[dim];
if (!style.maxDimension(dim).isUndefined() &&
yoga::inexactEquals(style.maxDimension(dim), style.minDimension(dim))) {
resolvedDimensions_[dim] = style.maxDimension(dim);
} else {
resolvedDimensions_[dim] = style.dimensions()[dim];
resolvedDimensions_[dim] = style.dimension(dim);
}
}
}