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_SWIPER_SWIPER_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SWIPER_SWIPER_COMPONENT_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "base/utils/macros.h"
21 #include "core/components/common/properties/swiper_indicator.h"
22 #include "core/components/declaration/swiper/swiper_declaration.h"
23 #include "core/components/swiper/swiper_controller.h"
24 #include "core/components_v2/foreach/lazy_foreach_component.h"
25 #include "core/pipeline/base/component_group.h"
26 
27 namespace OHOS::Ace {
28 
29 using SwiperChangeEndListener = std::function<void(int32_t)>;
30 using MoveCallback = std::function<void(int32_t)>;
31 
32 class ACE_EXPORT SwiperChangeEvent : public BaseEventInfo, public EventToJSONStringAdapter {
33     DECLARE_RELATIONSHIP_OF_CLASSES(SwiperChangeEvent, BaseEventInfo, EventToJSONStringAdapter);
34 
35 public:
SwiperChangeEvent(int32_t index)36     explicit SwiperChangeEvent(int32_t index) : BaseEventInfo("SwiperChangeEvent"), index_(index) {}
37     ~SwiperChangeEvent() = default;
38 
GetIndex()39     int32_t GetIndex() const
40     {
41         return index_;
42     }
43 
ToJSONString()44     std::string ToJSONString() const override
45     {
46         return std::string(R"("change",{"index":)").append(std::to_string(index_).append("},null"));
47     }
48 
49 private:
50     int32_t index_ = 0;
51 };
52 
53 class ACE_EXPORT SwiperComponent : public ComponentGroup {
54     DECLARE_ACE_TYPE(SwiperComponent, ComponentGroup);
55 
56 public:
57     explicit SwiperComponent(const std::list<RefPtr<Component>>& children);
58     SwiperComponent(const std::list<RefPtr<Component>>& children, bool showIndicator);
59     ~SwiperComponent() override = default;
60 
61     RefPtr<RenderNode> CreateRenderNode() override;
62     RefPtr<Element> CreateElement() override;
63     void AppendChild(const RefPtr<Component>& child) override;
64 
65     uint32_t GetIndex() const;
66     void SetIndex(uint32_t index);
67     void SetDuration(double duration);
68     double GetDuration() const;
69     Axis GetAxis() const;
70     void SetAxis(Axis axis);
71     bool IsLoop() const;
72     void SetLoop(bool loop);
73     bool IsAutoPlay() const;
74     void SetAutoPlay(bool autoPlay);
75     bool IsShow() const;
76     void SetShow(bool show);
77     bool IsAnimationOpacity() const;
78     void SetAnimationOpacity(bool animationOpacity);
79     const Dimension& GetItemSpace() const;
80     void SetItemSpace(const Dimension& space);
81     int32_t GetDisplayCount() const;
82     void SetDisplayCount(int32_t displayCount);
83     void SetDigitalIndicator(bool digitalIndicator);
84     bool GetDigitalIndicator() const;
85     Color GetFadeColor() const;
86     void SetFadeColor(const Color& fadeColor);
87     int32_t GetCachedSize() const;
88     void SetCachedSize(int32_t cachedSize);
89     const Dimension& GetPreviousMargin() const;
90     void SetPreviousMargin(const Dimension& margin);
91     const Dimension& GetNextMargin() const;
92     void SetNextMargin(const Dimension& margin);
93     EdgeEffect GetEdgeEffect() const;
94     void SetEdgeEffect(const EdgeEffect edgeEffect);
95     void SetAnimationFinishEventId(const EventMarker& animationFinishEventId);
96     const EventMarker& GetAnimationFinishEventId() const;
97     double GetAutoPlayInterval() const;
98     void SetAutoPlayInterval(double autoPlayInterval);
99     SwiperDisplayMode GetDisplayMode() const;
100     void SetDisplayMode(SwiperDisplayMode displayMode);
101 
102     AnimationCurve GetAnimationCurve() const;
103     void SetAnimationCurve(AnimationCurve animationCurve);
104 
105     const EventMarker& GetChangeEventId() const;
106     void SetChangeEventId(const EventMarker& changeEventId);
107     const EventMarker& GetAnimationStartEventId() const;
108     void SetAnimationStartEventId(const EventMarker& startAnimationEventId);
109     const EventMarker& GetAnimationEndEventId() const;
110     void SetAnimationEndEventId(const EventMarker& animationEndEventId);
111     const EventMarker& GetRotationEventId() const;
112     void SetRotationEventId(const EventMarker& rotationEventId);
113     const EventMarker& GetClickEventId() const;
114     void SetClickEventId(const EventMarker& clickEventId);
115     const EventMarker& GetRemoteMessageEventId() const;
116     void SetRemoteMessageEventId(const EventMarker& eventId);
117 
118     RefPtr<SwiperController> GetSwiperController() const;
119     const RefPtr<RotationController>& GetRotationController() const;
120 
121     void SetShowIndicator(bool showIndicator);
122     bool IsShowIndicator() const;
123     RefPtr<SwiperIndicator> GetIndicator() const;
124     void SetIndicator(const RefPtr<SwiperIndicator>& indicator);
125 
GetChangeEndListener()126     const SwiperChangeEndListener& GetChangeEndListener() const
127     {
128         return changeEndListener_;
129     }
130 
SetChangeEndListener(const SwiperChangeEndListener & changeEndListener)131     void SetChangeEndListener(const SwiperChangeEndListener& changeEndListener)
132     {
133         changeEndListener_ = changeEndListener;
134     }
135 
SetMoveCallback(const MoveCallback & moveCallback)136     void SetMoveCallback(const MoveCallback& moveCallback)
137     {
138         moveCallback_ = moveCallback;
139     }
140 
GetMoveCallback()141     const MoveCallback& GetMoveCallback() const
142     {
143         return moveCallback_;
144     }
145 
SetSlideContinue(bool slideContinued)146     void SetSlideContinue(bool slideContinued)
147     {
148         slideContinued_ = slideContinued;
149     }
150 
GetSlideContinue()151     bool GetSlideContinue() const
152     {
153         return slideContinued_;
154     }
155 
DisableSwipe(bool disableSwipe)156     void DisableSwipe(bool disableSwipe)
157     {
158         disableSwipe_ = disableSwipe;
159     }
160 
GetDisableSwipe()161     bool GetDisableSwipe() const
162     {
163         return disableSwipe_;
164     }
165 
SetDeclaration(const RefPtr<SwiperDeclaration> & declaration)166     void SetDeclaration(const RefPtr<SwiperDeclaration>& declaration)
167     {
168         if (declaration) {
169             declaration_ = declaration;
170         }
171     }
172 
GetDisableRotation()173     bool GetDisableRotation() const
174     {
175         return disableRation_;
176     }
177 
SetDisableRotation(bool disableRation)178     void SetDisableRotation(bool disableRation)
179     {
180         disableRation_ = disableRation;
181     }
182 
SetMainSwiperSize(MainSwiperSize mainSwiperSize)183     void SetMainSwiperSize(MainSwiperSize mainSwiperSize)
184     {
185         mainSwiperSize_ = mainSwiperSize;
186     }
187 
GetMainSwiperSize()188     MainSwiperSize GetMainSwiperSize() const
189     {
190         return mainSwiperSize_;
191     }
192 
GetLazyForEachComponent()193     RefPtr<V2::LazyForEachComponent> GetLazyForEachComponent() const
194     {
195         return lazyForEachComponent_.Upgrade();
196     }
197 
SetCurve(const RefPtr<Curve> & curve)198     void SetCurve(const RefPtr<Curve>& curve)
199     {
200         curve_ = curve;
201     }
202 
GetCurve()203     const RefPtr<Curve>& GetCurve() const
204     {
205         return curve_;
206     }
207 
208     bool operator==(const SwiperComponent& swiper) const
209     {
210         return *declaration_ == *swiper.declaration_ &&
211             show_ == swiper.show_ &&
212             slideContinued_ == swiper.slideContinued_ &&
213             disableRation_ == swiper.disableRation_ &&
214             disableSwipe_ == swiper.disableSwipe_ &&
215             mainSwiperSize_ == swiper.mainSwiperSize_ &&
216             curve_ == swiper.curve_;
217     }
218 
219 private:
220     RefPtr<SwiperDeclaration> declaration_;
221     bool show_ { true };
222     bool slideContinued_ { false };
223     bool disableRation_ { false };
224     bool disableSwipe_ { false };
225     SwiperChangeEndListener changeEndListener_;
226     MoveCallback moveCallback_;
227     MainSwiperSize mainSwiperSize_ = MainSwiperSize::AUTO;
228     WeakPtr<V2::LazyForEachComponent> lazyForEachComponent_;
229     RefPtr<Curve> curve_;
230 };
231 
232 } // namespace OHOS::Ace
233 
234 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SWIPER_SWIPER_COMPONENT_H
235