Skip to content

Commit 7d4e5ef

Browse files
javachemeta-codesync[bot]
authored andcommitted
Simplify LinearGradient angle (#55800)
Summary: Pull Request resolved: #55800 No need for a separate enum here when we can just use the std::variant itself. Changelog: [Internal] Changed layout of GradientDirection Reviewed By: sammy-SC Differential Revision: D94522238 fbshipit-source-id: 7f02db573de4bf49e912795726adb302e91de969
1 parent 178c30d commit 7d4e5ef

4 files changed

Lines changed: 33 additions & 59 deletions

File tree

packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,13 @@ + (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const LinearGradient &
2424
CGPoint startPoint;
2525
CGPoint endPoint;
2626

27-
if (direction.type == GradientDirectionType::Angle) {
28-
CGFloat angle = std::get<Float>(direction.value);
27+
if (std::holds_alternative<Float>(direction)) {
28+
CGFloat angle = std::get<Float>(direction);
2929
std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size);
30-
} else if (direction.type == GradientDirectionType::Keyword) {
31-
auto keyword = std::get<GradientKeyword>(direction.value);
30+
} else {
31+
auto keyword = std::get<GradientKeyword>(direction);
3232
CGFloat angle = getAngleForKeyword(keyword, size);
3333
std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size);
34-
} else {
35-
// Default to top-to-bottom gradient
36-
CGFloat centerX = size.width / 2;
37-
startPoint = CGPointMake(centerX, 0.0);
38-
endPoint = CGPointMake(centerX, size.height);
3934
}
4035

4136
CGFloat dx = endPoint.x - startPoint.x;

packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ void parseProcessedBackgroundImage(
119119
std::string directionType = (std::string)(directionTypeIt->second);
120120

121121
if (directionType == "angle") {
122-
linearGradient.direction.type = GradientDirectionType::Angle;
123122
if (valueIt->second.hasType<Float>()) {
124-
linearGradient.direction.value = (Float)(valueIt->second);
123+
linearGradient.direction = (Float)(valueIt->second);
125124
}
126125
} else if (directionType == "keyword") {
127-
linearGradient.direction.type = GradientDirectionType::Keyword;
128126
if (valueIt->second.hasType<std::string>()) {
129-
linearGradient.direction.value =
127+
linearGradient.direction =
130128
parseGradientKeyword((std::string)(valueIt->second));
131129
}
132130
}
@@ -309,30 +307,26 @@ void parseUnprocessedBackgroundImageList(
309307
std::get<CSSLinearGradientDirection>(cssDirection);
310308

311309
if (std::holds_alternative<CSSAngle>(direction.value)) {
312-
linearGradient.direction.type = GradientDirectionType::Angle;
313-
linearGradient.direction.value =
310+
linearGradient.direction =
314311
std::get<CSSAngle>(direction.value).degrees;
315312
} else if (std::holds_alternative<
316313
CSSLinearGradientDirectionKeyword>(
317314
direction.value)) {
318-
linearGradient.direction.type = GradientDirectionType::Keyword;
319315
auto keyword =
320316
std::get<CSSLinearGradientDirectionKeyword>(direction.value);
321317

322318
switch (keyword) {
323319
case CSSLinearGradientDirectionKeyword::ToTopLeft:
324-
linearGradient.direction.value = GradientKeyword::ToTopLeft;
320+
linearGradient.direction = GradientKeyword::ToTopLeft;
325321
break;
326322
case CSSLinearGradientDirectionKeyword::ToTopRight:
327-
linearGradient.direction.value = GradientKeyword::ToTopRight;
323+
linearGradient.direction = GradientKeyword::ToTopRight;
328324
break;
329325
case CSSLinearGradientDirectionKeyword::ToBottomLeft:
330-
linearGradient.direction.value =
331-
GradientKeyword::ToBottomLeft;
326+
linearGradient.direction = GradientKeyword::ToBottomLeft;
332327
break;
333328
case CSSLinearGradientDirectionKeyword::ToBottomRight:
334-
linearGradient.direction.value =
335-
GradientKeyword::ToBottomRight;
329+
linearGradient.direction = GradientKeyword::ToBottomRight;
336330
break;
337331
}
338332
}
@@ -486,26 +480,23 @@ std::optional<BackgroundImage> fromCSSBackgroundImage(
486480
if (gradient.direction.has_value()) {
487481
if (std::holds_alternative<CSSAngle>(gradient.direction->value)) {
488482
const auto& angle = std::get<CSSAngle>(gradient.direction->value);
489-
linearGradient.direction.type = GradientDirectionType::Angle;
490-
linearGradient.direction.value = angle.degrees;
483+
linearGradient.direction = angle.degrees;
491484
} else if (std::holds_alternative<CSSLinearGradientDirectionKeyword>(
492485
gradient.direction->value)) {
493486
const auto& dirKeyword = std::get<CSSLinearGradientDirectionKeyword>(
494487
gradient.direction->value);
495-
linearGradient.direction.type = GradientDirectionType::Keyword;
496-
497488
switch (dirKeyword) {
498489
case CSSLinearGradientDirectionKeyword::ToTopLeft:
499-
linearGradient.direction.value = GradientKeyword::ToTopLeft;
490+
linearGradient.direction = GradientKeyword::ToTopLeft;
500491
break;
501492
case CSSLinearGradientDirectionKeyword::ToTopRight:
502-
linearGradient.direction.value = GradientKeyword::ToTopRight;
493+
linearGradient.direction = GradientKeyword::ToTopRight;
503494
break;
504495
case CSSLinearGradientDirectionKeyword::ToBottomLeft:
505-
linearGradient.direction.value = GradientKeyword::ToBottomLeft;
496+
linearGradient.direction = GradientKeyword::ToBottomLeft;
506497
break;
507498
case CSSLinearGradientDirectionKeyword::ToBottomRight:
508-
linearGradient.direction.value = GradientKeyword::ToBottomRight;
499+
linearGradient.direction = GradientKeyword::ToBottomRight;
509500
break;
510501
}
511502
}
@@ -606,14 +597,14 @@ void parseUnprocessedBackgroundImageString(
606597
for (const auto& cssBackgroundImage :
607598
std::get<CSSBackgroundImageList>(backgroundImageList)) {
608599
if (auto backgroundImage = fromCSSBackgroundImage(cssBackgroundImage)) {
609-
backgroundImages.push_back(*backgroundImage);
600+
backgroundImages.push_back(std::move(*backgroundImage));
610601
} else {
611602
result = {};
612603
return;
613604
}
614605
}
615606

616-
result = backgroundImages;
607+
result = std::move(backgroundImages);
617608
}
618609

619610
} // namespace facebook::react

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@
1010
namespace facebook::react {
1111

1212
#ifdef RN_SERIALIZABLE_STATE
13-
folly::dynamic GradientDirection::toDynamic() const {
13+
namespace {
14+
folly::dynamic directionToDynamic(const GradientDirection& value) {
1415
folly::dynamic result = folly::dynamic::object();
15-
result["type"] = [&]() {
16-
switch (type) {
17-
case GradientDirectionType::Angle:
18-
return "angle";
19-
case GradientDirectionType::Keyword:
20-
return "keyword";
21-
}
22-
return "";
23-
}();
24-
2516
if (std::holds_alternative<Float>(value)) {
17+
result["type"] = "angle";
2618
result["value"] = std::get<Float>(value);
2719
} else if (std::holds_alternative<GradientKeyword>(value)) {
20+
result["type"] = "keyword";
2821
result["value"] = [&]() {
2922
switch (std::get<GradientKeyword>(value)) {
3023
case GradientKeyword::ToTopRight:
@@ -41,11 +34,12 @@ folly::dynamic GradientDirection::toDynamic() const {
4134
}
4235
return result;
4336
}
37+
} // namespace
4438

4539
folly::dynamic LinearGradient::toDynamic() const {
4640
folly::dynamic result = folly::dynamic::object();
4741
result["type"] = "linear-gradient";
48-
result["direction"] = direction.toDynamic();
42+
result["direction"] = directionToDynamic(direction);
4943

5044
folly::dynamic colorStopsArray = folly::dynamic::array();
5145
for (const auto& colorStop : colorStops) {
@@ -60,10 +54,10 @@ folly::dynamic LinearGradient::toDynamic() const {
6054
void LinearGradient::toString(std::stringstream& ss) const {
6155
ss << "linear-gradient(";
6256

63-
if (direction.type == GradientDirectionType::Angle) {
64-
ss << std::get<Float>(direction.value) << "deg";
57+
if (std::holds_alternative<Float>(direction)) {
58+
ss << std::get<Float>(direction) << "deg";
6559
} else {
66-
auto keyword = std::get<GradientKeyword>(direction.value);
60+
auto keyword = std::get<GradientKeyword>(direction);
6761
switch (keyword) {
6862
case GradientKeyword::ToTopRight:
6963
ss << "to top right";

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,19 @@
1818

1919
namespace facebook::react {
2020

21-
enum class GradientDirectionType { Angle, Keyword };
21+
enum class [[deprecated("Use std::holds_alternative")]] GradientDirectionType {
22+
Angle,
23+
Keyword,
24+
};
2225

23-
enum class GradientKeyword {
26+
enum class GradientKeyword : uint8_t {
2427
ToTopRight,
2528
ToBottomRight,
2629
ToTopLeft,
2730
ToBottomLeft,
2831
};
2932

30-
struct GradientDirection {
31-
GradientDirectionType type;
32-
std::variant<Float, GradientKeyword> value;
33-
34-
bool operator==(const GradientDirection &other) const = default;
35-
36-
#ifdef RN_SERIALIZABLE_STATE
37-
folly::dynamic toDynamic() const;
38-
#endif
39-
};
33+
using GradientDirection = std::variant<Float, GradientKeyword>;
4034

4135
struct LinearGradient {
4236
GradientDirection direction;

0 commit comments

Comments
 (0)