Skip to content

Commit

Permalink
Separate FlexLine functionality (#39396)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/yoga#1374

Pull Request resolved: #39396

Yoga today has a struct `CollectFlexItemsRowValues`, and function `calculateFlexItemsRowValues()`. These names have evolved over time into something not making much sense.

The job of `calculateFlexItemsRowValues()` is a flex-wrap container into lines (i.e. line-breaking main-axis content, which may be row or column). It returns line-breaking results, but some other fields on `calculateFlexItemsRowValues()` are set much later in the process, and the struct is acting effectivelty as a holder for the line-specific values.

This change:
1. Does some renaming (mainly to FlexLine)
2. Reconciles the count `itemsOnLine` and list `relativeChildren` to list `itemsInFlow` (`relativeChildren` is a lie, as it can include elements with `YGPositionTypeStatic` and exclude relative elements which have `display: "none"`. It really just means children which are included in the layout flow for the line)
3. Makes non-changing algorithm outputs const for clarity of what is a running value, and what is a result of line-breaking values with flex basis.
4. Moves working layout values to a substructure `flexLine.layout`
5. Replaces some dishonest documentation about `endOfLineIndex`.
6. Extracts this logic out of `CalculateLayout()` to a separate file
7. Extracts `boundAxis` wholesale into a separate file, to be usable outside of `CalculateLayout.cpp`

Reviewed By: rshest

Differential Revision: D49133837

fbshipit-source-id: ec68c5a3d2f01e7c9bd8d26e28298331a3fe2475
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 12, 2023
1 parent 3fbb788 commit c85e428
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 341 deletions.
72 changes: 72 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/algorithm/BoundAxis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <yoga/algorithm/FlexDirection.h>
#include <yoga/algorithm/ResolveValue.h>
#include <yoga/node/Node.h>
#include <yoga/numeric/Comparison.h>
#include <yoga/numeric/FloatOptional.h>

namespace facebook::yoga {

inline float paddingAndBorderForAxis(
const yoga::Node* const node,
const YGFlexDirection axis,
const float widthSize) {
return (node->getLeadingPaddingAndBorder(axis, widthSize) +
node->getTrailingPaddingAndBorder(axis, widthSize))
.unwrap();
}

inline FloatOptional boundAxisWithinMinAndMax(
const yoga::Node* const node,
const YGFlexDirection axis,
const FloatOptional value,
const float axisSize) {
FloatOptional min;
FloatOptional max;

if (isColumn(axis)) {
min = yoga::resolveValue(
node->getStyle().minDimensions()[YGDimensionHeight], axisSize);
max = yoga::resolveValue(
node->getStyle().maxDimensions()[YGDimensionHeight], axisSize);
} else if (isRow(axis)) {
min = yoga::resolveValue(
node->getStyle().minDimensions()[YGDimensionWidth], axisSize);
max = yoga::resolveValue(
node->getStyle().maxDimensions()[YGDimensionWidth], axisSize);
}

if (max >= FloatOptional{0} && value > max) {
return max;
}

if (min >= FloatOptional{0} && value < min) {
return min;
}

return value;
}

// Like boundAxisWithinMinAndMax but also ensures that the value doesn't
// go below the padding and border amount.
inline float boundAxis(
const yoga::Node* const node,
const YGFlexDirection axis,
const float value,
const float axisSize,
const float widthSize) {
return yoga::maxOrDefined(
boundAxisWithinMinAndMax(node, axis, FloatOptional{value}, axisSize)
.unwrap(),
paddingAndBorderForAxis(node, axis, widthSize));
}

} // namespace facebook::yoga
Loading

0 comments on commit c85e428

Please sign in to comment.