Skip to content

Commit 51a62f0

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Modify private apis to set, store, and get intrinsic sizing keywords (facebook#1721)
Summary: X-link: facebook/react-native#46938 The private internals of how we store styles needed to change a bit to support 3 new keyword values. Right now the only other keyword that can be stored is `auto`. As a result there isn't much fancy logic to support storing this and its just stored as a specific type inside of `StyleValueHandle`. There are only 3 bits for types (8 values), so it is not sustainable to just stuff every keyword in there. So the change writes the keyword as a value with a new `keyword` `Type`. I chose not to put `auto` in there even though it is a keyword since it is a hot path, I did not want to regress perf when I did not need to. I also make a new `StyleSizeValue` class to store size values - so values for `width`, `height`, etc. This way these new keywords are kept specific to sizes and we will not be able to create, for example, a margin: `max-content`. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D63927512
1 parent 6fed4df commit 51a62f0

File tree

17 files changed

+317
-54
lines changed

17 files changed

+317
-54
lines changed

enums.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88

99
ENUMS = {
1010
"Direction": ["Inherit", "LTR", "RTL"],
11-
"Unit": ["Undefined", "Point", "Percent", "Auto"],
11+
"Unit": [
12+
"Undefined",
13+
"Point",
14+
"Percent",
15+
"Auto",
16+
"MaxContent",
17+
"FitContent",
18+
"Stretch",
19+
],
1220
"FlexDirection": ["Column", "ColumnReverse", "Row", "RowReverse"],
1321
"Justify": [
1422
"FlexStart",

java/com/facebook/yoga/YogaUnit.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public enum YogaUnit {
1313
UNDEFINED(0),
1414
POINT(1),
1515
PERCENT(2),
16-
AUTO(3);
16+
AUTO(3),
17+
MAX_CONTENT(4),
18+
FIT_CONTENT(5),
19+
STRETCH(6);
1720

1821
private final int mIntValue;
1922

@@ -31,6 +34,9 @@ public static YogaUnit fromInt(int value) {
3134
case 1: return POINT;
3235
case 2: return PERCENT;
3336
case 3: return AUTO;
37+
case 4: return MAX_CONTENT;
38+
case 5: return FIT_CONTENT;
39+
case 6: return STRETCH;
3440
default: throw new IllegalArgumentException("Unknown enum value: " + value);
3541
}
3642
}

javascript/src/generated/YGEnums.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export enum Unit {
125125
Point = 1,
126126
Percent = 2,
127127
Auto = 3,
128+
MaxContent = 4,
129+
FitContent = 5,
130+
Stretch = 6,
128131
}
129132

130133
export enum Wrap {
@@ -203,6 +206,9 @@ const constants = {
203206
UNIT_POINT: Unit.Point,
204207
UNIT_PERCENT: Unit.Percent,
205208
UNIT_AUTO: Unit.Auto,
209+
UNIT_MAX_CONTENT: Unit.MaxContent,
210+
UNIT_FIT_CONTENT: Unit.FitContent,
211+
UNIT_STRETCH: Unit.Stretch,
206212
WRAP_NO_WRAP: Wrap.NoWrap,
207213
WRAP_WRAP: Wrap.Wrap,
208214
WRAP_WRAP_REVERSE: Wrap.WrapReverse,

tests/StyleValuePoolTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,19 @@ TEST(StyleValuePool, store_undefined_after_large_int) {
128128
EXPECT_EQ(pool.getLength(handle), StyleLength::undefined());
129129
}
130130

131+
TEST(StyleValuePool, store_keywords) {
132+
StyleValuePool pool;
133+
StyleValueHandle handleMaxContent;
134+
StyleValueHandle handleFitContent;
135+
StyleValueHandle handleStretch;
136+
137+
pool.store(handleMaxContent, StyleSizeLength::ofMaxContent());
138+
pool.store(handleFitContent, StyleSizeLength::ofFitContent());
139+
pool.store(handleStretch, StyleSizeLength::ofStretch());
140+
141+
EXPECT_EQ(pool.getSize(handleMaxContent), StyleSizeLength::ofMaxContent());
142+
EXPECT_EQ(pool.getSize(handleFitContent), StyleSizeLength::ofFitContent());
143+
EXPECT_EQ(pool.getSize(handleStretch), StyleSizeLength::ofStretch());
144+
}
145+
131146
} // namespace facebook::yoga

yoga/YGEnums.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ const char* YGUnitToString(const YGUnit value) {
245245
return "percent";
246246
case YGUnitAuto:
247247
return "auto";
248+
case YGUnitMaxContent:
249+
return "max-content";
250+
case YGUnitFitContent:
251+
return "fit-content";
252+
case YGUnitStretch:
253+
return "stretch";
248254
}
249255
return "unknown";
250256
}

yoga/YGEnums.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ YG_ENUM_DECL(
131131
YGUnitUndefined,
132132
YGUnitPoint,
133133
YGUnitPercent,
134-
YGUnitAuto)
134+
YGUnitAuto,
135+
YGUnitMaxContent,
136+
YGUnitFitContent,
137+
YGUnitStretch)
135138

136139
YG_ENUM_DECL(
137140
YGWrap,

yoga/YGNodeStyle.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,19 @@ float YGNodeStyleGetFlexShrink(const YGNodeConstRef nodeRef) {
177177

178178
void YGNodeStyleSetFlexBasis(const YGNodeRef node, const float flexBasis) {
179179
updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
180-
node, StyleLength::points(flexBasis));
180+
node, StyleSizeLength::points(flexBasis));
181181
}
182182

183183
void YGNodeStyleSetFlexBasisPercent(
184184
const YGNodeRef node,
185185
const float flexBasisPercent) {
186186
updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
187-
node, StyleLength::percent(flexBasisPercent));
187+
node, StyleSizeLength::percent(flexBasisPercent));
188188
}
189189

190190
void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) {
191191
updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
192-
node, StyleLength::ofAuto());
192+
node, StyleSizeLength::ofAuto());
193193
}
194194

195195
YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
@@ -308,17 +308,17 @@ YGBoxSizing YGNodeStyleGetBoxSizing(const YGNodeConstRef node) {
308308

309309
void YGNodeStyleSetWidth(YGNodeRef node, float points) {
310310
updateStyle<&Style::dimension, &Style::setDimension>(
311-
node, Dimension::Width, StyleLength::points(points));
311+
node, Dimension::Width, StyleSizeLength::points(points));
312312
}
313313

314314
void YGNodeStyleSetWidthPercent(YGNodeRef node, float percent) {
315315
updateStyle<&Style::dimension, &Style::setDimension>(
316-
node, Dimension::Width, StyleLength::percent(percent));
316+
node, Dimension::Width, StyleSizeLength::percent(percent));
317317
}
318318

319319
void YGNodeStyleSetWidthAuto(YGNodeRef node) {
320320
updateStyle<&Style::dimension, &Style::setDimension>(
321-
node, Dimension::Width, StyleLength::ofAuto());
321+
node, Dimension::Width, StyleSizeLength::ofAuto());
322322
}
323323

324324
YGValue YGNodeStyleGetWidth(YGNodeConstRef node) {
@@ -327,17 +327,17 @@ YGValue YGNodeStyleGetWidth(YGNodeConstRef node) {
327327

328328
void YGNodeStyleSetHeight(YGNodeRef node, float points) {
329329
updateStyle<&Style::dimension, &Style::setDimension>(
330-
node, Dimension::Height, StyleLength::points(points));
330+
node, Dimension::Height, StyleSizeLength::points(points));
331331
}
332332

333333
void YGNodeStyleSetHeightPercent(YGNodeRef node, float percent) {
334334
updateStyle<&Style::dimension, &Style::setDimension>(
335-
node, Dimension::Height, StyleLength::percent(percent));
335+
node, Dimension::Height, StyleSizeLength::percent(percent));
336336
}
337337

338338
void YGNodeStyleSetHeightAuto(YGNodeRef node) {
339339
updateStyle<&Style::dimension, &Style::setDimension>(
340-
node, Dimension::Height, StyleLength::ofAuto());
340+
node, Dimension::Height, StyleSizeLength::ofAuto());
341341
}
342342

343343
YGValue YGNodeStyleGetHeight(YGNodeConstRef node) {
@@ -346,12 +346,12 @@ YGValue YGNodeStyleGetHeight(YGNodeConstRef node) {
346346

347347
void YGNodeStyleSetMinWidth(const YGNodeRef node, const float minWidth) {
348348
updateStyle<&Style::minDimension, &Style::setMinDimension>(
349-
node, Dimension::Width, StyleLength::points(minWidth));
349+
node, Dimension::Width, StyleSizeLength::points(minWidth));
350350
}
351351

352352
void YGNodeStyleSetMinWidthPercent(const YGNodeRef node, const float minWidth) {
353353
updateStyle<&Style::minDimension, &Style::setMinDimension>(
354-
node, Dimension::Width, StyleLength::percent(minWidth));
354+
node, Dimension::Width, StyleSizeLength::percent(minWidth));
355355
}
356356

357357
YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) {
@@ -360,14 +360,14 @@ YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) {
360360

361361
void YGNodeStyleSetMinHeight(const YGNodeRef node, const float minHeight) {
362362
updateStyle<&Style::minDimension, &Style::setMinDimension>(
363-
node, Dimension::Height, StyleLength::points(minHeight));
363+
node, Dimension::Height, StyleSizeLength::points(minHeight));
364364
}
365365

366366
void YGNodeStyleSetMinHeightPercent(
367367
const YGNodeRef node,
368368
const float minHeight) {
369369
updateStyle<&Style::minDimension, &Style::setMinDimension>(
370-
node, Dimension::Height, StyleLength::percent(minHeight));
370+
node, Dimension::Height, StyleSizeLength::percent(minHeight));
371371
}
372372

373373
YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) {
@@ -376,12 +376,12 @@ YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) {
376376

377377
void YGNodeStyleSetMaxWidth(const YGNodeRef node, const float maxWidth) {
378378
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
379-
node, Dimension::Width, StyleLength::points(maxWidth));
379+
node, Dimension::Width, StyleSizeLength::points(maxWidth));
380380
}
381381

382382
void YGNodeStyleSetMaxWidthPercent(const YGNodeRef node, const float maxWidth) {
383383
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
384-
node, Dimension::Width, StyleLength::percent(maxWidth));
384+
node, Dimension::Width, StyleSizeLength::percent(maxWidth));
385385
}
386386

387387
YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) {
@@ -390,14 +390,14 @@ YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) {
390390

391391
void YGNodeStyleSetMaxHeight(const YGNodeRef node, const float maxHeight) {
392392
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
393-
node, Dimension::Height, StyleLength::points(maxHeight));
393+
node, Dimension::Height, StyleSizeLength::points(maxHeight));
394394
}
395395

396396
void YGNodeStyleSetMaxHeightPercent(
397397
const YGNodeRef node,
398398
const float maxHeight) {
399399
updateStyle<&Style::maxDimension, &Style::setMaxDimension>(
400-
node, Dimension::Height, StyleLength::percent(maxHeight));
400+
node, Dimension::Height, StyleSizeLength::percent(maxHeight));
401401
}
402402

403403
YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {

yoga/YGValue.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ inline bool operator==(const YGValue& lhs, const YGValue& rhs) {
6565
switch (lhs.unit) {
6666
case YGUnitUndefined:
6767
case YGUnitAuto:
68+
case YGUnitFitContent:
69+
case YGUnitMaxContent:
70+
case YGUnitStretch:
6871
return true;
6972
case YGUnitPoint:
7073
case YGUnitPercent:

yoga/algorithm/CalculateLayout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ static float distributeFreeSpaceSecondPass(
749749
marginCross;
750750
const bool isLoosePercentageMeasurement =
751751
currentLineChild->getProcessedDimension(dimension(crossAxis))
752-
.unit() == Unit::Percent &&
752+
.isPercent() &&
753753
sizingModeCrossDim != SizingMode::StretchFit;
754754
childCrossSizingMode =
755755
yoga::isUndefined(childCrossSize) || isLoosePercentageMeasurement

yoga/enums/Unit.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ enum class Unit : uint8_t {
2020
Point = YGUnitPoint,
2121
Percent = YGUnitPercent,
2222
Auto = YGUnitAuto,
23+
MaxContent = YGUnitMaxContent,
24+
FitContent = YGUnitFitContent,
25+
Stretch = YGUnitStretch,
2326
};
2427

2528
template <>
2629
constexpr int32_t ordinalCount<Unit>() {
27-
return 4;
30+
return 7;
2831
}
2932

3033
constexpr Unit scopedEnum(YGUnit unscoped) {

0 commit comments

Comments
 (0)