BREAKING: Change aspect ratio behavior

Summary:
== Before ==
- Aspect ratio would do its best to fit within it's parent constraints
- Aspect ratio would prioritize `alignItems: stretch` over other sizing properties.

== After ==
- Aspect ratio is allowed to make a node grow past its parent constraints. This matches many other aspects of flexbox where parent constraints are not treated as hard constraints but rather as suggestions.
- Aspect ratio only takes `alignItems: stretch` into account if no other size definition is defined. This matches the interaction of other properties with `alignItems: stretch`.

== Updating your code ==

**You probably don't need to do anything** but in case something does break in your product it should be as easy as  adding `{width: '100%', height: '100%', flexShrink: 1}` to the style declaring the `aspectRatio`.

Reviewed By: gkassabli

Differential Revision: D5639187

fbshipit-source-id: 603e8fcc3373f0b7f2461da2dad1625ab59dcb19
This commit is contained in:
Emil Sjolander 2017-08-21 03:09:09 -07:00 committed by Facebook Github Bot
parent 992777b765
commit 67c160cc6c

View File

@ -10,8 +10,8 @@
#include <string.h> #include <string.h>
#include "YGNodeList.h" #include "YGNodeList.h"
#include "Yoga.h"
#include "Yoga-internal.h" #include "Yoga-internal.h"
#include "Yoga.h"
#ifdef _MSC_VER #ifdef _MSC_VER
#include <float.h> #include <float.h>
@ -1514,31 +1514,42 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
} }
} }
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) {
childHeight = (childWidth - marginRow) / child->style.aspectRatio;
childHeightMeasureMode = YGMeasureModeExactly;
} else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
childWidth = (childHeight - marginColumn) * child->style.aspectRatio;
childWidthMeasureMode = YGMeasureModeExactly;
}
}
// If child has no defined size in the cross axis and is set to stretch, // If child has no defined size in the cross axis and is set to stretch,
// set the cross // set the cross
// axis to be measured exactly with the available inner width // axis to be measured exactly with the available inner width
if (!isMainAxisRow && !YGFloatIsUndefined(width) && !isRowStyleDimDefined &&
widthMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) { const bool hasExactWidth = !YGFloatIsUndefined(width) && widthMode == YGMeasureModeExactly;
const bool childWidthStretch = YGNodeAlignItem(node, child) == YGAlignStretch &&
childWidthMeasureMode != YGMeasureModeExactly;
if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth && childWidthStretch) {
childWidth = width; childWidth = width;
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
} if (!YGFloatIsUndefined(child->style.aspectRatio)) {
if (isMainAxisRow && !YGFloatIsUndefined(height) && !isColumnStyleDimDefined && childHeight = (childWidth - marginRow) / child->style.aspectRatio;
heightMode == YGMeasureModeExactly && YGNodeAlignItem(node, child) == YGAlignStretch) {
childHeight = height;
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
} }
}
const bool hasExactHeight = !YGFloatIsUndefined(height) && heightMode == YGMeasureModeExactly;
const bool childHeightStretch = YGNodeAlignItem(node, child) == YGAlignStretch &&
childHeightMeasureMode != YGMeasureModeExactly;
if (isMainAxisRow && !isColumnStyleDimDefined && hasExactHeight && childHeightStretch) {
childHeight = height;
childHeightMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(child->style.aspectRatio)) { if (!YGFloatIsUndefined(child->style.aspectRatio)) {
if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { childWidth = (childHeight - marginColumn) * child->style.aspectRatio;
child->layout.computedFlexBasis = childWidthMeasureMode = YGMeasureModeExactly;
fmaxf((childWidth - marginRow) / child->style.aspectRatio,
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn, parentWidth));
return;
} else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
child->layout.computedFlexBasis =
fmaxf((childHeight - marginColumn) * child->style.aspectRatio,
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, parentWidth));
return;
} }
} }
@ -1631,13 +1642,9 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) { if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) {
if (!YGFloatIsUndefined(child->style.aspectRatio)) { if (!YGFloatIsUndefined(child->style.aspectRatio)) {
if (YGFloatIsUndefined(childWidth)) { if (YGFloatIsUndefined(childWidth)) {
childWidth = childWidth = marginRow + (childHeight - marginColumn) * child->style.aspectRatio;
marginRow + fmaxf((childHeight - marginColumn) * child->style.aspectRatio,
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionColumn, width));
} else if (YGFloatIsUndefined(childHeight)) { } else if (YGFloatIsUndefined(childHeight)) {
childHeight = childHeight = marginColumn + (childWidth - marginRow) / child->style.aspectRatio;
marginColumn + fmaxf((childWidth - marginRow) / child->style.aspectRatio,
YGNodePaddingAndBorderForAxis(child, YGFlexDirectionRow, width));
} }
} }
} }
@ -1688,9 +1695,9 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
config); config);
if (YGNodeIsTrailingPosDefined(child, mainAxis) && !YGNodeIsLeadingPosDefined(child, mainAxis)) { if (YGNodeIsTrailingPosDefined(child, mainAxis) && !YGNodeIsLeadingPosDefined(child, mainAxis)) {
child->layout.position[leading[mainAxis]] = node->layout.measuredDimensions[dim[mainAxis]] - child->layout.position[leading[mainAxis]] =
child->layout.measuredDimensions[dim[mainAxis]] - node->layout.measuredDimensions[dim[mainAxis]] -
YGNodeTrailingBorder(node, mainAxis) - child->layout.measuredDimensions[dim[mainAxis]] - YGNodeTrailingBorder(node, mainAxis) -
YGNodeTrailingMargin(child, mainAxis, width) - YGNodeTrailingMargin(child, mainAxis, width) -
YGNodeTrailingPosition(child, mainAxis, isMainAxisRow ? width : height); YGNodeTrailingPosition(child, mainAxis, isMainAxisRow ? width : height);
} else if (!YGNodeIsLeadingPosDefined(child, mainAxis) && } else if (!YGNodeIsLeadingPosDefined(child, mainAxis) &&
@ -1706,9 +1713,9 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
if (YGNodeIsTrailingPosDefined(child, crossAxis) && if (YGNodeIsTrailingPosDefined(child, crossAxis) &&
!YGNodeIsLeadingPosDefined(child, crossAxis)) { !YGNodeIsLeadingPosDefined(child, crossAxis)) {
child->layout.position[leading[crossAxis]] = node->layout.measuredDimensions[dim[crossAxis]] - child->layout.position[leading[crossAxis]] =
child->layout.measuredDimensions[dim[crossAxis]] - node->layout.measuredDimensions[dim[crossAxis]] -
YGNodeTrailingBorder(node, crossAxis) - child->layout.measuredDimensions[dim[crossAxis]] - YGNodeTrailingBorder(node, crossAxis) -
YGNodeTrailingMargin(child, crossAxis, width) - YGNodeTrailingMargin(child, crossAxis, width) -
YGNodeTrailingPosition(child, crossAxis, isMainAxisRow ? height : width); YGNodeTrailingPosition(child, crossAxis, isMainAxisRow ? height : width);
} else if (!YGNodeIsLeadingPosDefined(child, crossAxis) && } else if (!YGNodeIsLeadingPosDefined(child, crossAxis) &&
@ -1718,7 +1725,8 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node,
child->layout.measuredDimensions[dim[crossAxis]]) / child->layout.measuredDimensions[dim[crossAxis]]) /
2.0f; 2.0f;
} else if (!YGNodeIsLeadingPosDefined(child, crossAxis) && } else if (!YGNodeIsLeadingPosDefined(child, crossAxis) &&
((YGNodeAlignItem(node, child) == YGAlignFlexEnd) ^ (node->style.flexWrap == YGWrapWrapReverse))) { ((YGNodeAlignItem(node, child) == YGAlignFlexEnd) ^
(node->style.flexWrap == YGWrapWrapReverse))) {
child->layout.position[leading[crossAxis]] = (node->layout.measuredDimensions[dim[crossAxis]] - child->layout.position[leading[crossAxis]] = (node->layout.measuredDimensions[dim[crossAxis]] -
child->layout.measuredDimensions[dim[crossAxis]]); child->layout.measuredDimensions[dim[crossAxis]]);
} }
@ -1744,7 +1752,8 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(const YGNodeRef node,
const float innerWidth = YGFloatIsUndefined(availableWidth) const float innerWidth = YGFloatIsUndefined(availableWidth)
? availableWidth ? availableWidth
: fmaxf(0, availableWidth - marginAxisRow - paddingAndBorderAxisRow); : fmaxf(0, availableWidth - marginAxisRow - paddingAndBorderAxisRow);
const float innerHeight = YGFloatIsUndefined(availableHeight) const float innerHeight =
YGFloatIsUndefined(availableHeight)
? availableHeight ? availableHeight
: fmaxf(0, availableHeight - marginAxisColumn - paddingAndBorderAxisColumn); : fmaxf(0, availableHeight - marginAxisColumn - paddingAndBorderAxisColumn);
@ -2498,8 +2507,18 @@ static void YGNodelayoutImpl(const YGNodeRef node,
YGMeasureMode childCrossMeasureMode; YGMeasureMode childCrossMeasureMode;
YGMeasureMode childMainMeasureMode = YGMeasureModeExactly; YGMeasureMode childMainMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(availableInnerCrossDim) && if (!YGFloatIsUndefined(currentRelativeChild->style.aspectRatio)) {
!YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis, availableInnerCrossDim) && childCrossSize =
isMainAxisRow
? (childMainSize - marginMain) / currentRelativeChild->style.aspectRatio
: (childMainSize - marginMain) * currentRelativeChild->style.aspectRatio;
childCrossMeasureMode = YGMeasureModeExactly;
childCrossSize += marginCross;
} else if (!YGFloatIsUndefined(availableInnerCrossDim) &&
!YGNodeIsStyleDimDefined(currentRelativeChild,
crossAxis,
availableInnerCrossDim) &&
measureModeCrossDim == YGMeasureModeExactly && measureModeCrossDim == YGMeasureModeExactly &&
!(isNodeFlexWrap && flexBasisOverflows) && !(isNodeFlexWrap && flexBasisOverflows) &&
YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) {
@ -2523,26 +2542,6 @@ static void YGNodelayoutImpl(const YGNodeRef node,
: YGMeasureModeExactly; : YGMeasureModeExactly;
} }
if (!YGFloatIsUndefined(currentRelativeChild->style.aspectRatio)) {
childCrossSize = fmaxf(
isMainAxisRow
? (childMainSize - marginMain) / currentRelativeChild->style.aspectRatio
: (childMainSize - marginMain) * currentRelativeChild->style.aspectRatio,
YGNodePaddingAndBorderForAxis(currentRelativeChild, crossAxis, availableInnerWidth));
childCrossMeasureMode = YGMeasureModeExactly;
// Parent size constraint should have higher priority than flex
if (YGNodeIsFlex(currentRelativeChild)) {
childCrossSize = fminf(childCrossSize - marginCross, availableInnerCrossDim);
childMainSize =
marginMain + (isMainAxisRow
? childCrossSize * currentRelativeChild->style.aspectRatio
: childCrossSize / currentRelativeChild->style.aspectRatio);
}
childCrossSize += marginCross;
}
YGConstrainMaxSizeForMode(currentRelativeChild, YGConstrainMaxSizeForMode(currentRelativeChild,
mainAxis, mainAxis,
availableInnerMainDim, availableInnerMainDim,
@ -3199,13 +3198,25 @@ bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
return false; return false;
} }
bool useRoundedComparison = config != NULL && config->pointScaleFactor != 0; bool useRoundedComparison = config != NULL && config->pointScaleFactor != 0;
const float effectiveWidth = useRoundedComparison ? YGRoundValueToPixelGrid(width, config->pointScaleFactor, false, false) : width; const float effectiveWidth =
const float effectiveHeight = useRoundedComparison ? YGRoundValueToPixelGrid(height, config->pointScaleFactor, false, false) : height; useRoundedComparison ? YGRoundValueToPixelGrid(width, config->pointScaleFactor, false, false)
const float effectiveLastWidth = useRoundedComparison ? YGRoundValueToPixelGrid(lastWidth, config->pointScaleFactor, false, false) : lastWidth; : width;
const float effectiveLastHeight = useRoundedComparison ? YGRoundValueToPixelGrid(lastHeight, config->pointScaleFactor, false, false) : lastHeight; const float effectiveHeight =
useRoundedComparison ? YGRoundValueToPixelGrid(height, config->pointScaleFactor, false, false)
: height;
const float effectiveLastWidth =
useRoundedComparison
? YGRoundValueToPixelGrid(lastWidth, config->pointScaleFactor, false, false)
: lastWidth;
const float effectiveLastHeight =
useRoundedComparison
? YGRoundValueToPixelGrid(lastHeight, config->pointScaleFactor, false, false)
: lastHeight;
const bool hasSameWidthSpec = lastWidthMode == widthMode && YGFloatsEqual(effectiveLastWidth, effectiveWidth); const bool hasSameWidthSpec =
const bool hasSameHeightSpec = lastHeightMode == heightMode && YGFloatsEqual(effectiveLastHeight, effectiveHeight); lastWidthMode == widthMode && YGFloatsEqual(effectiveLastWidth, effectiveWidth);
const bool hasSameHeightSpec =
lastHeightMode == heightMode && YGFloatsEqual(effectiveLastHeight, effectiveHeight);
const bool widthIsCompatible = const bool widthIsCompatible =
hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode, hasSameWidthSpec || YGMeasureModeSizeIsExactAndMatchesOldMeasuredSize(widthMode,
@ -3479,7 +3490,8 @@ static void YGRoundToPixelGrid(const YGNodeRef node,
node->layout.position[YGEdgeTop] = node->layout.position[YGEdgeTop] =
YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding); YGRoundValueToPixelGrid(nodeTop, pointScaleFactor, false, textRounding);
// We multiply dimension by scale factor and if the result is close to the whole number, we don't have any fraction // We multiply dimension by scale factor and if the result is close to the whole number, we don't
// have any fraction
// To verify if the result is close to whole number we want to check both floor and ceil numbers // To verify if the result is close to whole number we want to check both floor and ceil numbers
const bool hasFractionalWidth = !YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 0) && const bool hasFractionalWidth = !YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 0) &&
!YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 1.0); !YGFloatsEqual(fmodf(nodeWidth * pointScaleFactor, 1.0), 1.0);
@ -3487,15 +3499,13 @@ static void YGRoundToPixelGrid(const YGNodeRef node,
!YGFloatsEqual(fmodf(nodeHeight * pointScaleFactor, 1.0), 1.0); !YGFloatsEqual(fmodf(nodeHeight * pointScaleFactor, 1.0), 1.0);
node->layout.dimensions[YGDimensionWidth] = node->layout.dimensions[YGDimensionWidth] =
YGRoundValueToPixelGrid( YGRoundValueToPixelGrid(absoluteNodeRight,
absoluteNodeRight,
pointScaleFactor, pointScaleFactor,
(textRounding && hasFractionalWidth), (textRounding && hasFractionalWidth),
(textRounding && !hasFractionalWidth)) - (textRounding && !hasFractionalWidth)) -
YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, textRounding); YGRoundValueToPixelGrid(absoluteNodeLeft, pointScaleFactor, false, textRounding);
node->layout.dimensions[YGDimensionHeight] = node->layout.dimensions[YGDimensionHeight] =
YGRoundValueToPixelGrid( YGRoundValueToPixelGrid(absoluteNodeBottom,
absoluteNodeBottom,
pointScaleFactor, pointScaleFactor,
(textRounding && hasFractionalHeight), (textRounding && hasFractionalHeight),
(textRounding && !hasFractionalHeight)) - (textRounding && !hasFractionalHeight)) -