1 /* 2 * Copyright (c) 2025 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 OHOS_ROSEN_WM_ANIMATION_COMMON_H 17 #define OHOS_ROSEN_WM_ANIMATION_COMMON_H 18 19 #include <iomanip> 20 #include <memory> 21 #include <sstream> 22 #include <string> 23 24 #include <parcel.h> 25 26 namespace OHOS { 27 namespace Rosen { 28 /** 29 * @brief Enumerates window transition type. 30 */ 31 enum class WindowTransitionType : uint32_t { 32 /** 33 * window destroy. 34 */ 35 DESTROY = 0, 36 37 /** 38 * end type. 39 */ 40 END, 41 }; 42 43 /** 44 * @brief Enumerates window animation curve type. 45 */ 46 enum class WindowAnimationCurve : uint32_t { 47 /** 48 * animation curve type linear. 49 */ 50 LINEAR = 0, 51 52 /** 53 * animation curve type interpolation_spring. 54 */ 55 INTERPOLATION_SPRING = 1, 56 57 /** 58 * animation curve type cubic bezier curve. 59 */ 60 CUBIC_BEZIER = 2, 61 }; 62 63 /** 64 * Describes the window animation type 65 */ 66 enum class AnimationType : uint32_t { 67 /** 68 * window animation type fade in out 69 */ 70 FADE_IN_OUT = 0, 71 /** 72 * window animation type fade in 73 */ 74 FADE_IN = 1, 75 /** 76 * end type 77 */ 78 END, 79 }; 80 81 const uint32_t ANIMATION_PARAM_SIZE = 4; 82 const uint32_t ANIMATION_MAX_DURATION = 3000; 83 const uint32_t FLOAT_VALUE_LENGTH = 2; 84 85 struct WindowAnimationProperty : public Parcelable { 86 float targetScale = 0.0f; 87 MarshallingWindowAnimationProperty88 bool Marshalling(Parcel& parcel) const override 89 { 90 if (!parcel.WriteFloat(targetScale)) { 91 return false; 92 } 93 return true; 94 } 95 UnmarshallingWindowAnimationProperty96 static WindowAnimationProperty* Unmarshalling(Parcel& parcel) 97 { 98 WindowAnimationProperty* animationProperty = new WindowAnimationProperty(); 99 if (!parcel.ReadFloat(animationProperty->targetScale)) { 100 delete animationProperty; 101 return nullptr; 102 } 103 return animationProperty; 104 } 105 }; 106 107 /** 108 * @brief Window transition animation configuration. 109 */ 110 struct WindowAnimationOption : public Parcelable { 111 WindowAnimationCurve curve = WindowAnimationCurve::LINEAR; 112 uint32_t duration = 0; 113 std::array<float, ANIMATION_PARAM_SIZE> params; 114 MarshallingWindowAnimationOption115 bool Marshalling(Parcel& parcel) const override 116 { 117 if (!(parcel.WriteUint32(static_cast<uint32_t>(curve)) && parcel.WriteUint32(duration))) { 118 return false; 119 } 120 if (params.size() > ANIMATION_PARAM_SIZE) { 121 return false; 122 } 123 for (const auto& param: params) { 124 if (!parcel.WriteFloat(param)) { 125 return false; 126 } 127 } 128 return true; 129 } 130 UnmarshallingWindowAnimationOption131 static WindowAnimationOption* Unmarshalling(Parcel& parcel) 132 { 133 WindowAnimationOption* windowAnimationConfig = new WindowAnimationOption(); 134 uint32_t curve = 0; 135 if (!parcel.ReadUint32(curve)) { 136 delete windowAnimationConfig; 137 return nullptr; 138 } 139 windowAnimationConfig->curve = static_cast<WindowAnimationCurve>(curve); 140 if (!parcel.ReadUint32(windowAnimationConfig->duration)) { 141 delete windowAnimationConfig; 142 return nullptr; 143 } 144 if (windowAnimationConfig->params.size() > ANIMATION_PARAM_SIZE) { 145 delete windowAnimationConfig; 146 return nullptr; 147 } 148 for (auto& param: windowAnimationConfig->params) { 149 if (!parcel.ReadFloat(param)) { 150 delete windowAnimationConfig; 151 return nullptr; 152 } 153 } 154 return windowAnimationConfig; 155 } 156 ToStringWindowAnimationOption157 std::string ToString() const 158 { 159 std::ostringstream oss; 160 oss << "curve: " << std::to_string(static_cast<int32_t>(curve)) << ", duration: " << \ 161 std::to_string(static_cast<int32_t>(duration)) << ", params: [ "; 162 for (auto param: params) { 163 oss << std::fixed << std::setprecision(FLOAT_VALUE_LENGTH) << param << ", "; 164 } 165 oss << "]"; 166 return oss.str(); 167 } 168 }; 169 170 /** 171 * The animation configuration of start scene animation 172 */ 173 struct StartAnimationOptions : public Parcelable { 174 /** 175 * The type of window animation 176 */ 177 AnimationType animationType = AnimationType::FADE_IN_OUT; 178 MarshallingStartAnimationOptions179 bool Marshalling(Parcel& parcel) const override 180 { 181 if (!parcel.WriteUint32(static_cast<uint32_t>(animationType))) { 182 return false; 183 } 184 return true; 185 } 186 UnmarshallingStartAnimationOptions187 static StartAnimationOptions* Unmarshalling(Parcel& parcel) 188 { 189 StartAnimationOptions* startAnimationOptions = new StartAnimationOptions(); 190 uint32_t animationType = 0; 191 if (!parcel.ReadUint32(animationType)) { 192 delete startAnimationOptions; 193 return nullptr; 194 } 195 startAnimationOptions->animationType = static_cast<AnimationType>(animationType); 196 return startAnimationOptions; 197 } 198 }; 199 200 /** 201 * The animation configuration of SA start scene animation 202 */ 203 struct StartAnimationSystemOptions : public Parcelable { 204 /** 205 * The type of window animation 206 */ 207 AnimationType animationType = AnimationType::FADE_IN_OUT; 208 /** 209 * The config of start secne animation 210 */ 211 std::shared_ptr<WindowAnimationOption> animationConfig = nullptr; 212 MarshallingStartAnimationSystemOptions213 bool Marshalling(Parcel& parcel) const override 214 { 215 if (!parcel.WriteUint32(static_cast<uint32_t>(animationType))) { 216 return false; 217 } 218 if (!parcel.WriteParcelable(animationConfig.get())) { 219 return false; 220 } 221 return true; 222 } 223 UnmarshallingStartAnimationSystemOptions224 static StartAnimationSystemOptions* Unmarshalling(Parcel& parcel) 225 { 226 StartAnimationSystemOptions* startAnimationSystemOptions = new StartAnimationSystemOptions(); 227 uint32_t animationType = 0; 228 if (!parcel.ReadUint32(animationType)) { 229 delete startAnimationSystemOptions; 230 return nullptr; 231 } 232 startAnimationSystemOptions->animationType = static_cast<AnimationType>(animationType); 233 startAnimationSystemOptions->animationConfig = 234 std::shared_ptr<WindowAnimationOption>(parcel.ReadParcelable<WindowAnimationOption>()); 235 return startAnimationSystemOptions; 236 } 237 }; 238 239 /** 240 * The animation params of window create 241 */ 242 struct WindowCreateParams : public Parcelable { 243 /** 244 * The animation configuration of start scene animation 245 */ 246 std::shared_ptr<StartAnimationOptions> animationParams = nullptr; 247 /** 248 * The animation configuration of SA start scene animation 249 */ 250 std::shared_ptr<StartAnimationSystemOptions> animationSystemParams = nullptr; 251 MarshallingWindowCreateParams252 bool Marshalling(Parcel& parcel) const override 253 { 254 if (!parcel.WriteParcelable(animationParams.get())) { 255 return false; 256 } 257 if (!parcel.WriteParcelable(animationSystemParams.get())) { 258 return false; 259 } 260 return true; 261 } 262 UnmarshallingWindowCreateParams263 static WindowCreateParams* Unmarshalling(Parcel& parcel) 264 { 265 WindowCreateParams* windowCreateParams = new WindowCreateParams(); 266 windowCreateParams->animationParams = 267 std::shared_ptr<StartAnimationOptions>(parcel.ReadParcelable<StartAnimationOptions>()); 268 windowCreateParams->animationSystemParams = 269 std::shared_ptr<StartAnimationSystemOptions>(parcel.ReadParcelable<StartAnimationSystemOptions>()); 270 return windowCreateParams; 271 } 272 }; 273 274 /** 275 * @brief Transition animation configuration. 276 */ 277 struct TransitionAnimation : public Parcelable { 278 WindowAnimationOption config; 279 float opacity = 1.0f; 280 MarshallingTransitionAnimation281 bool Marshalling(Parcel& parcel) const override 282 { 283 if (!(parcel.WriteFloat(opacity) && parcel.WriteParcelable(&config))) { 284 return false; 285 } 286 return true; 287 } 288 UnmarshallingTransitionAnimation289 static TransitionAnimation* Unmarshalling(Parcel& parcel) 290 { 291 TransitionAnimation* transitionAnimation = new TransitionAnimation(); 292 if (!parcel.ReadFloat(transitionAnimation->opacity)) { 293 delete transitionAnimation; 294 return nullptr; 295 } 296 std::shared_ptr<WindowAnimationOption> animationConfig = 297 std::shared_ptr<WindowAnimationOption>(parcel.ReadParcelable<WindowAnimationOption>()); 298 if (animationConfig == nullptr) { 299 delete transitionAnimation; 300 return nullptr; 301 } 302 transitionAnimation->config = *animationConfig; 303 return transitionAnimation; 304 } 305 }; 306 } 307 } 308 #endif // OHOS_ROSEN_WM_ANIMATION_COMMON_H 309