From a3343dfca3ccad5f453eb846a395837503ff72e5 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 9 Sep 2021 11:55:31 -0700 Subject: [PATCH] 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 --- .../LayoutAnimationKeyFrameManager.cpp | 196 +--------------- .../react/renderer/animations/conversions.h | 212 ++++++++++++++++++ 2 files changed, 214 insertions(+), 194 deletions(-) create mode 100644 ReactCommon/react/renderer/animations/conversions.h diff --git a/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.cpp b/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.cpp index ca16b6a9c93..ab18a479a8a 100644 --- a/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.cpp +++ b/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -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 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 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 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{ - *animationType, - animationProperty, - duration, - delay, - springDamping, - initialVelocity}); -} - -// Parse animation config from JS -static better::optional 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{}) - : parseAnimationConfig(createConfigIt->second, duration, true); - - const auto updateConfigIt = config.find("update"); - const auto updateConfig = updateConfigIt == config.items().end() - ? better::optional(AnimationConfig{}) - : parseAnimationConfig(updateConfigIt->second, duration, false); - - const auto deleteConfigIt = config.find("delete"); - const auto deleteConfig = deleteConfigIt == config.items().end() - ? better::optional(AnimationConfig{}) - : parseAnimationConfig(deleteConfigIt->second, duration, true); - - if (!createConfig || !updateConfig || !deleteConfig) { - return {}; - } - - return better::optional(LayoutAnimationConfig{ - duration, *createConfig, *updateConfig, *deleteConfig}); -} - static inline float interpolateFloats(float coefficient, float oldValue, float newValue) { return oldValue + (newValue - oldValue) * coefficient; diff --git a/ReactCommon/react/renderer/animations/conversions.h b/ReactCommon/react/renderer/animations/conversions.h new file mode 100644 index 00000000000..9fdb7a5e08a --- /dev/null +++ b/ReactCommon/react/renderer/animations/conversions.h @@ -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 +#include + +#include + +namespace facebook { +namespace react { + +static inline better::optional 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 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 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{ + *animationType, + animationProperty, + duration, + delay, + springDamping, + initialVelocity}); +} + +// Parse animation config from JS +static inline better::optional +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{}) + : parseAnimationConfig(createConfigIt->second, duration, true); + + const auto updateConfigIt = config.find("update"); + const auto updateConfig = updateConfigIt == config.items().end() + ? better::optional(AnimationConfig{}) + : parseAnimationConfig(updateConfigIt->second, duration, false); + + const auto deleteConfigIt = config.find("delete"); + const auto deleteConfig = deleteConfigIt == config.items().end() + ? better::optional(AnimationConfig{}) + : parseAnimationConfig(deleteConfigIt->second, duration, true); + + if (!createConfig || !updateConfig || !deleteConfig) { + return {}; + } + + return LayoutAnimationConfig{ + duration, *createConfig, *updateConfig, *deleteConfig}; +} + +} // namespace react +} // namespace facebook