Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "BackgroundImage.h"

namespace facebook::react {

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic toDynamic(const BackgroundImage& backgroundImage) {
if (std::holds_alternative<LinearGradient>(backgroundImage)) {
return std::get<LinearGradient>(backgroundImage).toDynamic();
} else if (std::holds_alternative<RadialGradient>(backgroundImage)) {
return std::get<RadialGradient>(backgroundImage).toDynamic();
}
return folly::dynamic(nullptr);
}
#endif

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@
#include <react/renderer/graphics/ColorComponents.h>
#include <react/renderer/graphics/LinearGradient.h>
#include <react/renderer/graphics/RadialGradient.h>
#include <vector>

namespace facebook::react {

using BackgroundImage = std::variant<LinearGradient, RadialGradient>;

#ifdef RN_SERIALIZABLE_STATE
inline folly::dynamic toDynamic(const BackgroundImage& backgroundImage) {
if (std::holds_alternative<LinearGradient>(backgroundImage)) {
return std::get<LinearGradient>(backgroundImage).toDynamic();
} else if (std::holds_alternative<RadialGradient>(backgroundImage)) {
return std::get<RadialGradient>(backgroundImage).toDynamic();
}
return folly::dynamic(nullptr);
}
folly::dynamic toDynamic(const BackgroundImage& backgroundImage);
#endif

}; // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "ColorStop.h"

namespace facebook::react {

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic ColorStop::toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["color"] = *color;
result["position"] = position.toDynamic();
return result;
}
#endif

}; // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/ValueUnit.h>
#include <optional>

namespace facebook::react {

Expand All @@ -19,12 +20,7 @@ struct ColorStop {
ValueUnit position;

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["color"] = *color;
result["position"] = position.toDynamic();
return result;
}
folly::dynamic toDynamic() const;
#endif
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "LinearGradient.h"

namespace facebook::react {

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic GradientDirection::toDynamic() const {
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<Float>(value)) {
result["value"] = std::get<Float>(value);
} else if (std::holds_alternative<GradientKeyword>(value)) {
result["value"] = [&]() {
switch (std::get<GradientKeyword>(value)) {
case GradientKeyword::ToTopRight:
return "to top right";
case GradientKeyword::ToBottomRight:
return "to bottom right";
case GradientKeyword::ToTopLeft:
return "to top left";
case GradientKeyword::ToBottomLeft:
return "to bottom left";
}
return "";
}();
}
return result;
}

folly::dynamic LinearGradient::toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["type"] = "linear-gradient";
result["direction"] = direction.toDynamic();

folly::dynamic colorStopsArray = folly::dynamic::array();
for (const auto& colorStop : colorStops) {
colorStopsArray.push_back(colorStop.toDynamic());
}
result["colorStops"] = colorStopsArray;
return result;
}
#endif

}; // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <react/renderer/graphics/ColorStop.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/ValueUnit.h>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>
Expand All @@ -18,40 +19,13 @@ namespace facebook::react {

enum class GradientDirectionType { Angle, Keyword };

inline std::string toString(
const GradientDirectionType& gradientDirectionType) {
switch (gradientDirectionType) {
case GradientDirectionType::Angle:
return "angle";
case GradientDirectionType::Keyword:
return "keyword";
}

return "";
}

enum class GradientKeyword {
ToTopRight,
ToBottomRight,
ToTopLeft,
ToBottomLeft,
};

inline std::string toString(const GradientKeyword& gradientKeyword) {
switch (gradientKeyword) {
case GradientKeyword::ToTopRight:
return "to top right";
case GradientKeyword::ToBottomRight:
return "to bottom right";
case GradientKeyword::ToTopLeft:
return "to top left";
case GradientKeyword::ToBottomLeft:
return "to bottom left";
}

return "";
}

struct GradientDirection {
GradientDirectionType type;
std::variant<Float, GradientKeyword> value;
Expand All @@ -61,17 +35,7 @@ struct GradientDirection {
}

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["type"] = toString(type);

if (std::holds_alternative<Float>(value)) {
result["value"] = std::get<Float>(value);
} else if (std::holds_alternative<GradientKeyword>(value)) {
result["value"] = toString(std::get<GradientKeyword>(value));
}
return result;
}
folly::dynamic toDynamic() const;
#endif
};

Expand All @@ -84,18 +48,7 @@ struct LinearGradient {
}

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["type"] = "linear-gradient";
result["direction"] = direction.toDynamic();

folly::dynamic colorStopsArray = folly::dynamic::array();
for (const auto& colorStop : colorStops) {
colorStopsArray.push_back(colorStop.toDynamic());
}
result["colorStops"] = colorStopsArray;
return result;
}
folly::dynamic toDynamic() const;
#endif
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "RadialGradient.h"

namespace facebook::react {

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic RadialGradientSize::Dimensions::toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["x"] = x.toDynamic();
result["y"] = y.toDynamic();
return result;
}

folly::dynamic RadialGradientSize::toDynamic() const {
if (std::holds_alternative<SizeKeyword>(value)) {
switch (std::get<SizeKeyword>(value)) {
case SizeKeyword::ClosestSide:
return "closest-side";
case SizeKeyword::FarthestSide:
return "farthest-side";
case SizeKeyword::ClosestCorner:
return "closest-corner";
case SizeKeyword::FarthestCorner:
return "farthest-corner";
}
} else if (std::holds_alternative<Dimensions>(value)) {
return std::get<Dimensions>(value).toDynamic();
}

return nullptr;
}

folly::dynamic RadialGradientPosition::toDynamic() const {
folly::dynamic result = folly::dynamic::object();
if (top.has_value()) {
result["top"] = top.value().toDynamic();
}
if (left.has_value()) {
result["left"] = left.value().toDynamic();
}
if (right.has_value()) {
result["right"] = right.value().toDynamic();
}
if (bottom.has_value()) {
result["bottom"] = bottom.value().toDynamic();
}
return result;
}

folly::dynamic RadialGradient::toDynamic() const {
folly::dynamic result = folly::dynamic::object();
result["type"] = "radial-gradient";

switch (shape) {
case RadialGradientShape::Circle:
result["shape"] = "circle";
break;
case RadialGradientShape::Ellipse:
result["shape"] = "ellipse";
break;
default:
break;
}

result["size"] = size.toDynamic();
result["position"] = position.toDynamic();

folly::dynamic colorStopsArray = folly::dynamic::array();
for (const auto& colorStop : colorStops) {
colorStopsArray.push_back(colorStop.toDynamic());
}
result["colorStops"] = colorStopsArray;

return result;
}
#endif
}; // namespace facebook::react
Loading
Loading