From 53279ba4267c573d0c471380265a8e4e8fbd6f86 Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Thu, 12 Oct 2023 16:22:27 -0700 Subject: [PATCH] Fix issue where marginStart and marginEnd were not working with rowReverse flex direction (#40804) Summary: X-link: https://github.com/facebook/litho/pull/962 Pull Request resolved: https://github.com/facebook/react-native/pull/40804 X-link: https://github.com/facebook/yoga/pull/1420 This stack is ultimately aiming to solve https://github.com/facebook/yoga/issues/1208 **The problem** Turns out that we do not even check direction when determining which edge is the leading (start) and trailing (end) edges. This is not how web does it as the start/end is based on the writing direction NOT the flex direction: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox#start_and_end_lines. While web does not have marginStart and marginEnd, they do have margin-inline-start/end which relies on the writing mode to determine the "start"/"end": https://developer.mozilla.org/en-US/docs/Web/CSS/margin-inline-start. This means that if you do something like ``` export default function Playground(props: Props): React.Node { return ( ); } const styles = StyleSheet.create({ container: { marginEnd: 100, flexDirection: 'row-reverse', backgroundColor: 'red', display: 'flex', width: 100, height: 100, }, item: { backgroundColor: 'blue', width: 10, }, }); ``` You get {F1116264350} As you can see the margin gets applied to the left edge even thought the direction is ltr and it should be applied to the right edge. **The solution** I ended up fixing this by creating a new `leadingLayoutEdge` and `trailingLayoutEdge` function that take the flex direction as well as the direction. Based on the errata, the a few functions will use these new functions to determine which `YGEdge` is the starting/ending. You might be wondering why I did not put this logic inside of `leadingEdge(flexDirection)` / `trailingEdge(flexDirection)` since other areas could potentially have the same bug like `getLeadingPadding`. These functions are a bit overloaded and there are cases where we actually want to use the flexDirection to get the edge in question. For example, many of the calls to `setLayoutPosition` in `CalculateLayout.cpp` call `leadingEdge()` / `trailingEdge()` to set the proper position for cases like row-reverse where items need to line up in a different direction. Reviewed By: NickGerleman Differential Revision: D50140503 fbshipit-source-id: 5b580c7570f6ae1e2d031971926ac4e8f52dd362 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 41 ++++++----- .../yoga/yoga/algorithm/FlexDirection.h | 20 ++++++ .../ReactCommon/yoga/yoga/node/Node.cpp | 70 +++++++++++++++---- .../ReactCommon/yoga/yoga/node/Node.h | 17 ++++- 4 files changed, 115 insertions(+), 33 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index d2d56b7d20e330..dc9f785eacf701 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -54,8 +54,7 @@ static inline float dimensionWithMargin( const FlexDirection axis, const float widthSize) { return node->getLayout().measuredDimension(dimension(axis)) + - (node->getLeadingMargin(axis, widthSize) + - node->getTrailingMargin(axis, widthSize)); + node->getMarginForAxis(axis, widthSize); } static inline bool styleDefinesDimension( @@ -453,7 +452,8 @@ static void layoutAbsoluteChild( node->getLayout().measuredDimension(dimension(mainAxis)) - child->getLayout().measuredDimension(dimension(mainAxis)) - node->getTrailingBorder(mainAxis) - - child->getTrailingMargin(mainAxis, isMainAxisRow ? width : height) - + child->getTrailingMargin( + mainAxis, direction, isMainAxisRow ? width : height) - child->getTrailingPosition( mainAxis, isMainAxisRow ? width : height), leadingEdge(mainAxis)); @@ -483,6 +483,7 @@ static void layoutAbsoluteChild( node->getLeadingBorder(mainAxis) + child->getLeadingMargin( mainAxis, + direction, node->getLayout().measuredDimension(dimension(mainAxis))), leadingEdge(mainAxis)); } @@ -494,7 +495,7 @@ static void layoutAbsoluteChild( child->getLayout().measuredDimension(dimension(crossAxis)) - node->getTrailingBorder(crossAxis) - child->getTrailingMargin( - crossAxis, isMainAxisRow ? height : width) - + crossAxis, direction, isMainAxisRow ? height : width) - child->getTrailingPosition( crossAxis, isMainAxisRow ? height : width), leadingEdge(crossAxis)); @@ -526,6 +527,7 @@ static void layoutAbsoluteChild( node->getLeadingBorder(crossAxis) + child->getLeadingMargin( crossAxis, + direction, node->getLayout().measuredDimension(dimension(crossAxis))), leadingEdge(crossAxis)); } @@ -1186,6 +1188,7 @@ static void justifyMainAxis( const size_t startOfLineIndex, const FlexDirection mainAxis, const FlexDirection crossAxis, + const Direction direction, const MeasureMode measureModeMainDim, const MeasureMode measureModeCrossDim, const float mainAxisownerSize, @@ -1303,7 +1306,8 @@ static void justifyMainAxis( child->setLayoutPosition( child->getLeadingPosition(mainAxis, availableInnerMainDim) + node->getLeadingBorder(mainAxis) + - child->getLeadingMargin(mainAxis, availableInnerWidth), + child->getLeadingMargin( + mainAxis, direction, availableInnerWidth), leadingEdge(mainAxis)); } } else { @@ -1352,7 +1356,7 @@ static void justifyMainAxis( // calculated by adding maxAscent and maxDescent from the baseline. const float ascent = calculateBaseline(child) + child->getLeadingMargin( - FlexDirection::Column, availableInnerWidth); + FlexDirection::Column, direction, availableInnerWidth); const float descent = child->getLayout().measuredDimension(Dimension::Height) + child->getMarginForAxis( @@ -1498,16 +1502,16 @@ static void calculateLayoutImpl( const YGEdge endEdge = direction == Direction::LTR ? YGEdgeRight : YGEdgeLeft; const float marginRowLeading = - node->getLeadingMargin(flexRowDirection, ownerWidth); + node->getLeadingMargin(flexRowDirection, direction, ownerWidth); node->setLayoutMargin(marginRowLeading, startEdge); const float marginRowTrailing = - node->getTrailingMargin(flexRowDirection, ownerWidth); + node->getTrailingMargin(flexRowDirection, direction, ownerWidth); node->setLayoutMargin(marginRowTrailing, endEdge); const float marginColumnLeading = - node->getLeadingMargin(flexColumnDirection, ownerWidth); + node->getLeadingMargin(flexColumnDirection, direction, ownerWidth); node->setLayoutMargin(marginColumnLeading, YGEdgeTop); const float marginColumnTrailing = - node->getTrailingMargin(flexColumnDirection, ownerWidth); + node->getTrailingMargin(flexColumnDirection, direction, ownerWidth); node->setLayoutMargin(marginColumnTrailing, YGEdgeBottom); const float marginAxisRow = marginRowLeading + marginRowTrailing; @@ -1793,6 +1797,7 @@ static void calculateLayoutImpl( startOfLineIndex, mainAxis, crossAxis, + direction, measureModeMainDim, measureModeCrossDim, mainAxisownerSize, @@ -1849,7 +1854,8 @@ static void calculateLayoutImpl( child->setLayoutPosition( child->getLeadingPosition(crossAxis, availableInnerCrossDim) + node->getLeadingBorder(crossAxis) + - child->getLeadingMargin(crossAxis, availableInnerWidth), + child->getLeadingMargin( + crossAxis, direction, availableInnerWidth), leadingEdge(crossAxis)); } // If leading position is not defined or calculations result in Nan, @@ -1859,7 +1865,8 @@ static void calculateLayoutImpl( child->getLayout().position[leadingEdge(crossAxis)])) { child->setLayoutPosition( node->getLeadingBorder(crossAxis) + - child->getLeadingMargin(crossAxis, availableInnerWidth), + child->getLeadingMargin( + crossAxis, direction, availableInnerWidth), leadingEdge(crossAxis)); } } else { @@ -2053,7 +2060,7 @@ static void calculateLayoutImpl( if (resolveChildAlignment(node, child) == Align::Baseline) { const float ascent = calculateBaseline(child) + child->getLeadingMargin( - FlexDirection::Column, availableInnerWidth); + FlexDirection::Column, direction, availableInnerWidth); const float descent = child->getLayout().measuredDimension(Dimension::Height) + child->getMarginForAxis( @@ -2083,7 +2090,8 @@ static void calculateLayoutImpl( case Align::FlexStart: { child->setLayoutPosition( currentLead + - child->getLeadingMargin(crossAxis, availableInnerWidth), + child->getLeadingMargin( + crossAxis, direction, availableInnerWidth), leadingEdge(crossAxis)); break; } @@ -2091,7 +2099,7 @@ static void calculateLayoutImpl( child->setLayoutPosition( currentLead + lineHeight - child->getTrailingMargin( - crossAxis, availableInnerWidth) - + crossAxis, direction, availableInnerWidth) - child->getLayout().measuredDimension( dimension(crossAxis)), leadingEdge(crossAxis)); @@ -2109,7 +2117,8 @@ static void calculateLayoutImpl( case Align::Stretch: { child->setLayoutPosition( currentLead + - child->getLeadingMargin(crossAxis, availableInnerWidth), + child->getLeadingMargin( + crossAxis, direction, availableInnerWidth), leadingEdge(crossAxis)); // Remeasure child with the line height as it as been only diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexDirection.h b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexDirection.h index 010bfed40957fa..7dd2b4cce7b381 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexDirection.h +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexDirection.h @@ -77,6 +77,26 @@ inline YGEdge trailingEdge(const FlexDirection flexDirection) { fatalWithMessage("Invalid FlexDirection"); } +inline YGEdge leadingLayoutEdge( + const FlexDirection flexDirection, + const Direction direction) { + if (isRow(flexDirection)) { + return direction == Direction::RTL ? YGEdgeRight : YGEdgeLeft; + } + + return YGEdgeTop; +} + +inline YGEdge trailingLayoutEdge( + const FlexDirection flexDirection, + const Direction direction) { + if (isRow(flexDirection)) { + return direction == Direction::RTL ? YGEdgeLeft : YGEdgeRight; + } + + return YGEdgeBottom; +} + inline Dimension dimension(const FlexDirection flexDirection) { switch (flexDirection) { case FlexDirection::Column: diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp index d84aac542fc5fb..c7cee82930777e 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp @@ -83,6 +83,22 @@ CompactValue Node::computeEdgeValueForColumn( } } +YGEdge Node::getLeadingLayoutEdgeUsingErrata( + FlexDirection flexDirection, + Direction direction) const { + return hasErrata(Errata::StartingEndingEdgeFromFlexDirection) + ? leadingEdge(flexDirection) + : leadingLayoutEdge(flexDirection, direction); +} + +YGEdge Node::getTrailingLayoutEdgeUsingErrata( + FlexDirection flexDirection, + Direction direction) const { + return hasErrata(Errata::StartingEndingEdgeFromFlexDirection) + ? trailingEdge(flexDirection) + : trailingLayoutEdge(flexDirection, direction); +} + bool Node::isLeadingPositionDefined(FlexDirection axis) const { auto leadingPosition = isRow(axis) ? computeEdgeValueForRow( @@ -117,18 +133,26 @@ float Node::getTrailingPosition(FlexDirection axis, float axisSize) const { return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f); } -float Node::getLeadingMargin(FlexDirection axis, float widthSize) const { +float Node::getLeadingMargin( + FlexDirection axis, + Direction direction, + float widthSize) const { + const YGEdge startEdge = getLeadingLayoutEdgeUsingErrata(axis, direction); auto leadingMargin = isRow(axis) - ? computeEdgeValueForRow(style_.margin(), YGEdgeStart, leadingEdge(axis)) - : computeEdgeValueForColumn(style_.margin(), leadingEdge(axis)); + ? computeEdgeValueForRow(style_.margin(), YGEdgeStart, startEdge) + : computeEdgeValueForColumn(style_.margin(), startEdge); return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f); } -float Node::getTrailingMargin(FlexDirection axis, float widthSize) const { +float Node::getTrailingMargin( + FlexDirection axis, + Direction direction, + float widthSize) const { + const YGEdge endEdge = getTrailingLayoutEdgeUsingErrata(axis, direction); auto trailingMargin = isRow(axis) - ? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, trailingEdge(axis)) - : computeEdgeValueForColumn(style_.margin(), trailingEdge(axis)); + ? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, endEdge) + : computeEdgeValueForColumn(style_.margin(), endEdge); return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f); } @@ -176,7 +200,10 @@ float Node::getTrailingPaddingAndBorder(FlexDirection axis, float widthSize) } float Node::getMarginForAxis(FlexDirection axis, float widthSize) const { - return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize); + // The total margin for a given axis does not depend on the direction + // so hardcoding LTR here to avoid piping direction to this function + return getLeadingMargin(axis, Direction::LTR, widthSize) + + getTrailingMargin(axis, Direction::LTR, widthSize); } float Node::getGapForAxis(FlexDirection axis) const { @@ -360,18 +387,31 @@ void Node::setPosition( const float relativePositionMain = relativePosition(mainAxis, mainSize); const float relativePositionCross = relativePosition(crossAxis, crossSize); + const YGEdge mainAxisLeadingEdge = + getLeadingLayoutEdgeUsingErrata(mainAxis, direction); + const YGEdge mainAxisTrailingEdge = + getTrailingLayoutEdgeUsingErrata(mainAxis, direction); + const YGEdge crossAxisLeadingEdge = + getLeadingLayoutEdgeUsingErrata(crossAxis, direction); + const YGEdge crossAxisTrailingEdge = + getTrailingLayoutEdgeUsingErrata(crossAxis, direction); + setLayoutPosition( - (getLeadingMargin(mainAxis, ownerWidth) + relativePositionMain), - leadingEdge(mainAxis)); + (getLeadingMargin(mainAxis, direction, ownerWidth) + + relativePositionMain), + mainAxisLeadingEdge); setLayoutPosition( - (getTrailingMargin(mainAxis, ownerWidth) + relativePositionMain), - trailingEdge(mainAxis)); + (getTrailingMargin(mainAxis, direction, ownerWidth) + + relativePositionMain), + mainAxisTrailingEdge); setLayoutPosition( - (getLeadingMargin(crossAxis, ownerWidth) + relativePositionCross), - leadingEdge(crossAxis)); + (getLeadingMargin(crossAxis, direction, ownerWidth) + + relativePositionCross), + crossAxisLeadingEdge); setLayoutPosition( - (getTrailingMargin(crossAxis, ownerWidth) + relativePositionCross), - trailingEdge(crossAxis)); + (getTrailingMargin(crossAxis, direction, ownerWidth) + + relativePositionCross), + crossAxisTrailingEdge); } YGValue Node::marginLeadingValue(FlexDirection axis) const { diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index bf1f89f3325b56..4aaf0f7b3c68e5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -50,6 +50,13 @@ class YG_EXPORT Node : public ::YGNode { float relativePosition(FlexDirection axis, const float axisSize) const; + YGEdge getLeadingLayoutEdgeUsingErrata( + FlexDirection flexDirection, + Direction direction) const; + YGEdge getTrailingLayoutEdgeUsingErrata( + FlexDirection flexDirection, + Direction direction) const; + void useWebDefaults() { style_.flexDirection() = FlexDirection::Row; style_.alignContent() = Align::Stretch; @@ -193,8 +200,14 @@ class YG_EXPORT Node : public ::YGNode { bool isTrailingPosDefined(FlexDirection axis) const; float getLeadingPosition(FlexDirection axis, float axisSize) const; float getTrailingPosition(FlexDirection axis, float axisSize) const; - float getLeadingMargin(FlexDirection axis, float widthSize) const; - float getTrailingMargin(FlexDirection axis, float widthSize) const; + float getLeadingMargin( + FlexDirection axis, + Direction direction, + float widthSize) const; + float getTrailingMargin( + FlexDirection axis, + Direction direction, + float widthSize) const; float getLeadingBorder(FlexDirection flexDirection) const; float getTrailingBorder(FlexDirection flexDirection) const; float getLeadingPadding(FlexDirection axis, float widthSize) const;