Skip to content

Commit e7eda39

Browse files
committed
Add some CSS code improvements
1 parent 4197a13 commit e7eda39

15 files changed

+258
-251
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSLength.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ folly::dynamic CSSLength::toDynamic() const {
6565
}
6666

6767
std::string CSSLength::toString() const {
68-
if (isRelative) {
69-
return std::to_string(value * 100) + "%";
70-
}
71-
return std::to_string(value);
68+
return isRelative ? (std::to_string(value * 100) + "%")
69+
: std::to_string(value);
7270
}
7371

7472
CSSLength CSSLength::interpolate(
7573
const double progress,
7674
const CSSLength &to,
77-
const CSSResolvableValueInterpolationContext &context) const {
75+
const ResolvableValueInterpolationContext &context) const {
7876
// If both value types are the same, we can interpolate without reading the
7977
// relative value from the shadow node
8078
// (also, when one of the values is 0, and the other is relative)
@@ -97,7 +95,7 @@ CSSLength CSSLength::interpolate(
9795
}
9896

9997
std::optional<double> CSSLength::resolve(
100-
const CSSResolvableValueInterpolationContext &context) const {
98+
const ResolvableValueInterpolationContext &context) const {
10199
if (!isRelative) {
102100
return value;
103101
}

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSLength.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ struct CSSLength : public CSSResolvableValue<CSSLength, double> {
2727
CSSLength interpolate(
2828
double progress,
2929
const CSSLength &to,
30-
const CSSResolvableValueInterpolationContext &context) const override;
30+
const ResolvableValueInterpolationContext &context) const override;
3131
std::optional<double> resolve(
32-
const CSSResolvableValueInterpolationContext &context) const override;
32+
const ResolvableValueInterpolationContext &context) const override;
3333

3434
bool operator==(const CSSLength &other) const;
3535

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ enum class RelativeTo {
1515
Self,
1616
};
1717

18-
struct CSSValueInterpolationContext {
18+
struct ValueInterpolationContext {
1919
const std::shared_ptr<const ShadowNode> &node;
2020
const double fallbackInterpolateThreshold;
2121
};
2222

23-
struct CSSResolvableValueInterpolationContext {
23+
struct ResolvableValueInterpolationContext {
2424
const std::shared_ptr<const ShadowNode> &node;
2525
const double fallbackInterpolateThreshold;
2626
const std::shared_ptr<ViewStylesRepository> &viewStylesRepository;
@@ -57,9 +57,9 @@ struct CSSResolvableValue : public CSSValue {
5757
virtual TDerived interpolate(
5858
double progress,
5959
const TDerived &to,
60-
const CSSResolvableValueInterpolationContext &context) const = 0;
60+
const ResolvableValueInterpolationContext &context) const = 0;
6161
virtual std::optional<TResolved> resolve(
62-
const CSSResolvableValueInterpolationContext &context) const = 0;
62+
const ResolvableValueInterpolationContext &context) const = 0;
6363
virtual bool canInterpolateTo(const TDerived &to) const {
6464
return true;
6565
}

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.h

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ concept DynamicConstructibleCSSValue = requires(const folly::dynamic &value) {
5858
*
5959
* A std::variant-based container for multiple CSSValue-derived types.
6060
*/
61-
template <typename... AllowedTypes>
61+
template <CSSValueDerived... AllowedTypes>
6262
class CSSValueVariant final : public CSSValue {
63-
static_assert(
64-
(CSSValueDerived<AllowedTypes> && ...),
65-
"CSSValueVariant accepts only CSSValue-derived types");
6663
static_assert(
6764
(JSIConstructibleCSSValue<AllowedTypes> && ...),
6865
"CSSValueVariant accepts only types that can be constructed from a jsi::Value");
@@ -74,42 +71,22 @@ class CSSValueVariant final : public CSSValue {
7471
CSSValueVariant() = default;
7572

7673
/**
77-
* Construct from any value that is or can construct one of the AllowedTypes
74+
* Construct from any TValue that is or can construct one of the AllowedTypes
7875
* (chooses the first one that matches)
7976
*/
80-
explicit CSSValueVariant(auto &&value)
81-
requires((std::is_constructible_v<AllowedTypes, decltype(value)> || ...))
77+
template <typename TValue>
78+
explicit CSSValueVariant(TValue &&value)
79+
requires((std::is_constructible_v<AllowedTypes, TValue> || ...))
8280
{ // NOLINT(whitespace/braces)
83-
using ValueType = decltype(value);
84-
85-
// If value type exactly matches one of AllowedTypes, store it directly:
81+
// If TValue exactly matches one of AllowedTypes, store it directly:
8682
if constexpr ((std::is_same_v<
87-
std::remove_reference_t<ValueType>,
83+
std::remove_reference_t<TValue>,
8884
AllowedTypes> ||
8985
...)) {
90-
storage_ = std::forward<ValueType>(value);
86+
storage_ = std::forward<TValue>(value);
9187
} else {
92-
// Otherwise, try to construct the CSSValue from each type in turn
93-
auto tryOne = [&]<typename TCSSValue>() -> bool {
94-
if constexpr (std::is_constructible_v<TCSSValue, ValueType>) {
95-
if constexpr (ValueConstructibleCSSValue<TCSSValue, ValueType>) {
96-
// For construction from a non-jsi::Value, we perform a runtime
97-
// canConstruct check only if the type has a canConstruct method.
98-
// (this is needed e.g. when different CSS value types can be
99-
// constructed from the same value type, like CSSLength and
100-
// CSSKeyword)
101-
if (!TCSSValue::canConstruct(std::forward<ValueType>(value))) {
102-
return false;
103-
}
104-
}
105-
storage_ = TCSSValue(std::forward<ValueType>(value));
106-
return true;
107-
}
108-
return false;
109-
};
110-
111-
// Try constructing with each allowed type until one succeeds
112-
if (!(tryOne.template operator()<AllowedTypes>() || ...)) {
88+
// Otherwise, try each type in turn
89+
if (!tryConstruct(std::forward<TValue>(value))) {
11390
throw std::runtime_error(
11491
"[Reanimated] No compatible type found for construction");
11592
}
@@ -121,43 +98,15 @@ class CSSValueVariant final : public CSSValue {
12198
* (chooses the first one that matches)
12299
*/
123100
CSSValueVariant(jsi::Runtime &rt, const jsi::Value &jsiValue) {
124-
auto tryOne = [&]<typename TCSSValue>() -> bool {
125-
// We have to check in a runtime if the type can be constructed from the
126-
// provided jsi::Value. The first match will be used to construct the
127-
// CSS value.
128-
if (!TCSSValue::canConstruct(rt, jsiValue)) {
129-
return false;
130-
}
131-
storage_ = TCSSValue(rt, jsiValue);
132-
return true;
133-
};
134-
135-
// Try constructing with each allowed type until one succeeds
136-
if (!(tryOne.template operator()<AllowedTypes>() || ...)) {
101+
if (!tryConstruct(rt, jsiValue)) {
137102
throw std::runtime_error(
138103
"[Reanimated] No compatible type found for construction from: " +
139104
stringifyJSIValue(rt, jsiValue));
140105
}
141106
}
142107

143-
/**
144-
* Construct from folly::dynamic if it matches any AllowedType's constructor
145-
* (chooses the first one that matches)
146-
*/
147108
explicit CSSValueVariant(const folly::dynamic &value) {
148-
auto tryOne = [&]<typename TCSSValue>() -> bool {
149-
// We have to check in a runtime if the type can be constructed from the
150-
// provided folly::dynamic. The first match will be used to construct the
151-
// CSS value.
152-
if (!TCSSValue::canConstruct(value)) {
153-
return false;
154-
}
155-
storage_ = TCSSValue(value);
156-
return true;
157-
};
158-
159-
// Try constructing with each allowed type until one succeeds
160-
if (!(tryOne.template operator()<AllowedTypes>() || ...)) {
109+
if (!tryConstruct(value)) {
161110
throw std::runtime_error(
162111
"[Reanimated] No compatible type found for construction from: " +
163112
folly::toJson(value));
@@ -201,7 +150,7 @@ class CSSValueVariant final : public CSSValue {
201150
CSSValueVariant interpolate(
202151
const double progress,
203152
const CSSValueVariant &to,
204-
const CSSValueInterpolationContext &context) const {
153+
const ValueInterpolationContext &context) const {
205154
if (storage_.index() != to.storage_.index()) {
206155
return fallbackInterpolate(
207156
progress, to, context.fallbackInterpolateThreshold);
@@ -230,7 +179,7 @@ class CSSValueVariant final : public CSSValue {
230179
CSSValueVariant interpolate(
231180
const double progress,
232181
const CSSValueVariant &to,
233-
const CSSResolvableValueInterpolationContext &context) const {
182+
const ResolvableValueInterpolationContext &context) const {
234183
if (storage_.index() != to.storage_.index()) {
235184
return fallbackInterpolate(
236185
progress, to, context.fallbackInterpolateThreshold);
@@ -263,6 +212,70 @@ class CSSValueVariant final : public CSSValue {
263212
const double fallbackInterpolateThreshold) const {
264213
return (progress < fallbackInterpolateThreshold) ? *this : to;
265214
}
215+
216+
/**
217+
* Tries to construct type from a given value
218+
*/
219+
template <typename TValue>
220+
bool tryConstruct(TValue &&value) {
221+
auto tryOne = [&]<typename TCSSValue>() -> bool {
222+
if constexpr (std::is_constructible_v<TCSSValue, TValue>) {
223+
if constexpr (ValueConstructibleCSSValue<TCSSValue, TValue>) {
224+
// For construction from a non-jsi::Value, we perform a runtime
225+
// canConstruct check only if the type has a canConstruct method.
226+
// (this is needed e.g. when different CSS value types can be
227+
// constructed from the same value type, like CSSLength and
228+
// CSSKeyword)
229+
if (!TCSSValue::canConstruct(std::forward<TValue>(value))) {
230+
return false;
231+
}
232+
}
233+
storage_ = TCSSValue(std::forward<TValue>(value));
234+
return true;
235+
}
236+
return false;
237+
};
238+
239+
// Try constructing with each allowed type until one succeeds
240+
return (tryOne.template operator()<AllowedTypes>() || ...);
241+
}
242+
243+
/**
244+
* Tries to construct type from a given jsi::Value
245+
*/
246+
bool tryConstruct(jsi::Runtime &rt, const jsi::Value &jsiValue) {
247+
auto tryOne = [&]<typename TCSSValue>() -> bool {
248+
// We have to check in a runtime if the type can be constructed from the
249+
// provided jsi::Value. The first match will be used to construct the
250+
// CSS value.
251+
if (!TCSSValue::canConstruct(rt, jsiValue)) {
252+
return false;
253+
}
254+
storage_ = TCSSValue(rt, jsiValue);
255+
return true;
256+
};
257+
258+
// Try constructing with each allowed type until one succeeds
259+
return (tryOne.template operator()<AllowedTypes>() || ...);
260+
}
261+
262+
/**
263+
* Tries to construct type from a given folly::dynamic
264+
*/
265+
bool tryConstruct(const folly::dynamic &value) {
266+
auto tryOne = [&]<typename TCSSValue>() -> bool {
267+
// We have to check in a runtime if the type can be constructed from the
268+
// provided folly::dynamic. The first match will be used to construct the
269+
// CSS value.
270+
if (!TCSSValue::canConstruct(value)) {
271+
return false;
272+
}
273+
storage_ = TCSSValue(value);
274+
return true;
275+
};
276+
// Try constructing with each allowed type until one succeeds
277+
return (tryOne.template operator()<AllowedTypes>() || ...);
278+
}
266279
};
267280

268281
} // namespace reanimated::css

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/InterpolatorFactory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ArrayInterpolatorFactory : public PropertyInterpolatorFactory {
7272
class TransformsInterpolatorFactory : public PropertyInterpolatorFactory {
7373
public:
7474
explicit TransformsInterpolatorFactory(
75-
const std::shared_ptr<TransformInterpolators> &interpolators)
75+
const std::shared_ptr<TransformOperationInterpolators> &interpolators)
7676
: PropertyInterpolatorFactory(), interpolators_(interpolators) {}
7777

7878
const CSSValue &getDefaultValue() const override {
@@ -105,7 +105,7 @@ class TransformsInterpolatorFactory : public PropertyInterpolatorFactory {
105105
}
106106
};
107107

108-
const std::shared_ptr<TransformInterpolators> interpolators_;
108+
const std::shared_ptr<TransformOperationInterpolators> interpolators_;
109109
};
110110

111111
// Non-template function implementations
@@ -123,12 +123,12 @@ std::shared_ptr<PropertyInterpolatorFactory> transforms(
123123
const std::unordered_map<
124124
std::string,
125125
std::shared_ptr<TransformInterpolator>> &interpolators) {
126-
TransformInterpolators result;
126+
TransformOperationInterpolators result;
127127
for (const auto &[property, interpolator] : interpolators) {
128128
result[getTransformOperationType(property)] = interpolator;
129129
}
130130
return std::make_shared<TransformsInterpolatorFactory>(
131-
std::make_shared<TransformInterpolators>(result));
131+
std::make_shared<TransformOperationInterpolators>(result));
132132
}
133133

134134
} // namespace reanimated::css

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/InterpolatorFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ auto transformOp(
114114
-> std::enable_if_t<
115115
std::is_base_of_v<TransformOperation, TOperation> &&
116116
std::is_constructible_v<TOperation, decltype(defaultValue)> &&
117-
ResolvableOperation<TOperation>,
117+
ResolvableTransformOp<TOperation>,
118118
std::shared_ptr<TransformInterpolator>> {
119119
return std::make_shared<TransformOperationInterpolator<TOperation>>(
120120
std::make_shared<TOperation>(defaultValue), std::move(config));

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/transforms/TransformInterpolator.h

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)