Skip to content

Commit 54af7fc

Browse files
davidaureliofacebook-github-bot
authored andcommitted
YGStyle: wrap all fields into accessors
Summary: @public In order to encapsulate property access on `YGStyle`, as a first measure we wrap all fields with accessors. This will e.g. enable dynamic property storage and instrumentation in the future. All accessors have a `const` version that allows direct access via `const&`. For mutation, bit fields are wrapped with a custom reference object. This style allows for the least amount of changes in client code. Property access simply needs appended parens, eg `style.direction` becomes `style.direction`. Reviewed By: shergin Differential Revision: D14999096 fbshipit-source-id: fbf29f7ddab520513d4618f5e70094c4f6330b30
1 parent af84193 commit 54af7fc

File tree

16 files changed

+588
-453
lines changed

16 files changed

+588
-453
lines changed

React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ - (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps
185185
}
186186

187187
// `overflow`
188-
if (oldViewProps.yogaStyle.overflow != newViewProps.yogaStyle.overflow) {
189-
self.clipsToBounds = newViewProps.yogaStyle.overflow != YGOverflowVisible;
188+
if (oldViewProps.yogaStyle.overflow() != newViewProps.yogaStyle.overflow()) {
189+
self.clipsToBounds = newViewProps.yogaStyle.overflow() != YGOverflowVisible;
190190
needsInvalidateLayer = YES;
191191
}
192192

ReactAndroid/src/main/java/com/facebook/react/fabric/jsi/jni/Binding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ local_ref<JString> getPlatformComponentName(const ShadowView& shadowView) {
161161
std::dynamic_pointer_cast<const ScrollViewProps>(shadowView.props);
162162

163163
if (newViewProps &&
164-
newViewProps->yogaStyle.flexDirection == YGFlexDirectionRow) {
164+
newViewProps->yogaStyle.flexDirection() == YGFlexDirectionRow) {
165165
componentName = make_jstring("AndroidHorizontalScrollView");
166166
} else {
167167
componentName = make_jstring(shadowView.componentName);

ReactCommon/fabric/components/root/RootProps.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ namespace react {
1616
static YGStyle yogaStyleFromLayoutConstraints(
1717
const LayoutConstraints &layoutConstraints) {
1818
auto yogaStyle = YGStyle{};
19-
yogaStyle.minDimensions[YGDimensionWidth] =
19+
yogaStyle.minDimensions()[YGDimensionWidth] =
2020
yogaStyleValueFromFloat(layoutConstraints.minimumSize.width);
21-
yogaStyle.minDimensions[YGDimensionHeight] =
21+
yogaStyle.minDimensions()[YGDimensionHeight] =
2222
yogaStyleValueFromFloat(layoutConstraints.minimumSize.height);
2323

24-
yogaStyle.maxDimensions[YGDimensionWidth] =
24+
yogaStyle.maxDimensions()[YGDimensionWidth] =
2525
yogaStyleValueFromFloat(layoutConstraints.maximumSize.width);
26-
yogaStyle.maxDimensions[YGDimensionHeight] =
26+
yogaStyle.maxDimensions()[YGDimensionHeight] =
2727
yogaStyleValueFromFloat(layoutConstraints.maximumSize.height);
2828

29-
yogaStyle.direction =
29+
yogaStyle.direction() =
3030
yogaDirectionFromLayoutDirection(layoutConstraints.layoutDirection);
3131

3232
return yogaStyle;

ReactCommon/fabric/components/view/ViewProps.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,20 @@ BorderMetrics ViewProps::resolveBorderMetrics(
121121
bool{layoutMetrics.layoutDirection == LayoutDirection::RightToLeft};
122122

123123
auto borderWidths = CascadedBorderWidths{
124-
/* .left = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeLeft]),
125-
/* .top = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeTop]),
126-
/* .right = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeRight]),
124+
/* .left = */ optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeLeft]),
125+
/* .top = */ optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeTop]),
126+
/* .right = */
127+
optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeRight]),
127128
/* .bottom = */
128-
optionalFloatFromYogaValue(yogaStyle.border[YGEdgeBottom]),
129-
/* .start = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeStart]),
130-
/* .end = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeEnd]),
129+
optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeBottom]),
130+
/* .start = */
131+
optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeStart]),
132+
/* .end = */ optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeEnd]),
131133
/* .horizontal = */
132-
optionalFloatFromYogaValue(yogaStyle.border[YGEdgeHorizontal]),
134+
optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeHorizontal]),
133135
/* .vertical = */
134-
optionalFloatFromYogaValue(yogaStyle.border[YGEdgeVertical]),
135-
/* .all = */ optionalFloatFromYogaValue(yogaStyle.border[YGEdgeAll]),
136+
optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeVertical]),
137+
/* .all = */ optionalFloatFromYogaValue(yogaStyle.border()[YGEdgeAll]),
136138
};
137139

138140
return {

ReactCommon/fabric/components/view/ViewShadowNode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool ViewShadowNode::isLayoutOnly() const {
2323
// Accessibility Props
2424
!viewProps.accessible &&
2525
// Style Props
26-
viewProps.yogaStyle.overflow == YGOverflowVisible &&
26+
viewProps.yogaStyle.overflow() == YGOverflowVisible &&
2727
viewProps.opacity == 1.0 && !viewProps.backgroundColor &&
2828
!viewProps.foregroundColor && !viewProps.shadowColor &&
2929
viewProps.transform == Transform{} && viewProps.zIndex == 0 &&

ReactCommon/fabric/components/view/conversions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ inline LayoutMetrics layoutMetricsFromYogaNode(YGNode &yogaNode) {
107107
layoutMetrics.borderWidth.bottom +
108108
floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeBottom))};
109109

110-
layoutMetrics.displayType = yogaNode.getStyle().display == YGDisplayNone
110+
layoutMetrics.displayType = yogaNode.getStyle().display() == YGDisplayNone
111111
? DisplayType::None
112112
: DisplayType::Flex;
113113

@@ -304,7 +304,7 @@ inline void fromRawValue(const RawValue &value, YGDisplay &result) {
304304

305305
inline void fromRawValue(
306306
const RawValue &value,
307-
decltype(YGStyle{}.margin[0]) /* type is subject to change */ &result) {
307+
decltype(YGStyle{}.margin()[0]) /* type is subject to change */ &result) {
308308
if (value.hasType<Float>()) {
309309
result = yogaStyleValueFromFloat((Float)value);
310310
return;

ReactCommon/fabric/components/view/propsConversions.h

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -115,71 +115,77 @@ static inline YGStyle convertRawProp(
115115
const RawProps &rawProps,
116116
const YGStyle &sourceValue) {
117117
auto yogaStyle = YGStyle{};
118-
yogaStyle.direction = convertRawProp(
119-
rawProps, "direction", sourceValue.direction, yogaStyle.direction);
120-
yogaStyle.flexDirection = convertRawProp(
118+
yogaStyle.direction() = convertRawProp(
119+
rawProps, "direction", sourceValue.direction(), yogaStyle.direction());
120+
yogaStyle.flexDirection() = convertRawProp(
121121
rawProps,
122122
"flexDirection",
123-
sourceValue.flexDirection,
124-
yogaStyle.flexDirection);
125-
yogaStyle.justifyContent = convertRawProp(
123+
sourceValue.flexDirection(),
124+
yogaStyle.flexDirection());
125+
yogaStyle.justifyContent() = convertRawProp(
126126
rawProps,
127127
"justifyContent",
128-
sourceValue.justifyContent,
129-
yogaStyle.justifyContent);
130-
yogaStyle.alignContent = convertRawProp(
128+
sourceValue.justifyContent(),
129+
yogaStyle.justifyContent());
130+
yogaStyle.alignContent() = convertRawProp(
131131
rawProps,
132132
"alignContent",
133-
sourceValue.alignContent,
134-
yogaStyle.alignContent);
135-
yogaStyle.alignItems = convertRawProp(
136-
rawProps, "alignItems", sourceValue.alignItems, yogaStyle.alignItems);
137-
yogaStyle.alignSelf = convertRawProp(
138-
rawProps, "alignSelf", sourceValue.alignSelf, yogaStyle.alignSelf);
139-
yogaStyle.positionType = convertRawProp(
140-
rawProps, "position", sourceValue.positionType, yogaStyle.positionType);
141-
yogaStyle.flexWrap = convertRawProp(
142-
rawProps, "flexWrap", sourceValue.flexWrap, yogaStyle.flexWrap);
143-
yogaStyle.overflow = convertRawProp(
144-
rawProps, "overflow", sourceValue.overflow, yogaStyle.overflow);
145-
yogaStyle.display = convertRawProp(
146-
rawProps, "display", sourceValue.display, yogaStyle.display);
147-
yogaStyle.flex =
148-
convertRawProp(rawProps, "flex", sourceValue.flex, yogaStyle.flex);
149-
yogaStyle.flexGrow = convertRawProp(
150-
rawProps, "flexGrow", sourceValue.flexGrow, yogaStyle.flexGrow);
151-
yogaStyle.flexShrink = convertRawProp(
152-
rawProps, "flexShrink", sourceValue.flexShrink, yogaStyle.flexShrink);
153-
yogaStyle.flexBasis = convertRawProp(
154-
rawProps, "flexBasis", sourceValue.flexBasis, yogaStyle.flexBasis);
155-
yogaStyle.margin = convertRawProp(
156-
rawProps, "margin", "", sourceValue.margin, yogaStyle.margin);
157-
yogaStyle.position =
158-
convertRawProp(rawProps, sourceValue.position, yogaStyle.position);
159-
yogaStyle.padding = convertRawProp(
160-
rawProps, "padding", "", sourceValue.padding, yogaStyle.padding);
161-
yogaStyle.border = convertRawProp(
162-
rawProps, "border", "Width", sourceValue.border, yogaStyle.border);
163-
yogaStyle.dimensions = convertRawProp(
133+
sourceValue.alignContent(),
134+
yogaStyle.alignContent());
135+
yogaStyle.alignItems() = convertRawProp(
136+
rawProps, "alignItems", sourceValue.alignItems(), yogaStyle.alignItems());
137+
yogaStyle.alignSelf() = convertRawProp(
138+
rawProps, "alignSelf", sourceValue.alignSelf(), yogaStyle.alignSelf());
139+
yogaStyle.positionType() = convertRawProp(
140+
rawProps,
141+
"position",
142+
sourceValue.positionType(),
143+
yogaStyle.positionType());
144+
yogaStyle.flexWrap() = convertRawProp(
145+
rawProps, "flexWrap", sourceValue.flexWrap(), yogaStyle.flexWrap());
146+
yogaStyle.overflow() = convertRawProp(
147+
rawProps, "overflow", sourceValue.overflow(), yogaStyle.overflow());
148+
yogaStyle.display() = convertRawProp(
149+
rawProps, "display", sourceValue.display(), yogaStyle.display());
150+
yogaStyle.flex() =
151+
convertRawProp(rawProps, "flex", sourceValue.flex(), yogaStyle.flex());
152+
yogaStyle.flexGrow() = convertRawProp(
153+
rawProps, "flexGrow", sourceValue.flexGrow(), yogaStyle.flexGrow());
154+
yogaStyle.flexShrink() = convertRawProp(
155+
rawProps, "flexShrink", sourceValue.flexShrink(), yogaStyle.flexShrink());
156+
yogaStyle.flexBasis() = convertRawProp(
157+
rawProps, "flexBasis", sourceValue.flexBasis(), yogaStyle.flexBasis());
158+
yogaStyle.margin() = convertRawProp(
159+
rawProps, "margin", "", sourceValue.margin(), yogaStyle.margin());
160+
yogaStyle.position() =
161+
convertRawProp(rawProps, sourceValue.position(), yogaStyle.position());
162+
yogaStyle.padding() = convertRawProp(
163+
rawProps, "padding", "", sourceValue.padding(), yogaStyle.padding());
164+
yogaStyle.border() = convertRawProp(
165+
rawProps, "border", "Width", sourceValue.border(), yogaStyle.border());
166+
yogaStyle.dimensions() = convertRawProp(
164167
rawProps,
165168
"width",
166169
"height",
167-
sourceValue.dimensions,
168-
yogaStyle.dimensions);
169-
yogaStyle.minDimensions = convertRawProp(
170+
sourceValue.dimensions(),
171+
yogaStyle.dimensions());
172+
yogaStyle.minDimensions() = convertRawProp(
170173
rawProps,
171174
"minWidth",
172175
"minHeight",
173-
sourceValue.minDimensions,
174-
yogaStyle.minDimensions);
175-
yogaStyle.maxDimensions = convertRawProp(
176+
sourceValue.minDimensions(),
177+
yogaStyle.minDimensions());
178+
yogaStyle.maxDimensions() = convertRawProp(
176179
rawProps,
177180
"maxWidth",
178181
"maxHeight",
179-
sourceValue.maxDimensions,
180-
yogaStyle.maxDimensions);
181-
yogaStyle.aspectRatio = convertRawProp(
182-
rawProps, "aspectRatio", sourceValue.aspectRatio, yogaStyle.aspectRatio);
182+
sourceValue.maxDimensions(),
183+
yogaStyle.maxDimensions());
184+
yogaStyle.aspectRatio() = convertRawProp(
185+
rawProps,
186+
"aspectRatio",
187+
sourceValue.aspectRatio(),
188+
yogaStyle.aspectRatio());
183189
return yogaStyle;
184190
}
185191

ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void YogaLayoutableShadowNode::setSize(Size size) const {
135135
ensureUnsealed();
136136

137137
auto style = yogaNode_.getStyle();
138-
style.dimensions[YGDimensionWidth] = yogaStyleValueFromFloat(size.width);
139-
style.dimensions[YGDimensionHeight] = yogaStyleValueFromFloat(size.height);
138+
style.dimensions()[YGDimensionWidth] = yogaStyleValueFromFloat(size.width);
139+
style.dimensions()[YGDimensionHeight] = yogaStyleValueFromFloat(size.height);
140140
yogaNode_.setStyle(style);
141141
yogaNode_.setDirty(true);
142142
}
@@ -146,7 +146,7 @@ void YogaLayoutableShadowNode::setPositionType(
146146
ensureUnsealed();
147147

148148
auto style = yogaNode_.getStyle();
149-
style.positionType = positionType;
149+
style.positionType() = positionType;
150150
yogaNode_.setStyle(style);
151151
yogaNode_.setDirty(true);
152152
}

ReactCommon/fabric/components/view/yoga/YogaStylableProps.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,66 @@ YogaStylableProps::YogaStylableProps(
3131

3232
#if RN_DEBUG_STRING_CONVERTIBLE
3333
SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const {
34-
auto defaultYogaStyle = YGStyle{};
34+
const auto defaultYogaStyle = YGStyle{};
3535
return {
3636
debugStringConvertibleItem(
37-
"direction", yogaStyle.direction, defaultYogaStyle.direction),
37+
"direction", yogaStyle.direction(), defaultYogaStyle.direction()),
3838
debugStringConvertibleItem(
3939
"flexDirection",
40-
yogaStyle.flexDirection,
41-
defaultYogaStyle.flexDirection),
40+
yogaStyle.flexDirection(),
41+
defaultYogaStyle.flexDirection()),
4242
debugStringConvertibleItem(
4343
"justifyContent",
44-
yogaStyle.justifyContent,
45-
defaultYogaStyle.justifyContent),
44+
yogaStyle.justifyContent(),
45+
defaultYogaStyle.justifyContent()),
4646
debugStringConvertibleItem(
4747
"alignContent",
48-
yogaStyle.alignContent,
49-
defaultYogaStyle.alignContent),
48+
yogaStyle.alignContent(),
49+
defaultYogaStyle.alignContent()),
5050
debugStringConvertibleItem(
51-
"alignItems", yogaStyle.alignItems, defaultYogaStyle.alignItems),
51+
"alignItems", yogaStyle.alignItems(), defaultYogaStyle.alignItems()),
5252
debugStringConvertibleItem(
53-
"alignSelf", yogaStyle.alignSelf, defaultYogaStyle.alignSelf),
53+
"alignSelf", yogaStyle.alignSelf(), defaultYogaStyle.alignSelf()),
5454
debugStringConvertibleItem(
5555
"positionType",
56-
yogaStyle.positionType,
57-
defaultYogaStyle.positionType),
56+
yogaStyle.positionType(),
57+
defaultYogaStyle.positionType()),
5858
debugStringConvertibleItem(
59-
"flexWrap", yogaStyle.flexWrap, defaultYogaStyle.flexWrap),
59+
"flexWrap", yogaStyle.flexWrap(), defaultYogaStyle.flexWrap()),
6060
debugStringConvertibleItem(
61-
"overflow", yogaStyle.overflow, defaultYogaStyle.overflow),
61+
"overflow", yogaStyle.overflow(), defaultYogaStyle.overflow()),
6262
debugStringConvertibleItem(
63-
"display", yogaStyle.display, defaultYogaStyle.display),
64-
debugStringConvertibleItem("flex", yogaStyle.flex, defaultYogaStyle.flex),
63+
"display", yogaStyle.display(), defaultYogaStyle.display()),
6564
debugStringConvertibleItem(
66-
"flexGrow", yogaStyle.flexGrow, defaultYogaStyle.flexGrow),
65+
"flex", yogaStyle.flex(), defaultYogaStyle.flex()),
6766
debugStringConvertibleItem(
68-
"flexShrink", yogaStyle.flexShrink, defaultYogaStyle.flexShrink),
67+
"flexGrow", yogaStyle.flexGrow(), defaultYogaStyle.flexGrow()),
6968
debugStringConvertibleItem(
70-
"flexBasis", yogaStyle.flexBasis, defaultYogaStyle.flexBasis),
69+
"flexShrink", yogaStyle.flexShrink(), defaultYogaStyle.flexShrink()),
7170
debugStringConvertibleItem(
72-
"margin", yogaStyle.margin, defaultYogaStyle.margin),
71+
"flexBasis", yogaStyle.flexBasis(), defaultYogaStyle.flexBasis()),
7372
debugStringConvertibleItem(
74-
"position", yogaStyle.position, defaultYogaStyle.position),
73+
"margin", yogaStyle.margin(), defaultYogaStyle.margin()),
7574
debugStringConvertibleItem(
76-
"padding", yogaStyle.padding, defaultYogaStyle.padding),
75+
"position", yogaStyle.position(), defaultYogaStyle.position()),
7776
debugStringConvertibleItem(
78-
"border", yogaStyle.border, defaultYogaStyle.border),
77+
"padding", yogaStyle.padding(), defaultYogaStyle.padding()),
7978
debugStringConvertibleItem(
80-
"dimensions", yogaStyle.dimensions, defaultYogaStyle.dimensions),
79+
"border", yogaStyle.border(), defaultYogaStyle.border()),
80+
debugStringConvertibleItem(
81+
"dimensions", yogaStyle.dimensions(), defaultYogaStyle.dimensions()),
8182
debugStringConvertibleItem(
8283
"minDimensions",
83-
yogaStyle.minDimensions,
84-
defaultYogaStyle.minDimensions),
84+
yogaStyle.minDimensions(),
85+
defaultYogaStyle.minDimensions()),
8586
debugStringConvertibleItem(
8687
"maxDimensions",
87-
yogaStyle.maxDimensions,
88-
defaultYogaStyle.maxDimensions),
88+
yogaStyle.maxDimensions(),
89+
defaultYogaStyle.maxDimensions()),
8990
debugStringConvertibleItem(
90-
"aspectRatio", yogaStyle.aspectRatio, defaultYogaStyle.aspectRatio),
91+
"aspectRatio",
92+
yogaStyle.aspectRatio(),
93+
defaultYogaStyle.aspectRatio()),
9194
};
9295
}
9396
#endif

ReactCommon/fabric/core/propsConversions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ void fromRawValue(const RawValue &rawValue, std::vector<T> &result) {
4545
result.push_back(itemResult);
4646
}
4747

48-
template <typename T>
48+
template <typename T, typename U = T>
4949
T convertRawProp(
5050
const RawProps &rawProps,
5151
const std::string &name,
5252
const T &sourceValue,
53-
const T &defaultValue = T()) {
53+
const U &defaultValue = U()) {
5454
const auto rawValue = rawProps.at(name);
5555

5656
if (!rawValue) {

0 commit comments

Comments
 (0)