From 7d4e5ef5670e5503c3d419e9fa2ac683e7abdbbe Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 27 Feb 2026 08:02:21 -0800 Subject: [PATCH] Simplify LinearGradient angle (#55800) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../React/Fabric/Utils/RCTLinearGradient.mm | 15 +++----- .../view/BackgroundImagePropsConversions.cpp | 37 +++++++------------ .../renderer/graphics/LinearGradient.cpp | 24 +++++------- .../react/renderer/graphics/LinearGradient.h | 18 +++------ 4 files changed, 34 insertions(+), 60 deletions(-) diff --git a/packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm b/packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm index a7fafe3733b..2aa56c860df 100644 --- a/packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm +++ b/packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm @@ -24,18 +24,13 @@ using namespace facebook::react; CGPoint startPoint; CGPoint endPoint; - if (direction.type == GradientDirectionType::Angle) { - CGFloat angle = std::get(direction.value); - std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size); - } else if (direction.type == GradientDirectionType::Keyword) { - auto keyword = std::get(direction.value); - CGFloat angle = getAngleForKeyword(keyword, size); + if (std::holds_alternative(direction)) { + CGFloat angle = std::get(direction); std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size); } else { - // Default to top-to-bottom gradient - CGFloat centerX = size.width / 2; - startPoint = CGPointMake(centerX, 0.0); - endPoint = CGPointMake(centerX, size.height); + auto keyword = std::get(direction); + CGFloat angle = getAngleForKeyword(keyword, size); + std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size); } CGFloat dx = endPoint.x - startPoint.x; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp index 0875fd04e96..7759acebe2a 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.cpp @@ -119,14 +119,12 @@ void parseProcessedBackgroundImage( std::string directionType = (std::string)(directionTypeIt->second); if (directionType == "angle") { - linearGradient.direction.type = GradientDirectionType::Angle; if (valueIt->second.hasType()) { - linearGradient.direction.value = (Float)(valueIt->second); + linearGradient.direction = (Float)(valueIt->second); } } else if (directionType == "keyword") { - linearGradient.direction.type = GradientDirectionType::Keyword; if (valueIt->second.hasType()) { - linearGradient.direction.value = + linearGradient.direction = parseGradientKeyword((std::string)(valueIt->second)); } } @@ -309,30 +307,26 @@ void parseUnprocessedBackgroundImageList( std::get(cssDirection); if (std::holds_alternative(direction.value)) { - linearGradient.direction.type = GradientDirectionType::Angle; - linearGradient.direction.value = + linearGradient.direction = std::get(direction.value).degrees; } else if (std::holds_alternative< CSSLinearGradientDirectionKeyword>( direction.value)) { - linearGradient.direction.type = GradientDirectionType::Keyword; auto keyword = std::get(direction.value); switch (keyword) { case CSSLinearGradientDirectionKeyword::ToTopLeft: - linearGradient.direction.value = GradientKeyword::ToTopLeft; + linearGradient.direction = GradientKeyword::ToTopLeft; break; case CSSLinearGradientDirectionKeyword::ToTopRight: - linearGradient.direction.value = GradientKeyword::ToTopRight; + linearGradient.direction = GradientKeyword::ToTopRight; break; case CSSLinearGradientDirectionKeyword::ToBottomLeft: - linearGradient.direction.value = - GradientKeyword::ToBottomLeft; + linearGradient.direction = GradientKeyword::ToBottomLeft; break; case CSSLinearGradientDirectionKeyword::ToBottomRight: - linearGradient.direction.value = - GradientKeyword::ToBottomRight; + linearGradient.direction = GradientKeyword::ToBottomRight; break; } } @@ -486,26 +480,23 @@ std::optional fromCSSBackgroundImage( if (gradient.direction.has_value()) { if (std::holds_alternative(gradient.direction->value)) { const auto& angle = std::get(gradient.direction->value); - linearGradient.direction.type = GradientDirectionType::Angle; - linearGradient.direction.value = angle.degrees; + linearGradient.direction = angle.degrees; } else if (std::holds_alternative( gradient.direction->value)) { const auto& dirKeyword = std::get( gradient.direction->value); - linearGradient.direction.type = GradientDirectionType::Keyword; - switch (dirKeyword) { case CSSLinearGradientDirectionKeyword::ToTopLeft: - linearGradient.direction.value = GradientKeyword::ToTopLeft; + linearGradient.direction = GradientKeyword::ToTopLeft; break; case CSSLinearGradientDirectionKeyword::ToTopRight: - linearGradient.direction.value = GradientKeyword::ToTopRight; + linearGradient.direction = GradientKeyword::ToTopRight; break; case CSSLinearGradientDirectionKeyword::ToBottomLeft: - linearGradient.direction.value = GradientKeyword::ToBottomLeft; + linearGradient.direction = GradientKeyword::ToBottomLeft; break; case CSSLinearGradientDirectionKeyword::ToBottomRight: - linearGradient.direction.value = GradientKeyword::ToBottomRight; + linearGradient.direction = GradientKeyword::ToBottomRight; break; } } @@ -606,14 +597,14 @@ void parseUnprocessedBackgroundImageString( for (const auto& cssBackgroundImage : std::get(backgroundImageList)) { if (auto backgroundImage = fromCSSBackgroundImage(cssBackgroundImage)) { - backgroundImages.push_back(*backgroundImage); + backgroundImages.push_back(std::move(*backgroundImage)); } else { result = {}; return; } } - result = backgroundImages; + result = std::move(backgroundImages); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.cpp index 6e590b28471..413dac4c63a 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.cpp @@ -10,21 +10,14 @@ namespace facebook::react { #ifdef RN_SERIALIZABLE_STATE -folly::dynamic GradientDirection::toDynamic() const { +namespace { +folly::dynamic directionToDynamic(const GradientDirection& value) { folly::dynamic result = folly::dynamic::object(); - result["type"] = [&]() { - switch (type) { - case GradientDirectionType::Angle: - return "angle"; - case GradientDirectionType::Keyword: - return "keyword"; - } - return ""; - }(); - if (std::holds_alternative(value)) { + result["type"] = "angle"; result["value"] = std::get(value); } else if (std::holds_alternative(value)) { + result["type"] = "keyword"; result["value"] = [&]() { switch (std::get(value)) { case GradientKeyword::ToTopRight: @@ -41,11 +34,12 @@ folly::dynamic GradientDirection::toDynamic() const { } return result; } +} // namespace folly::dynamic LinearGradient::toDynamic() const { folly::dynamic result = folly::dynamic::object(); result["type"] = "linear-gradient"; - result["direction"] = direction.toDynamic(); + result["direction"] = directionToDynamic(direction); folly::dynamic colorStopsArray = folly::dynamic::array(); for (const auto& colorStop : colorStops) { @@ -60,10 +54,10 @@ folly::dynamic LinearGradient::toDynamic() const { void LinearGradient::toString(std::stringstream& ss) const { ss << "linear-gradient("; - if (direction.type == GradientDirectionType::Angle) { - ss << std::get(direction.value) << "deg"; + if (std::holds_alternative(direction)) { + ss << std::get(direction) << "deg"; } else { - auto keyword = std::get(direction.value); + auto keyword = std::get(direction); switch (keyword) { case GradientKeyword::ToTopRight: ss << "to top right"; diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.h b/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.h index c761cdbf726..243333975f2 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/LinearGradient.h @@ -18,25 +18,19 @@ namespace facebook::react { -enum class GradientDirectionType { Angle, Keyword }; +enum class [[deprecated("Use std::holds_alternative")]] GradientDirectionType { + Angle, + Keyword, +}; -enum class GradientKeyword { +enum class GradientKeyword : uint8_t { ToTopRight, ToBottomRight, ToTopLeft, ToBottomLeft, }; -struct GradientDirection { - GradientDirectionType type; - std::variant value; - - bool operator==(const GradientDirection &other) const = default; - -#ifdef RN_SERIALIZABLE_STATE - folly::dynamic toDynamic() const; -#endif -}; +using GradientDirection = std::variant; struct LinearGradient { GradientDirection direction;