1 /*
2  * Copyright (c) 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_NG_PATTERN_LIST_LIST_LANES_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LIST_LIST_LANES_LAYOUT_ALGORITHM_H
18 
19 #include "core/components_ng/pattern/list/list_layout_algorithm.h"
20 
21 namespace OHOS::Ace::NG {
22 
23 // TextLayoutAlgorithm acts as the underlying text layout.
24 class ACE_EXPORT ListLanesLayoutAlgorithm : public ListLayoutAlgorithm {
25     DECLARE_ACE_TYPE(ListLanesLayoutAlgorithm, ListLayoutAlgorithm);
26 public:
SwapLanesItemRange(std::map<int32_t,int32_t> & itemRange)27     void SwapLanesItemRange(std::map<int32_t, int32_t>& itemRange)
28     {
29         lanesItemRange_.swap(itemRange);
30     }
SetLanes(int32_t lanes)31     void SetLanes(int32_t lanes)
32     {
33         lanes_ = lanes;
34     }
GetLanes()35     int32_t GetLanes() const override
36     {
37         return lanes_;
38     }
39 
40     float MeasureAndGetChildHeight(LayoutWrapper* layoutWrapper, int32_t childIndex,
41         bool groupLayoutAll = true) override;
42 
43     float GetChildHeight(LayoutWrapper* layoutWrapper, int32_t childIndex) override;
44 
45     static int32_t CalculateLanesParam(std::optional<float>& minLaneLength, std::optional<float>& maxLaneLength,
46         int32_t lanes, std::optional<float> crossSizeOptional, float laneGutter = 0.0f);
47 
GetGroupLayoutConstraint()48     const LayoutConstraintF& GetGroupLayoutConstraint() const override
49     {
50         return groupLayoutConstraint_;
51     }
52 
53 protected:
54     void UpdateListItemConstraint(Axis axis, const OptionalSizeF& selfIdealSize,
55         LayoutConstraintF& contentConstraint) override;
56     int32_t LayoutALineForward(
57         LayoutWrapper* layoutWrapper, int32_t& currentIndex, float startPos, float& endPos) override;
58     int32_t LayoutALineBackward(
59         LayoutWrapper* layoutWrapper, int32_t& currentIndex, float endPos, float& startPos) override;
60     float CalculateLaneCrossOffset(float crossSize, float childCrossSize) override;
61     void CalculateLanes(const RefPtr<ListLayoutProperty>& layoutProperty,
62         const LayoutConstraintF& layoutConstraint, std::optional<float> crossSizeOptional, Axis axis) override;
63     int32_t GetLanesFloor(LayoutWrapper* layoutWrapper, int32_t index) override;
64     int32_t GetLanesCeil(LayoutWrapper* layoutWrapper, int32_t index) override;
65     void SetCacheCount(LayoutWrapper* layoutWrapper, int32_t cacheCount) override;
66 
67 private:
68     static void ModifyLaneLength(
69         std::optional<float>& minLaneLength, std::optional<float>& maxLaneLength, float crossSize);
70     int32_t FindLanesStartIndex(LayoutWrapper* layoutWrapper, int32_t index);
71     bool CheckCurRowMeasureFinished(LayoutWrapper* layoutWrapper, int32_t curIndex, bool isGroup);
72     std::list<int32_t> LayoutCachedItem(LayoutWrapper* layoutWrapper, int32_t cacheCount) override;
73     std::list<int32_t> LayoutCachedALineForward(
74         LayoutWrapper* layoutWrapper, int32_t& index, float& startPos, float crossSize);
75     std::list<int32_t> LayoutCachedALineBackward(
76         LayoutWrapper* layoutWrapper, int32_t& index, float& endPos, float crossSize);
77     void LayoutCachedALine(LayoutWrapper* layoutWrapper, std::pair<const int, ListItemInfo>& pos,
78         int32_t startIndex, float crossSize);
79     std::pair<bool, bool> CheckACachedItem(const RefPtr<LayoutWrapper>& wrapper, int32_t cnt, bool& isGroup) const;
80     int32_t LayoutCachedForward(LayoutWrapper* layoutWrapper, int32_t cacheCount,
81         int32_t& cachedCount, int32_t curIndex, std::list<PredictLayoutItem>& predictList, bool show) override;
82     int32_t LayoutCachedBackward(LayoutWrapper* layoutWrapper, int32_t cacheCount,
83         int32_t& cachedCount, int32_t curIndex, std::list<PredictLayoutItem>& predictList, bool show) override;
84     static int32_t FindLanesStartIndex(LayoutWrapper* layoutWrapper, int32_t startIndex, int32_t index);
85     static int32_t GetLazyForEachIndex(const RefPtr<FrameNode>& host);
86     void MeasureGroup(LayoutWrapper* listWrapper, const RefPtr<LayoutWrapper>& groupWrapper,
87         int32_t index, float& pos, bool forward);
88     void MeasureItem(const RefPtr<LayoutWrapper>& itemWrapper, int32_t index, bool forward);
89 
90     int32_t lanes_ = 1;
91     std::optional<float> minLaneLength_;
92     std::optional<float> maxLaneLength_;
93     std::map<int32_t, int32_t> lanesItemRange_;
94 
95     LayoutConstraintF groupLayoutConstraint_;
96 };
97 } // namespace OHOS::Ace::NG
98 
99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LIST_LIST_LAYOUT_ALGORITHM_H