BREAKING - Increase priority of AspectRatio to override flex, align stretch, and fixed sizes if specified.

Summary:
@public
AspectRatio is a new addition and soon after introduction we noticed use cases which is did not support. Specifically we wanted to support a node being as large as possible within a container while maintaining an arbitrary aspect ratio. This was not possible due to the low priority of AspectRatio, by increasing the priority of AspectRatio this is now possible as FlexGrow will grow an item to fit its parent unless the AspectRatio makes it too big in the cross axis, the AspectRatio will now override the FlexGrow in the main axis in that case.

Differential Revision: D4346720

fbshipit-source-id: 1f15613604190e3ad5ff4a467ba57db4bcfd2741
This commit is contained in:
Emil Sjolander
2016-12-22 02:57:16 -08:00
committed by Facebook Github Bot
parent 071f576db9
commit 1b5eb7da5e
2 changed files with 292 additions and 5 deletions

View File

@@ -63,12 +63,15 @@ TEST(YogaTest, aspect_ratio_main_defined) {
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_both_dimensions_defined) {
TEST(YogaTest, aspect_ratio_both_dimensions_defined_row) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 100);
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
@@ -78,6 +81,28 @@ TEST(YogaTest, aspect_ratio_both_dimensions_defined) {
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_both_dimensions_defined_column) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 100);
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
@@ -405,3 +430,249 @@ TEST(YogaTest, aspect_ratio_with_measure_func) {
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_width_height_flex_grow_row) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 200);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_width_height_flex_grow_column) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 200);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_height_as_flex_basis) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 200);
YGNodeStyleSetHeight(root, 200);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetHeight(root_child1, 100);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeStyleSetAspectRatio(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(75, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(75, YGNodeLayoutGetHeight(root_child0));
ASSERT_EQ(75, YGNodeLayoutGetLeft(root_child1));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child1));
ASSERT_EQ(125, YGNodeLayoutGetWidth(root_child1));
ASSERT_EQ(125, YGNodeLayoutGetHeight(root_child1));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_width_as_flex_basis) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 200);
YGNodeStyleSetHeight(root, 200);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 100);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeStyleSetAspectRatio(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(75, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(75, YGNodeLayoutGetHeight(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child1));
ASSERT_EQ(75, YGNodeLayoutGetTop(root_child1));
ASSERT_EQ(125, YGNodeLayoutGetWidth(root_child1));
ASSERT_EQ(125, YGNodeLayoutGetHeight(root_child1));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_overrides_flex_grow_row) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 0.5);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_overrides_flex_grow_column) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetAspectRatio(root_child0, 2);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_left_right_absolute) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 10);
YGNodeStyleSetPosition(root_child0, YGEdgeTop, 10);
YGNodeStyleSetPosition(root_child0, YGEdgeRight, 10);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(10, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(10, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(80, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(80, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_top_bottom_absolute) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 10);
YGNodeStyleSetPosition(root_child0, YGEdgeTop, 10);
YGNodeStyleSetPosition(root_child0, YGEdgeBottom, 10);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(10, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(10, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(80, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(80, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_width_overrides_align_stretch_row) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, aspect_ratio_height_overrides_align_stretch_column) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetHeight(root_child0, 50);
YGNodeStyleSetAspectRatio(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
}

View File

@@ -1878,16 +1878,22 @@ static void YGNodelayoutImpl(const YGNodeRef node,
}
if (!YGValueIsUndefined(currentRelativeChild->style.aspectRatio)) {
if (isMainAxisRow && childHeightMeasureMode != YGMeasureModeExactly) {
if (isMainAxisRow) {
childHeight =
fmaxf(childWidth / currentRelativeChild->style.aspectRatio,
YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionColumn));
childHeightMeasureMode = YGMeasureModeExactly;
} else if (!isMainAxisRow && childWidthMeasureMode != YGMeasureModeExactly) {
childHeight = fminf(childHeight, availableInnerHeight);
childWidth = childHeight * currentRelativeChild->style.aspectRatio;
} else {
childWidth =
fmaxf(childHeight * currentRelativeChild->style.aspectRatio,
YGNodePaddingAndBorderForAxis(currentRelativeChild, YGFlexDirectionRow));
childWidthMeasureMode = YGMeasureModeExactly;
childWidth = fminf(childWidth, availableInnerWidth);
childHeight = childWidth / currentRelativeChild->style.aspectRatio;
}
}
@@ -2080,13 +2086,23 @@ static void YGNodelayoutImpl(const YGNodeRef node,
YGMeasureMode childHeightMeasureMode = YGMeasureModeExactly;
if (isMainAxisRow) {
childHeight = crossDim;
childWidth = child->layout.measuredDimensions[YGDimensionWidth] +
YGNodeMarginForAxis(child, YGFlexDirectionRow);
if (!YGValueIsUndefined(child->style.aspectRatio)) {
childHeight = childWidth / child->style.aspectRatio;
} else {
childHeight = crossDim;
}
} else {
childWidth = crossDim;
childHeight = child->layout.measuredDimensions[YGDimensionHeight] +
YGNodeMarginForAxis(child, YGFlexDirectionColumn);
if (!YGValueIsUndefined(child->style.aspectRatio)) {
childWidth = childHeight * child->style.aspectRatio;
} else {
childWidth = crossDim;
}
}
YGConstrainMaxSizeForMode(child->style.maxDimensions[YGDimensionWidth],