Skip to content

Commit 08bb2d8

Browse files
authored
chore: Some CSS code improvements (#8249)
## Summary This PR contains some code extracted from the #8195 that can be added separately in order not to add too much complexity to the already large PR.
1 parent fc5e2ca commit 08bb2d8

18 files changed

+184
-193
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.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ template <typename... AllowedTypes>
103103
CSSValueVariant<AllowedTypes...> CSSValueVariant<AllowedTypes...>::interpolate(
104104
const double progress,
105105
const CSSValueVariant &to,
106-
const CSSValueInterpolationContext &context) const {
106+
const ValueInterpolationContext &context) const {
107107
if (storage_.index() != to.storage_.index()) {
108108
return fallbackInterpolate(
109109
progress, to, context.fallbackInterpolateThreshold);
@@ -130,7 +130,7 @@ template <typename... AllowedTypes>
130130
CSSValueVariant<AllowedTypes...> CSSValueVariant<AllowedTypes...>::interpolate(
131131
const double progress,
132132
const CSSValueVariant &to,
133-
const CSSResolvableValueInterpolationContext &context) const {
133+
const ResolvableValueInterpolationContext &context) const {
134134
if (storage_.index() != to.storage_.index()) {
135135
return fallbackInterpolate(
136136
progress, to, context.fallbackInterpolateThreshold);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ class CSSValueVariant final : public CSSValue {
102102
CSSValueVariant interpolate(
103103
const double progress,
104104
const CSSValueVariant &to,
105-
const CSSValueInterpolationContext &context) const;
105+
const ValueInterpolationContext &context) const;
106106

107107
/**
108108
* Interpolate (resolvable)
109109
*/
110110
CSSValueVariant interpolate(
111111
const double progress,
112112
const CSSValueVariant &to,
113-
const CSSResolvableValueInterpolationContext &context) const;
113+
const ResolvableValueInterpolationContext &context) const;
114114

115115
private:
116116
std::variant<AllowedTypes...> storage_;

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
@@ -153,7 +153,7 @@ auto transformOp(
153153
-> std::enable_if_t<
154154
std::is_base_of_v<TransformOperation, TOperation> &&
155155
std::is_constructible_v<TOperation, decltype(defaultValue)> &&
156-
ResolvableOperation<TOperation>,
156+
ResolvableTransformOp<TOperation>,
157157
std::shared_ptr<TransformInterpolator>> {
158158
return std::make_shared<TransformOperationInterpolator<TOperation>>(
159159
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.

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,58 @@
22

33
namespace reanimated::css {
44

5-
PerspectiveOperation
5+
std::shared_ptr<TransformOperation> TransformInterpolator::resolveOperation(
6+
const std::shared_ptr<TransformOperation> &operation,
7+
const UpdateContext &context) const {
8+
return operation;
9+
}
10+
11+
folly::dynamic
612
TransformOperationInterpolator<PerspectiveOperation>::interpolate(
713
double progress,
8-
const PerspectiveOperation &from,
9-
const PerspectiveOperation &to,
10-
const TransformInterpolatorUpdateContext &context) const {
11-
if (to.value.value == 0)
12-
return PerspectiveOperation(0);
13-
if (from.value.value == 0)
14-
return PerspectiveOperation(to.value);
15-
16-
return PerspectiveOperation(from.value.interpolate(progress, to.value));
14+
const std::shared_ptr<TransformOperation> &from,
15+
const std::shared_ptr<TransformOperation> &to,
16+
const TransformInterpolationContext &context) const {
17+
const auto &fromValue =
18+
std::static_pointer_cast<PerspectiveOperation>(from)->value;
19+
const auto &toValue =
20+
std::static_pointer_cast<PerspectiveOperation>(to)->value;
21+
22+
if (fromValue.value == 0)
23+
return from->toDynamic();
24+
if (toValue.value == 0)
25+
return to->toDynamic();
26+
27+
return PerspectiveOperation(fromValue.interpolate(progress, toValue))
28+
.toDynamic();
1729
}
1830

19-
MatrixOperation TransformOperationInterpolator<MatrixOperation>::interpolate(
31+
folly::dynamic TransformOperationInterpolator<MatrixOperation>::interpolate(
2032
double progress,
21-
const MatrixOperation &from,
22-
const MatrixOperation &to,
23-
const TransformInterpolatorUpdateContext &context) const {
24-
const auto fromMatrix = matrixFromOperation(from, context);
25-
const auto toMatrix = matrixFromOperation(to, context);
33+
const std::shared_ptr<TransformOperation> &from,
34+
const std::shared_ptr<TransformOperation> &to,
35+
const TransformInterpolationContext &context) const {
36+
const auto fromMatrix = matrixFromOperation(
37+
*std::static_pointer_cast<MatrixOperation>(from), context);
38+
const auto toMatrix = matrixFromOperation(
39+
*std::static_pointer_cast<MatrixOperation>(to), context);
2640
const auto decomposedFrom = fromMatrix.decompose();
2741
const auto decomposedTo = toMatrix.decompose();
2842

2943
if (!decomposedFrom.has_value() || !decomposedTo.has_value()) {
30-
return MatrixOperation(progress < 0.5 ? fromMatrix : toMatrix);
44+
return MatrixOperation(progress < 0.5 ? fromMatrix : toMatrix).toDynamic();
3145
}
3246

33-
return MatrixOperation(TransformMatrix3D::recompose(
34-
decomposedFrom->interpolate(progress, decomposedTo.value())));
47+
return MatrixOperation(
48+
TransformMatrix3D::recompose(
49+
decomposedFrom->interpolate(progress, decomposedTo.value())))
50+
.toDynamic();
3551
}
3652

3753
TransformMatrix3D
3854
TransformOperationInterpolator<MatrixOperation>::matrixFromOperation(
3955
const MatrixOperation &matrixOperation,
40-
const TransformInterpolatorUpdateContext &context) const {
56+
const TransformInterpolationContext &context) const {
4157
if (std::holds_alternative<TransformOperations>(matrixOperation.value)) {
4258
const auto &operations =
4359
std::get<TransformOperations>(matrixOperation.value);

0 commit comments

Comments
 (0)