Changed the type of computedFlexBasis to YGFloatOptional in YGLayout

Reviewed By: emilsjolander

Differential Revision: D7340413

fbshipit-source-id: 39247b2b582a682e602f49f58b4bbd2bf0c995af
This commit is contained in:
Pritesh Nandgaonkar 2018-04-04 07:55:39 -07:00 committed by Facebook Github Bot
parent fe3a9d4013
commit 0186ccf006
6 changed files with 48 additions and 37 deletions

View File

@ -5,14 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/
#pragma once
struct YGFloatOptional {
private:
float value_;
bool isUndefined_;
public:
YGFloatOptional(const float& value);
YGFloatOptional();
explicit YGFloatOptional(const float& value);
explicit YGFloatOptional();
// Program will terminate if the value of an undefined is accessed. Please
// make sure to check if the optional is defined before calling this function.

View File

@ -19,7 +19,7 @@ YGLayout::YGLayout()
padding(),
direction(YGDirectionInherit),
computedFlexBasisGeneration(0),
computedFlexBasis(YGUndefined),
computedFlexBasis(YGFloatOptional()),
hadOverflow(false),
generationCount(0),
lastOwnerDirection((YGDirection)-1),
@ -39,16 +39,13 @@ bool YGLayout::operator==(YGLayout layout) const {
direction == layout.direction && hadOverflow == layout.hadOverflow &&
lastOwnerDirection == layout.lastOwnerDirection &&
nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex &&
cachedLayout == layout.cachedLayout;
cachedLayout == layout.cachedLayout &&
computedFlexBasis == layout.computedFlexBasis;
for (uint32_t i = 0; i < YG_MAX_CACHED_RESULT_COUNT && isEqual; ++i) {
isEqual = isEqual && cachedMeasurements[i] == layout.cachedMeasurements[i];
}
if (!YGFloatIsUndefined(computedFlexBasis) ||
!YGFloatIsUndefined(layout.computedFlexBasis)) {
isEqual = isEqual && (computedFlexBasis == layout.computedFlexBasis);
}
if (!YGFloatIsUndefined(measuredDimensions[0]) ||
!YGFloatIsUndefined(layout.measuredDimensions[0])) {
isEqual =

View File

@ -6,6 +6,7 @@
*/
#pragma once
#include "YGFloatOptional.h"
#include "Yoga-internal.h"
struct YGLayout {
@ -17,7 +18,7 @@ struct YGLayout {
YGDirection direction;
uint32_t computedFlexBasisGeneration;
float computedFlexBasis;
YGFloatOptional computedFlexBasis;
bool hadOverflow;
// Instead of recomputing the entire layout every single time, we

View File

@ -312,7 +312,8 @@ void YGNode::setLayoutLastOwnerDirection(YGDirection direction) {
layout_.lastOwnerDirection = direction;
}
void YGNode::setLayoutComputedFlexBasis(float computedFlexBasis) {
void YGNode::setLayoutComputedFlexBasis(
const YGFloatOptional& computedFlexBasis) {
layout_.computedFlexBasis = computedFlexBasis;
}
@ -579,7 +580,7 @@ void YGNode::cloneChildrenIfNeeded() {
void YGNode::markDirtyAndPropogate() {
if (!isDirty_) {
setDirty(true);
setLayoutComputedFlexBasis(YGUndefined);
setLayoutComputedFlexBasis(YGFloatOptional());
if (owner_) {
owner_->markDirtyAndPropogate();
}

View File

@ -126,7 +126,7 @@ struct YGNode {
void setConfig(YGConfigRef config);
void setDirty(bool isDirty);
void setLayoutLastOwnerDirection(YGDirection direction);
void setLayoutComputedFlexBasis(float computedFlexBasis);
void setLayoutComputedFlexBasis(const YGFloatOptional& computedFlexBasis);
void setLayoutComputedFlexBasisGeneration(
uint32_t computedFlexBasisGeneration);
void setLayoutMeasuredDimension(float measuredDimension, int index);

View File

@ -1220,35 +1220,41 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
YGMeasureMode childWidthMeasureMode;
YGMeasureMode childHeightMeasureMode;
const float resolvedFlexBasis =
YGUnwrapFloatOptional(YGResolveValue(child->resolveFlexBasisPtr(), mainAxisownerSize));
const YGFloatOptional resolvedFlexBasis =
YGResolveValue(child->resolveFlexBasisPtr(), mainAxisownerSize);
const bool isRowStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionRow, ownerWidth);
const bool isColumnStyleDimDefined =
YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn, ownerHeight);
if (!YGFloatIsUndefined(resolvedFlexBasis) && !YGFloatIsUndefined(mainAxisSize)) {
if (YGFloatIsUndefined(child->getLayout().computedFlexBasis) ||
if (!resolvedFlexBasis.isUndefined() && !YGFloatIsUndefined(mainAxisSize)) {
if (child->getLayout().computedFlexBasis.isUndefined() ||
(YGConfigIsExperimentalFeatureEnabled(
child->getConfig(), YGExperimentalFeatureWebFlexBasis) &&
child->getLayout().computedFlexBasisGeneration !=
gCurrentGenerationCount)) {
child->setLayoutComputedFlexBasis(YGFloatMax(
resolvedFlexBasis,
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth)));
const YGFloatOptional& paddingAndBorder = YGFloatOptional(
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth));
child->setLayoutComputedFlexBasis(
YGFloatOptionalMax(resolvedFlexBasis, paddingAndBorder));
}
} else if (isMainAxisRow && isRowStyleDimDefined) {
// The width is definite, so use that as the flex basis.
child->setLayoutComputedFlexBasis(YGFloatMax(
YGUnwrapFloatOptional(YGResolveValue(
child->getResolvedDimension(YGDimensionWidth), ownerWidth)),
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, ownerWidth)));
const YGFloatOptional& paddingAndBorder = YGFloatOptional(
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, ownerWidth));
child->setLayoutComputedFlexBasis(YGFloatOptionalMax(
YGResolveValue(
child->getResolvedDimension(YGDimensionWidth), ownerWidth),
paddingAndBorder));
} else if (!isMainAxisRow && isColumnStyleDimDefined) {
// The height is definite, so use that as the flex basis.
child->setLayoutComputedFlexBasis(YGFloatMax(
YGUnwrapFloatOptional(YGResolveValue(
child->getResolvedDimension(YGDimensionHeight), ownerHeight)),
YGNodePaddingAndBorderForAxis(
child, YGFlexDirectionColumn, ownerWidth)));
const YGFloatOptional& paddingAndBorder =
YGFloatOptional(YGNodePaddingAndBorderForAxis(
child, YGFlexDirectionColumn, ownerWidth));
child->setLayoutComputedFlexBasis(YGFloatOptionalMax(
YGResolveValue(
child->getResolvedDimension(YGDimensionHeight), ownerHeight),
paddingAndBorder));
} else {
// Compute the flex basis and hypothetical main size (i.e. the clamped
// flex basis).
@ -1361,9 +1367,9 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
"measure",
config);
child->setLayoutComputedFlexBasis(YGFloatMax(
child->setLayoutComputedFlexBasis(YGFloatOptional(YGFloatMax(
child->getLayout().measuredDimensions[dim[mainAxis]],
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth)));
YGNodePaddingAndBorderForAxis(child, mainAxis, ownerWidth))));
}
child->setLayoutComputedFlexBasisGeneration(gCurrentGenerationCount);
}
@ -1825,7 +1831,7 @@ static void YGNodeComputeFlexBasisForChildren(
}
if (child == singleFlexChild) {
child->setLayoutComputedFlexBasisGeneration(gCurrentGenerationCount);
child->setLayoutComputedFlexBasis(0);
child->setLayoutComputedFlexBasis(YGFloatOptional(0));
} else {
YGNodeComputeFlexBasisForChild(
node,
@ -1840,7 +1846,8 @@ static void YGNodeComputeFlexBasisForChildren(
config);
}
totalOuterFlexBasis += child->getLayout().computedFlexBasis +
totalOuterFlexBasis +=
YGUnwrapFloatOptional(child->getLayout().computedFlexBasis) +
child->getMarginForAxis(mainAxis, availableInnerWidth);
}
}
@ -1880,7 +1887,7 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues(
YGUnwrapFloatOptional(YGNodeBoundAxisWithinMinAndMax(
child,
mainAxis,
child->getLayout().computedFlexBasis,
YGUnwrapFloatOptional(child->getLayout().computedFlexBasis),
mainAxisownerSize));
// If this is a multi-line flow and this item pushes us over the
@ -1906,7 +1913,8 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues(
// Unlike the grow factor, the shrink factor is scaled relative to the
// child dimension.
flexAlgoRowMeasurement.totalFlexShrinkScaledFactors +=
-child->resolveFlexShrink() * child->getLayout().computedFlexBasis;
-child->resolveFlexShrink() *
YGUnwrapFloatOptional(child->getLayout().computedFlexBasis);
}
flexAlgoRowMeasurement.relativeChildren.push_back(child);
@ -1956,7 +1964,8 @@ static float YGDistributeFreeSpaceSecondPass(
childFlexBasis = YGUnwrapFloatOptional(YGNodeBoundAxisWithinMinAndMax(
currentRelativeChild,
mainAxis,
currentRelativeChild->getLayout().computedFlexBasis,
YGUnwrapFloatOptional(
currentRelativeChild->getLayout().computedFlexBasis),
mainAxisownerSize));
float updatedMainSize = childFlexBasis;
@ -2131,7 +2140,8 @@ static void YGDistributeFreeSpaceFirstPass(
float childFlexBasis = YGUnwrapFloatOptional(YGNodeBoundAxisWithinMinAndMax(
currentRelativeChild,
mainAxis,
currentRelativeChild->getLayout().computedFlexBasis,
YGUnwrapFloatOptional(
currentRelativeChild->getLayout().computedFlexBasis),
mainAxisownerSize));
if (collectedFlexItemsValues.remainingFreeSpace < 0) {
@ -2417,7 +2427,7 @@ static void YGJustifyMainAxis(
// YGNodeDimWithMargin.
collectedFlexItemsValues.mainDim += betweenMainDim +
child->getMarginForAxis(mainAxis, availableInnerWidth) +
childLayout.computedFlexBasis;
YGUnwrapFloatOptional(childLayout.computedFlexBasis);
collectedFlexItemsValues.crossDim = availableInnerCrossDim;
} else {
// The main dimension is the sum of all the elements dimension plus