• 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_REFRESH_RENDER_REFRESH_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_RENDER_REFRESH_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/system_properties.h"
21 #include "core/animation/animation.h"
22 #include "core/animation/animator.h"
23 #include "core/components/box/box_component.h"
24 #include "core/components/box/render_box.h"
25 #include "core/components/display/render_display.h"
26 #include "core/components/progress/loading_progress_component.h"
27 #include "core/components/progress/render_loading_progress.h"
28 #include "core/components/refresh/refresh_component.h"
29 #include "core/components/refresh/refresh_controller.h"
30 #include "core/components/scroll/scrollable.h"
31 #include "core/components/text/render_text.h"
32 #include "core/components/text/text_component.h"
33 #include "core/gestures/drag_recognizer.h"
34 #include "core/pipeline/base/render_node.h"
35 
36 namespace OHOS::Ace {
37 
38 class RenderRefresh : public RenderNode {
39     DECLARE_ACE_TYPE(RenderRefresh, RenderNode);
40 
41 public:
42     RenderRefresh();
43     ~RenderRefresh() override = default;
44 
45     static RefPtr<RenderNode> Create();
46     void UpdateTouchRect() override;
47     void Update(const RefPtr<Component>& component) override;
48     void OnHiddenChanged(bool hidden) override;
49     void PerformLayout() override;
50     void UpdateScrollableOffset(double delta);
51     void HandleDragUpdate(double delta);
52     void HandleDragEnd();
53     void HandleDragCancel();
SetHasScrollableChild(bool hasScrollableChild)54     void SetHasScrollableChild(bool hasScrollableChild)
55     {
56         hasScrollableChild_ = hasScrollableChild;
57     }
58 
GetStatus()59     RefreshStatus GetStatus() const
60     {
61         return refreshStatus_;
62     }
63 
SetRefreshStatus(bool refreshing)64     void SetRefreshStatus(bool refreshing)
65     {
66         refreshing_ = refreshing;
67     }
68 
GetRefreshing()69     bool GetRefreshing() const
70     {
71         return refreshing_;
72     }
73 
GetInspectorOffset()74     Dimension GetInspectorOffset() const
75     {
76         return inspectorOffset_;
77     }
78 
GetFriction()79     double GetFriction() const
80     {
81         // Percent
82         return frictionRatio_ * 100.0;
83     }
84 
85 protected:
86     void OnTouchTestHit(
87         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
88 
GetTimeBox()89     const RefPtr<RenderBox>& GetTimeBox() const
90     {
91         return timeBox_;
92     }
93 
GetDisplay()94     const RefPtr<RenderDisplay>& GetDisplay() const
95     {
96         return display_;
97     }
98 
IsShowLastTime()99     bool IsShowLastTime() const
100     {
101         return showLastTime_;
102     }
103 
IsWatch()104     bool IsWatch() const
105     {
106         return SystemProperties::GetDeviceType() == DeviceType::WATCH ||
107                SystemProperties::GetDeviceType() == DeviceType::WEARABLE;
108     }
109 
110 private:
111     void CalcLoadingParams(const RefPtr<Component>& component);
112     void Initialize();
113     void InitAccessibilityEventListener();
114 
115     void UpdateScrollOffset(double value);
116     void FireRefreshEvent() const;
117     void FirePullDownEvent(const std::string& state) const;
118     void StartAnimation(double start, double end, bool isFinished);
119     void HandleStopListener(const bool isFinished);
120     void ResetStatus();
121     RefreshStatus GetNextStatus();
122 
123     double MaxScrollableHeight() const;
124     double GetFriction(double percentage) const;
125     double GetOffset(double offset) const;
126     double GetLoadingDiameter() const;
127     double GetOpacity() const;
128     Offset GetShowTimeOffset() const;
129     Offset GetLoadingOffset() const;
130     std::string GetFormatDateTime();
131 
132     Offset scrollableOffset_;
133     RefPtr<DragRecognizer> dragDetector_;
134     RefPtr<Animation<double>> translate_;
135     RefPtr<Animator> animator_;
136 
137     // onRefresh event
138     std::function<void(const std::string&)> refreshEvent_;
139 
140     // OnPullDownStartEvent
141     std::function<void(const std::string&)> pullDownEvent_;
142 
143     std::function<void(int)> onStateChange_;
144     std::function<void(void)> onRefreshing_;
145 
146     bool showLastTime_ = false;
147     bool refreshing_ = false;
148     bool isInitialized_ = false;
149     bool isRefresh_ = false;
150     bool isUseOffset_ = false; // Whether use the indicator offset or default
151     RefreshType refreshType_ = RefreshType::AUTO;
152     Color progressColor_;
153     Color backgroundColor_;
154 
155     RefPtr<RefreshController> refreshController_;
156     RefreshStatus refreshStatus_ = RefreshStatus::INACTIVE;
157 
158     // Used for loading
159     RefPtr<LoadingProgressComponent> loadingComponent_;
160     RefPtr<BoxComponent> loadingBoxComponent_;
161     RefPtr<BoxComponent> loadingBackgroundBoxComponent_;
162 
163     // This is the progress loading
164     RefPtr<RenderLoadingProgress> loading_;
165 
166     // This is used for position
167     RefPtr<RenderBox> loadingBox_;
168 
169     // This is used for progress background include circle and color
170     RefPtr<Decoration> decoration_;
171     RefPtr<RenderBox> loadingBackgroundBox_;
172     RefPtr<RenderNode> columnChild_;
173 
174     // Used for loading time
175     RefPtr<TextComponent> timeComponent_;
176     RefPtr<BoxComponent> timeBoxComponent_;
177     RefPtr<DisplayComponent> displayComponent_;
178     RefPtr<RenderText> time_;
179     RefPtr<RenderBox> timeBox_;
180     RefPtr<RenderDisplay> display_;
181 
182     // Used for different status
183     double triggerRefreshDistance_ = 0.0;
184     double triggerLoadingDistance_ = 0.0;
185     double triggerShowTimeDistance_ = 0.0;
186     double loadingDiameter_ = 0.0;
187     double maxScrollOffset_ = 0.0;
188     double indicatorOffset_ = 0.0;
189     double frictionRatio_ = 0.0;
190     double timeDistance_ = 0.0;
191     double timeOffset_ = 0.0;
192     bool hasScrollableChild_ = false;
193     Dimension inspectorOffset_;
194 
195     // Use for update loading size when screen size changed.
196     double scale_ = 0.0;
197     WeakPtr<Component> refreshComponent_;
198 
199     std::string timeText_;
200     std::string lastTimeText_;
201     std::function<void(const std::string&)> changeEvent_;
202 };
203 
204 } // namespace OHOS::Ace
205 
206 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_RENDER_REFRESH_H
207