Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <yoga/Yoga.h>
#include <yoga/debug/AssertFatal.h>
#include <yoga/node/Node.h>
#include <yoga/style/GridTrack.h>
#include <yoga/style/GridTrackSize.h>

using namespace facebook;
using namespace facebook::yoga;
Expand Down
67 changes: 67 additions & 0 deletions yoga/algorithm/grid/GridItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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/enums/Dimension.h>
#include <yoga/node/Node.h>
#include <cstddef>
#include <map>
#include <vector>

namespace facebook::yoga {

// A grid item with its resolved position in the grid. Positions are set by the
// auto-placement algorithm and consumed by the track sizing algorithm.
struct GridItem {
size_t columnStart;
size_t columnEnd;
size_t rowStart;
size_t rowEnd;
yoga::Node* node;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need a full back-reference to the Yoga node?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use node reference to call measure methods in TrackSizing algorithm. e.g. calling calculateLayoutInternal. Also to get style from node like item.node->hasDefiniteLength(), item.node->style().computeMarginForAxis etc in track sizing.


// Additional offset added to item to align baselines
// https://www.w3.org/TR/css-grid-1/#algo-baseline-shims
float baselineShim = 0.0f;

// Flags used for optimizations in track sizing algorithm.
bool crossesIntrinsicRow = false;
bool crossesIntrinsicColumn = false;
bool crossesFlexibleRow = false;
bool crossesFlexibleColumn = false;

GridItem(
size_t columnStart,
size_t columnEnd,
size_t rowStart,
size_t rowEnd,
yoga::Node* node,
float baselineShim = 0.0f)
: columnStart(columnStart),
columnEnd(columnEnd),
rowStart(rowStart),
rowEnd(rowEnd),
node(node),
baselineShim(baselineShim) {}

// Helper functions used by the track sizing algorithm
bool crossesIntrinsicTrack(Dimension dimension) const {
return dimension == Dimension::Width ? crossesIntrinsicColumn
: crossesIntrinsicRow;
}
bool crossesFlexibleTrack(Dimension dimension) const {
return dimension == Dimension::Width ? crossesFlexibleColumn
: crossesFlexibleRow;
}
};

// Baseline sharing groups - items grouped by their starting row for the
// resolve intrinsic size step in track sizing algorithm.
// https://www.w3.org/TR/css-grid-1/#algo-baseline-shims
using BaselineItemGroups = std::map<size_t, std::vector<GridItem*>>;

} // namespace facebook::yoga
30 changes: 30 additions & 0 deletions yoga/algorithm/grid/GridTrack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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/style/GridTrackSize.h>

namespace facebook::yoga {

// https://www.w3.org/TR/css-grid-1/#algo-track-sizing
struct GridTrack {
// Sizing functions for this track, immutable after construction
const GridTrackSize trackSize;

// Mutable state used by the track sizing algorithm
// https://www.w3.org/TR/css-grid-1/#base-size
float baseSize = 0.0f;
// https://www.w3.org/TR/css-grid-1/#growth-limit
float growthLimit = 0.0f;
// https://www.w3.org/TR/css-grid-1/#infinitely-growable
bool infinitelyGrowable = false;

explicit GridTrack(const GridTrackSize& ts) : trackSize(ts) {}
};

} // namespace facebook::yoga
9 changes: 2 additions & 7 deletions yoga/style/GridTrack.h → yoga/style/GridTrackSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
#include <vector>

namespace facebook::yoga {
// Represents a track size as defined in
// https://www.w3.org/TR/css-grid-1/#typedef-track-size
// and helper functions for creating common track sizes.
struct GridTrackSize {
StyleSizeLength minSizingFunction;
StyleSizeLength maxSizingFunction;

// These are used in the grid layout algorithm when distributing spaces among
// tracks
// TODO: maybe move them to TrackSizing since these are track states
float baseSize = 0.0f;
float growthLimit = 0.0f;
bool infinitelyGrowable = false;

// Static factory methods for common cases
constexpr static GridTrackSize auto_() {
return GridTrackSize{
Expand Down
2 changes: 1 addition & 1 deletion yoga/style/Style.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <yoga/enums/Wrap.h>
#include <yoga/numeric/FloatOptional.h>
#include <yoga/style/GridLine.h>
#include <yoga/style/GridTrack.h>
#include <yoga/style/GridTrackSize.h>
#include <yoga/style/StyleLength.h>
#include <yoga/style/StyleSizeLength.h>
#include <yoga/style/StyleValuePool.h>
Expand Down
Loading