Skip to content

Commit

Permalink
Refactored the flexbox step to determine available space in main and …
Browse files Browse the repository at this point in the history
…cross dim

Reviewed By: emilsjolander

Differential Revision: D6693777

fbshipit-source-id: d6c87670e74bdc22b32916c2d77fe423d831b378
  • Loading branch information
priteshrnandgaonkar authored and facebook-github-bot committed Jan 15, 2018
1 parent 5264832 commit ac1c8c2
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,39 @@ static void YGZeroOutLayoutRecursivly(const YGNodeRef node) {
}
}

static float YGNodeCalculateAvailableInnerDim(
const YGNodeRef node,
YGFlexDirection axis,
float availableDim,
float parentDim) {
YGFlexDirection direction =
YGFlexDirectionIsRow(axis) ? YGFlexDirectionRow : YGFlexDirectionColumn;
YGDimension dimension =
YGFlexDirectionIsRow(axis) ? YGDimensionWidth : YGDimensionHeight;

const float margin = YGNodeMarginForAxis(node, direction, parentDim);
const float paddingAndBorder =
YGNodePaddingAndBorderForAxis(node, direction, parentDim);

float availableInnerDim = availableDim - margin - paddingAndBorder;
// Max dimension overrides predefined dimension value; Min dimension in turn
// overrides both of the above
if (!YGFloatIsUndefined(availableInnerDim)) {
// We want to make sure our available height does not violate min and max
// constraints
const float minInnerDim =
YGResolveValue(node->getStyle().minDimensions[dimension], parentDim) -
paddingAndBorder;
const float maxInnerDim =
YGResolveValue(node->getStyle().maxDimensions[dimension], parentDim) -
paddingAndBorder;
availableInnerDim =
fmaxf(fminf(availableInnerDim, maxInnerDim), minInnerDim);
}

return availableInnerDim;
}

//
// This is the main routine that implements a subset of the flexbox layout
// algorithm
Expand Down Expand Up @@ -1775,7 +1808,6 @@ static void YGNodelayoutImpl(const YGNodeRef node,
const float marginAxisRow = YGNodeMarginForAxis(node, YGFlexDirectionRow, parentWidth);
const float marginAxisColumn = YGNodeMarginForAxis(node, YGFlexDirectionColumn, parentWidth);

// STEP 2: DETERMINE AVAILABLE SIZE IN MAIN AND CROSS DIRECTIONS
const float minInnerWidth =
YGResolveValue(
node->getStyle().minDimensions[YGDimensionWidth], parentWidth) -
Expand All @@ -1795,19 +1827,12 @@ static void YGNodelayoutImpl(const YGNodeRef node,
const float minInnerMainDim = isMainAxisRow ? minInnerWidth : minInnerHeight;
const float maxInnerMainDim = isMainAxisRow ? maxInnerWidth : maxInnerHeight;

// Max dimension overrides predefined dimension value; Min dimension in turn overrides both of the
// above
float availableInnerWidth = availableWidth - marginAxisRow - paddingAndBorderAxisRow;
if (!YGFloatIsUndefined(availableInnerWidth)) {
// We want to make sure our available width does not violate min and max constraints
availableInnerWidth = fmaxf(fminf(availableInnerWidth, maxInnerWidth), minInnerWidth);
}
// STEP 2: DETERMINE AVAILABLE SIZE IN MAIN AND CROSS DIRECTIONS

float availableInnerHeight = availableHeight - marginAxisColumn - paddingAndBorderAxisColumn;
if (!YGFloatIsUndefined(availableInnerHeight)) {
// We want to make sure our available height does not violate min and max constraints
availableInnerHeight = fmaxf(fminf(availableInnerHeight, maxInnerHeight), minInnerHeight);
}
float availableInnerWidth = YGNodeCalculateAvailableInnerDim(
node, YGFlexDirectionRow, availableWidth, parentWidth);
float availableInnerHeight = YGNodeCalculateAvailableInnerDim(
node, YGFlexDirectionColumn, availableHeight, parentHeight);

float availableInnerMainDim = isMainAxisRow ? availableInnerWidth : availableInnerHeight;
const float availableInnerCrossDim = isMainAxisRow ? availableInnerHeight : availableInnerWidth;
Expand Down

0 comments on commit ac1c8c2

Please sign in to comment.