From 9f7e70363a36807f0113fb958b0438cc5003a66a Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Wed, 14 Mar 2018 04:17:07 -0700 Subject: [PATCH] Changed the return type of YGResolveValue Reviewed By: emilsjolander Differential Revision: D7195099 fbshipit-source-id: 72c4163cd08691cf6e40df05394cc52e83b0de14 --- ReactCommon/yoga/yoga/Utils.cpp | 4 ++ ReactCommon/yoga/yoga/Utils.h | 17 +++-- ReactCommon/yoga/yoga/YGNode.cpp | 30 ++++----- ReactCommon/yoga/yoga/Yoga.cpp | 106 +++++++++++++++---------------- 4 files changed, 79 insertions(+), 78 deletions(-) diff --git a/ReactCommon/yoga/yoga/Utils.cpp b/ReactCommon/yoga/yoga/Utils.cpp index 5ca834c84..c57e5af3f 100644 --- a/ReactCommon/yoga/yoga/Utils.cpp +++ b/ReactCommon/yoga/yoga/Utils.cpp @@ -53,3 +53,7 @@ bool YGFloatsEqual(const float a, const float b) { float YGFloatSanitize(const float& val) { return YGFloatIsUndefined(val) ? 0 : val; } + +float YGUnwrapFloatOptional(const YGFloatOptional& op) { + return op.isUndefined ? YGUndefined : op.value; +} diff --git a/ReactCommon/yoga/yoga/Utils.h b/ReactCommon/yoga/yoga/Utils.h index 44cfd300f..6fbd33a44 100644 --- a/ReactCommon/yoga/yoga/Utils.h +++ b/ReactCommon/yoga/yoga/Utils.h @@ -89,6 +89,11 @@ bool YGFloatArrayEqual( // This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise float YGFloatSanitize(const float& val); +// This function unwraps optional and returns YGUndefined if not defined or +// op.value otherwise +// TODO: Get rid off this function +float YGUnwrapFloatOptional(const YGFloatOptional& op); + YGFlexDirection YGFlexDirectionCross( const YGFlexDirection flexDirection, const YGDirection direction); @@ -98,17 +103,17 @@ inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { flexDirection == YGFlexDirectionRowReverse; } -inline float YGResolveValue(const YGValue value, const float parentSize) { +inline YGFloatOptional YGResolveValue(const YGValue value, const float parentSize) { switch (value.unit) { case YGUnitUndefined: case YGUnitAuto: - return YGUndefined; + return {true, 0}; case YGUnitPoint: - return value.value; + return {false, value.value}; case YGUnitPercent: - return value.value * parentSize * 0.01; + return {false, static_cast(value.value * parentSize * 0.01)}; } - return YGUndefined; + return {true, 0}; } inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { @@ -133,5 +138,5 @@ inline YGFlexDirection YGResolveFlexDirection( static inline float YGResolveValueMargin( const YGValue value, const float parentSize) { - return value.unit == YGUnitAuto ? 0 : YGResolveValue(value, parentSize); + return value.unit == YGUnitAuto ? 0 : YGUnwrapFloatOptional(YGResolveValue(value, parentSize)); } diff --git a/ReactCommon/yoga/yoga/YGNode.cpp b/ReactCommon/yoga/yoga/YGNode.cpp index ae7fce26e..6b02eeb45 100644 --- a/ReactCommon/yoga/yoga/YGNode.cpp +++ b/ReactCommon/yoga/yoga/YGNode.cpp @@ -92,7 +92,7 @@ float YGNode::getLeadingPosition( const YGValue* leadingPosition = YGComputedEdgeValue(style_.position, YGEdgeStart, &YGValueUndefined); if (leadingPosition->unit != YGUnitUndefined) { - return YGResolveValue(*leadingPosition, axisSize); + return YGUnwrapFloatOptional(YGResolveValue(*leadingPosition, axisSize)); } } @@ -101,7 +101,7 @@ float YGNode::getLeadingPosition( return leadingPosition->unit == YGUnitUndefined ? 0.0f - : YGResolveValue(*leadingPosition, axisSize); + : YGUnwrapFloatOptional(YGResolveValue(*leadingPosition, axisSize)); } float YGNode::getTrailingPosition( @@ -111,7 +111,7 @@ float YGNode::getTrailingPosition( const YGValue* trailingPosition = YGComputedEdgeValue(style_.position, YGEdgeEnd, &YGValueUndefined); if (trailingPosition->unit != YGUnitUndefined) { - return YGResolveValue(*trailingPosition, axisSize); + return YGUnwrapFloatOptional(YGResolveValue(*trailingPosition, axisSize)); } } @@ -120,7 +120,7 @@ float YGNode::getTrailingPosition( return trailingPosition->unit == YGUnitUndefined ? 0.0f - : YGResolveValue(*trailingPosition, axisSize); + : YGUnwrapFloatOptional(YGResolveValue(*trailingPosition, axisSize)); } bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) { @@ -653,15 +653,14 @@ float YGNode::getLeadingPadding( const float widthSize) { if (YGFlexDirectionIsRow(axis) && style_.padding[YGEdgeStart].unit != YGUnitUndefined && - !YGFloatIsUndefined( - YGResolveValue(style_.padding[YGEdgeStart], widthSize)) && - YGResolveValue(style_.padding[YGEdgeStart], widthSize) > 0.0f) { - return YGResolveValue(style_.padding[YGEdgeStart], widthSize); + !YGResolveValue(style_.padding[YGEdgeStart], widthSize).isUndefined && + YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeStart], widthSize)) > 0.0f) { + return YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeStart], widthSize)); } - float resolvedValue = YGResolveValue( + float resolvedValue = YGUnwrapFloatOptional(YGResolveValue( *YGComputedEdgeValue(style_.padding, leading[axis], &YGValueZero), - widthSize); + widthSize)); return YGFloatMax(resolvedValue, 0.0f); } @@ -670,15 +669,14 @@ float YGNode::getTrailingPadding( const float widthSize) { if (YGFlexDirectionIsRow(axis) && style_.padding[YGEdgeEnd].unit != YGUnitUndefined && - !YGFloatIsUndefined( - YGResolveValue(style_.padding[YGEdgeEnd], widthSize)) && - YGResolveValue(style_.padding[YGEdgeEnd], widthSize) >= 0.0f) { - return YGResolveValue(style_.padding[YGEdgeEnd], widthSize); + !YGResolveValue(style_.padding[YGEdgeEnd], widthSize).isUndefined && + YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeEnd], widthSize)) >= 0.0f) { + return YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeEnd], widthSize)); } - float resolvedValue = YGResolveValue( + float resolvedValue = YGUnwrapFloatOptional(YGResolveValue( *YGComputedEdgeValue(style_.padding, trailing[axis], &YGValueZero), - widthSize); + widthSize)); return YGFloatMax(resolvedValue, 0.0f); } diff --git a/ReactCommon/yoga/yoga/Yoga.cpp b/ReactCommon/yoga/yoga/Yoga.cpp index 61ceee775..7b4719f3a 100644 --- a/ReactCommon/yoga/yoga/Yoga.cpp +++ b/ReactCommon/yoga/yoga/Yoga.cpp @@ -6,6 +6,7 @@ */ #include "Yoga.h" +#include #include #include #include "Utils.h" @@ -967,15 +968,15 @@ static float YGNodeBoundAxisWithinMinAndMax(const YGNodeRef node, float max = YGUndefined; if (YGFlexDirectionIsColumn(axis)) { - min = YGResolveValue( - node->getStyle().minDimensions[YGDimensionHeight], axisSize); - max = YGResolveValue( - node->getStyle().maxDimensions[YGDimensionHeight], axisSize); + min = YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().minDimensions[YGDimensionHeight], axisSize)); + max = YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().maxDimensions[YGDimensionHeight], axisSize)); } else if (YGFlexDirectionIsRow(axis)) { - min = YGResolveValue( - node->getStyle().minDimensions[YGDimensionWidth], axisSize); - max = YGResolveValue( - node->getStyle().maxDimensions[YGDimensionWidth], axisSize); + min = YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().minDimensions[YGDimensionWidth], axisSize)); + max = YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().maxDimensions[YGDimensionWidth], axisSize)); } float boundValue = value; @@ -1021,8 +1022,8 @@ static void YGConstrainMaxSizeForMode(const YGNodeRef node, YGMeasureMode *mode, float *size) { const float maxSize = - YGResolveValue( - node->getStyle().maxDimensions[dim[axis]], parentAxisSize) + + YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().maxDimensions[dim[axis]], parentAxisSize)) + node->getMarginForAxis(axis, parentWidth); switch (*mode) { case YGMeasureModeExactly: @@ -1060,7 +1061,7 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, YGMeasureMode childHeightMeasureMode; const float resolvedFlexBasis = - YGResolveValue(child->resolveFlexBasisPtr(), mainAxisParentSize); + YGUnwrapFloatOptional(YGResolveValue(child->resolveFlexBasisPtr(), mainAxisParentSize)); const bool isRowStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionRow, parentWidth); const bool isColumnStyleDimDefined = YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn, parentHeight); @@ -1078,14 +1079,14 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, } else if (isMainAxisRow && isRowStyleDimDefined) { // The width is definite, so use that as the flex basis. child->setLayoutComputedFlexBasis(YGFloatMax( - YGResolveValue( - child->getResolvedDimension(YGDimensionWidth), parentWidth), + YGUnwrapFloatOptional(YGResolveValue( + child->getResolvedDimension(YGDimensionWidth), parentWidth)), YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, parentWidth))); } else if (!isMainAxisRow && isColumnStyleDimDefined) { // The height is definite, so use that as the flex basis. child->setLayoutComputedFlexBasis(YGFloatMax( - YGResolveValue( - child->getResolvedDimension(YGDimensionHeight), parentHeight), + YGUnwrapFloatOptional(YGResolveValue( + child->getResolvedDimension(YGDimensionHeight), parentHeight)), YGNodePaddingAndBorderForAxis( child, YGFlexDirectionColumn, parentWidth))); } else { @@ -1103,15 +1104,15 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, if (isRowStyleDimDefined) { childWidth = - YGResolveValue( - child->getResolvedDimension(YGDimensionWidth), parentWidth) + + YGUnwrapFloatOptional(YGResolveValue( + child->getResolvedDimension(YGDimensionWidth), parentWidth)) + marginRow; childWidthMeasureMode = YGMeasureModeExactly; } if (isColumnStyleDimDefined) { childHeight = - YGResolveValue( - child->getResolvedDimension(YGDimensionHeight), parentHeight) + + YGUnwrapFloatOptional(YGResolveValue( + child->getResolvedDimension(YGDimensionHeight), parentHeight)) + marginColumn; childHeightMeasureMode = YGMeasureModeExactly; } @@ -1228,7 +1229,7 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, if (YGNodeIsStyleDimDefined(child, YGFlexDirectionRow, width)) { childWidth = - YGResolveValue(child->getResolvedDimension(YGDimensionWidth), width) + + YGUnwrapFloatOptional(YGResolveValue(child->getResolvedDimension(YGDimensionWidth), width)) + marginRow; } else { // If the child doesn't have a specified width, compute the width based @@ -1247,7 +1248,7 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, if (YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn, height)) { childHeight = - YGResolveValue(child->getResolvedDimension(YGDimensionHeight), height) + + YGUnwrapFloatOptional(YGResolveValue(child->getResolvedDimension(YGDimensionHeight), height)) + marginColumn; } else { // If the child doesn't have a specified height, compute the height @@ -1584,15 +1585,14 @@ static float YGNodeCalculateAvailableInnerDim( if (!YGFloatIsUndefined(availableInnerDim)) { // We want to make sure our available height does not violate min and max // constraints - const float minInnerDim = - YGFloatIsUndefined(YGResolveValue( - node->getStyle().minDimensions[dimension], parentDim)) + const YGFloatOptional minDimensionOptional = YGResolveValue(node->getStyle().minDimensions[dimension], parentDim); + const float minInnerDim = minDimensionOptional.isUndefined ? 0.0f - : YGResolveValue(node->getStyle().minDimensions[dimension], parentDim) - - paddingAndBorder; - const float maxInnerDim = - YGResolveValue(node->getStyle().maxDimensions[dimension], parentDim) - - paddingAndBorder; + : minDimensionOptional.value - paddingAndBorder; + + const YGFloatOptional maxDimensionOptional = YGResolveValue(node->getStyle().maxDimensions[dimension], parentDim) ; + + const float maxInnerDim = maxDimensionOptional.isUndefined ? FLT_MAX : maxDimensionOptional.value - paddingAndBorder; availableInnerDim = YGFloatMax(YGFloatMin(availableInnerDim, maxInnerDim), minInnerDim); } @@ -1881,9 +1881,9 @@ static float YGDistributeFreeSpaceSecondPass( : YGMeasureModeAtMost; } else { childCrossSize = - YGResolveValue( + YGUnwrapFloatOptional(YGResolveValue( currentRelativeChild->getResolvedDimension(dim[crossAxis]), - availableInnerCrossDim) + + availableInnerCrossDim)) + marginCross; const bool isLoosePercentageMeasurement = currentRelativeChild->getResolvedDimension(dim[crossAxis]).unit == @@ -2124,12 +2124,11 @@ static void YGJustifyMainAxis( if (measureModeMainDim == YGMeasureModeAtMost && collectedFlexItemsValues.remainingFreeSpace > 0) { if (style.minDimensions[dim[mainAxis]].unit != YGUnitUndefined && - !YGFloatIsUndefined(YGResolveValue( - style.minDimensions[dim[mainAxis]], mainAxisParentSize))) { + !YGResolveValue(style.minDimensions[dim[mainAxis]], mainAxisParentSize).isUndefined) { collectedFlexItemsValues.remainingFreeSpace = YGFloatMax( 0, - YGResolveValue( - style.minDimensions[dim[mainAxis]], mainAxisParentSize) - + YGUnwrapFloatOptional(YGResolveValue( + style.minDimensions[dim[mainAxis]], mainAxisParentSize)) - (availableInnerMainDim - collectedFlexItemsValues.remainingFreeSpace)); } else { @@ -2487,20 +2486,17 @@ static void YGNodelayoutImpl(const YGNodeRef node, node->getMarginForAxis(YGFlexDirectionColumn, parentWidth); const float minInnerWidth = - YGResolveValue( - node->getStyle().minDimensions[YGDimensionWidth], parentWidth) - + YGUnwrapFloatOptional(YGResolveValue(node->getStyle().minDimensions[YGDimensionWidth], parentWidth)) - paddingAndBorderAxisRow; const float maxInnerWidth = - YGResolveValue( - node->getStyle().maxDimensions[YGDimensionWidth], parentWidth) - + YGUnwrapFloatOptional(YGResolveValue(node->getStyle().maxDimensions[YGDimensionWidth], parentWidth)) - paddingAndBorderAxisRow; const float minInnerHeight = - YGResolveValue( - node->getStyle().minDimensions[YGDimensionHeight], parentHeight) - + YGUnwrapFloatOptional(YGResolveValue(node->getStyle().minDimensions[YGDimensionHeight], parentHeight)) - paddingAndBorderAxisColumn; const float maxInnerHeight = - YGResolveValue( - node->getStyle().maxDimensions[YGDimensionHeight], parentHeight) - + YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().maxDimensions[YGDimensionHeight], parentHeight)) - paddingAndBorderAxisColumn; const float minInnerMainDim = isMainAxisRow ? minInnerWidth : minInnerHeight; @@ -3629,15 +3625,15 @@ void YGNodeCalculateLayout( YGMeasureMode widthMeasureMode = YGMeasureModeUndefined; if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, parentWidth)) { width = - YGResolveValue( - node->getResolvedDimension(dim[YGFlexDirectionRow]), parentWidth) + + YGUnwrapFloatOptional(YGResolveValue( + node->getResolvedDimension(dim[YGFlexDirectionRow]), parentWidth)) + node->getMarginForAxis(YGFlexDirectionRow, parentWidth); widthMeasureMode = YGMeasureModeExactly; - } else if (!YGFloatIsUndefined(YGResolveValue( + } else if (!YGResolveValue( node->getStyle().maxDimensions[YGDimensionWidth], - parentWidth))) { - width = YGResolveValue( - node->getStyle().maxDimensions[YGDimensionWidth], parentWidth); + parentWidth).isUndefined) { + width = YGUnwrapFloatOptional(YGResolveValue( + node->getStyle().maxDimensions[YGDimensionWidth], parentWidth)); widthMeasureMode = YGMeasureModeAtMost; } else { width = parentWidth; @@ -3648,16 +3644,14 @@ void YGNodeCalculateLayout( float height = YGUndefined; YGMeasureMode heightMeasureMode = YGMeasureModeUndefined; if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, parentHeight)) { - height = YGResolveValue( + height = YGUnwrapFloatOptional(YGResolveValue( node->getResolvedDimension(dim[YGFlexDirectionColumn]), - parentHeight) + + parentHeight)) + node->getMarginForAxis(YGFlexDirectionColumn, parentWidth); heightMeasureMode = YGMeasureModeExactly; - } else if (!YGFloatIsUndefined(YGResolveValue( - node->getStyle().maxDimensions[YGDimensionHeight], - parentHeight))) { - height = YGResolveValue( - node->getStyle().maxDimensions[YGDimensionHeight], parentHeight); + } else if (!YGResolveValue(node->getStyle().maxDimensions[YGDimensionHeight], + parentHeight).isUndefined) { + height = YGUnwrapFloatOptional(YGResolveValue(node->getStyle().maxDimensions[YGDimensionHeight], parentHeight)); heightMeasureMode = YGMeasureModeAtMost; } else { height = parentHeight;