Permalink
Browse files

Moved setPosition as a method on YGNode

Reviewed By: emilsjolander

Differential Revision: D6683387

fbshipit-source-id: 83f64101faa700933771c69b222056ec2a6b8d1e
  • Loading branch information...
priteshrnandgaonkar authored and facebook-github-bot committed Jan 11, 2018
1 parent 89c1457 commit 4b4959a21cb1e9e356eab51bfba0f16b76e8ec7f
Showing with 55 additions and 50 deletions.
  1. +40 −0 ReactCommon/yoga/yoga/YGNode.cpp
  2. +8 −0 ReactCommon/yoga/yoga/YGNode.h
  3. +7 −50 ReactCommon/yoga/yoga/Yoga.cpp
@@ -326,6 +326,46 @@ void YGNode::setLayoutDimension(float dimension, int index) {
layout_.dimensions[index] = dimension;
}
// 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) ? getLeadingPosition(axis, axisSize)
: -getTrailingPosition(axis, axisSize);
}
void YGNode::setPosition(
const YGDirection direction,
const float mainSize,
const float crossSize,
const float parentWidth) {
/* Root nodes should be always layouted as LTR, so we don't return negative
* values. */
const YGDirection directionRespectingRoot =
parent_ != nullptr ? direction : YGDirectionLTR;
const YGFlexDirection mainAxis =
YGResolveFlexDirection(style_.flexDirection, directionRespectingRoot);
const YGFlexDirection crossAxis =
YGFlexDirectionCross(mainAxis, directionRespectingRoot);
const float relativePositionMain = relativePosition(mainAxis, mainSize);
const float relativePositionCross = relativePosition(crossAxis, crossSize);
setLayoutPosition(
getLeadingMargin(mainAxis, parentWidth) + relativePositionMain,
leading[mainAxis]);
setLayoutPosition(
getTrailingMargin(mainAxis, parentWidth) + relativePositionMain,
trailing[mainAxis]);
setLayoutPosition(
getLeadingMargin(crossAxis, parentWidth) + relativePositionCross,
leading[crossAxis]);
setLayoutPosition(
getTrailingMargin(crossAxis, parentWidth) + relativePositionCross,
trailing[crossAxis]);
}
YGNode::YGNode()
: context_(nullptr),
print_(nullptr),
@@ -31,6 +31,8 @@ struct YGNode {
bool isDirty_;
std::array<YGValue, 2> resolvedDimensions_;
float relativePosition(const YGFlexDirection axis, const float axisSize);
public:
YGNode();
~YGNode();
@@ -110,6 +112,12 @@ struct YGNode {
void setLayoutHadOverflow(bool hadOverflow);
void setLayoutDimension(float dimension, int index);
void setPosition(
const YGDirection direction,
const float mainSize,
const float crossSize,
const float parentWidth);
// Other methods
YGValue marginLeadingValue(const YGFlexDirection axis) const;
YGValue marginTrailingValue(const YGFlexDirection axis) const;
@@ -1020,16 +1020,6 @@ static void YGNodeSetChildTrailingPosition(const YGNodeRef node,
trailing[axis]);
}
// If both left and right are defined, then use left. Otherwise return
// +left or -right depending on which is defined.
static float YGNodeRelativePosition(const YGNodeRef node,
const YGFlexDirection axis,
const float axisSize) {
return node->isLeadingPositionDefined(axis)
? node->getLeadingPosition(axis, axisSize)
: -node->getTrailingPosition(axis, axisSize);
}
static void YGConstrainMaxSizeForMode(const YGNodeRef node,
const enum YGFlexDirection axis,
const float parentAxisSize,
@@ -1054,35 +1044,6 @@ static void YGConstrainMaxSizeForMode(const YGNodeRef node,
}
}
static void YGNodeSetPosition(const YGNodeRef node,
const YGDirection direction,
const float mainSize,
const float crossSize,
const float parentWidth) {
/* Root nodes should be always layouted as LTR, so we don't return negative values. */
const YGDirection directionRespectingRoot =
node->getParent() != nullptr ? direction : YGDirectionLTR;
const YGFlexDirection mainAxis = YGResolveFlexDirection(
node->getStyle().flexDirection, directionRespectingRoot);
const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, directionRespectingRoot);
const float relativePositionMain = YGNodeRelativePosition(node, mainAxis, mainSize);
const float relativePositionCross = YGNodeRelativePosition(node, crossAxis, crossSize);
node->setLayoutPosition(
node->getLeadingMargin(mainAxis, parentWidth) + relativePositionMain,
leading[mainAxis]);
node->setLayoutPosition(
node->getTrailingMargin(mainAxis, parentWidth) + relativePositionMain,
trailing[mainAxis]);
node->setLayoutPosition(
node->getLeadingMargin(crossAxis, parentWidth) + relativePositionCross,
leading[crossAxis]);
node->setLayoutPosition(
node->getTrailingMargin(crossAxis, parentWidth) + relativePositionCross,
trailing[crossAxis]);
}
static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
const YGNodeRef child,
const float width,
@@ -1887,11 +1848,11 @@ static void YGNodelayoutImpl(const YGNodeRef node,
if (performLayout) {
// Set the initial position (relative to the parent).
const YGDirection childDirection = YGNodeResolveDirection(child, direction);
YGNodeSetPosition(child,
childDirection,
availableInnerMainDim,
availableInnerCrossDim,
availableInnerWidth);
child->setPosition(
childDirection,
availableInnerMainDim,
availableInnerCrossDim,
availableInnerWidth);
}
// Absolute-positioned children don't participate in flex layout. Add them
@@ -3477,12 +3438,8 @@ void YGNodeCalculateLayout(const YGNodeRef node,
true,
"initial",
node->getConfig())) {
YGNodeSetPosition(
node,
node->getLayout().direction,
parentWidth,
parentHeight,
parentWidth);
node->setPosition(
node->getLayout().direction, parentWidth, parentHeight, parentWidth);
YGRoundToPixelGrid(node, node->getConfig()->pointScaleFactor, 0.0f, 0.0f);
if (gPrintTree) {

0 comments on commit 4b4959a

Please sign in to comment.