Removed duplicate code by calculating with mainSize/crossSize

Summary:
This PR removes some duplicate code by calculating with ```mainSize```/```crossSize``` and converting that to ```width``` or ```height``` at the end. See #395 .
Closes https://github.com/facebook/yoga/pull/396

Reviewed By: astreet

Differential Revision: D4564713

Pulled By: emilsjolander

fbshipit-source-id: 0b24e69cc9dc75cdf93deeb6c076dcacf134c6d8
This commit is contained in:
Lukas Wöhrl 2017-02-16 07:39:14 -08:00 committed by Facebook Github Bot
parent 1b5eb36e2f
commit 167ac4993a
1 changed files with 84 additions and 146 deletions

View File

@ -1442,8 +1442,7 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
"measure"); "measure");
child->layout.computedFlexBasis = child->layout.computedFlexBasis =
fmaxf(isMainAxisRow ? child->layout.measuredDimensions[YGDimensionWidth] fmaxf(child->layout.measuredDimensions[dim[mainAxis]],
: child->layout.measuredDimensions[YGDimensionHeight],
YGNodePaddingAndBorderForAxis(child, mainAxis, parentWidth)); YGNodePaddingAndBorderForAxis(child, mainAxis, parentWidth));
} }
@ -2310,118 +2309,80 @@ static void YGNodelayoutImpl(const YGNodeRef node,
deltaFreeSpace -= updatedMainSize - childFlexBasis; deltaFreeSpace -= updatedMainSize - childFlexBasis;
float childWidth; const float marginMain =
float childHeight; YGNodeMarginForAxis(currentRelativeChild, mainAxis, availableInnerWidth);
YGMeasureMode childWidthMeasureMode; const float marginCross =
YGMeasureMode childHeightMeasureMode; YGNodeMarginForAxis(currentRelativeChild, crossAxis, availableInnerWidth);
const float marginRow = float childCrossSize;
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionRow, availableInnerWidth); float childMainSize = updatedMainSize + marginMain;
const float marginColumn = YGMeasureMode childCrossMeasureMode;
YGNodeMarginForAxis(currentRelativeChild, YGFlexDirectionColumn, availableInnerWidth); YGMeasureMode childMainMeasureMode = YGMeasureModeExactly;
if (isMainAxisRow) {
childWidth = updatedMainSize + marginRow;
childWidthMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(availableInnerCrossDim) && if (!YGFloatIsUndefined(availableInnerCrossDim) &&
!YGNodeIsStyleDimDefined(currentRelativeChild, !YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis, availableInnerCrossDim) &&
YGFlexDirectionColumn, measureModeCrossDim == YGMeasureModeExactly &&
availableInnerHeight) &&
heightMeasureMode == YGMeasureModeExactly &&
!(isNodeFlexWrap && flexBasisOverflows) && !(isNodeFlexWrap && flexBasisOverflows) &&
YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) { YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) {
childHeight = availableInnerCrossDim; childCrossSize = availableInnerCrossDim;
childHeightMeasureMode = YGMeasureModeExactly; childCrossMeasureMode = YGMeasureModeExactly;
} else if (!YGNodeIsStyleDimDefined(currentRelativeChild, } else if (!YGNodeIsStyleDimDefined(currentRelativeChild,
YGFlexDirectionColumn, crossAxis,
availableInnerHeight)) { availableInnerCrossDim)) {
childHeight = availableInnerCrossDim; childCrossSize = availableInnerCrossDim;
childHeightMeasureMode = childCrossMeasureMode =
YGFloatIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeAtMost; YGFloatIsUndefined(childCrossSize) ? YGMeasureModeUndefined : YGMeasureModeAtMost;
} else { } else {
childHeight = childCrossSize = YGValueResolve(currentRelativeChild->resolvedDimensions[dim[crossAxis]],
YGValueResolve(currentRelativeChild->resolvedDimensions[YGDimensionHeight], availableInnerCrossDim) +
availableInnerHeight) + marginCross;
marginColumn; childCrossMeasureMode =
childHeightMeasureMode = YGFloatIsUndefined(childCrossSize) ? YGMeasureModeUndefined : YGMeasureModeExactly;
YGFloatIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly;
}
} else {
childHeight = updatedMainSize + marginColumn;
childHeightMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(availableInnerCrossDim) &&
!YGNodeIsStyleDimDefined(currentRelativeChild,
YGFlexDirectionRow,
availableInnerWidth) &&
widthMeasureMode == YGMeasureModeExactly && !(isNodeFlexWrap && flexBasisOverflows) &&
YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch) {
childWidth = availableInnerCrossDim;
childWidthMeasureMode = YGMeasureModeExactly;
} else if (!YGNodeIsStyleDimDefined(currentRelativeChild,
YGFlexDirectionRow,
availableInnerWidth)) {
childWidth = availableInnerCrossDim;
childWidthMeasureMode =
YGFloatIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeAtMost;
} else {
childWidth = YGValueResolve(currentRelativeChild->resolvedDimensions[YGDimensionWidth],
availableInnerWidth) +
marginRow;
childWidthMeasureMode =
YGFloatIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly;
}
} }
if (!YGFloatIsUndefined(currentRelativeChild->style.aspectRatio)) { if (!YGFloatIsUndefined(currentRelativeChild->style.aspectRatio)) {
if (isMainAxisRow) { childCrossSize = fmaxf(
childHeight = fmaxf((childWidth - marginRow) / currentRelativeChild->style.aspectRatio, isMainAxisRow
YGNodePaddingAndBorderForAxis(currentRelativeChild, ? (childMainSize - marginMain) / currentRelativeChild->style.aspectRatio
YGFlexDirectionColumn, : (childMainSize - marginMain) * currentRelativeChild->style.aspectRatio,
availableInnerWidth)); YGNodePaddingAndBorderForAxis(currentRelativeChild, crossAxis, availableInnerWidth));
childHeightMeasureMode = YGMeasureModeExactly; childCrossMeasureMode = YGMeasureModeExactly;
// Parent size constraint should have higher priority than flex // Parent size constraint should have higher priority than flex
if (YGNodeIsFlex(currentRelativeChild)) { if (YGNodeIsFlex(currentRelativeChild)) {
childHeight = fminf((childHeight - marginColumn), availableInnerHeight); childCrossSize = fminf(childCrossSize - marginCross, availableInnerCrossDim);
childWidth = marginRow + childHeight * currentRelativeChild->style.aspectRatio; childMainSize =
marginMain + (isMainAxisRow
? childCrossSize * currentRelativeChild->style.aspectRatio
: childCrossSize / currentRelativeChild->style.aspectRatio);
} }
childHeight += marginColumn; childCrossSize += marginCross;
} else {
childWidth =
fmaxf((childHeight - marginColumn) * currentRelativeChild->style.aspectRatio,
YGNodePaddingAndBorderForAxis(currentRelativeChild,
YGFlexDirectionRow,
availableInnerWidth));
childWidthMeasureMode = YGMeasureModeExactly;
// Parent size constraint should have higher priority than flex
if (YGNodeIsFlex(currentRelativeChild)) {
childWidth = fminf((childWidth - marginRow), availableInnerWidth);
childHeight = marginColumn + childWidth / currentRelativeChild->style.aspectRatio;
}
childWidth += marginRow;
}
} }
YGConstrainMaxSizeForMode( YGConstrainMaxSizeForMode(
YGValueResolve(&currentRelativeChild->style.maxDimensions[YGDimensionWidth], YGValueResolve(&currentRelativeChild->style.maxDimensions[dim[mainAxis]],
availableInnerWidth), availableInnerWidth),
&childWidthMeasureMode, &childMainMeasureMode,
&childWidth); &childMainSize);
YGConstrainMaxSizeForMode( YGConstrainMaxSizeForMode(
YGValueResolve(&currentRelativeChild->style.maxDimensions[YGDimensionHeight], YGValueResolve(&currentRelativeChild->style.maxDimensions[dim[crossAxis]],
availableInnerHeight), availableInnerHeight),
&childHeightMeasureMode, &childCrossMeasureMode,
&childHeight); &childCrossSize);
const bool requiresStretchLayout = const bool requiresStretchLayout =
!YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis, availableInnerCrossDim) && !YGNodeIsStyleDimDefined(currentRelativeChild, crossAxis, availableInnerCrossDim) &&
YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch; YGNodeAlignItem(node, currentRelativeChild) == YGAlignStretch;
const float childWidth = isMainAxisRow ? childMainSize : childCrossSize;
const float childHeight = !isMainAxisRow ? childMainSize : childCrossSize;
const YGMeasureMode childWidthMeasureMode =
isMainAxisRow ? childMainMeasureMode : childCrossMeasureMode;
const YGMeasureMode childHeightMeasureMode =
!isMainAxisRow ? childMainMeasureMode : childCrossMeasureMode;
// Recursively call the layout algorithm for this child with the updated // Recursively call the layout algorithm for this child with the updated
// main size. // main size.
YGLayoutNodeInternal(currentRelativeChild, YGLayoutNodeInternal(currentRelativeChild,
@ -2630,59 +2591,36 @@ static void YGNodelayoutImpl(const YGNodeRef node,
if (alignItem == YGAlignStretch && if (alignItem == YGAlignStretch &&
child->style.margin[leading[crossAxis]].unit != YGUnitAuto && child->style.margin[leading[crossAxis]].unit != YGUnitAuto &&
child->style.margin[trailing[crossAxis]].unit != YGUnitAuto) { child->style.margin[trailing[crossAxis]].unit != YGUnitAuto) {
const bool isCrossSizeDefinite =
(isMainAxisRow &&
YGNodeIsStyleDimDefined(child, YGFlexDirectionColumn, availableInnerHeight)) ||
(!isMainAxisRow &&
YGNodeIsStyleDimDefined(child, YGFlexDirectionRow, availableInnerWidth));
float childWidth;
float childHeight;
YGMeasureMode childWidthMeasureMode = YGMeasureModeExactly;
YGMeasureMode childHeightMeasureMode = YGMeasureModeExactly;
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 = marginColumn + childWidth / child->style.aspectRatio;
} else {
childHeight = crossDim;
}
childWidth += marginRow;
} else {
childHeight = child->layout.measuredDimensions[YGDimensionHeight];
if (!YGFloatIsUndefined(child->style.aspectRatio)) {
childWidth = marginRow + childHeight * child->style.aspectRatio;
} else {
childWidth = crossDim;
}
childHeight += marginColumn;
}
YGConstrainMaxSizeForMode(YGValueResolve(&child->style.maxDimensions[YGDimensionWidth],
availableInnerWidth),
&childWidthMeasureMode,
&childWidth);
YGConstrainMaxSizeForMode(YGValueResolve(&child->style.maxDimensions[YGDimensionHeight],
availableInnerHeight),
&childHeightMeasureMode,
&childHeight);
// If the child defines a definite size for its cross axis, there's // If the child defines a definite size for its cross axis, there's
// no need to stretch. // no need to stretch.
if (!isCrossSizeDefinite) { if (!YGNodeIsStyleDimDefined(child, crossAxis, availableInnerCrossDim)) {
childWidthMeasureMode = float childMainSize = child->layout.measuredDimensions[dim[mainAxis]];
float childCrossSize =
!YGFloatIsUndefined(child->style.aspectRatio)
? ((YGNodeMarginForAxis(child, crossAxis, availableInnerWidth) +
(isMainAxisRow ? childMainSize / child->style.aspectRatio
: childMainSize * child->style.aspectRatio)))
: crossDim;
childMainSize += YGNodeMarginForAxis(child, mainAxis, availableInnerWidth);
YGMeasureMode childMainMeasureMode = YGMeasureModeExactly;
YGMeasureMode childCrossMeasureMode = YGMeasureModeExactly;
YGConstrainMaxSizeForMode(YGValueResolve(&child->style.maxDimensions[dim[mainAxis]],
availableInnerMainDim),
&childMainMeasureMode,
&childMainSize);
YGConstrainMaxSizeForMode(YGValueResolve(&child->style.maxDimensions[dim[crossAxis]],
availableInnerCrossDim),
&childCrossMeasureMode,
&childCrossSize);
const float childWidth = isMainAxisRow ? childMainSize : childCrossSize;
const float childHeight = !isMainAxisRow ? childMainSize : childCrossSize;
const YGMeasureMode childWidthMeasureMode =
YGFloatIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly; YGFloatIsUndefined(childWidth) ? YGMeasureModeUndefined : YGMeasureModeExactly;
childHeightMeasureMode = const YGMeasureMode childHeightMeasureMode =
YGFloatIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly; YGFloatIsUndefined(childHeight) ? YGMeasureModeUndefined : YGMeasureModeExactly;
YGLayoutNodeInternal(child, YGLayoutNodeInternal(child,
@ -2769,7 +2707,7 @@ static void YGNodelayoutImpl(const YGNodeRef node,
uint32_t endIndex = 0; uint32_t endIndex = 0;
for (uint32_t i = 0; i < lineCount; i++) { for (uint32_t i = 0; i < lineCount; i++) {
uint32_t startIndex = endIndex; const uint32_t startIndex = endIndex;
uint32_t ii; uint32_t ii;
// compute the line's height and find the endIndex // compute the line's height and find the endIndex