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_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LIST_LIST_PAINT_METHOD_H
18 
19 #include "core/components_ng/pattern/list/list_content_modifier.h"
20 #include "core/components_ng/pattern/scroll/inner/scroll_bar.h"
21 #include "core/components_ng/pattern/scroll/scroll_edge_effect.h"
22 #include "core/components_ng/pattern/scrollable/scrollable_paint_method.h"
23 #include "core/components_v2/list/list_properties.h"
24 
25 namespace OHOS::Ace::NG {
26 struct DividerInfo {
27     float constrainStrokeWidth = 0.0f;
28     float mainSize = 0.0f;
29     float crossSize = 0.0f;
30     float mainPadding = 0.0f;
31     float crossPadding = 0.0f;
32     float startMargin = 0.0f;
33     float endMargin = 0.0f;
34     float space = 0.0f;
35     float laneGutter = 0.0f;
36     int32_t lanes = 1;
37     int32_t totalItemCount = 0;
38     Color color = Color::TRANSPARENT;
39     bool isVertical = true;
40 };
41 
42 class ACE_EXPORT ListPaintMethod : public ScrollablePaintMethod {
43     DECLARE_ACE_TYPE(ListPaintMethod, ScrollablePaintMethod)
44 public:
45     using PositionMap = ListLayoutAlgorithm::PositionMap;
ListPaintMethod(const V2::ItemDivider & divider,bool vertical,bool isReverse,int32_t lanes,float space)46     ListPaintMethod(const V2::ItemDivider& divider, bool vertical, bool isReverse, int32_t lanes, float space)
47         : ScrollablePaintMethod(vertical, isReverse), divider_(divider), lanes_(lanes), space_(space)
48     {}
49     ~ListPaintMethod() override = default;
50 
51     CanvasDrawFunction GetForegroundDrawFunction(PaintWrapper* paintWrapper) override;
52 
GetContentModifier(PaintWrapper * paintWrapper)53     RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override
54     {
55         return listContentModifier_;
56     }
57 
58     void UpdateContentModifier(PaintWrapper* paintWrapper) override;
59 
60     void UpdateDividerList(const DividerInfo& dividerInfo);
61 
62     ListDivider HandleDividerList(int32_t index, bool lastIsGroup, int32_t laneIdx, const DividerInfo& dividerInfo);
63     ListDivider HandleLastLineIndex(int32_t index, int32_t laneIdx, const DividerInfo& dividerInfo);
64 
65     void PaintEdgeEffect(PaintWrapper* paintWrapper, RSCanvas& canvas);
66 
SetScrollBar(WeakPtr<ScrollBar> && scrollBar)67     void SetScrollBar(WeakPtr<ScrollBar>&& scrollBar)
68     {
69         scrollBar_ = scrollBar;
70     }
71 
SetEdgeEffect(WeakPtr<ScrollEdgeEffect> && edgeEffect)72     void SetEdgeEffect(WeakPtr<ScrollEdgeEffect>&& edgeEffect)
73     {
74         edgeEffect_ = edgeEffect;
75     }
76 
SetTotalItemCount(int32_t totalItemCount)77     void SetTotalItemCount(int32_t totalItemCount)
78     {
79         totalItemCount_ = totalItemCount;
80     }
81 
SetDirection(bool isRTL)82     void SetDirection(bool isRTL)
83     {
84         isRTL_ = isRTL;
85     }
86 
SetContentModifier(const RefPtr<ListContentModifier> & modify)87     void SetContentModifier(const RefPtr<ListContentModifier>& modify)
88     {
89         listContentModifier_ = modify;
90     }
91 
SetItemsPosition(const PositionMap & positionMap,const PositionMap & cachedPositionMap,const std::set<int32_t> & pressedItem)92     void SetItemsPosition(const PositionMap& positionMap, const PositionMap& cachedPositionMap,
93         const std::set<int32_t>& pressedItem)
94     {
95         itemPosition_ = positionMap;
96         for (auto& [index, pos] : cachedPositionMap) {
97             itemPosition_[index] = pos;
98         }
99         if (!pressedItem.empty()) {
100             for (auto& child : itemPosition_) {
101                 if (pressedItem.find(child.second.id) != pressedItem.end()) {
102                     child.second.isPressed = true;
103                 }
104             }
105         }
106     }
107 
SetLaneGutter(float laneGutter)108     void SetLaneGutter(float laneGutter)
109     {
110         laneGutter_ = laneGutter;
111     }
112 
GetLaneGutter()113     float GetLaneGutter()
114     {
115         return laneGutter_;
116     }
117 
SetScrollBarOverlayModifier(WeakPtr<ScrollBarOverlayModifier> && scrollBarOverlayModifier)118     void SetScrollBarOverlayModifier(WeakPtr<ScrollBarOverlayModifier>&& scrollBarOverlayModifier)
119     {
120         scrollBarOverlayModifier_ = scrollBarOverlayModifier;
121     }
122 
GetOverlayModifier(PaintWrapper * paintWrapper)123     RefPtr<Modifier> GetOverlayModifier(PaintWrapper* paintWrapper) override
124     {
125         return scrollBarOverlayModifier_.Upgrade();
126     }
127 
128     void UpdateOverlayModifier(PaintWrapper* paintWrapper) override;
129 
130 private:
131     V2::ItemDivider divider_;
132     int32_t lanes_ = 1;
133     int32_t totalItemCount_ = 0;
134     float space_;
135     float laneGutter_ = 0.0f;
136     PositionMap itemPosition_;
137     RefPtr<ListContentModifier> listContentModifier_;
138 
139     WeakPtr<ScrollBar> scrollBar_;
140     WeakPtr<ScrollEdgeEffect> edgeEffect_;
141     WeakPtr<ScrollBarOverlayModifier> scrollBarOverlayModifier_;
142     bool isRTL_ = false;
143 };
144 } // namespace OHOS::Ace::NG
145 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LIST_LIST_PAINT_METHOD_H