Skip to content

Commit 1eac7a6

Browse files
committed
Add some CSS code improvements
1 parent 5099fdc commit 1eac7a6

18 files changed

+186
-192
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
@@ -144,7 +144,7 @@ template <typename... AllowedTypes>
144144
CSSValueVariant<AllowedTypes...> CSSValueVariant<AllowedTypes...>::interpolate(
145145
const double progress,
146146
const CSSValueVariant &to,
147-
const CSSValueInterpolationContext &context) const {
147+
const ValueInterpolationContext &context) const {
148148
if (storage_.index() != to.storage_.index()) {
149149
return fallbackInterpolate(
150150
progress, to, context.fallbackInterpolateThreshold);
@@ -171,7 +171,7 @@ template <typename... AllowedTypes>
171171
CSSValueVariant<AllowedTypes...> CSSValueVariant<AllowedTypes...>::interpolate(
172172
const double progress,
173173
const CSSValueVariant &to,
174-
const CSSResolvableValueInterpolationContext &context) const {
174+
const ResolvableValueInterpolationContext &context) const {
175175
if (storage_.index() != to.storage_.index()) {
176176
return fallbackInterpolate(
177177
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
@@ -109,15 +109,15 @@ class CSSValueVariant final : public CSSValue {
109109
CSSValueVariant interpolate(
110110
const double progress,
111111
const CSSValueVariant &to,
112-
const CSSValueInterpolationContext &context) const;
112+
const ValueInterpolationContext &context) const;
113113

114114
/**
115115
* Interpolate (resolvable)
116116
*/
117117
CSSValueVariant interpolate(
118118
const double progress,
119119
const CSSValueVariant &to,
120-
const CSSResolvableValueInterpolationContext &context) const;
120+
const ResolvableValueInterpolationContext &context) const;
121121

122122
private:
123123
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
@@ -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.

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)