Skip to content

Commit

Permalink
Fix main size calculation from the aspect ratio
Browse files Browse the repository at this point in the history
Summary:
When the following conditions are met, the main size become smaller by the margins in the main axis.
* The aspect ratio is defined
* The main size is not defined
* The cross size is defined
* The main margin is defined

This is because the main margin size is not included when calculating the main size from the aspect ratio.
Closes #715

Reviewed By: emilsjolander

Differential Revision: D6998988

Pulled By: priteshrnandgaonkar

fbshipit-source-id: f6f69c47ece17bd7c5e41517b96032bf0c149356
  • Loading branch information
Yuichi ONO authored and facebook-github-bot committed Feb 15, 2018
1 parent aac40cf commit 4462291
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tests/YGAspectRatioTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,31 @@ TEST(YogaTest, aspect_ratio_defined_cross_with_margin) {
YGNodeFreeRecursive(root);
}

TEST(YogaTest, aspect_ratio_defined_cross_with_main_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, YGEdgeTop, 10);
YGNodeStyleSetMargin(root_child0, YGEdgeBottom, 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_should_prefer_explicit_height) {
const YGConfigRef config = YGConfigNew();
YGConfigSetUseWebDefaults(config, true);
Expand Down
5 changes: 3 additions & 2 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,11 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,

if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) {
if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) {
childHeight = (childWidth - marginRow) / child->getStyle().aspectRatio;
childHeight = marginColumn +
(childWidth - marginRow) / child->getStyle().aspectRatio;
childHeightMeasureMode = YGMeasureModeExactly;
} else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
childWidth =
childWidth = marginRow +
(childHeight - marginColumn) * child->getStyle().aspectRatio;
childWidthMeasureMode = YGMeasureModeExactly;
}
Expand Down

0 comments on commit 4462291

Please sign in to comment.