Skip to content

Commit d9d3b3e

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Add toDynamic conversion for RadialGradient (facebook#54041)
Summary: Pull Request resolved: facebook#54041 Implement toDynamic conversion of the RadialGradient following the parse logic as described here: https://www.internalfb.com/code/fbsource/[28de0c66daab]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/RadialGradient.kt?lines=33-129 Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D83788007 fbshipit-source-id: 305e6f5011f16103a559132e61b6d4eae24cc137
1 parent c53541f commit d9d3b3e

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

packages/react-native/ReactCommon/react/renderer/graphics/RadialGradient.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,27 @@
1515
#include <variant>
1616
#include <vector>
1717

18+
#ifdef RN_SERIALIZABLE_STATE
19+
#include <folly/dynamic.h>
20+
#endif
21+
1822
namespace facebook::react {
1923

2024
enum class RadialGradientShape { Circle, Ellipse };
2125

26+
#ifdef RN_SERIALIZABLE_STATE
27+
inline std::string toString(const RadialGradientShape& radialGradientShape) {
28+
switch (radialGradientShape) {
29+
case RadialGradientShape::Circle:
30+
return "circle";
31+
case RadialGradientShape::Ellipse:
32+
return "ellipse";
33+
}
34+
35+
return "";
36+
}
37+
#endif
38+
2239
struct RadialGradientSize {
2340
enum class SizeKeyword {
2441
ClosestSide,
@@ -37,6 +54,15 @@ struct RadialGradientSize {
3754
bool operator!=(const Dimensions& other) const {
3855
return !(*this == other);
3956
}
57+
58+
#ifdef RN_SERIALIZABLE_STATE
59+
folly::dynamic toDynamic() const {
60+
folly::dynamic result = folly::dynamic::object();
61+
result["x"] = x.toDynamic();
62+
result["y"] = y.toDynamic();
63+
return result;
64+
}
65+
#endif
4066
};
4167

4268
std::variant<SizeKeyword, Dimensions> value;
@@ -56,6 +82,27 @@ struct RadialGradientSize {
5682
bool operator!=(const RadialGradientSize& other) const {
5783
return !(*this == other);
5884
}
85+
86+
#ifdef RN_SERIALIZABLE_STATE
87+
folly::dynamic toDynamic() const {
88+
if (std::holds_alternative<SizeKeyword>(value)) {
89+
switch (std::get<SizeKeyword>(value)) {
90+
case SizeKeyword::ClosestSide:
91+
return "closest-side";
92+
case SizeKeyword::FarthestSide:
93+
return "farthest-side";
94+
case SizeKeyword::ClosestCorner:
95+
return "closest-corner";
96+
case SizeKeyword::FarthestCorner:
97+
return "farthest-corner";
98+
}
99+
} else if (std::holds_alternative<Dimensions>(value)) {
100+
return std::get<Dimensions>(value).toDynamic();
101+
}
102+
103+
return nullptr;
104+
}
105+
#endif
59106
};
60107

61108
struct RadialGradientPosition {
@@ -72,6 +119,25 @@ struct RadialGradientPosition {
72119
bool operator!=(const RadialGradientPosition& other) const {
73120
return !(*this == other);
74121
}
122+
123+
#ifdef RN_SERIALIZABLE_STATE
124+
folly::dynamic toDynamic() const {
125+
folly::dynamic result = folly::dynamic::object();
126+
if (top.has_value()) {
127+
result["top"] = top.value().toDynamic();
128+
}
129+
if (left.has_value()) {
130+
result["left"] = left.value().toDynamic();
131+
}
132+
if (right.has_value()) {
133+
result["right"] = right.value().toDynamic();
134+
}
135+
if (bottom.has_value()) {
136+
result["bottom"] = bottom.value().toDynamic();
137+
}
138+
return result;
139+
}
140+
#endif
75141
};
76142

77143
struct RadialGradient {
@@ -87,6 +153,24 @@ struct RadialGradient {
87153
bool operator!=(const RadialGradient& other) const {
88154
return !(*this == other);
89155
}
156+
157+
#ifdef RN_SERIALIZABLE_STATE
158+
folly::dynamic toDynamic() const {
159+
folly::dynamic result = folly::dynamic::object();
160+
result["type"] = "radial-gradient";
161+
result["shape"] = toString(shape);
162+
result["size"] = size.toDynamic();
163+
result["position"] = position.toDynamic();
164+
165+
folly::dynamic colorStopsArray = folly::dynamic::array();
166+
for (const auto& colorStop : colorStops) {
167+
colorStopsArray.push_back(colorStop.toDynamic());
168+
}
169+
result["colorStops"] = colorStopsArray;
170+
171+
return result;
172+
}
173+
#endif
90174
};
91175

92176
}; // namespace facebook::react

0 commit comments

Comments
 (0)