Fix aspectratio with margins
Summary: aspect ratio did not account for the widths and heights being including padding. This diff fixes that. Reviewed By: astreet Differential Revision: D4473024 fbshipit-source-id: 5a747e2f267b077203bb3b63e4c152847dc30774
This commit is contained in:
committed by
Facebook Github Bot
parent
abb91ae77b
commit
7fa4adb0d9
@@ -697,3 +697,53 @@ TEST(YogaTest, aspect_ratio_allow_child_overflow_parent_size) {
|
||||
|
||||
YGNodeFreeRecursive(root);
|
||||
}
|
||||
|
||||
TEST(YogaTest, aspect_ratio_defined_main_with_margin) {
|
||||
const YGNodeRef root = YGNodeNew();
|
||||
YGNodeStyleSetAlignItems(root, YGAlignCenter);
|
||||
YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
|
||||
YGNodeStyleSetWidth(root, 100);
|
||||
YGNodeStyleSetHeight(root, 100);
|
||||
|
||||
const YGNodeRef root_child0 = YGNodeNew();
|
||||
YGNodeStyleSetHeight(root_child0, 50);
|
||||
YGNodeStyleSetAspectRatio(root_child0, 1);
|
||||
YGNodeStyleSetMargin(root_child0, YGEdgeLeft, 10);
|
||||
YGNodeStyleSetMargin(root_child0, YGEdgeRight, 10);
|
||||
YGNodeInsertChild(root, root_child0, 0);
|
||||
|
||||
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
||||
|
||||
ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
|
||||
ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
|
||||
|
||||
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
|
||||
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
|
||||
|
||||
YGNodeFreeRecursive(root);
|
||||
}
|
||||
|
||||
TEST(YogaTest, aspect_ratio_defined_cross_with_margin) {
|
||||
const YGNodeRef root = YGNodeNew();
|
||||
YGNodeStyleSetAlignItems(root, YGAlignCenter);
|
||||
YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
|
||||
YGNodeStyleSetWidth(root, 100);
|
||||
YGNodeStyleSetHeight(root, 100);
|
||||
|
||||
const YGNodeRef root_child0 = YGNodeNew();
|
||||
YGNodeStyleSetWidth(root_child0, 50);
|
||||
YGNodeStyleSetAspectRatio(root_child0, 1);
|
||||
YGNodeStyleSetMargin(root_child0, YGEdgeLeft, 10);
|
||||
YGNodeStyleSetMargin(root_child0, YGEdgeRight, 10);
|
||||
YGNodeInsertChild(root, root_child0, 0);
|
||||
|
||||
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
||||
|
||||
ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
|
||||
ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
|
||||
|
||||
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
|
||||
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
|
||||
|
||||
YGNodeFreeRecursive(root);
|
||||
}
|
||||
|
89
yoga/Yoga.c
89
yoga/Yoga.c
@@ -1274,14 +1274,17 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
|
||||
childWidthMeasureMode = YGMeasureModeUndefined;
|
||||
childHeightMeasureMode = YGMeasureModeUndefined;
|
||||
|
||||
const float marginRow = YGNodeMarginForAxis(child, YGFlexDirectionRow, parentWidth);
|
||||
const float marginColumn = YGNodeMarginForAxis(child, YGFlexDirectionColumn, parentWidth);
|
||||
|
||||
if (isRowStyleDimDefined) {
|
||||
childWidth = YGValueResolve(&child->style.dimensions[YGDimensionWidth], parentWidth) +
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionRow, parentWidth);
|
||||
childWidth =
|
||||
YGValueResolve(&child->style.dimensions[YGDimensionWidth], parentWidth) + marginRow;
|
||||
childWidthMeasureMode = YGMeasureModeExactly;
|
||||
}
|
||||
if (isColumnStyleDimDefined) {
|
||||
childHeight = YGValueResolve(&child->style.dimensions[YGDimensionHeight], parentHeight) +
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionColumn, parentWidth);
|
||||
childHeight =
|
||||
YGValueResolve(&child->style.dimensions[YGDimensionHeight], parentHeight) + marginColumn;
|
||||
childHeightMeasureMode = YGMeasureModeExactly;
|
||||
}
|
||||
|
||||
@@ -1320,12 +1323,12 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
|
||||
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
|
||||
if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) {
|
||||
child->layout.computedFlexBasis =
|
||||
fmaxf(childWidth / child->style.aspectRatio,
|
||||
fmaxf((childWidth - marginRow) / child->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn, parentWidth));
|
||||
return;
|
||||
} else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
|
||||
child->layout.computedFlexBasis =
|
||||
fmaxf(childHeight * child->style.aspectRatio,
|
||||
fmaxf((childHeight - marginColumn) * child->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, parentWidth));
|
||||
return;
|
||||
}
|
||||
@@ -1376,9 +1379,11 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
|
||||
YGMeasureMode childWidthMeasureMode = YGMeasureModeUndefined;
|
||||
YGMeasureMode childHeightMeasureMode = YGMeasureModeUndefined;
|
||||
|
||||
const float marginRow = YGNodeMarginForAxis(child, YGFlexDirectionRow, width);
|
||||
const float marginColumn = YGNodeMarginForAxis(child, YGFlexDirectionColumn, width);
|
||||
|
||||
if (YGNodeIsStyleDimDefined(child, YGFlexDirectionRow)) {
|
||||
childWidth = YGValueResolve(&child->style.dimensions[YGDimensionWidth], width) +
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionRow, width);
|
||||
childWidth = YGValueResolve(&child->style.dimensions[YGDimensionWidth], width) + marginRow;
|
||||
} else {
|
||||
// If the child doesn't have a specified width, compute the width based
|
||||
// on the left/right
|
||||
@@ -1395,8 +1400,8 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
|
||||
}
|
||||
|
||||
if (YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn)) {
|
||||
childHeight = YGValueResolve(&child->style.dimensions[YGDimensionHeight], height) +
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionColumn, width);
|
||||
childHeight =
|
||||
YGValueResolve(&child->style.dimensions[YGDimensionHeight], height) + marginColumn;
|
||||
} else {
|
||||
// If the child doesn't have a specified height, compute the height
|
||||
// based on the top/bottom
|
||||
@@ -1417,10 +1422,12 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
|
||||
if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) {
|
||||
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
|
||||
if (YGFloatIsUndefined(childWidth)) {
|
||||
childWidth = fmaxf(childHeight * child->style.aspectRatio,
|
||||
childWidth =
|
||||
marginRow + fmaxf((childHeight - marginColumn) * child->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn, width));
|
||||
} else if (YGFloatIsUndefined(childHeight)) {
|
||||
childHeight = fmaxf(childWidth / child->style.aspectRatio,
|
||||
childHeight =
|
||||
marginColumn + fmaxf((childWidth - marginRow) / child->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, width));
|
||||
}
|
||||
}
|
||||
@@ -2213,10 +2220,13 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
YGMeasureMode childWidthMeasureMode;
|
||||
YGMeasureMode childHeightMeasureMode;
|
||||
|
||||
if (isMainAxisRow) {
|
||||
childWidth =
|
||||
updatedMainSize +
|
||||
const float marginRow =
|
||||
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow, availableInnerWidth);
|
||||
const float marginColumn =
|
||||
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn, availableInnerWidth);
|
||||
|
||||
if (isMainAxisRow) {
|
||||
childWidth = updatedMainSize + marginRow;
|
||||
childWidthMeasureMode = YGMeasureModeExactly;
|
||||
|
||||
if (!YGFloatIsUndefined(availableInnerCrossDim) &&
|
||||
@@ -2232,15 +2242,11 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
} else {
|
||||
childHeight = YGValueResolve(¤tRelativeChild->style.dimensions[YGDimensionHeight],
|
||||
availableInnerHeight) +
|
||||
YGNodeMarginForAxis(currentRelativeChild,
|
||||
YGFlexDirectionColumn,
|
||||
availableInnerWidth);
|
||||
marginColumn;
|
||||
childHeightMeasureMode = YGMeasureModeExactly;
|
||||
}
|
||||
} else {
|
||||
childHeight =
|
||||
updatedMainSize +
|
||||
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn, availableInnerWidth);
|
||||
childHeight = updatedMainSize + marginColumn;
|
||||
childHeightMeasureMode = YGMeasureModeExactly;
|
||||
|
||||
if (!YGFloatIsUndefined(availableInnerCrossDim) &&
|
||||
@@ -2254,17 +2260,16 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
childWidthMeasureMode =
|
||||
YGFloatIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost;
|
||||
} else {
|
||||
childWidth =
|
||||
YGValueResolve(¤tRelativeChild->style.dimensions[YGDimensionWidth],
|
||||
childWidth = YGValueResolve(¤tRelativeChild->style.dimensions[YGDimensionWidth],
|
||||
availableInnerWidth) +
|
||||
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow, availableInnerWidth);
|
||||
marginRow;
|
||||
childWidthMeasureMode = YGMeasureModeExactly;
|
||||
}
|
||||
}
|
||||
|
||||
if (!YGFloatIsUndefined(currentRelativeChild->style.aspectRatio)) {
|
||||
if (isMainAxisRow) {
|
||||
childHeight = fmaxf(childWidth / currentRelativeChild->style.aspectRatio,
|
||||
childHeight = fmaxf((childWidth - marginRow) / currentRelativeChild->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(currentRelativeChild,
|
||||
YGFlexDirectionColumn,
|
||||
availableInnerWidth));
|
||||
@@ -2272,11 +2277,14 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
|
||||
// Parent size constraint should have higher priority than flex
|
||||
if (YGNodeIsFlex(currentRelativeChild)) {
|
||||
childHeight = fminf(childHeight, availableInnerHeight);
|
||||
childWidth = childHeight * currentRelativeChild->style.aspectRatio;
|
||||
childHeight = fminf((childHeight - marginColumn), availableInnerHeight);
|
||||
childWidth = marginRow + childHeight * currentRelativeChild->style.aspectRatio;
|
||||
}
|
||||
|
||||
childHeight += marginColumn;
|
||||
} else {
|
||||
childWidth = fmaxf(childHeight * currentRelativeChild->style.aspectRatio,
|
||||
childWidth =
|
||||
fmaxf((childHeight - marginColumn) * currentRelativeChild->style.aspectRatio,
|
||||
YGNodePaddingAndBorderForAxis(currentRelativeChild,
|
||||
YGFlexDirectionRow,
|
||||
availableInnerWidth));
|
||||
@@ -2284,9 +2292,11 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
|
||||
// Parent size constraint should have higher priority than flex
|
||||
if (YGNodeIsFlex(currentRelativeChild)) {
|
||||
childWidth = fminf(childWidth, availableInnerWidth);
|
||||
childHeight = childWidth / currentRelativeChild->style.aspectRatio;
|
||||
childWidth = fminf((childWidth - marginRow), availableInnerWidth);
|
||||
childHeight = marginColumn + childWidth / currentRelativeChild->style.aspectRatio;
|
||||
}
|
||||
|
||||
childWidth += marginRow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2495,24 +2505,31 @@ static void YGNodelayoutImpl(const YGNodeRef node,
|
||||
YGMeasureMode childWidthMeasureMode = YGMeasureModeExactly;
|
||||
YGMeasureMode childHeightMeasureMode = YGMeasureModeExactly;
|
||||
|
||||
if (isMainAxisRow) {
|
||||
childWidth = child->layout.measuredDimensions[YGDimensionWidth] +
|
||||
const float marginRow =
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionRow, availableInnerWidth);
|
||||
const float marginColumn =
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionColumn, availableInnerWidth);
|
||||
|
||||
if (isMainAxisRow) {
|
||||
childWidth = child->layout.measuredDimensions[YGDimensionWidth];
|
||||
|
||||
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
|
||||
childHeight = childWidth / child->style.aspectRatio;
|
||||
childHeight = marginColumn + childWidth / child->style.aspectRatio;
|
||||
} else {
|
||||
childHeight = crossDim;
|
||||
}
|
||||
|
||||
childWidth += marginRow;
|
||||
} else {
|
||||
childHeight = child->layout.measuredDimensions[YGDimensionHeight] +
|
||||
YGNodeMarginForAxis(child, YGFlexDirectionColumn, availableInnerWidth);
|
||||
childHeight = child->layout.measuredDimensions[YGDimensionHeight];
|
||||
|
||||
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
|
||||
childWidth = childHeight * child->style.aspectRatio;
|
||||
childWidth = marginRow + childHeight * child->style.aspectRatio;
|
||||
} else {
|
||||
childWidth = crossDim;
|
||||
}
|
||||
|
||||
childHeight += marginColumn;
|
||||
}
|
||||
|
||||
YGConstrainMaxSizeForMode(YGValueResolve(&child->style.maxDimensions[YGDimensionWidth],
|
||||
|
Reference in New Issue
Block a user