From f0490d121737c8acc59e2ce181cf92915621f678 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Wed, 4 Apr 2018 07:55:46 -0700 Subject: [PATCH] Refactored relativePosition private method to YGFloatOptional return type Reviewed By: emilsjolander Differential Revision: D7344939 fbshipit-source-id: 72443d38426a211d3e84be814e6a61baf4d26703 --- ReactCommon/yoga/yoga/YGNode.cpp | 40 +++++++++++++++++++++----------- ReactCommon/yoga/yoga/YGNode.h | 8 ++++--- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/ReactCommon/yoga/yoga/YGNode.cpp b/ReactCommon/yoga/yoga/YGNode.cpp index 68d7b1040..7d29caf04 100644 --- a/ReactCommon/yoga/yoga/YGNode.cpp +++ b/ReactCommon/yoga/yoga/YGNode.cpp @@ -123,7 +123,7 @@ YGFloatOptional YGNode::getTrailingPosition( : YGResolveValue(*trailingPosition, axisSize); } -bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) const { +bool YGNode::isLeadingPositionDefined(const YGFlexDirection& axis) const { return (YGFlexDirectionIsRow(axis) && YGComputedEdgeValue(style_.position, YGEdgeStart, &YGValueUndefined) ->unit != YGUnitUndefined) || @@ -131,7 +131,7 @@ bool YGNode::isLeadingPositionDefined(const YGFlexDirection axis) const { ->unit != YGUnitUndefined; } -bool YGNode::isTrailingPosDefined(const YGFlexDirection axis) const { +bool YGNode::isTrailingPosDefined(const YGFlexDirection& axis) const { return (YGFlexDirectionIsRow(axis) && YGComputedEdgeValue(style_.position, YGEdgeEnd, &YGValueUndefined) ->unit != YGUnitUndefined) || @@ -340,12 +340,18 @@ void YGNode::setLayoutDimension(float dimension, int index) { // If both left and right are defined, then use left. Otherwise return // +left or -right depending on which is defined. -float YGNode::relativePosition( - const YGFlexDirection axis, - const float axisSize) { - return isLeadingPositionDefined(axis) - ? YGUnwrapFloatOptional(getLeadingPosition(axis, axisSize)) - : -YGUnwrapFloatOptional(getTrailingPosition(axis, axisSize)); +YGFloatOptional YGNode::relativePosition( + const YGFlexDirection& axis, + const float& axisSize) const { + if (isLeadingPositionDefined(axis)) { + return getLeadingPosition(axis, axisSize); + } + + YGFloatOptional trailingPosition = getTrailingPosition(axis, axisSize); + if (!trailingPosition.isUndefined()) { + trailingPosition.setValue(-1 * trailingPosition.getValue()); + } + return trailingPosition; } void YGNode::setPosition( @@ -362,20 +368,26 @@ void YGNode::setPosition( const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, directionRespectingRoot); - const float relativePositionMain = relativePosition(mainAxis, mainSize); - const float relativePositionCross = relativePosition(crossAxis, crossSize); + const YGFloatOptional relativePositionMain = + relativePosition(mainAxis, mainSize); + const YGFloatOptional relativePositionCross = + relativePosition(crossAxis, crossSize); setLayoutPosition( - getLeadingMargin(mainAxis, ownerWidth) + relativePositionMain, + getLeadingMargin(mainAxis, ownerWidth) + + YGUnwrapFloatOptional(relativePositionMain), leading[mainAxis]); setLayoutPosition( - getTrailingMargin(mainAxis, ownerWidth) + relativePositionMain, + getTrailingMargin(mainAxis, ownerWidth) + + YGUnwrapFloatOptional(relativePositionMain), trailing[mainAxis]); setLayoutPosition( - getLeadingMargin(crossAxis, ownerWidth) + relativePositionCross, + getLeadingMargin(crossAxis, ownerWidth) + + YGUnwrapFloatOptional(relativePositionCross), leading[crossAxis]); setLayoutPosition( - getTrailingMargin(crossAxis, ownerWidth) + relativePositionCross, + getTrailingMargin(crossAxis, ownerWidth) + + YGUnwrapFloatOptional(relativePositionCross), trailing[crossAxis]); } diff --git a/ReactCommon/yoga/yoga/YGNode.h b/ReactCommon/yoga/yoga/YGNode.h index 876379915..95fac7214 100644 --- a/ReactCommon/yoga/yoga/YGNode.h +++ b/ReactCommon/yoga/yoga/YGNode.h @@ -30,7 +30,9 @@ struct YGNode { bool isDirty_; std::array resolvedDimensions_; - float relativePosition(const YGFlexDirection axis, const float axisSize); + YGFloatOptional relativePosition( + const YGFlexDirection& axis, + const float& axisSize) const; public: YGNode(); @@ -87,8 +89,8 @@ struct YGNode { // Methods related to positions, margin, padding and border YGFloatOptional getLeadingPosition(const YGFlexDirection& axis, const float& axisSize) const; - bool isLeadingPositionDefined(const YGFlexDirection axis) const; - bool isTrailingPosDefined(const YGFlexDirection axis) const; + bool isLeadingPositionDefined(const YGFlexDirection& axis) const; + bool isTrailingPosDefined(const YGFlexDirection& axis) const; YGFloatOptional getTrailingPosition( const YGFlexDirection& axis, const float& axisSize) const;