1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_STAGE_PAGE_TRANSITION_EFFECT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_STAGE_PAGE_TRANSITION_EFFECT_H 18 19 #include <memory> 20 #include <optional> 21 22 #include "core/animation/page_transition_common.h" 23 #include "core/components_ng/property/transition_property.h" 24 25 namespace OHOS::Ace::NG { 26 using PageTransitionEventFunc = std::function<void(RouteType, const float&)>; 27 28 class PageTransitionEffect : public AceType { 29 DECLARE_ACE_TYPE(PageTransitionEffect, AceType); 30 31 public: PageTransitionEffect(PageTransitionType type,const PageTransitionOption & option)32 PageTransitionEffect(PageTransitionType type, const PageTransitionOption& option) 33 : animationOption_(option), type_(type) 34 {} 35 ~PageTransitionEffect() override = default; 36 SetPageTransitionOption(const PageTransitionOption & animationOption)37 void SetPageTransitionOption(const PageTransitionOption& animationOption) 38 { 39 animationOption_ = animationOption; 40 } 41 SetTranslateEffect(const TranslateOptions & translate)42 void SetTranslateEffect(const TranslateOptions& translate) 43 { 44 translate_ = translate; 45 } 46 GetTranslateEffect()47 const std::optional<TranslateOptions>& GetTranslateEffect() const 48 { 49 return translate_; 50 } 51 SetSlideEffect(const SlideEffect & slide)52 void SetSlideEffect(const SlideEffect& slide) 53 { 54 slide_ = slide; 55 } 56 SetUserCallback(PageTransitionEventFunc && callback)57 void SetUserCallback(PageTransitionEventFunc&& callback) 58 { 59 userCallback_ = callback; 60 } 61 GetSlideEffect()62 const std::optional<SlideEffect>& GetSlideEffect() const 63 { 64 return slide_; 65 } 66 SetScaleEffect(const ScaleOptions & scale)67 void SetScaleEffect(const ScaleOptions& scale) 68 { 69 scale_ = scale; 70 } 71 GetScaleEffect()72 const std::optional<ScaleOptions>& GetScaleEffect() const 73 { 74 return scale_; 75 } 76 SetOpacityEffect(float opacity)77 void SetOpacityEffect(float opacity) 78 { 79 opacity_ = opacity; 80 } 81 GetOpacityEffect()82 const std::optional<float>& GetOpacityEffect() const 83 { 84 return opacity_; 85 } 86 SetPageTransitionRectF(const RectF & pageTransitionRectF)87 void SetPageTransitionRectF(const RectF& pageTransitionRectF) 88 { 89 pageTransitionRectF_ = pageTransitionRectF; 90 } 91 GetPageTransitionRectF()92 const std::optional<RectF>& GetPageTransitionRectF() const 93 { 94 return pageTransitionRectF_; 95 } 96 SetDefaultPageTransitionRectF(const RectF & defaultPageTransitionRectF)97 void SetDefaultPageTransitionRectF(const RectF& defaultPageTransitionRectF) 98 { 99 defaultPageTransitionRectF_ = defaultPageTransitionRectF; 100 } 101 GetDefaultPageTransitionRectF()102 const std::optional<RectF>& GetDefaultPageTransitionRectF() const 103 { 104 return defaultPageTransitionRectF_; 105 } 106 SetInitialBackgroundColor(const Color & initialBackgroundColor)107 void SetInitialBackgroundColor(const Color& initialBackgroundColor) 108 { 109 initialBackgroundColor_ = initialBackgroundColor; 110 } 111 GetInitialBackgroundColor()112 const std::optional<Color>& GetInitialBackgroundColor() const 113 { 114 return initialBackgroundColor_; 115 } 116 SetBackgroundColor(const Color & backgroundColor)117 void SetBackgroundColor(const Color& backgroundColor) 118 { 119 backgroundColor_ = backgroundColor; 120 } 121 GetBackgroundColor()122 const std::optional<Color>& GetBackgroundColor() const 123 { 124 return backgroundColor_; 125 } 126 127 // test whether this effect can match the pageTransitionType CanFit(PageTransitionType type)128 bool CanFit(PageTransitionType type) const 129 { 130 switch (type) { 131 case PageTransitionType::ENTER_PUSH: 132 return type_ == PageTransitionType::ENTER && animationOption_.routeType != RouteType::POP; 133 case PageTransitionType::ENTER_POP: 134 return type_ == PageTransitionType::ENTER && animationOption_.routeType != RouteType::PUSH; 135 case PageTransitionType::EXIT_PUSH: 136 return type_ == PageTransitionType::EXIT && animationOption_.routeType != RouteType::POP; 137 case PageTransitionType::EXIT_POP: 138 return type_ == PageTransitionType::EXIT && animationOption_.routeType != RouteType::PUSH; 139 default: 140 return false; 141 } 142 } 143 GetDuration()144 int32_t GetDuration() const 145 { 146 return animationOption_.duration; 147 } 148 GetDelay()149 int32_t GetDelay() const 150 { 151 return animationOption_.delay; 152 } 153 GetCurve()154 const RefPtr<Curve>& GetCurve() const 155 { 156 return animationOption_.curve; 157 } 158 GetUserCallback()159 const PageTransitionEventFunc& GetUserCallback() const 160 { 161 return userCallback_; 162 } 163 GetPageTransitionType()164 PageTransitionType GetPageTransitionType() const 165 { 166 return type_; 167 } 168 GetPageTransitionOption()169 PageTransitionOption GetPageTransitionOption() const 170 { 171 return animationOption_; 172 } 173 174 private: 175 std::optional<TranslateOptions> translate_; 176 std::optional<ScaleOptions> scale_; 177 std::optional<SlideEffect> slide_; 178 std::optional<float> opacity_; 179 std::optional<RectF> pageTransitionRectF_; 180 std::optional<RectF> defaultPageTransitionRectF_; 181 std::optional<Color> initialBackgroundColor_; 182 std::optional<Color> backgroundColor_; 183 PageTransitionOption animationOption_; 184 // user defined onEnter or onExit callback; 185 PageTransitionEventFunc userCallback_; 186 PageTransitionType type_ = PageTransitionType::ENTER; 187 }; 188 } // namespace OHOS::Ace::NG 189 190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_STAGE_PAGE_TRANSITION_EFFECT_H 191