From 0cfdb50477e861661ed72bf651d686b8420382eb Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Thu, 11 Jan 2018 04:47:35 -0800 Subject: [PATCH] Moved YGNodeLeading position as a method on YGNode Summary: Moved YGNodeLeading position as a method on YGNode Reviewed By: emilsjolander Differential Revision: D6682929 fbshipit-source-id: 3607aab1544b62b1126c5d75b2f6fb8f5ca2d45f --- yoga/Utils.cpp | 8 ++++ yoga/Utils.h | 52 +++++++++++++++++++++++++- yoga/YGNode.cpp | 20 ++++++++++ yoga/YGNode.h | 1 + yoga/Yoga-internal.h | 1 - yoga/Yoga.cpp | 89 +++++--------------------------------------- 6 files changed, 89 insertions(+), 82 deletions(-) diff --git a/yoga/Utils.cpp b/yoga/Utils.cpp index e8c27036..d7f548e6 100644 --- a/yoga/Utils.cpp +++ b/yoga/Utils.cpp @@ -9,6 +9,14 @@ #include "Utils.h" +YGFlexDirection YGFlexDirectionCross( + const YGFlexDirection flexDirection, + const YGDirection direction) { + return YGFlexDirectionIsColumn(flexDirection) + ? YGResolveFlexDirection(YGFlexDirectionRow, direction) + : YGFlexDirectionColumn; +} + bool YGValueEqual(const YGValue a, const YGValue b) { if (a.unit != b.unit) { return false; diff --git a/yoga/Utils.h b/yoga/Utils.h index 11a908d3..019ed5e5 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -7,11 +7,61 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#pragma once +#include "YGNode.h" #include "Yoga-internal.h" +bool YGValueEqual(const YGValue a, const YGValue b); + +YGFlexDirection YGFlexDirectionCross( + const YGFlexDirection flexDirection, + const YGDirection direction); + inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { return flexDirection == YGFlexDirectionRow || flexDirection == YGFlexDirectionRowReverse; } -bool YGValueEqual(const YGValue a, const YGValue b); +inline float YGResolveValue(const YGValue value, const float parentSize) { + switch (value.unit) { + case YGUnitUndefined: + case YGUnitAuto: + return YGUndefined; + case YGUnitPoint: + return value.value; + case YGUnitPercent: + return value.value * parentSize / 100.0f; + } + return YGUndefined; +} + +inline bool YGNodeIsLeadingPosDefined( + const YGNodeRef node, + const YGFlexDirection axis) { + return (YGFlexDirectionIsRow(axis) && + YGComputedEdgeValue( + node->getStyle().position, YGEdgeStart, &YGValueUndefined) + ->unit != YGUnitUndefined) || + YGComputedEdgeValue( + node->getStyle().position, leading[axis], &YGValueUndefined) + ->unit != YGUnitUndefined; +} + +inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { + return flexDirection == YGFlexDirectionColumn || + flexDirection == YGFlexDirectionColumnReverse; +} + +inline YGFlexDirection YGResolveFlexDirection( + const YGFlexDirection flexDirection, + const YGDirection direction) { + if (direction == YGDirectionRTL) { + if (flexDirection == YGFlexDirectionRow) { + return YGFlexDirectionRowReverse; + } else if (flexDirection == YGFlexDirectionRowReverse) { + return YGFlexDirectionRow; + } + } + + return flexDirection; +} diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 9ee729b5..b1b5f4d8 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -82,6 +82,26 @@ YGValue YGNode::getResolvedDimension(int index) { std::array YGNode::getResolvedDimensions() const { return resolvedDimensions_; } + +float YGNode::getLeadingPosition( + const YGFlexDirection axis, + const float axisSize) { + if (YGFlexDirectionIsRow(axis)) { + const YGValue* leadingPosition = + YGComputedEdgeValue(style_.position, YGEdgeStart, &YGValueUndefined); + if (leadingPosition->unit != YGUnitUndefined) { + return YGResolveValue(*leadingPosition, axisSize); + } + } + + const YGValue* leadingPosition = + YGComputedEdgeValue(style_.position, leading[axis], &YGValueUndefined); + + return leadingPosition->unit == YGUnitUndefined + ? 0.0f + : YGResolveValue(*leadingPosition, axisSize); +} + // Setters void YGNode::setContext(void* context) { diff --git a/yoga/YGNode.h b/yoga/YGNode.h index 79947e71..1a05c6e0 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -76,6 +76,7 @@ struct YGNode { bool isDirty() const; std::array getResolvedDimensions() const; YGValue getResolvedDimension(int index); + float getLeadingPosition(const YGFlexDirection axis, const float axisSize); // Setters diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index 1ea80704..081c9344 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -28,7 +28,6 @@ YG_EXTERN_C_END extern const std::array trailing; extern const std::array leading; -extern bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection); extern bool YGValueEqual(const YGValue a, const YGValue b); extern const YGValue YGValueUndefined; extern const YGValue YGValueAuto; diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index c843dc54..3d4a301b 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -151,21 +151,6 @@ const YGValue* YGComputedEdgeValue( return defaultValue; } -static inline float YGResolveValue( - const YGValue value, - const float parentSize) { - switch (value.unit) { - case YGUnitUndefined: - case YGUnitAuto: - return YGUndefined; - case YGUnitPoint: - return value.value; - case YGUnitPercent: - return value.value * parentSize / 100.0f; - } - return YGUndefined; -} - static inline float YGResolveValueMargin( const YGValue value, const float parentSize) { @@ -782,10 +767,6 @@ static const std::array pos = {{ static const std::array dim = { {YGDimensionHeight, YGDimensionHeight, YGDimensionWidth, YGDimensionWidth}}; -static inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { - return flexDirection == YGFlexDirectionColumn || flexDirection == YGFlexDirectionColumnReverse; -} - static inline float YGNodeLeadingMargin(const YGNodeRef node, const YGFlexDirection axis, const float widthSize) { @@ -967,26 +948,6 @@ static float YGBaseline(const YGNodeRef node) { return baseline + baselineChild->getLayout().position[YGEdgeTop]; } -static inline YGFlexDirection YGResolveFlexDirection(const YGFlexDirection flexDirection, - const YGDirection direction) { - if (direction == YGDirectionRTL) { - if (flexDirection == YGFlexDirectionRow) { - return YGFlexDirectionRowReverse; - } else if (flexDirection == YGFlexDirectionRowReverse) { - return YGFlexDirectionRow; - } - } - - return flexDirection; -} - -static YGFlexDirection YGFlexDirectionCross(const YGFlexDirection flexDirection, - const YGDirection direction) { - return YGFlexDirectionIsColumn(flexDirection) - ? YGResolveFlexDirection(YGFlexDirectionRow, direction) - : YGFlexDirectionColumn; -} - static inline bool YGNodeIsFlex(const YGNodeRef node) { return ( node->getStyle().positionType == YGPositionTypeRelative && @@ -1038,15 +999,6 @@ static inline bool YGNodeIsLayoutDimDefined(const YGNodeRef node, const YGFlexDi return !YGFloatIsUndefined(value) && value >= 0.0f; } -static inline bool YGNodeIsLeadingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { - return (YGFlexDirectionIsRow(axis) && - YGComputedEdgeValue( - node->getStyle().position, YGEdgeStart, &YGValueUndefined) - ->unit != YGUnitUndefined) || - YGComputedEdgeValue( - node->getStyle().position, leading[axis], &YGValueUndefined) - ->unit != YGUnitUndefined; -} static inline bool YGNodeIsTrailingPosDefined(const YGNodeRef node, const YGFlexDirection axis) { return (YGFlexDirectionIsRow(axis) && @@ -1058,27 +1010,6 @@ static inline bool YGNodeIsTrailingPosDefined(const YGNodeRef node, const YGFlex ->unit != YGUnitUndefined; } -static float YGNodeLeadingPosition(const YGNodeRef node, - const YGFlexDirection axis, - const float axisSize) { - if (YGFlexDirectionIsRow(axis)) { - const YGValue* leadingPosition = YGComputedEdgeValue( - node->getStyle().position, YGEdgeStart, &YGValueUndefined); - if (leadingPosition->unit != YGUnitUndefined) { - return YGResolveValue( - *leadingPosition, - axisSize); // leadingPosition->resolveValue(axisSize); - } - } - - const YGValue* leadingPosition = YGComputedEdgeValue( - node->getStyle().position, leading[axis], &YGValueUndefined); - - return leadingPosition->unit == YGUnitUndefined - ? 0.0f - : YGResolveValue(*leadingPosition, axisSize); -} - static float YGNodeTrailingPosition(const YGNodeRef node, const YGFlexDirection axis, const float axisSize) { @@ -1157,8 +1088,9 @@ static void YGNodeSetChildTrailingPosition(const YGNodeRef node, static float YGNodeRelativePosition(const YGNodeRef node, const YGFlexDirection axis, const float axisSize) { - return YGNodeIsLeadingPosDefined(node, axis) ? YGNodeLeadingPosition(node, axis, axisSize) - : -YGNodeTrailingPosition(node, axis, axisSize); + return YGNodeIsLeadingPosDefined(node, axis) + ? node->getLeadingPosition(axis, axisSize) + : -YGNodeTrailingPosition(node, axis, axisSize); } static void YGConstrainMaxSizeForMode(const YGNodeRef node, @@ -1414,7 +1346,7 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, childWidth = node->getLayout().measuredDimensions[YGDimensionWidth] - (YGNodeLeadingBorder(node, YGFlexDirectionRow) + YGNodeTrailingBorder(node, YGFlexDirectionRow)) - - (YGNodeLeadingPosition(child, YGFlexDirectionRow, width) + + (child->getLeadingPosition(YGFlexDirectionRow, width) + YGNodeTrailingPosition(child, YGFlexDirectionRow, width)); childWidth = YGNodeBoundAxis(child, YGFlexDirectionRow, childWidth, width, width); } @@ -1433,7 +1365,7 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, childHeight = node->getLayout().measuredDimensions[YGDimensionHeight] - (YGNodeLeadingBorder(node, YGFlexDirectionColumn) + YGNodeTrailingBorder(node, YGFlexDirectionColumn)) - - (YGNodeLeadingPosition(child, YGFlexDirectionColumn, height) + + (child->getLeadingPosition(YGFlexDirectionColumn, height) + YGNodeTrailingPosition(child, YGFlexDirectionColumn, height)); childHeight = YGNodeBoundAxis(child, YGFlexDirectionColumn, childHeight, height, width); } @@ -2572,7 +2504,7 @@ static void YGNodelayoutImpl(const YGNodeRef node, // defined, we override the position to whatever the user said // (and margin/border). child->setLayoutPosition( - YGNodeLeadingPosition(child, mainAxis, availableInnerMainDim) + + child->getLeadingPosition(mainAxis, availableInnerMainDim) + YGNodeLeadingBorder(node, mainAxis) + YGNodeLeadingMargin(child, mainAxis, availableInnerWidth), pos[mainAxis]); @@ -2664,8 +2596,7 @@ static void YGNodelayoutImpl(const YGNodeRef node, const bool isChildLeadingPosDefined = YGNodeIsLeadingPosDefined(child, crossAxis); if (isChildLeadingPosDefined) { child->setLayoutPosition( - YGNodeLeadingPosition( - child, crossAxis, availableInnerCrossDim) + + child->getLeadingPosition(crossAxis, availableInnerCrossDim) + YGNodeLeadingBorder(node, crossAxis) + YGNodeLeadingMargin(child, crossAxis, availableInnerWidth), pos[crossAxis]); @@ -2948,10 +2879,8 @@ static void YGNodelayoutImpl(const YGNodeRef node, case YGAlignBaseline: { child->setLayoutPosition( currentLead + maxAscentForCurrentLine - YGBaseline(child) + - YGNodeLeadingPosition( - child, - YGFlexDirectionColumn, - availableInnerCrossDim), + child->getLeadingPosition( + YGFlexDirectionColumn, availableInnerCrossDim), YGEdgeTop); break;