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