From 0186ccf0068579e6f7c5864c4cf41973d6d8dff1 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Wed, 4 Apr 2018 07:55:39 -0700 Subject: [PATCH] Changed the type of computedFlexBasis to YGFloatOptional in YGLayout Reviewed By: emilsjolander Differential Revision: D7340413 fbshipit-source-id: 39247b2b582a682e602f49f58b4bbd2bf0c995af --- ReactCommon/yoga/yoga/YGFloatOptional.h | 6 ++- ReactCommon/yoga/yoga/YGLayout.cpp | 9 ++-- ReactCommon/yoga/yoga/YGLayout.h | 3 +- ReactCommon/yoga/yoga/YGNode.cpp | 5 ++- ReactCommon/yoga/yoga/YGNode.h | 2 +- ReactCommon/yoga/yoga/Yoga.cpp | 60 ++++++++++++++----------- 6 files changed, 48 insertions(+), 37 deletions(-) diff --git a/ReactCommon/yoga/yoga/YGFloatOptional.h b/ReactCommon/yoga/yoga/YGFloatOptional.h index 9ecad46f3..21af2a806 100644 --- a/ReactCommon/yoga/yoga/YGFloatOptional.h +++ b/ReactCommon/yoga/yoga/YGFloatOptional.h @@ -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. diff --git a/ReactCommon/yoga/yoga/YGLayout.cpp b/ReactCommon/yoga/yoga/YGLayout.cpp index 117638d7a..6e367bd50 100644 --- a/ReactCommon/yoga/yoga/YGLayout.cpp +++ b/ReactCommon/yoga/yoga/YGLayout.cpp @@ -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 = diff --git a/ReactCommon/yoga/yoga/YGLayout.h b/ReactCommon/yoga/yoga/YGLayout.h index 81cc66317..46ca130d2 100644 --- a/ReactCommon/yoga/yoga/YGLayout.h +++ b/ReactCommon/yoga/yoga/YGLayout.h @@ -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 diff --git a/ReactCommon/yoga/yoga/YGNode.cpp b/ReactCommon/yoga/yoga/YGNode.cpp index 06a583951..eb840ef1d 100644 --- a/ReactCommon/yoga/yoga/YGNode.cpp +++ b/ReactCommon/yoga/yoga/YGNode.cpp @@ -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(); } diff --git a/ReactCommon/yoga/yoga/YGNode.h b/ReactCommon/yoga/yoga/YGNode.h index d65b0b71d..fe24fa037 100644 --- a/ReactCommon/yoga/yoga/YGNode.h +++ b/ReactCommon/yoga/yoga/YGNode.h @@ -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); diff --git a/ReactCommon/yoga/yoga/Yoga.cpp b/ReactCommon/yoga/yoga/Yoga.cpp index 19cb30df1..3b117243e 100644 --- a/ReactCommon/yoga/yoga/Yoga.cpp +++ b/ReactCommon/yoga/yoga/Yoga.cpp @@ -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