Skip to content

Commit c53541f

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Add toDynamic conversion for LinearGradient (facebook#54040)
Summary: Pull Request resolved: facebook#54040 Implement toDynamic conversion of the LinearGradient following the parse logic as described here: https://www.internalfb.com/code/fbsource/[5fa7eb2b733a]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/LinearGradient.kt?lines=24-76 Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D83788005 fbshipit-source-id: 9114451b12e31ad86abde54dbe2c8819a304e777
1 parent e88962b commit c53541f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,61 @@ namespace facebook::react {
1818

1919
enum class GradientDirectionType { Angle, Keyword };
2020

21+
inline std::string toString(
22+
const GradientDirectionType& gradientDirectionType) {
23+
switch (gradientDirectionType) {
24+
case GradientDirectionType::Angle:
25+
return "angle";
26+
case GradientDirectionType::Keyword:
27+
return "keyword";
28+
}
29+
30+
return "";
31+
}
32+
2133
enum class GradientKeyword {
2234
ToTopRight,
2335
ToBottomRight,
2436
ToTopLeft,
2537
ToBottomLeft,
2638
};
2739

40+
inline std::string toString(const GradientKeyword& gradientKeyword) {
41+
switch (gradientKeyword) {
42+
case GradientKeyword::ToTopRight:
43+
return "to top right";
44+
case GradientKeyword::ToBottomRight:
45+
return "to bottom right";
46+
case GradientKeyword::ToTopLeft:
47+
return "to top left";
48+
case GradientKeyword::ToBottomLeft:
49+
return "to bottom left";
50+
}
51+
52+
return "";
53+
}
54+
2855
struct GradientDirection {
2956
GradientDirectionType type;
3057
std::variant<Float, GradientKeyword> value;
3158

3259
bool operator==(const GradientDirection& other) const {
3360
return type == other.type && value == other.value;
3461
}
62+
63+
#ifdef RN_SERIALIZABLE_STATE
64+
folly::dynamic toDynamic() const {
65+
folly::dynamic result = folly::dynamic::object();
66+
result["type"] = toString(type);
67+
68+
if (std::holds_alternative<Float>(value)) {
69+
result["value"] = std::get<Float>(value);
70+
} else if (std::holds_alternative<GradientKeyword>(value)) {
71+
result["value"] = toString(std::get<GradientKeyword>(value));
72+
}
73+
return result;
74+
}
75+
#endif
3576
};
3677

3778
struct LinearGradient {
@@ -41,6 +82,21 @@ struct LinearGradient {
4182
bool operator==(const LinearGradient& other) const {
4283
return direction == other.direction && colorStops == other.colorStops;
4384
}
85+
86+
#ifdef RN_SERIALIZABLE_STATE
87+
folly::dynamic toDynamic() const {
88+
folly::dynamic result = folly::dynamic::object();
89+
result["type"] = "linear-gradient";
90+
result["direction"] = direction.toDynamic();
91+
92+
folly::dynamic colorStopsArray = folly::dynamic::array();
93+
for (const auto& colorStop : colorStops) {
94+
colorStopsArray.push_back(colorStop.toDynamic());
95+
}
96+
result["colorStops"] = colorStopsArray;
97+
return result;
98+
}
99+
#endif
44100
};
45101

46102
inline GradientKeyword parseGradientKeyword(const std::string& keyword) {

0 commit comments

Comments
 (0)