1 /* 2 * Copyright (c) 2021-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_DECLARATION_SWIPER_SWIPER_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SWIPER_SWIPER_DECLARATION_H 18 19 #include "core/components/common/properties/swiper_indicator.h" 20 #include "core/components/declaration/common/declaration.h" 21 #include "core/components/swiper/swiper_controller.h" 22 #include "frameworks/core/components/common/rotation/rotation_controller.h" 23 24 namespace OHOS::Ace { 25 26 inline constexpr uint32_t DEFAULT_SWIPER_CURRENT_INDEX = 0; 27 inline constexpr double DEFAULT_SWIPER_ANIMATION_DURATION = 500.0; 28 inline constexpr double DEFAULT_SWIPER_AUTOPLAY_INTERVAL = 3000.0; 29 inline constexpr int32_t DEFAULT_SWIPER_CACHED_SIZE = -1; 30 inline constexpr int32_t DEFAULT_SWIPER_DISPLAY_COUNT = 1; 31 32 enum class SwiperDisplayMode { 33 STRETCH = 0, 34 AUTO_LINEAR, 35 }; 36 37 struct SwiperAttribute : Attribute { 38 uint32_t index = DEFAULT_SWIPER_CURRENT_INDEX; 39 double duration = DEFAULT_SWIPER_ANIMATION_DURATION; 40 Axis axis = Axis::HORIZONTAL; 41 bool loop = true; 42 bool autoPlay = false; 43 bool animationOpacity = true; 44 bool digitalIndicator = false; 45 double autoPlayInterval = DEFAULT_SWIPER_AUTOPLAY_INTERVAL; 46 int32_t cachedSize = DEFAULT_SWIPER_CACHED_SIZE; 47 EdgeEffect edgeEffect = EdgeEffect::SPRING; 48 SwiperDisplayMode displayMode = SwiperDisplayMode::STRETCH; 49 int32_t displayCount = DEFAULT_SWIPER_DISPLAY_COUNT; 50 }; 51 52 struct SwiperStyle : Style { 53 AnimationCurve animationCurve { AnimationCurve::FRICTION }; 54 Dimension itemSpace; 55 Color fadeColor = Color::GRAY; 56 Dimension previousMargin; 57 Dimension nextMargin; 58 }; 59 60 struct SwiperEvent : Event { 61 EventMarker changeEventId; 62 EventMarker rotationEventId; 63 EventMarker clickEventId; 64 EventMarker animationFinishEventId; 65 EventMarker animationStartEventId; 66 EventMarker animationEndEventId; 67 }; 68 69 struct SwiperMethod : Method {}; 70 71 class SwiperDeclaration : public Declaration { 72 DECLARE_ACE_TYPE(SwiperDeclaration, Declaration); 73 74 public: 75 SwiperDeclaration(); 76 ~SwiperDeclaration() override = default; 77 void InitializeStyle() override; GetIndex()78 uint32_t GetIndex() const 79 { 80 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 81 return attribute.index; 82 } SetIndex(uint32_t index)83 void SetIndex(uint32_t index) 84 { 85 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 86 attribute.index = index; 87 } 88 GetDuration()89 double GetDuration() const 90 { 91 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 92 return attribute.duration; 93 } SetDuration(double duration)94 void SetDuration(double duration) 95 { 96 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 97 attribute.duration = duration; 98 } 99 GetAxis()100 Axis GetAxis() const 101 { 102 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 103 return attribute.axis; 104 } SetAxis(Axis axis)105 void SetAxis(Axis axis) 106 { 107 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 108 attribute.axis = axis; 109 } 110 IsLoop()111 bool IsLoop() const 112 { 113 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 114 return attribute.loop; 115 } SetLoop(bool loop)116 void SetLoop(bool loop) 117 { 118 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 119 attribute.loop = loop; 120 } 121 GetDisplayCount()122 int32_t GetDisplayCount() const 123 { 124 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 125 return attribute.displayCount; 126 } 127 SetDisplayCount(int32_t displayCount)128 void SetDisplayCount(int32_t displayCount) 129 { 130 if (displayCount > 0) { 131 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 132 attribute.displayCount = displayCount; 133 } 134 } 135 IsAutoPlay()136 bool IsAutoPlay() const 137 { 138 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 139 return attribute.autoPlay; 140 } SetAutoPlay(bool autoPlay)141 void SetAutoPlay(bool autoPlay) 142 { 143 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 144 attribute.autoPlay = autoPlay; 145 } 146 GetAutoPlayInterval()147 double GetAutoPlayInterval() const 148 { 149 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 150 return attribute.autoPlayInterval; 151 } SetAutoPlayInterval(double autoPlayInterval)152 void SetAutoPlayInterval(double autoPlayInterval) 153 { 154 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 155 attribute.autoPlayInterval = autoPlayInterval; 156 } 157 GetCachedSize()158 int32_t GetCachedSize() const 159 { 160 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 161 return attribute.cachedSize; 162 } 163 SetCachedSize(int32_t cachedSize)164 void SetCachedSize(int32_t cachedSize) 165 { 166 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 167 attribute.cachedSize = cachedSize; 168 } 169 GetEdgeEffect()170 EdgeEffect GetEdgeEffect() const 171 { 172 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 173 return attribute.edgeEffect; 174 } 175 SetEdgeEffect(const EdgeEffect edgeEffect)176 void SetEdgeEffect(const EdgeEffect edgeEffect) 177 { 178 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 179 attribute.edgeEffect = edgeEffect; 180 } 181 IsAnimationOpacity()182 bool IsAnimationOpacity() const 183 { 184 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 185 return attribute.animationOpacity; 186 } SetAnimationOpacity(bool animationOpacity)187 void SetAnimationOpacity(bool animationOpacity) 188 { 189 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 190 attribute.animationOpacity = animationOpacity; 191 } 192 GetDigitalIndicator()193 bool GetDigitalIndicator() const 194 { 195 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 196 return attribute.digitalIndicator; 197 } SetDigitalIndicator(bool digitalIndicator)198 void SetDigitalIndicator(bool digitalIndicator) 199 { 200 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 201 attribute.digitalIndicator = digitalIndicator; 202 } 203 GetDisplayMode()204 SwiperDisplayMode GetDisplayMode() const 205 { 206 auto& attribute = static_cast<SwiperAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 207 return attribute.displayMode; 208 } 209 SetDisplayMode(SwiperDisplayMode displayMode)210 void SetDisplayMode(SwiperDisplayMode displayMode) 211 { 212 auto& attribute = MaybeResetAttribute<SwiperAttribute>(AttributeTag::SPECIALIZED_ATTR); 213 attribute.displayMode = displayMode; 214 } 215 GetAnimationCurve()216 AnimationCurve GetAnimationCurve() const 217 { 218 auto& style = static_cast<SwiperStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 219 return style.animationCurve; 220 } SetAnimationCurve(AnimationCurve animationCurve)221 void SetAnimationCurve(AnimationCurve animationCurve) 222 { 223 auto& style = MaybeResetStyle<SwiperStyle>(StyleTag::SPECIALIZED_STYLE); 224 style.animationCurve = animationCurve; 225 } 226 GetItemSpace()227 const Dimension& GetItemSpace() const 228 { 229 auto& style = static_cast<SwiperStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 230 return style.itemSpace; 231 } 232 SetItemSpace(const Dimension & space)233 void SetItemSpace(const Dimension& space) 234 { 235 auto& style = MaybeResetStyle<SwiperStyle>(StyleTag::SPECIALIZED_STYLE); 236 style.itemSpace = space; 237 } 238 GetFadeColor()239 Color GetFadeColor() const 240 { 241 auto& style = static_cast<SwiperStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 242 return style.fadeColor; 243 } 244 SetFadeColor(Color fadeColor)245 void SetFadeColor(Color fadeColor) 246 { 247 auto& style = MaybeResetStyle<SwiperStyle>(StyleTag::SPECIALIZED_STYLE); 248 style.fadeColor = fadeColor; 249 } 250 SetPreviousMargin(const Dimension & margin)251 void SetPreviousMargin(const Dimension& margin) 252 { 253 auto& style = MaybeResetStyle<SwiperStyle>(StyleTag::SPECIALIZED_STYLE); 254 style.previousMargin = margin; 255 } 256 GetPreviousMargin()257 const Dimension& GetPreviousMargin() const 258 { 259 auto& style = static_cast<SwiperStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 260 return style.previousMargin; 261 } 262 SetNextMargin(const Dimension & margin)263 void SetNextMargin(const Dimension& margin) 264 { 265 auto& style = MaybeResetStyle<SwiperStyle>(StyleTag::SPECIALIZED_STYLE); 266 style.nextMargin = margin; 267 } 268 GetNextMargin()269 const Dimension& GetNextMargin() const 270 { 271 auto& style = static_cast<SwiperStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 272 return style.nextMargin; 273 } 274 GetChangeEventId()275 const EventMarker& GetChangeEventId() const 276 { 277 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 278 return event.changeEventId; 279 } SetChangeEventId(const EventMarker & changeEventId)280 void SetChangeEventId(const EventMarker& changeEventId) 281 { 282 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 283 event.changeEventId = changeEventId; 284 } 285 GetAnimationStartEventId()286 const EventMarker& GetAnimationStartEventId() const 287 { 288 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 289 return event.animationStartEventId; 290 } SetAnimationStartEventId(const EventMarker & animationStartEventId)291 void SetAnimationStartEventId(const EventMarker& animationStartEventId) 292 { 293 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 294 event.animationStartEventId = animationStartEventId; 295 } 296 GetAnimationEndEventId()297 const EventMarker& GetAnimationEndEventId() const 298 { 299 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 300 return event.animationEndEventId; 301 } SetAnimationEndEventId(const EventMarker & animationEndEventId)302 void SetAnimationEndEventId(const EventMarker& animationEndEventId) 303 { 304 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 305 event.animationEndEventId = animationEndEventId; 306 } 307 GetRotationEventId()308 const EventMarker& GetRotationEventId() const 309 { 310 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 311 return event.rotationEventId; 312 } SetRotationEventId(const EventMarker & rotationEventId)313 void SetRotationEventId(const EventMarker& rotationEventId) 314 { 315 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 316 event.rotationEventId = rotationEventId; 317 } 318 GetClickEventId()319 const EventMarker& GetClickEventId() const 320 { 321 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 322 return event.clickEventId; 323 } 324 SetClickEventId(const EventMarker & clickEventId)325 void SetClickEventId(const EventMarker& clickEventId) 326 { 327 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 328 event.clickEventId = clickEventId; 329 } 330 GetRemoteMessageEventId()331 const EventMarker& GetRemoteMessageEventId() const 332 { 333 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_REMOTE_MESSAGE_EVENT)); 334 return event.clickEventId; 335 } 336 SetRemoteMessageEventId(const EventMarker & eventId)337 void SetRemoteMessageEventId(const EventMarker& eventId) 338 { 339 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_REMOTE_MESSAGE_EVENT); 340 event.clickEventId = eventId; 341 } 342 SetAnimationFinishEventId(const EventMarker & animationFinishEventId)343 void SetAnimationFinishEventId(const EventMarker& animationFinishEventId) 344 { 345 auto& event = MaybeResetEvent<SwiperEvent>(EventTag::SPECIALIZED_EVENT); 346 event.animationFinishEventId = animationFinishEventId; 347 } 348 GetAnimationFinishEventId()349 const EventMarker& GetAnimationFinishEventId() const 350 { 351 auto& event = static_cast<SwiperEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 352 return event.animationFinishEventId; 353 } 354 GetSwiperController()355 RefPtr<SwiperController> GetSwiperController() const 356 { 357 return swiperController_; 358 } 359 GetRotationController()360 const RefPtr<RotationController>& GetRotationController() const 361 { 362 return rotationController_; 363 } 364 IsShowIndicator()365 bool IsShowIndicator() const 366 { 367 return showIndicator_; 368 } SetShowIndicator(bool showIndicator)369 void SetShowIndicator(bool showIndicator) 370 { 371 showIndicator_ = showIndicator; 372 } GetIndicator()373 const RefPtr<SwiperIndicator>& GetIndicator() const 374 { 375 return indicator_; 376 } SetIndicator(const RefPtr<SwiperIndicator> & indicator)377 void SetIndicator(const RefPtr<SwiperIndicator>& indicator) 378 { 379 indicator_ = indicator; 380 } 381 382 bool operator==(const SwiperDeclaration& swiper) const 383 { 384 return GetDuration() == swiper.GetDuration() && 385 GetAxis() == swiper.GetAxis() && 386 IsLoop() == swiper.IsLoop() && 387 IsAutoPlay() == swiper.IsAutoPlay() && 388 GetAutoPlayInterval() == swiper.GetAutoPlayInterval() && 389 GetCachedSize() == swiper.GetCachedSize() && 390 IsAnimationOpacity() == swiper.IsAnimationOpacity() && 391 GetEdgeEffect() == swiper.GetEdgeEffect() && 392 GetDigitalIndicator() == swiper.GetDigitalIndicator() && 393 GetDisplayMode() == swiper.GetDisplayMode() && 394 GetAnimationCurve() == swiper.GetAnimationCurve() && 395 GetItemSpace() == swiper.GetItemSpace() && 396 GetFadeColor() == swiper.GetFadeColor() && 397 GetPreviousMargin() == swiper.GetPreviousMargin() && 398 GetNextMargin() == swiper.GetNextMargin() && 399 IsShowIndicator() == swiper.IsShowIndicator() && 400 GetDisplayCount() == swiper.GetDisplayCount(); 401 } 402 403 protected: 404 void InitSpecialized() override; 405 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 406 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 407 bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override; 408 void CallSpecializedMethod(const std::string& method, const std::string& args) override; 409 410 private: 411 bool showIndicator_ = true; 412 RefPtr<SwiperIndicator> indicator_; 413 RefPtr<SwiperController> swiperController_; 414 RefPtr<RotationController> rotationController_; 415 }; 416 417 } // namespace OHOS::Ace 418 419 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SWIPER_SWIPER_DECLARATION_H 420