From 3e322e60e4aa0e5ca868421299cf90ebf5cf95ba Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Wed, 4 Apr 2018 07:55:30 -0700 Subject: [PATCH] Updated the implementation of leading padding Summary: Changed the arguments for the getter of leading padding to avoid copies. Added an assetion in getter of leading padding, as padding would always be defined even in the case when the user has not explicitly defined the value. In these cases it would take the default value of 0. So changing the type of `getLayoutPadding` to `YGFloatOptional`, doesn't make sense. Reviewed By: emilsjolander Differential Revision: D7336690 fbshipit-source-id: b2a2f010026f26fc2cc9fb35ad921da8f7017c9f --- yoga/Utils.cpp | 9 +++++++++ yoga/Utils.h | 4 ++++ yoga/YGNode.cpp | 23 ++++++++++++----------- yoga/YGNode.h | 4 +++- yoga/Yoga.cpp | 8 ++++++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/yoga/Utils.cpp b/yoga/Utils.cpp index 5c670c31..6fa8df82 100644 --- a/yoga/Utils.cpp +++ b/yoga/Utils.cpp @@ -57,3 +57,12 @@ float YGFloatSanitize(const float& val) { float YGUnwrapFloatOptional(const YGFloatOptional& op) { return op.isUndefined() ? YGUndefined : op.getValue(); } + +YGFloatOptional YGFloatOptionalMax( + const YGFloatOptional& op1, + const YGFloatOptional& op2) { + if (!op1.isUndefined() && !op2.isUndefined()) { + return op1.getValue() > op2.getValue() ? op1 : op2; + } + return op1.isUndefined() ? op2 : op1; +} diff --git a/yoga/Utils.h b/yoga/Utils.h index ce45e348..6c95b1e7 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -65,6 +65,10 @@ bool YGFloatsEqual(const float a, const float b); // compiler flag. float YGFloatMax(const float a, const float b); +YGFloatOptional YGFloatOptionalMax( + const YGFloatOptional& op1, + const YGFloatOptional& op2); + // We need custom min function, since we want that, if one argument is // YGUndefined then the min funtion should return the other argument as the min // value. We wouldn't have needed a custom min function if YGUndefined was NAN diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index d10f1941..913ec329 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -654,21 +654,21 @@ float YGNode::getTrailingBorder(const YGFlexDirection flexDirection) const { return YGFloatMax(computedEdgeValue, 0.0f); } -float YGNode::getLeadingPadding( - const YGFlexDirection axis, - const float widthSize) const { +YGFloatOptional YGNode::getLeadingPadding( + const YGFlexDirection& axis, + const float& widthSize) const { + const YGFloatOptional& paddingEdgeStart = + YGResolveValue(style_.padding[YGEdgeStart], widthSize); if (YGFlexDirectionIsRow(axis) && style_.padding[YGEdgeStart].unit != YGUnitUndefined && - !YGResolveValue(style_.padding[YGEdgeStart], widthSize).isUndefined() && - YGUnwrapFloatOptional( - YGResolveValue(style_.padding[YGEdgeStart], widthSize)) > 0.0f) { - return YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeStart], widthSize)); + !paddingEdgeStart.isUndefined() && paddingEdgeStart.getValue() > 0.0f) { + return paddingEdgeStart; } - float resolvedValue = YGUnwrapFloatOptional(YGResolveValue( + YGFloatOptional resolvedValue = YGResolveValue( *YGComputedEdgeValue(style_.padding, leading[axis], &YGValueZero), - widthSize)); - return YGFloatMax(resolvedValue, 0.0f); + widthSize); + return YGFloatOptionalMax(resolvedValue, YGFloatOptional(0.0f)); } float YGNode::getTrailingPadding( @@ -692,7 +692,8 @@ float YGNode::getTrailingPadding( float YGNode::getLeadingPaddingAndBorder( const YGFlexDirection axis, const float widthSize) const { - return getLeadingPadding(axis, widthSize) + getLeadingBorder(axis); + return YGUnwrapFloatOptional(getLeadingPadding(axis, widthSize)) + + getLeadingBorder(axis); } float YGNode::getTrailingPaddingAndBorder( diff --git a/yoga/YGNode.h b/yoga/YGNode.h index 02afb830..cdb6fae4 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -93,7 +93,9 @@ struct YGNode { float getTrailingMargin(const YGFlexDirection axis, const float widthSize) const; float getLeadingBorder(const YGFlexDirection flexDirection) const; float getTrailingBorder(const YGFlexDirection flexDirection) const; - float getLeadingPadding(const YGFlexDirection axis, const float widthSize) const; + YGFloatOptional getLeadingPadding( + const YGFlexDirection& axis, + const float& widthSize) const; float getTrailingPadding(const YGFlexDirection axis, const float widthSize) const; float getLeadingPaddingAndBorder( const YGFlexDirection axis, diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 271eff3a..12f8facb 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -2573,11 +2573,15 @@ static void YGNodelayoutImpl(const YGNodeRef node, node->getTrailingBorder(flexColumnDirection), YGEdgeBottom); node->setLayoutPadding( - node->getLeadingPadding(flexRowDirection, ownerWidth), YGEdgeStart); + YGUnwrapFloatOptional( + node->getLeadingPadding(flexRowDirection, ownerWidth)), + YGEdgeStart); node->setLayoutPadding( node->getTrailingPadding(flexRowDirection, ownerWidth), YGEdgeEnd); node->setLayoutPadding( - node->getLeadingPadding(flexColumnDirection, ownerWidth), YGEdgeTop); + YGUnwrapFloatOptional( + node->getLeadingPadding(flexColumnDirection, ownerWidth)), + YGEdgeTop); node->setLayoutPadding( node->getTrailingPadding(flexColumnDirection, ownerWidth), YGEdgeBottom);