1 /*
2  * Copyright (c) 2021-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_GRID_LAYOUT_GRID_LAYOUT_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_COMPONENT_H
18 
19 #include "base/utils/macros.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components/common/properties/scroll_bar.h"
22 #include "core/components/scroll_bar/scroll_bar_proxy.h"
23 #include "core/components_v2/foreach/lazy_foreach_component.h"
24 #include "core/components_v2/grid/grid_position_controller.h"
25 #include "core/pipeline/base/component_group.h"
26 
27 namespace OHOS::Ace {
28 
29 using OnGridDragEnterFunc = std::function<void(const ItemDragInfo&)>;
30 using OnGridDragMoveFunc = std::function<void(const ItemDragInfo&, int32_t, int32_t)>;
31 using OnGridDragLeaveFunc = std::function<void(const ItemDragInfo&, int32_t)>;
32 using OnGridDragStartFunc = std::function<RefPtr<Component>(const ItemDragInfo&, int32_t)>;
33 using OnGridDropFunc = std::function<void(const ItemDragInfo&, int32_t, int32_t, bool)>;
34 
35 class ACE_EXPORT GridLayoutComponent : public ComponentGroup {
36     DECLARE_ACE_TYPE(GridLayoutComponent, ComponentGroup);
37 
38 public:
GridLayoutComponent(const std::list<RefPtr<Component>> & children)39     explicit GridLayoutComponent(const std::list<RefPtr<Component>>& children) : ComponentGroup(children) {}
40 
41     ~GridLayoutComponent() override = default;
42 
43     RefPtr<Element> CreateElement() override;
44 
UseNonProxiedCodePath()45     bool UseNonProxiedCodePath() const
46     {
47         return (((rowsArgs_.empty() && (!columnsArgs_.empty())) || ((!rowsArgs_.empty()) && columnsArgs_.empty())) &&
48                 (maxCount_ >= minCount_) && (minCount_ >= 1) && (cellLength_ > 0) && editMode_);
49     }
50 
51     RefPtr<RenderNode> CreateRenderNode() override;
52 
53     void SetDirection(FlexDirection direction);
54     void SetFlexAlign(FlexAlign flexAlign);
55     void SetColumnCount(int32_t count);
56     void SetRowCount(int32_t count);
57     void SetWidth(double width);
58     void SetHeight(double height);
59     void SetColumnsArgs(const std::string& columnsArgs);
60     void SetRowsArgs(const std::string& rowsArgs);
61     void SetColumnGap(const Dimension& columnGap);
62     void SetRowGap(const Dimension& rowGap);
63     void SetRightToLeft(bool rightToLeft);
64 
65     // set scroll bar color
66     void SetScrollBarColor(const std::string& color);
67 
68     // set scroll bar width
69     void SetScrollBarWidth(const std::string& width);
70 
71     void SetScrollBar(DisplayMode displayMode);
72 
GetColumnsArgs()73     const std::string& GetColumnsArgs() const
74     {
75         return columnsArgs_;
76     }
77 
GetRowsArgs()78     const std::string& GetRowsArgs() const
79     {
80         return rowsArgs_;
81     }
82 
GetColumnGap()83     const Dimension& GetColumnGap() const
84     {
85         return columnGap_;
86     }
87 
GetRowGap()88     const Dimension& GetRowGap() const
89     {
90         return rowGap_;
91     }
92 
GetDirection()93     FlexDirection GetDirection() const
94     {
95         return direction_;
96     }
97 
GetFlexAlign()98     FlexAlign GetFlexAlign() const
99     {
100         return flexAlign_;
101     }
102 
GetColumnCount()103     int32_t GetColumnCount() const
104     {
105         return columnCount_;
106     }
107 
GetRowCount()108     int32_t GetRowCount() const
109     {
110         return rowCount_;
111     }
112 
GetWidth()113     double GetWidth() const
114     {
115         return width_;
116     }
117 
GetHeight()118     double GetHeight() const
119     {
120         return height_;
121     }
122 
GetRightToLeft()123     bool GetRightToLeft() const
124     {
125         return rightToLeft_;
126     }
127 
SetUseScroll(bool flag)128     void SetUseScroll(bool flag)
129     {
130         useScroll_ = flag;
131     }
132 
GetScrollBarColor()133     const std::string& GetScrollBarColor() const
134     {
135         return scrollBarColor_;
136     }
137 
GetScrollBarWidth()138     const std::string& GetScrollBarWidth() const
139     {
140         return scrollBarWidth_;
141     }
142 
GetScrollBar()143     DisplayMode GetScrollBar()
144     {
145         return displayMode_;
146     }
147 
SetDeclarative()148     void SetDeclarative()
149     {
150         isDeclarative_ = true;
151     }
152 
IsDeclarative()153     bool IsDeclarative()
154     {
155         return isDeclarative_;
156     }
157 
SetNeedShrink(bool flag)158     void SetNeedShrink(bool flag)
159     {
160         needShrink_ = flag;
161     }
162 
NeedShrink()163     bool NeedShrink()
164     {
165         return needShrink_;
166     }
167 
GetController()168     const RefPtr<V2::GridPositionController>& GetController() const
169     {
170         return controller_;
171     }
172 
SetController(const RefPtr<V2::GridPositionController> & controller)173     void SetController(const RefPtr<V2::GridPositionController>& controller)
174     {
175         controller_ = controller;
176     }
177 
SetScrolledEvent(const EventMarker & event)178     void SetScrolledEvent(const EventMarker& event)
179     {
180         scrolledEvent_ = event;
181     }
182 
GetScrolledEvent()183     const EventMarker& GetScrolledEvent() const
184     {
185         return scrolledEvent_;
186     }
187 
SetCachedCount(int32_t cacheCount)188     void SetCachedCount(int32_t cacheCount)
189     {
190         cacheCount_ = cacheCount;
191     }
192 
GetCacheCount()193     int32_t GetCacheCount() const
194     {
195         return cacheCount_;
196     }
197 
SetScrollBarProxy(const RefPtr<ScrollBarProxy> & scrollBarProxy)198     void SetScrollBarProxy(const RefPtr<ScrollBarProxy>& scrollBarProxy)
199     {
200         scrollBarProxy_ = scrollBarProxy;
201     }
202 
GetScrollBarProxy()203     const RefPtr<ScrollBarProxy>& GetScrollBarProxy() const
204     {
205         return scrollBarProxy_;
206     }
207 
SetEditMode(bool editMode)208     void SetEditMode(bool editMode)
209     {
210         editMode_ = editMode;
211     }
212 
GetEditMode()213     bool GetEditMode() const
214     {
215         return editMode_;
216     }
217 
SetMaxCount(int32_t maxCount)218     void SetMaxCount(int32_t maxCount)
219     {
220         maxCount_ = maxCount;
221     }
222 
GetMaxCount(void)223     int32_t GetMaxCount(void) const
224     {
225         return maxCount_;
226     }
227 
SetMinCount(int32_t minCount)228     void SetMinCount(int32_t minCount)
229     {
230         minCount_ = minCount;
231     }
232 
GetMinCount(void)233     int32_t GetMinCount(void) const
234     {
235         return minCount_;
236     }
237 
SetCellLength(int32_t cellLength)238     void SetCellLength(int32_t cellLength)
239     {
240         cellLength_ = cellLength;
241     }
242 
GetCellLength(void)243     int32_t GetCellLength(void) const
244     {
245         return cellLength_;
246     }
247 
SetSupportAnimation(bool supportAnimation)248     void SetSupportAnimation(bool supportAnimation)
249     {
250         supportAnimation_ = supportAnimation;
251     }
252 
GetSupportAnimation()253     bool GetSupportAnimation()
254     {
255         return supportAnimation_;
256     }
257 
SetDragAnimation(bool value)258     void SetDragAnimation(bool value)
259     {
260         supportDragAnimation_ = value;
261     }
262 
GetDragAnimation()263     bool GetDragAnimation()
264     {
265         return supportDragAnimation_;
266     }
267 
SetEdgeEffect(EdgeEffect value)268     void SetEdgeEffect(EdgeEffect value)
269     {
270         if (value < EdgeEffect::SPRING || value > EdgeEffect::NONE) {
271             LOGW("Invalid edgeEffect %{public}d", value);
272             return;
273         }
274         edgeEffect_ = value;
275     }
276 
GetEdgeEffect()277     EdgeEffect GetEdgeEffect()
278     {
279         return edgeEffect_;
280     }
281 
282     void SetOnGridDragEnterId(const OnGridDragEnterFunc& onGridDragEnterId);
283     void SetOnGridDragMoveId(const OnGridDragMoveFunc& onGridDragMoveId);
284     void SetOnGridDragLeaveId(const OnGridDragLeaveFunc& onGridDragLeaveId);
285     void SetOnGridDragStartId(const OnGridDragStartFunc& onGridDragStartId);
286     void SetOnGridDropId(const OnGridDropFunc& onGridDropId);
287     const OnGridDragEnterFunc& GetOnGridDragEnterId() const;
288     const OnGridDragMoveFunc& GetOnGridDragMoveId() const;
289     const OnGridDragLeaveFunc& GetOnGridDragLeaveId() const;
290     const OnGridDragStartFunc& GetOnGridDragStartId() const;
291     const OnGridDropFunc& GetOnGridDropId() const;
292 
SetMultiSelectable(bool multiSelectable)293     void SetMultiSelectable(bool multiSelectable)
294     {
295         multiSelectable_ = multiSelectable;
296     }
297 
GetMultiSelectable()298     bool GetMultiSelectable() const
299     {
300         return multiSelectable_;
301     }
302 
303 private:
304     FlexDirection direction_ = FlexDirection::COLUMN;
305     FlexAlign flexAlign_ = FlexAlign::CENTER;
306     EdgeEffect edgeEffect_ = EdgeEffect::NONE;
307     double width_ = -1.0;
308     double height_ = -1.0;
309     int32_t columnCount_ = 1;
310     int32_t rowCount_ = 1;
311     bool isDeclarative_ = false;
312     int32_t cacheCount_ = 3;
313     bool supportAnimation_ = false;
314     bool supportDragAnimation_ = false;
315 
316     std::string columnsArgs_;
317     std::string rowsArgs_;
318     Dimension columnGap_ = 0.0_px;
319     Dimension rowGap_ = 0.0_px;
320     bool rightToLeft_ = false;
321     bool useScroll_ = true;
322 
323     // scroll bar attribute
324     std::string scrollBarColor_;
325     std::string scrollBarWidth_;
326     DisplayMode displayMode_ = DisplayMode::OFF;
327     RefPtr<V2::GridPositionController> controller_;
328     EventMarker scrolledEvent_;
329     RefPtr<ScrollBarProxy> scrollBarProxy_;
330     bool needShrink_ = false;
331 
332     // drag in grid attribute
333     bool editMode_ = false;
334     bool multiSelectable_ = false;
335     int32_t maxCount_ = 1;
336     int32_t minCount_ = 1;
337     int32_t cellLength_ = 0;
338     OnGridDragEnterFunc onGridDragEnterId_;
339     OnGridDragMoveFunc onGridDragMoveId_;
340     OnGridDragLeaveFunc onGridDragLeaveId_;
341     OnGridDragStartFunc onGridDragStartId_;
342     OnGridDropFunc onGridDropId_;
343 };
344 
345 } // namespace OHOS::Ace
346 
347 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_COMPONENT_H
348