• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_LIST_LIST_ITEM_GROUP_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_ITEM_GROUP_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/utils/noncopyable.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/pattern/list/list_item_group_accessibility_property.h"
23 #include "core/components_ng/pattern/list/list_item_group_layout_algorithm.h"
24 #include "core/components_ng/pattern/list/list_item_group_layout_property.h"
25 #include "core/components_ng/pattern/list/list_layout_property.h"
26 #include "core/components_ng/pattern/pattern.h"
27 #include "core/components_ng/syntax/shallow_builder.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 struct ListItemGroupPaintInfo {
32     bool vertical = false;
33     int32_t lanes = 1;
34     float spaceWidth = 0.0f;
35     float laneGutter = 0.0f;
36     int32_t totalItemCount = 0;
37 };
38 
39 class ACE_EXPORT ListItemGroupPattern : public Pattern {
40     DECLARE_ACE_TYPE(ListItemGroupPattern, Pattern);
41 
42 public:
ListItemGroupPattern(const RefPtr<ShallowBuilder> & shallowBuilder,V2::ListItemGroupStyle listItemGroupStyle)43     explicit ListItemGroupPattern(
44         const RefPtr<ShallowBuilder>& shallowBuilder, V2::ListItemGroupStyle listItemGroupStyle)
45         : shallowBuilder_(shallowBuilder), listItemGroupStyle_(listItemGroupStyle)
46     {}
47     ~ListItemGroupPattern() override = default;
48 
IsAtomicNode()49     bool IsAtomicNode() const override
50     {
51         return false;
52     }
53 
CreateLayoutProperty()54     RefPtr<LayoutProperty> CreateLayoutProperty() override
55     {
56         return MakeRefPtr<ListItemGroupLayoutProperty>();
57     }
58 
CreateAccessibilityProperty()59     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
60     {
61         return MakeRefPtr<ListItemGroupAccessibilityProperty>();
62     }
63 
64     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override;
65 
66     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
67 
AddHeader(const RefPtr<NG::UINode> & header)68     void AddHeader(const RefPtr<NG::UINode>& header)
69     {
70         auto host = GetHost();
71         CHECK_NULL_VOID(host);
72         if (headerIndex_ < 0) {
73             headerIndex_ = itemStartIndex_;
74             host->AddChild(header);
75             itemStartIndex_++;
76         } else {
77             host->ReplaceChild(host->GetChildAtIndex(headerIndex_), header);
78             host->MarkDirtyNode(PROPERTY_UPDATE_BY_CHILD_REQUEST);
79         }
80         auto frameNode = AceType::DynamicCast<FrameNode>(header->GetFrameChildByIndex(0, false));
81         CHECK_NULL_VOID(frameNode);
82         auto renderContext = frameNode->GetRenderContext();
83         CHECK_NULL_VOID(renderContext);
84         renderContext->UpdateZIndex(1);
85     }
86 
AddFooter(const RefPtr<NG::UINode> & footer)87     void AddFooter(const RefPtr<NG::UINode>& footer)
88     {
89         auto host = GetHost();
90         CHECK_NULL_VOID(host);
91         if (footerIndex_ < 0) {
92             footerIndex_ = itemStartIndex_;
93             host->AddChild(footer);
94             itemStartIndex_++;
95         } else {
96             host->ReplaceChild(host->GetChildAtIndex(footerIndex_), footer);
97             host->MarkDirtyNode(PROPERTY_UPDATE_BY_CHILD_REQUEST);
98         }
99         auto frameNode = AceType::DynamicCast<FrameNode>(footer->GetFrameChildByIndex(0, false));
100         CHECK_NULL_VOID(frameNode);
101         auto renderContext = frameNode->GetRenderContext();
102         CHECK_NULL_VOID(renderContext);
103         renderContext->UpdateZIndex(1);
104     }
105 
GetItemPosition()106     const ListItemGroupLayoutAlgorithm::PositionMap& GetItemPosition()
107     {
108         return itemPosition_;
109     }
110 
SetIndexInList(int32_t index)111     void SetIndexInList(int32_t index)
112     {
113         indexInList_ = index;
114     }
115 
GetIndexInList()116     int32_t GetIndexInList() const
117     {
118         return indexInList_;
119     }
120 
GetDisplayEndIndexInGroup()121     int32_t GetDisplayEndIndexInGroup() const
122     {
123         return itemDisplayEndIndex_;
124     }
125 
GetDisplayStartIndexInGroup()126     int32_t GetDisplayStartIndexInGroup() const
127     {
128         return itemDisplayStartIndex_;
129     }
130 
GetItemStartIndex()131     int32_t GetItemStartIndex() const
132     {
133         return itemStartIndex_;
134     }
135 
GetEndIndexInGroup()136     int32_t GetEndIndexInGroup() const
137     {
138         return (itemTotalCount_ - 1);
139     }
140 
GetTotalItemCount()141     int32_t GetTotalItemCount() const
142     {
143         return itemTotalCount_;
144     }
145 
IsDisplayStart()146     bool IsDisplayStart() const
147     {
148         return itemDisplayStartIndex_ == 0;
149     }
150 
IsDisplayEnd()151     int32_t IsDisplayEnd() const
152     {
153         return itemTotalCount_ == 0 || itemDisplayEndIndex_ == (itemTotalCount_ - 1);
154     }
155 
GetLanesInGroup()156     int32_t GetLanesInGroup() const
157     {
158         return lanes_;
159     }
160 
GetListItemGroupStyle()161     V2::ListItemGroupStyle GetListItemGroupStyle()
162     {
163         return listItemGroupStyle_;
164     }
165 
GetHeaderMainSize()166     float GetHeaderMainSize() const
167     {
168         return headerMainSize_;
169     }
170 
GetFooterMainSize()171     float GetFooterMainSize() const
172     {
173         return footerMainSize_;
174     }
175 
176     float GetEstimateOffset(float height, const std::pair<float, float>& targetPos) const;
177     float GetEstimateHeight(float& averageHeight) const;
HasLayoutedItem()178     bool HasLayoutedItem() const
179     {
180         return layouted_ && (layoutedItemInfo_.has_value() || itemTotalCount_ == 0);
181     }
182 
183 private:
IsNeedInitClickEventRecorder()184     bool IsNeedInitClickEventRecorder() const override
185     {
186         return true;
187     }
188 
189     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
190     void OnAttachToFrameNode() override;
191     void SetListItemGroupDefaultAttributes(const RefPtr<FrameNode>& itemGroupNode);
192     void CheckListDirectionInCardStyle();
193     RefPtr<ShallowBuilder> shallowBuilder_;
194     V2::ListItemGroupStyle listItemGroupStyle_ = V2::ListItemGroupStyle::NONE;
195 
196     int32_t indexInList_ = 0;
197 
198     int32_t headerIndex_ = -1;
199     int32_t footerIndex_ = -1;
200     int32_t itemStartIndex_ = 0;
201     int32_t itemTotalCount_ = -1;
202     int32_t itemDisplayEndIndex_ = -1;
203     int32_t itemDisplayStartIndex_ = -1;
204     float_t headerMainSize_ = 0.0f;
205     float_t footerMainSize_ = 0.0f;
206 
207     std::optional<LayoutedItemInfo> layoutedItemInfo_;
208     bool layouted_ = false;
209 
210     ListItemGroupLayoutAlgorithm::PositionMap itemPosition_;
211     float spaceWidth_ = 0.0f;
212     Axis axis_ = Axis::VERTICAL;
213     int32_t lanes_ = 1;
214     float laneGutter_ = 0.0f;
215     ACE_DISALLOW_COPY_AND_MOVE(ListItemGroupPattern);
216 };
217 } // namespace OHOS::Ace::NG
218 
219 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_ITEM_PATTERN_H
220