Skip to content

Commit

Permalink
Moved setPosition as a method on YGNode
Browse files Browse the repository at this point in the history
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 4b4959a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
40 changes: 40 additions & 0 deletions ReactCommon/yoga/yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions ReactCommon/yoga/yoga/YGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct YGNode {
bool isDirty_;
std::array<YGValue, 2> resolvedDimensions_;

float relativePosition(const YGFlexDirection axis, const float axisSize);

public:
YGNode();
~YGNode();
Expand Down Expand Up @@ -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;
Expand Down
57 changes: 7 additions & 50 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4b4959a

Please sign in to comment.