Skip to content

Commit

Permalink
Fix handling of negative flex gap (#39596)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39596

X-link: facebook/yoga#1405

I noticed that we weren't clamping negative flex gap values to zero. This fixes that bug.

Reviewed By: rshest

Differential Revision: D49530494

fbshipit-source-id: 069db7312f72a085c5c4b01ead7bc66a353a07e5
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Oct 1, 2023
1 parent df492cc commit aa65e92
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ static void justifyMainAxis(
node->getLeadingPaddingAndBorder(mainAxis, ownerWidth).unwrap();
const float trailingPaddingAndBorderMain =
node->getTrailingPaddingAndBorder(mainAxis, ownerWidth).unwrap();
const float gap = node->getGapForAxis(mainAxis, ownerWidth).unwrap();
const float gap = node->getGapForAxis(mainAxis, ownerWidth);
// If we are using "at most" rules in the main axis, make sure that
// remainingFreeSpace is 0 when min main dimension is not given
if (measureModeMainDim == MeasureMode::AtMost &&
Expand Down Expand Up @@ -1666,8 +1666,7 @@ static void calculateLayoutImpl(
generationCount);

if (childCount > 1) {
totalMainDim +=
node->getGapForAxis(mainAxis, availableInnerCrossDim).unwrap() *
totalMainDim += node->getGapForAxis(mainAxis, availableInnerCrossDim) *
static_cast<float>(childCount - 1);
}

Expand All @@ -1692,7 +1691,7 @@ static void calculateLayoutImpl(
float totalLineCrossDim = 0;

const float crossAxisGap =
node->getGapForAxis(crossAxis, availableInnerCrossDim).unwrap();
node->getGapForAxis(crossAxis, availableInnerCrossDim);

// Max main dimension of all the lines.
float maxLineMainDim = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ FlexLine calculateFlexLine(
const FlexDirection mainAxis = resolveDirection(
node->getStyle().flexDirection(), node->resolveDirection(ownerDirection));
const bool isNodeFlexWrap = node->getStyle().flexWrap() != Wrap::NoWrap;
const float gap = node->getGapForAxis(mainAxis, availableInnerWidth).unwrap();
const float gap = node->getGapForAxis(mainAxis, availableInnerWidth);

// Add items to the current line until it's full or we run out of items.
for (; endOfLineIndex < node->getChildren().size(); endOfLineIndex++) {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ FloatOptional Node::getMarginForAxis(
return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize);
}

FloatOptional Node::getGapForAxis(
const FlexDirection axis,
const float widthSize) const {
float Node::getGapForAxis(const FlexDirection axis, const float widthSize)
const {
auto gap = isRow(axis)
? computeColumnGap(style_.gap(), CompactValue::ofZero())
: computeRowGap(style_.gap(), CompactValue::ofZero());
return yoga::resolveValue(gap, widthSize);
? computeColumnGap(style_.gap(), CompactValue::ofUndefined())
: computeRowGap(style_.gap(), CompactValue::ofUndefined());
auto resolvedGap = yoga::resolveValue(gap, widthSize);
return maxOrDefined(resolvedGap.unwrap(), 0);
}

YGSize Node::measure(
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ class YG_EXPORT Node : public ::YGNode {
FloatOptional getMarginForAxis(
const FlexDirection axis,
const float widthSize) const;
FloatOptional getGapForAxis(const FlexDirection axis, const float widthSize)
const;
float getGapForAxis(const FlexDirection axis, const float widthSize) const;
// Setters

void setContext(void* context) {
Expand Down

0 comments on commit aa65e92

Please sign in to comment.