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