mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Move conversion functions to separate file
Summary: changelog: [internal] Just move conversion functions to separate file Reviewed By: philIip Differential Revision: D30732128 fbshipit-source-id: 092523de949ee0088b1317c887aca91913991647
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ae4068d63c
commit
a3343dfca3
@@ -12,6 +12,7 @@
|
||||
#include <react/debug/flags.h>
|
||||
#include <react/debug/react_native_assert.h>
|
||||
|
||||
#include <react/renderer/animations/conversions.h>
|
||||
#include <react/renderer/componentregistry/ComponentDescriptorFactory.h>
|
||||
#include <react/renderer/components/view/ViewProps.h>
|
||||
#include <react/renderer/core/ComponentDescriptor.h>
|
||||
@@ -69,6 +70,7 @@ void PrintMutationInstruction(
|
||||
return stream;
|
||||
}(LOG(ERROR));
|
||||
}
|
||||
|
||||
void PrintMutationInstructionRelative(
|
||||
std::string message,
|
||||
ShadowViewMutation const &mutation,
|
||||
@@ -80,200 +82,6 @@ void PrintMutationInstructionRelative(
|
||||
}
|
||||
#endif
|
||||
|
||||
static better::optional<AnimationType> parseAnimationType(std::string param) {
|
||||
if (param == "spring") {
|
||||
return AnimationType::Spring;
|
||||
}
|
||||
if (param == "linear") {
|
||||
return AnimationType::Linear;
|
||||
}
|
||||
if (param == "easeInEaseOut") {
|
||||
return AnimationType::EaseInEaseOut;
|
||||
}
|
||||
if (param == "easeIn") {
|
||||
return AnimationType::EaseIn;
|
||||
}
|
||||
if (param == "easeOut") {
|
||||
return AnimationType::EaseOut;
|
||||
}
|
||||
if (param == "keyboard") {
|
||||
return AnimationType::Keyboard;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "Error parsing animation type: " << param;
|
||||
return {};
|
||||
}
|
||||
|
||||
static better::optional<AnimationProperty> parseAnimationProperty(
|
||||
std::string param) {
|
||||
if (param == "opacity") {
|
||||
return AnimationProperty::Opacity;
|
||||
}
|
||||
if (param == "scaleX") {
|
||||
return AnimationProperty::ScaleX;
|
||||
}
|
||||
if (param == "scaleY") {
|
||||
return AnimationProperty::ScaleY;
|
||||
}
|
||||
if (param == "scaleXY") {
|
||||
return AnimationProperty::ScaleXY;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "Error parsing animation property: " << param;
|
||||
return {};
|
||||
}
|
||||
|
||||
static better::optional<AnimationConfig> parseAnimationConfig(
|
||||
folly::dynamic const &config,
|
||||
double defaultDuration,
|
||||
bool parsePropertyType) {
|
||||
if (config.empty() || !config.isObject()) {
|
||||
return AnimationConfig{
|
||||
AnimationType::Linear,
|
||||
AnimationProperty::NotApplicable,
|
||||
defaultDuration,
|
||||
0,
|
||||
0,
|
||||
0};
|
||||
}
|
||||
|
||||
auto const typeIt = config.find("type");
|
||||
if (typeIt == config.items().end()) {
|
||||
LOG(ERROR) << "Error parsing animation config: could not find field `type`";
|
||||
return {};
|
||||
}
|
||||
auto const animationTypeParam = typeIt->second;
|
||||
if (animationTypeParam.empty() || !animationTypeParam.isString()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not unwrap field `type`";
|
||||
return {};
|
||||
}
|
||||
const auto animationType = parseAnimationType(animationTypeParam.asString());
|
||||
if (!animationType) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not parse field `type`";
|
||||
return {};
|
||||
}
|
||||
|
||||
AnimationProperty animationProperty = AnimationProperty::NotApplicable;
|
||||
if (parsePropertyType) {
|
||||
auto const propertyIt = config.find("property");
|
||||
if (propertyIt == config.items().end()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not find field `property`";
|
||||
return {};
|
||||
}
|
||||
auto const animationPropertyParam = propertyIt->second;
|
||||
if (animationPropertyParam.empty() || !animationPropertyParam.isString()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not unwrap field `property`";
|
||||
return {};
|
||||
}
|
||||
const auto animationPropertyParsed =
|
||||
parseAnimationProperty(animationPropertyParam.asString());
|
||||
if (!animationPropertyParsed) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not parse field `property`";
|
||||
return {};
|
||||
}
|
||||
animationProperty = *animationPropertyParsed;
|
||||
}
|
||||
|
||||
double duration = defaultDuration;
|
||||
double delay = 0;
|
||||
double springDamping = 0.5;
|
||||
double initialVelocity = 0;
|
||||
|
||||
auto const durationIt = config.find("duration");
|
||||
if (durationIt != config.items().end()) {
|
||||
if (durationIt->second.isDouble()) {
|
||||
duration = durationIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `duration` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const delayIt = config.find("delay");
|
||||
if (delayIt != config.items().end()) {
|
||||
if (delayIt->second.isDouble()) {
|
||||
delay = delayIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `delay` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const springDampingIt = config.find("springDamping");
|
||||
if (springDampingIt != config.items().end() &&
|
||||
springDampingIt->second.isDouble()) {
|
||||
if (springDampingIt->second.isDouble()) {
|
||||
springDamping = springDampingIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `springDamping` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const initialVelocityIt = config.find("initialVelocity");
|
||||
if (initialVelocityIt != config.items().end()) {
|
||||
if (initialVelocityIt->second.isDouble()) {
|
||||
initialVelocity = initialVelocityIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `initialVelocity` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return better::optional<AnimationConfig>(AnimationConfig{
|
||||
*animationType,
|
||||
animationProperty,
|
||||
duration,
|
||||
delay,
|
||||
springDamping,
|
||||
initialVelocity});
|
||||
}
|
||||
|
||||
// Parse animation config from JS
|
||||
static better::optional<LayoutAnimationConfig> parseLayoutAnimationConfig(
|
||||
folly::dynamic const &config) {
|
||||
if (config.empty() || !config.isObject()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto durationIt = config.find("duration");
|
||||
if (durationIt == config.items().end() || !durationIt->second.isDouble()) {
|
||||
return {};
|
||||
}
|
||||
const double duration = durationIt->second.asDouble();
|
||||
|
||||
const auto createConfigIt = config.find("create");
|
||||
const auto createConfig = createConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(createConfigIt->second, duration, true);
|
||||
|
||||
const auto updateConfigIt = config.find("update");
|
||||
const auto updateConfig = updateConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(updateConfigIt->second, duration, false);
|
||||
|
||||
const auto deleteConfigIt = config.find("delete");
|
||||
const auto deleteConfig = deleteConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(deleteConfigIt->second, duration, true);
|
||||
|
||||
if (!createConfig || !updateConfig || !deleteConfig) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return better::optional<LayoutAnimationConfig>(LayoutAnimationConfig{
|
||||
duration, *createConfig, *updateConfig, *deleteConfig});
|
||||
}
|
||||
|
||||
static inline float
|
||||
interpolateFloats(float coefficient, float oldValue, float newValue) {
|
||||
return oldValue + (newValue - oldValue) * coefficient;
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <better/optional.h>
|
||||
#include <react/renderer/animations/primitives.h>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
static inline better::optional<AnimationType> parseAnimationType(
|
||||
std::string param) {
|
||||
if (param == "spring") {
|
||||
return AnimationType::Spring;
|
||||
}
|
||||
if (param == "linear") {
|
||||
return AnimationType::Linear;
|
||||
}
|
||||
if (param == "easeInEaseOut") {
|
||||
return AnimationType::EaseInEaseOut;
|
||||
}
|
||||
if (param == "easeIn") {
|
||||
return AnimationType::EaseIn;
|
||||
}
|
||||
if (param == "easeOut") {
|
||||
return AnimationType::EaseOut;
|
||||
}
|
||||
if (param == "keyboard") {
|
||||
return AnimationType::Keyboard;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "Error parsing animation type: " << param;
|
||||
return {};
|
||||
}
|
||||
|
||||
static inline better::optional<AnimationProperty> parseAnimationProperty(
|
||||
std::string param) {
|
||||
if (param == "opacity") {
|
||||
return AnimationProperty::Opacity;
|
||||
}
|
||||
if (param == "scaleX") {
|
||||
return AnimationProperty::ScaleX;
|
||||
}
|
||||
if (param == "scaleY") {
|
||||
return AnimationProperty::ScaleY;
|
||||
}
|
||||
if (param == "scaleXY") {
|
||||
return AnimationProperty::ScaleXY;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "Error parsing animation property: " << param;
|
||||
return {};
|
||||
}
|
||||
|
||||
static inline better::optional<AnimationConfig> parseAnimationConfig(
|
||||
folly::dynamic const &config,
|
||||
double defaultDuration,
|
||||
bool parsePropertyType) {
|
||||
if (config.empty() || !config.isObject()) {
|
||||
return AnimationConfig{
|
||||
AnimationType::Linear,
|
||||
AnimationProperty::NotApplicable,
|
||||
defaultDuration,
|
||||
0,
|
||||
0,
|
||||
0};
|
||||
}
|
||||
|
||||
auto const typeIt = config.find("type");
|
||||
if (typeIt == config.items().end()) {
|
||||
LOG(ERROR) << "Error parsing animation config: could not find field `type`";
|
||||
return {};
|
||||
}
|
||||
auto const animationTypeParam = typeIt->second;
|
||||
if (animationTypeParam.empty() || !animationTypeParam.isString()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not unwrap field `type`";
|
||||
return {};
|
||||
}
|
||||
const auto animationType = parseAnimationType(animationTypeParam.asString());
|
||||
if (!animationType) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not parse field `type`";
|
||||
return {};
|
||||
}
|
||||
|
||||
AnimationProperty animationProperty = AnimationProperty::NotApplicable;
|
||||
if (parsePropertyType) {
|
||||
auto const propertyIt = config.find("property");
|
||||
if (propertyIt == config.items().end()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not find field `property`";
|
||||
return {};
|
||||
}
|
||||
auto const animationPropertyParam = propertyIt->second;
|
||||
if (animationPropertyParam.empty() || !animationPropertyParam.isString()) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not unwrap field `property`";
|
||||
return {};
|
||||
}
|
||||
const auto animationPropertyParsed =
|
||||
parseAnimationProperty(animationPropertyParam.asString());
|
||||
if (!animationPropertyParsed) {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: could not parse field `property`";
|
||||
return {};
|
||||
}
|
||||
animationProperty = *animationPropertyParsed;
|
||||
}
|
||||
|
||||
double duration = defaultDuration;
|
||||
double delay = 0;
|
||||
double springDamping = 0.5;
|
||||
double initialVelocity = 0;
|
||||
|
||||
auto const durationIt = config.find("duration");
|
||||
if (durationIt != config.items().end()) {
|
||||
if (durationIt->second.isDouble()) {
|
||||
duration = durationIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `duration` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const delayIt = config.find("delay");
|
||||
if (delayIt != config.items().end()) {
|
||||
if (delayIt->second.isDouble()) {
|
||||
delay = delayIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `delay` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const springDampingIt = config.find("springDamping");
|
||||
if (springDampingIt != config.items().end() &&
|
||||
springDampingIt->second.isDouble()) {
|
||||
if (springDampingIt->second.isDouble()) {
|
||||
springDamping = springDampingIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `springDamping` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto const initialVelocityIt = config.find("initialVelocity");
|
||||
if (initialVelocityIt != config.items().end()) {
|
||||
if (initialVelocityIt->second.isDouble()) {
|
||||
initialVelocity = initialVelocityIt->second.asDouble();
|
||||
} else {
|
||||
LOG(ERROR)
|
||||
<< "Error parsing animation config: field `initialVelocity` must be a number";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return better::optional<AnimationConfig>(AnimationConfig{
|
||||
*animationType,
|
||||
animationProperty,
|
||||
duration,
|
||||
delay,
|
||||
springDamping,
|
||||
initialVelocity});
|
||||
}
|
||||
|
||||
// Parse animation config from JS
|
||||
static inline better::optional<LayoutAnimationConfig>
|
||||
parseLayoutAnimationConfig(folly::dynamic const &config) {
|
||||
if (config.empty() || !config.isObject()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto durationIt = config.find("duration");
|
||||
if (durationIt == config.items().end() || !durationIt->second.isDouble()) {
|
||||
return {};
|
||||
}
|
||||
const double duration = durationIt->second.asDouble();
|
||||
|
||||
const auto createConfigIt = config.find("create");
|
||||
const auto createConfig = createConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(createConfigIt->second, duration, true);
|
||||
|
||||
const auto updateConfigIt = config.find("update");
|
||||
const auto updateConfig = updateConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(updateConfigIt->second, duration, false);
|
||||
|
||||
const auto deleteConfigIt = config.find("delete");
|
||||
const auto deleteConfig = deleteConfigIt == config.items().end()
|
||||
? better::optional<AnimationConfig>(AnimationConfig{})
|
||||
: parseAnimationConfig(deleteConfigIt->second, duration, true);
|
||||
|
||||
if (!createConfig || !updateConfig || !deleteConfig) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return LayoutAnimationConfig{
|
||||
duration, *createConfig, *updateConfig, *deleteConfig};
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user