Skip to content

Commit 2a1f9cf

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`. Differential Revision: D63927512
1 parent 2437d26 commit 2a1f9cf

File tree

15 files changed

+292
-42
lines changed

15 files changed

+292
-42
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
@@ -124,6 +124,9 @@ export enum Unit {
124124
Point = 1,
125125
Percent = 2,
126126
Auto = 3,
127+
MaxContent = 4,
128+
FitContent = 5,
129+
Stretch = 6,
127130
}
128131

129132
export enum Wrap {
@@ -201,6 +204,9 @@ const constants = {
201204
UNIT_POINT: Unit.Point,
202205
UNIT_PERCENT: Unit.Percent,
203206
UNIT_AUTO: Unit.Auto,
207+
UNIT_MAX_CONTENT: Unit.MaxContent,
208+
UNIT_FIT_CONTENT: Unit.FitContent,
209+
UNIT_STRETCH: Unit.Stretch,
204210
WRAP_NO_WRAP: Wrap.NoWrap,
205211
WRAP_WRAP: Wrap.Wrap,
206212
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, StyleSizeValue::ofMaxContent());
138+
pool.store(handleFitContent, StyleSizeValue::ofFitContent());
139+
pool.store(handleStretch, StyleSizeValue::ofStretch());
140+
141+
EXPECT_EQ(pool.getSize(handleMaxContent), StyleSizeValue::ofMaxContent());
142+
EXPECT_EQ(pool.getSize(handleFitContent), StyleSizeValue::ofFitContent());
143+
EXPECT_EQ(pool.getSize(handleStretch), StyleSizeValue::ofStretch());
144+
}
145+
131146
} // namespace facebook::yoga

yoga/YGEnums.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ const char* YGUnitToString(const YGUnit value) {
243243
return "percent";
244244
case YGUnitAuto:
245245
return "auto";
246+
case YGUnitMaxContent:
247+
return "max-content";
248+
case YGUnitFitContent:
249+
return "fit-content";
250+
case YGUnitStretch:
251+
return "stretch";
246252
}
247253
return "unknown";
248254
}

yoga/YGEnums.h

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

135138
YG_ENUM_DECL(
136139
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, StyleSizeValue::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, StyleSizeValue::percent(flexBasisPercent));
188188
}
189189

190190
void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) {
191191
updateStyle<&Style::flexBasis, &Style::setFlexBasis>(
192-
node, StyleLength::ofAuto());
192+
node, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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, StyleSizeValue::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/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) {

yoga/node/Node.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,16 @@ void Node::setPosition(
282282
crossAxisTrailingEdge);
283283
}
284284

285-
Style::Length Node::processFlexBasis() const {
286-
Style::Length flexBasis = style_.flexBasis();
285+
Style::SizeValue Node::processFlexBasis() const {
286+
Style::SizeValue flexBasis = style_.flexBasis();
287287
if (flexBasis.unit() != Unit::Auto && flexBasis.unit() != Unit::Undefined) {
288288
return flexBasis;
289289
}
290290
if (style_.flex().isDefined() && style_.flex().unwrap() > 0.0f) {
291-
return config_->useWebDefaults() ? StyleLength::ofAuto()
292-
: StyleLength::points(0);
291+
return config_->useWebDefaults() ? StyleSizeValue::ofAuto()
292+
: StyleSizeValue::points(0);
293293
}
294-
return StyleLength::ofAuto();
294+
return StyleSizeValue::ofAuto();
295295
}
296296

297297
FloatOptional Node::resolveFlexBasis(

0 commit comments

Comments
 (0)