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_REFRESH_REFRESH_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_REFRESH_COMPONENT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components_v2/common/common_def.h"
21 #include "core/pipeline/base/sole_child_component.h"
22 
23 namespace OHOS::Ace {
24 
25 inline constexpr double DEFAULT_INDICATOR_OFFSET = 16.0;
26 inline constexpr int32_t MIN_FRICTION_RATIO = 0;
27 inline constexpr int32_t MAX_FRICTION_RATIO = 100;
28 #ifdef WEARABLE_PRODUCT
29 inline constexpr int32_t DEFAULT_FRICTION_RATIO = 62;
30 #else
31 inline constexpr int32_t DEFAULT_FRICTION_RATIO = 42;
32 #endif
33 
34 class ACE_EXPORT RefreshComponent : public SoleChildComponent {
35     DECLARE_ACE_TYPE(RefreshComponent, SoleChildComponent);
36 
37 public:
RefreshComponent()38     RefreshComponent() : SoleChildComponent() {}
39 
40     ~RefreshComponent() override = default;
41 
42     RefPtr<Element> CreateElement() override;
43 
44     RefPtr<RenderNode> CreateRenderNode() override;
45 
GetIndicatorOffset()46     const Dimension& GetIndicatorOffset() const
47     {
48         return indicatorOffset_;
49     }
50 
51     ACE_DEFINE_COMPONENT_EVENT(OnStateChange, void(int));
52 
53     ACE_DEFINE_COMPONENT_EVENT(OnRefreshing, void());
54 
55     ACE_DEFINE_COMPONENT_EVENT(OnOffsetChange, void(float));
56 
SetIndicatorOffset(const Dimension & indicatorOffset)57     void SetIndicatorOffset(const Dimension& indicatorOffset)
58     {
59         if (GreatOrEqual(indicatorOffset.Value(), 0.0)) {
60             indicatorOffset_ = indicatorOffset;
61         }
62     }
63 
IsRefreshing()64     bool IsRefreshing() const
65     {
66         return refreshing_;
67     }
68 
IsRefresh(bool isRefresh)69     void IsRefresh(bool isRefresh)
70     {
71         isRefresh_ = isRefresh;
72     }
73 
GetIsRefresh()74     bool GetIsRefresh() const
75     {
76         return isRefresh_;
77     }
78 
SetRefreshing(bool isRefreshing)79     void SetRefreshing(bool isRefreshing)
80     {
81         refreshing_ = isRefreshing;
82     }
83 
IsUseOffset()84     bool IsUseOffset() const
85     {
86         return useOffset_;
87     }
88 
SetUseOffset(bool isUseOffset)89     void SetUseOffset(bool isUseOffset)
90     {
91         useOffset_ = isUseOffset;
92     }
93 
GetRefreshType()94     RefreshType GetRefreshType() const
95     {
96         return refreshType_;
97     }
98 
SetRefreshType(RefreshType refreshType)99     void SetRefreshType(RefreshType refreshType)
100     {
101         refreshType_ = refreshType;
102     }
103 
IsShowLastTime()104     bool IsShowLastTime() const
105     {
106         return showLastTime_;
107     }
108 
SetShowLastTime(bool isShowLastTime)109     void SetShowLastTime(bool isShowLastTime)
110     {
111         showLastTime_ = isShowLastTime;
112     }
113 
SetRefreshEventId(const EventMarker & refreshEventId)114     void SetRefreshEventId(const EventMarker& refreshEventId)
115     {
116         refreshEventId_ = refreshEventId;
117     }
118 
SetTimeOffset(const Dimension & timeOffset)119     void SetTimeOffset(const Dimension& timeOffset)
120     {
121         timeOffset_ = timeOffset;
122     }
123 
GetTimeOffset()124     const Dimension& GetTimeOffset() const
125     {
126         return timeOffset_;
127     }
128 
GetRefreshEventId()129     const EventMarker& GetRefreshEventId() const
130     {
131         return refreshEventId_;
132     }
133 
SetPulldownEventId(const EventMarker & pulldownEventId)134     void SetPulldownEventId(const EventMarker& pulldownEventId)
135     {
136         pullDownEventId_ = pulldownEventId;
137     }
138 
GetPulldownEventId()139     const EventMarker& GetPulldownEventId() const
140     {
141         return pullDownEventId_;
142     }
143 
GetBackgroundColor()144     const Color& GetBackgroundColor() const
145     {
146         return backgroundColor_;
147     }
148 
SetBackgroundColor(const Color & backgroundColor)149     void SetBackgroundColor(const Color& backgroundColor)
150     {
151         backgroundColor_ = backgroundColor;
152     }
153 
GetProgressColor()154     const Color& GetProgressColor() const
155     {
156         return progressColor_;
157     }
158 
SetProgressColor(const Color & progressColor)159     void SetProgressColor(const Color& progressColor)
160     {
161         progressColor_ = progressColor;
162     }
163 
GetLoadingDistance()164     const Dimension& GetLoadingDistance() const
165     {
166         return loadingDistance_;
167     }
168 
SetLoadingDistance(const Dimension & loadingDistance)169     void SetLoadingDistance(const Dimension& loadingDistance)
170     {
171         loadingDistance_ = loadingDistance;
172     }
173 
GetRefreshDistance()174     const Dimension& GetRefreshDistance() const
175     {
176         return refreshDistance_;
177     }
178 
SetRefreshDistance(const Dimension & refreshDistance)179     void SetRefreshDistance(const Dimension& refreshDistance)
180     {
181         if (GreatOrEqual(refreshDistance.Value(), 0.0)) {
182             refreshDistance_ = refreshDistance;
183         } else {
184             refreshDistance_ = Dimension(0.0, DimensionUnit::VP);
185         }
186     }
187 
GetProgressDistance()188     const Dimension& GetProgressDistance() const
189     {
190         return progressDistance_;
191     }
192 
SetProgressDistance(const Dimension & progressDistance)193     void SetProgressDistance(const Dimension& progressDistance)
194     {
195         progressDistance_ = progressDistance;
196     }
197 
GetMaxDistance()198     const Dimension& GetMaxDistance() const
199     {
200         return maxDistance_;
201     }
202 
SetMaxDistance(const Dimension & maxDistance)203     void SetMaxDistance(const Dimension& maxDistance)
204     {
205         maxDistance_ = maxDistance;
206     }
207 
GetProgressDiameter()208     const Dimension& GetProgressDiameter() const
209     {
210         return progressDiameter_;
211     }
212 
SetProgressDiameter(const Dimension & progressDiameter)213     void SetProgressDiameter(const Dimension& progressDiameter)
214     {
215         progressDiameter_ = progressDiameter;
216     }
217 
GetShowTimeDistance()218     const Dimension& GetShowTimeDistance() const
219     {
220         return showTimeDistance_;
221     }
222 
SetShowTimeDistance(const Dimension & showTimeDistance)223     void SetShowTimeDistance(const Dimension& showTimeDistance)
224     {
225         showTimeDistance_ = showTimeDistance;
226     }
227 
SetFriction(int32_t friction)228     void SetFriction(int32_t friction)
229     {
230         if (friction >= MIN_FRICTION_RATIO && friction <= MAX_FRICTION_RATIO) {
231             friction_ = friction;
232         } else if (friction < MIN_FRICTION_RATIO) {
233             friction_ = MIN_FRICTION_RATIO;
234         } else {
235             friction_ = MAX_FRICTION_RATIO;
236         }
237     }
238 
GetFriction()239     int32_t GetFriction() const
240     {
241         return friction_;
242     }
243 
GetTextStyle()244     const TextStyle& GetTextStyle() const
245     {
246         return textStyle_;
247     }
248 
SetTextStyle(const TextStyle & textStyle)249     void SetTextStyle(const TextStyle& textStyle)
250     {
251         textStyle_ = textStyle;
252     }
253 
GetChangeEvent()254     const EventMarker& GetChangeEvent() const
255     {
256         return changeEvent_;
257     }
258 
SetChangeEvent(const EventMarker & changeEvent)259     void SetChangeEvent(const EventMarker& changeEvent)
260     {
261         changeEvent_ = changeEvent;
262     }
263 
264 private:
265     Dimension indicatorOffset_ = Dimension(DEFAULT_INDICATOR_OFFSET, DimensionUnit::VP);
266     Dimension loadingDistance_;
267     Dimension progressDistance_;
268     Dimension showTimeDistance_;
269     Dimension refreshDistance_;
270     Dimension maxDistance_;
271     Dimension progressDiameter_;
272     Dimension timeOffset_;
273 
274     bool useOffset_ = false;
275     bool showLastTime_ = false;
276     bool refreshing_ = false;
277     RefreshType refreshType_ = RefreshType::AUTO;
278     bool isRefresh_ = false;
279 
280     int32_t friction_ = DEFAULT_FRICTION_RATIO;
281     TextStyle textStyle_;
282 
283 #ifdef WEARABLE_PRODUCT
284     Color backgroundColor_ = Color::BLACK;
285     Color progressColor_ = Color::WHITE;
286 #else
287     Color backgroundColor_ = Color::WHITE;
288     Color progressColor_ = Color::BLACK;
289 #endif
290     EventMarker refreshEventId_;
291     EventMarker pullDownEventId_;
292     EventMarker changeEvent_;
293 };
294 
295 } // namespace OHOS::Ace
296 
297 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_REFRESH_COMPONENT_H
298