Files
yoga/yoga/algorithm/FlexLine.h
Nick Gerleman a4f36bdb51 Separate FlexLine functionality (#1374)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1374

X-link: https://github.com/facebook/react-native/pull/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
2023-09-11 19:51:40 -07:00

75 lines
2.5 KiB
C++

/*
* 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 <vector>
#include <yoga/Yoga.h>
#include <yoga/node/Node.h>
namespace facebook::yoga {
struct FlexLineRunningLayout {
// Total flex grow factors of flex items which are to be laid in the current
// line. This is decremented as free space is distributed.
float totalFlexGrowFactors{0.0f};
// Total flex shrink factors of flex items which are to be laid in the current
// line. This is decremented as free space is distributed.
float totalFlexShrinkScaledFactors{0.0f};
// The amount of available space within inner dimensions of the line which may
// still be distributed.
float remainingFreeSpace{0.0f};
// The size of the mainDim for the row after considering size, padding, margin
// and border of flex items. This is used to calculate maxLineDim after going
// through all the rows to decide on the main axis size of owner.
float mainDim{0.0f};
// The size of the crossDim for the row after considering size, padding,
// margin and border of flex items. Used for calculating containers crossSize.
float crossDim{0.0f};
};
struct FlexLine {
// List of children which are part of the line flow. This means they are not
// positioned absolutely, or with `display: "none"`, and do not overflow the
// available dimensions.
const std::vector<yoga::Node*> itemsInFlow{};
// Accumulation of the dimensions and margin of all the children on the
// current line. This will be used in order to either set the dimensions of
// the node if none already exist or to compute the remaining space left for
// the flexible children.
const float sizeConsumed{0.0f};
// The index of the first item beyond the current line.
const size_t endOfLineIndex{0};
// Layout information about the line computed in steps after line-breaking
FlexLineRunningLayout layout{};
};
// Calculates where a line starting at a given index should break, returning
// information about the collective children on the liune.
//
// This function assumes that all the children of node have their
// computedFlexBasis properly computed(To do this use
// computeFlexBasisForChildren function).
FlexLine calculateFlexLine(
yoga::Node* const node,
YGDirection ownerDirection,
float mainAxisownerSize,
float availableInnerWidth,
float availableInnerMainDim,
size_t startOfLineIndex,
size_t lineCount);
} // namespace facebook::yoga