• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_NG_PATTERNS_WATERFLOW_WATER_FLOW_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WATERFLOW_WATER_FLOW_PATTERN_H
18 
19 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h"
20 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_algorithm_base.h"
21 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_info_base.h"
22 #include "core/components_ng/pattern/waterflow/water_flow_accessibility_property.h"
23 #include "core/components_ng/pattern/waterflow/water_flow_content_modifier.h"
24 #include "core/components_ng/pattern/waterflow/water_flow_event_hub.h"
25 #include "core/components_ng/pattern/waterflow/water_flow_layout_property.h"
26 #include "core/components_ng/pattern/waterflow/water_flow_sections.h"
27 
28 namespace OHOS::Ace::NG {
29 class ACE_EXPORT WaterFlowPattern : public ScrollablePattern {
30     DECLARE_ACE_TYPE(WaterFlowPattern, ScrollablePattern);
31 
32 public:
33     bool UpdateCurrentOffset(float delta, int32_t source) override;
34     bool IsScrollable() const override;
35     bool IsAtTop() const override;
36     bool IsAtBottom() const override;
37     bool IsReverse() const override;
hasFooter()38     bool hasFooter()
39     {
40         return footer_.Upgrade() != nullptr;
41     };
42     OverScrollOffset GetOverScrollOffset(double delta) const override;
43     void UpdateScrollBarOffset() override;
44 
45     using LayoutMode = WaterFlowLayoutMode;
46     void SetLayoutMode(LayoutMode mode);
GetLayoutMode()47     LayoutMode GetLayoutMode() const
48     {
49         return layoutInfo_->Mode();
50     }
51 
52     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override;
53 
CreateLayoutProperty()54     RefPtr<LayoutProperty> CreateLayoutProperty() override
55     {
56         return MakeRefPtr<WaterFlowLayoutProperty>();
57     }
58 
CreateEventHub()59     RefPtr<EventHub> CreateEventHub() override
60     {
61         return MakeRefPtr<WaterFlowEventHub>();
62     }
63 
CreateAccessibilityProperty()64     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
65     {
66         return MakeRefPtr<WaterFlowAccessibilityProperty>();
67     }
68 
GetPositionController()69     RefPtr<ScrollableController> GetPositionController() const
70     {
71         return positionController_;
72     }
73 
74     void TriggerModifyDone();
75 
76     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
77 
78     bool UpdateStartIndex(int32_t index);
79 
80     void AddFooter(const RefPtr<NG::UINode>& footer);
81 
82     void ResetLayoutInfo();
83 
GetBeginIndex()84     int32_t GetBeginIndex() const
85     {
86         return layoutInfo_->startIndex_;
87     }
88 
GetEndIndex()89     int32_t GetEndIndex() const
90     {
91         return layoutInfo_->endIndex_;
92     }
93 
94     int32_t GetChildrenCount() const;
95 
GetTotalOffset()96     float GetTotalOffset() const override
97     {
98         return -layoutInfo_->Offset();
99     }
100 
101     int32_t GetRows() const;
102 
103     int32_t GetColumns() const;
104 
105     void OnAnimateStop() override;
106     /**
107      * @brief LayoutMode::SLIDING_WINDOW doesn't support scrollTo and animateTo
108      */
109     void ScrollTo(float position) override;
110     /**
111      * @brief LayoutMode::SLIDING_WINDOW doesn't support animateTo
112      */
113     void AnimateTo(
114         float position, float duration, const RefPtr<Curve>& curve, bool smooth, bool canOverScroll,
115         bool useTotalOffset = true) override;
116 
117     void ScrollPage(bool reverse, AccessibilityScrollType scrollType = AccessibilityScrollType::SCROLL_FULL);
118 
119     void ScrollToIndex(int32_t index, bool smooth = false, ScrollAlign align = ScrollAlign::START,
120         std::optional<float> extraOffset = std::nullopt) override;
121 
GetStoredOffset()122     double GetStoredOffset() const
123     {
124         return layoutInfo_->storedOffset_;
125     }
126 
SetRestoreOffset(double restoreOffset)127     void SetRestoreOffset(double restoreOffset)
128     {
129         layoutInfo_->restoreOffset_ = restoreOffset;
130     }
131 
SetScrollAlign(ScrollAlign align)132     void SetScrollAlign(ScrollAlign align)
133     {
134         layoutInfo_->align_ = align;
135     }
136 
137     std::string ProvideRestoreInfo() override;
138     void OnRestoreInfo(const std::string& restoreInfo) override;
139     Rect GetItemRect(int32_t index) const override;
140 
141     RefPtr<WaterFlowSections> GetSections() const;
142     RefPtr<WaterFlowSections> GetOrCreateWaterFlowSections();
143     void ResetSections();
144 
145     /**
146      * @brief Callback function when Sections data has changed.
147      *
148      * @param start the index of the first modified section.
149      */
150     void OnSectionChanged(int32_t start);
151 
152     void DumpAdvanceInfo() override;
153 
SetPreloadList(std::list<int32_t> && preload)154     void SetPreloadList(std::list<int32_t>&& preload)
155     {
156         preloadItems_ = std::move(preload);
157     }
PreloadListEmpty()158     bool PreloadListEmpty() const
159     {
160         return preloadItems_.empty();
161     }
MovePreloadList()162     std::list<int32_t>&& MovePreloadList()
163     {
164         return std::move(preloadItems_);
165     }
SetCacheLayoutAlgo(const RefPtr<WaterFlowLayoutBase> & algo)166     void SetCacheLayoutAlgo(const RefPtr<WaterFlowLayoutBase>& algo)
167     {
168         cacheLayout_ = algo;
169     }
GetCacheLayoutAlgo()170     const RefPtr<WaterFlowLayoutBase>& GetCacheLayoutAlgo() const
171     {
172         return cacheLayout_;
173     }
174 
175     void NotifyDataChange(int32_t index, int32_t count) override;
176 
177     // ------------------------ Focus adapter --------------------------------
GetFocusPattern()178     FocusPattern GetFocusPattern() const override
179     {
180         return { FocusType::SCOPE, true };
181     }
182     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override;
183     std::function<bool(int32_t)> GetScrollIndexAbility() override;
184     // ------------------------ Focus ^^^ --------------------------------
185     void BeforeCreateLayoutWrapper() override;
186 
AddSectionChangeStartPos(int32_t start)187     void AddSectionChangeStartPos(int32_t start)
188     {
189         sectionChangeStartPos_.emplace_back(start);
190         MarkDirtyNodeSelf();
191     }
192 
GetPrevOffset()193     float GetPrevOffset() const
194     {
195         return prevOffset_;
196     }
197 
GetDefaultCachedCount()198     int32_t GetDefaultCachedCount() const
199     {
200         return layoutInfo_->defCachedCount_;
201     }
202 
203 private:
GetDefaultScrollBarDisplayMode()204     DisplayMode GetDefaultScrollBarDisplayMode() const override
205     {
206         return DisplayMode::OFF;
207     }
208     void OnModifyDone() override;
209     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
210     void CheckScrollable();
211     bool IsOutOfBoundary(bool useCurrentDelta = true) override;
212 
213     void TriggerPostLayoutEvents();
214 
215     void SetEdgeEffectCallback(const RefPtr<ScrollEdgeEffect>& scrollEffect) override;
216     SizeF GetContentSize() const;
217     void MarkDirtyNodeSelf();
218     void OnScrollEndCallback() override;
219     bool ScrollToTargetIndex(int32_t index);
220     bool NeedRender();
221     void FireOnReachStart(const OnReachEvent& onReachStart) override;
222     void FireOnReachEnd(const OnReachEvent& onReachEnd) override;
223     void FireOnScrollIndex(bool indexChanged, const ScrollIndexFunc& onScrollIndex);
224 
225     /**
226      * @param step FocusStep
227      * @param currentFocusNode the currently focused FlowItem.
228      * @return WeakPtr<FocusHub> of the next FlowItem to focus on.
229      */
230     WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
231 
232     std::optional<int32_t> targetIndex_;
233     RefPtr<WaterFlowLayoutInfoBase> layoutInfo_ = WaterFlowLayoutInfoBase::Create(LayoutMode::TOP_DOWN);
234     RefPtr<WaterFlowSections> sections_;
235 
236     float prevOffset_ = 0.0f;
237     SizeF lastSize_;
238     std::pair<int32_t, int32_t> itemRange_ = { -1, -1 };
239     WeakPtr<UINode> footer_;
240     // for keepVisiableContentPosition mode temporarily.
241     bool keepContentPosition_ = true;
242 
243     // clip padding of WaterFlow
244     RefPtr<WaterFlowContentModifier> contentModifier_;
245 
246     std::list<int32_t> preloadItems_;
247     RefPtr<WaterFlowLayoutBase> cacheLayout_;
248 
249     std::vector<int32_t> sectionChangeStartPos_;
250 };
251 } // namespace OHOS::Ace::NG
252 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WATERFLOW_WATER_FLOW_PATTERN_H
253