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_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_ALGORITHM_H
18 
19 #include "core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_info.h"
20 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_algorithm_base.h"
21 #include "core/components_ng/pattern/waterflow/water_flow_layout_property.h"
22 
23 namespace OHOS::Ace::NG {
24 class ACE_EXPORT WaterFlowLayoutAlgorithm : public WaterFlowLayoutBase {
25     DECLARE_ACE_TYPE(WaterFlowLayoutAlgorithm, WaterFlowLayoutBase);
26 
27 public:
WaterFlowLayoutAlgorithm(const RefPtr<WaterFlowLayoutInfo> & layoutInfo)28     explicit WaterFlowLayoutAlgorithm(const RefPtr<WaterFlowLayoutInfo>& layoutInfo) : layoutInfo_(layoutInfo) {}
29     ~WaterFlowLayoutAlgorithm() override = default;
30 
31     void Measure(LayoutWrapper* layoutWrapper) override;
32 
33     void Layout(LayoutWrapper* layoutWrapper) override;
34 
SetCanOverScroll(bool canOverScroll)35     void SetCanOverScroll(bool canOverScroll) override
36     {
37         canOverScroll_ = canOverScroll;
38     }
39 
40     bool PreloadItem(LayoutWrapper* host, int32_t itemIdx, int64_t deadline) override;
41 
42 private:
43     FlowItemPosition GetItemPosition(int32_t index);
44     bool MeasureToTarget(LayoutWrapper* layoutWrapper, int32_t startFrom, std::optional<int64_t> cacheDeadline);
45     void FillViewport(float mainSize, LayoutWrapper* layoutWrapper);
46     void ModifyCurrentOffsetWhenReachEnd(float mainSize, LayoutWrapper* layoutWrapper);
47     float ComputeCrossPosition(int32_t crossIndex) const;
48     void InitialItemsCrossSize(
49         const RefPtr<WaterFlowLayoutProperty>& layoutProperty, const SizeF& frameSize, int32_t childrenCount);
GetChildIndexWithFooter(int32_t index)50     int32_t GetChildIndexWithFooter(int32_t index) const
51     {
52         return index + layoutInfo_->footerIndex_ + 1;
53     }
54     void LayoutFooter(LayoutWrapper* layoutWrapper, const OffsetF& childFrameOffset, bool reverse);
55 
56     void SyncPreloadItem(LayoutWrapper* host, int32_t itemIdx) override;
57 
58     std::map<int32_t, float> itemsCrossSize_;
59     std::map<int32_t, float> itemsCrossPosition_;
60     Axis axis_ = Axis::VERTICAL;
61 
62     RefPtr<WaterFlowLayoutInfo> layoutInfo_;
63 
64     float mainGap_ = 0.0f;
65     float crossGap_ = 0.0f;
66     float mainSize_ = 0.0f;
67     float footerMainSize_ = 0.0f;
68     float footerMainStartPos_ = 0.0f;
69     bool canOverScroll_ = false;
70     bool skipMeasure_ = false;
71 };
72 } // namespace OHOS::Ace::NG
73 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_ALGORITHM_H
74