Skip to content

Commit 1cd7363

Browse files
Emil Sjolanderfacebook-github-bot
authored andcommitted
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
1 parent 17e3dca commit 1cd7363

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

tests/YGRelayoutTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ TEST(YogaTest, dont_cache_computed_flex_basis_between_layouts) {
1414
YGSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);
1515

1616
const YGNodeRef root = YGNodeNew();
17+
YGNodeStyleSetHeightPercent(root, 100);
18+
YGNodeStyleSetWidthPercent(root, 100);
1719

1820
const YGNodeRef root_child0 = YGNodeNew();
19-
YGNodeStyleSetHeight(root_child0, 10);
20-
YGNodeStyleSetFlexBasis(root_child0, 20);
21+
YGNodeStyleSetFlexBasisPercent(root_child0, 100);
2122
YGNodeInsertChild(root, root_child0, 0);
2223

2324
YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
2425
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
2526

26-
ASSERT_FLOAT_EQ(20, YGNodeLayoutGetHeight(root_child0));
27+
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetHeight(root_child0));
2728

2829
YGNodeFreeRecursive(root);
2930

yoga/Yoga.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,8 +3280,8 @@ static void YGRoundToPixelGrid(const YGNodeRef node) {
32803280
}
32813281

32823282
void YGNodeCalculateLayout(const YGNodeRef node,
3283-
const float availableWidth,
3284-
const float availableHeight,
3283+
const float parentWidth,
3284+
const float parentHeight,
32853285
const YGDirection parentDirection) {
32863286
// Increment the generation count. This will force the recursive routine to
32873287
// visit
@@ -3290,33 +3290,28 @@ void YGNodeCalculateLayout(const YGNodeRef node,
32903290
// parameters don't change.
32913291
gCurrentGenerationCount++;
32923292

3293-
float width = availableWidth;
3294-
float height = availableHeight;
3295-
YGMeasureMode widthMeasureMode = YGMeasureModeUndefined;
3296-
YGMeasureMode heightMeasureMode = YGMeasureModeUndefined;
3297-
32983293
YGResolveDimensions(node);
32993294

3300-
if (!YGFloatIsUndefined(width)) {
3301-
widthMeasureMode = YGMeasureModeExactly;
3302-
} else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, availableWidth)) {
3303-
width = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionRow]], availableWidth) +
3304-
YGNodeMarginForAxis(node, YGFlexDirectionRow, availableWidth);
3295+
float width = YGUndefined;
3296+
YGMeasureMode widthMeasureMode = YGMeasureModeUndefined;
3297+
if (YGNodeIsStyleDimDefined(node, YGFlexDirectionRow, parentWidth)) {
3298+
width = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionRow]], parentWidth) +
3299+
YGNodeMarginForAxis(node, YGFlexDirectionRow, parentWidth);
33053300
widthMeasureMode = YGMeasureModeExactly;
3306-
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], availableWidth) >= 0.0f) {
3307-
width = YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], availableWidth);
3301+
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], parentWidth) >= 0.0f) {
3302+
width = YGValueResolve(&node->style.maxDimensions[YGDimensionWidth], parentWidth);
33083303
widthMeasureMode = YGMeasureModeAtMost;
33093304
}
33103305

3311-
if (!YGFloatIsUndefined(height)) {
3312-
heightMeasureMode = YGMeasureModeExactly;
3313-
} else if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, availableHeight)) {
3314-
height = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionColumn]], availableHeight) +
3315-
YGNodeMarginForAxis(node, YGFlexDirectionColumn, availableWidth);
3306+
float height = YGUndefined;
3307+
YGMeasureMode heightMeasureMode = YGMeasureModeUndefined;
3308+
if (YGNodeIsStyleDimDefined(node, YGFlexDirectionColumn, parentHeight)) {
3309+
height = YGValueResolve(node->resolvedDimensions[dim[YGFlexDirectionColumn]], parentHeight) +
3310+
YGNodeMarginForAxis(node, YGFlexDirectionColumn, parentWidth);
33163311
heightMeasureMode = YGMeasureModeExactly;
3317-
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], availableHeight) >=
3312+
} else if (YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], parentHeight) >=
33183313
0.0f) {
3319-
height = YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], availableHeight);
3314+
height = YGValueResolve(&node->style.maxDimensions[YGDimensionHeight], parentHeight);
33203315
heightMeasureMode = YGMeasureModeAtMost;
33213316
}
33223317

@@ -3326,12 +3321,12 @@ void YGNodeCalculateLayout(const YGNodeRef node,
33263321
parentDirection,
33273322
widthMeasureMode,
33283323
heightMeasureMode,
3329-
availableWidth,
3330-
availableHeight,
3324+
parentWidth,
3325+
parentHeight,
33313326
true,
33323327
"initia"
33333328
"l")) {
3334-
YGNodeSetPosition(node, node->layout.direction, availableWidth, availableHeight, availableWidth);
3329+
YGNodeSetPosition(node, node->layout.direction, node->layout.dimensions[YGDimensionWidth], node->layout.dimensions[YGDimensionHeight], parentWidth);
33353330

33363331
if (YGIsExperimentalFeatureEnabled(YGExperimentalFeatureRounding)) {
33373332
YGRoundToPixelGrid(node);

0 commit comments

Comments
 (0)