Skip to content

Commit 0dc0552

Browse files
CSS Grid 1/N: data structures
1 parent 8c5ea16 commit 0dc0552

5 files changed

Lines changed: 97 additions & 9 deletions

File tree

yoga/YGNodeStyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <yoga/Yoga.h>
99
#include <yoga/debug/AssertFatal.h>
1010
#include <yoga/node/Node.h>
11-
#include <yoga/style/GridTrack.h>
11+
#include <yoga/style/GridTrackSize.h>
1212

1313
using namespace facebook;
1414
using namespace facebook::yoga;

yoga/algorithm/grid/GridItem.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <yoga/enums/Dimension.h>
11+
#include <yoga/node/Node.h>
12+
#include <cstddef>
13+
#include <map>
14+
#include <vector>
15+
16+
namespace facebook::yoga {
17+
18+
// A grid item with its resolved position in the grid. Positions are set by the
19+
// auto-placement algorithm and consumed by the track sizing algorithm.
20+
struct GridItem {
21+
size_t columnStart;
22+
size_t columnEnd;
23+
size_t rowStart;
24+
size_t rowEnd;
25+
yoga::Node* node;
26+
27+
// Additional offset added to item to align baselines
28+
// https://www.w3.org/TR/css-grid-1/#algo-baseline-shims
29+
float baselineShim = 0.0f;
30+
31+
// Flags used for optimizations in track sizing algorithm.
32+
bool crossesIntrinsicRow = false;
33+
bool crossesIntrinsicColumn = false;
34+
bool crossesFlexibleRow = false;
35+
bool crossesFlexibleColumn = false;
36+
37+
GridItem(
38+
size_t columnStart,
39+
size_t columnEnd,
40+
size_t rowStart,
41+
size_t rowEnd,
42+
yoga::Node* node,
43+
float baselineShim = 0.0f)
44+
: columnStart(columnStart),
45+
columnEnd(columnEnd),
46+
rowStart(rowStart),
47+
rowEnd(rowEnd),
48+
node(node),
49+
baselineShim(baselineShim) {}
50+
51+
// Helper functions used by the track sizing algorithm
52+
bool crossesIntrinsicTrack(Dimension dimension) const {
53+
return dimension == Dimension::Width ? crossesIntrinsicColumn
54+
: crossesIntrinsicRow;
55+
}
56+
bool crossesFlexibleTrack(Dimension dimension) const {
57+
return dimension == Dimension::Width ? crossesFlexibleColumn
58+
: crossesFlexibleRow;
59+
}
60+
};
61+
62+
// Baseline sharing groups - items grouped by their starting row for the
63+
// resolve intrinsic size step in track sizing algorithm.
64+
// https://www.w3.org/TR/css-grid-1/#algo-baseline-shims
65+
using BaselineItemGroups = std::map<size_t, std::vector<GridItem*>>;
66+
67+
} // namespace facebook::yoga

yoga/algorithm/grid/GridTrack.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <yoga/style/GridTrackSize.h>
11+
12+
namespace facebook::yoga {
13+
14+
// Represents a resolved grid track size during layout computation.
15+
// Includes variables used by the track sizing algorithm.
16+
// https://www.w3.org/TR/css-grid-1/#algo-track-sizing
17+
struct GridTrack {
18+
GridTrackSize trackSize;
19+
float baseSize = 0.0f;
20+
float growthLimit = 0.0f;
21+
bool infinitelyGrowable = false;
22+
23+
explicit GridTrack(const GridTrackSize& ts) : trackSize(ts) {}
24+
};
25+
26+
} // namespace facebook::yoga
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@
1111
#include <vector>
1212

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

19-
// These are used in the grid layout algorithm when distributing spaces among
20-
// tracks
21-
// TODO: maybe move them to TrackSizing since these are track states
22-
float baseSize = 0.0f;
23-
float growthLimit = 0.0f;
24-
bool infinitelyGrowable = false;
25-
2621
// Static factory methods for common cases
2722
constexpr static GridTrackSize auto_() {
2823
return GridTrackSize{

yoga/style/Style.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <yoga/enums/Wrap.h>
3131
#include <yoga/numeric/FloatOptional.h>
3232
#include <yoga/style/GridLine.h>
33-
#include <yoga/style/GridTrack.h>
33+
#include <yoga/style/GridTrackSize.h>
3434
#include <yoga/style/StyleLength.h>
3535
#include <yoga/style/StyleSizeLength.h>
3636
#include <yoga/style/StyleValuePool.h>

0 commit comments

Comments
 (0)