Skip to content

Commit

Permalink
Pass the parent size to YGNodeCalculateLayout instead of the node size
Browse files Browse the repository at this point in the history
Summary: The size of the node is already set on the node however there was no way to set the size of the parent to the root so that the root could use percentages. This change fixes this by making the width and height passed to calculate layout be the width and height of the hypothetical parent.

Reviewed By: astreet

Differential Revision: D4611417

fbshipit-source-id: 2fb0eedffa17f0ec89b601722a1717a72e216b9e
  • Loading branch information
Emil Sjolander authored and facebook-github-bot committed Feb 28, 2017
1 parent 17e3dca commit 1cd7363
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
7 changes: 4 additions & 3 deletions tests/YGRelayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ TEST(YogaTest, dont_cache_computed_flex_basis_between_layouts) {
YGSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);

const YGNodeRef root = YGNodeNew();
YGNodeStyleSetHeightPercent(root, 100);
YGNodeStyleSetWidthPercent(root, 100);

const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetHeight(root_child0, 10);
YGNodeStyleSetFlexBasis(root_child0, 20);
YGNodeStyleSetFlexBasisPercent(root_child0, 100);
YGNodeInsertChild(root, root_child0, 0);

YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);

ASSERT_FLOAT_EQ(20, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetHeight(root_child0));

YGNodeFreeRecursive(root);

Expand Down
43 changes: 19 additions & 24 deletions yoga/Yoga.c
Original file line number Diff line number Diff line change
Expand Up @@ -3280,8 +3280,8 @@ static void YGRoundToPixelGrid(const YGNodeRef node) {
}

void YGNodeCalculateLayout(const YGNodeRef node,
const float availableWidth,
const float availableHeight,
const float parentWidth,
const float parentHeight,
const YGDirection parentDirection) {
// Increment the generation count. This will force the recursive routine to
// visit
Expand All @@ -3290,33 +3290,28 @@ void YGNodeCalculateLayout(const YGNodeRef node,
// parameters don't change.
gCurrentGenerationCount++;

float width = availableWidth;
float height = availableHeight;
YGMeasureMode widthMeasureMode = YGMeasureModeUndefined;
YGMeasureMode heightMeasureMode = YGMeasureModeUndefined;

YGResolveDimensions(node);

if (!YGFloatIsUndefined(width)) {
widthMeasureMode = YGMeasureModeExactly;
} else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, availableWidth)) {
width = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionRow]], availableWidth) +
YGNodeMarginForAxis(node, YGFlexDirectionRow, availableWidth);
float width = YGUndefined;
YGMeasureMode widthMeasureMode = YGMeasureModeUndefined;
if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, parentWidth)) {
width = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionRow]], parentWidth) +
YGNodeMarginForAxis(node, YGFlexDirectionRow, parentWidth);
widthMeasureMode = YGMeasureModeExactly;
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], availableWidth) >= 0.0f) {
width = YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], availableWidth);
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], parentWidth) >= 0.0f) {
width = YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], parentWidth);
widthMeasureMode = YGMeasureModeAtMost;
}

if (!YGFloatIsUndefined(height)) {
heightMeasureMode = YGMeasureModeExactly;
} else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, availableHeight)) {
height = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionColumn]], availableHeight) +
YGNodeMarginForAxis(node, YGFlexDirectionColumn, availableWidth);
float height = YGUndefined;
YGMeasureMode heightMeasureMode = YGMeasureModeUndefined;
if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, parentHeight)) {
height = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionColumn]], parentHeight) +
YGNodeMarginForAxis(node, YGFlexDirectionColumn, parentWidth);
heightMeasureMode = YGMeasureModeExactly;
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], availableHeight) >=
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], parentHeight) >=
0.0f) {
height = YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], availableHeight);
height = YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], parentHeight);
heightMeasureMode = YGMeasureModeAtMost;
}

Expand All @@ -3326,12 +3321,12 @@ void YGNodeCalculateLayout(const YGNodeRef node,
parentDirection,
widthMeasureMode,
heightMeasureMode,
availableWidth,
availableHeight,
parentWidth,
parentHeight,
true,
"initia"
"l")) {
YGNodeSetPosition(node, node->layout.direction, availableWidth, availableHeight, availableWidth);
YGNodeSetPosition(node, node->layout.direction, node->layout.dimensions[YGDimensionWidth], node->layout.dimensions[YGDimensionHeight], parentWidth);

if (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) {
YGRoundToPixelGrid(node);
Expand Down

0 comments on commit 1cd7363

Please sign in to comment.