Pass the parent size to YGNodeCalculateLayout instead of the node size

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
This commit is contained in:
Emil Sjolander
2017-02-28 08:10:34 -08:00
committed by Facebook Github Bot
parent 17e3dca9f9
commit 1cd7363bea
2 changed files with 23 additions and 27 deletions

View File

@@ -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);

View File

@@ -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
@@ -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)) {
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 (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, availableWidth)) {
width = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionRow]], availableWidth) +
YGNodeMarginForAxis(node, YGFlexDirectionRow, availableWidth);
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)) {
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 (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, availableHeight)) {
height = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionColumn]], availableHeight) +
YGNodeMarginForAxis(node, YGFlexDirectionColumn, availableWidth);
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;
}
@@ -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);