• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_LIST_LIST_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_COMPONENT_H
18 
19 #include "base/utils/macros.h"
20 #include "core/animation/simple_spring_chain.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/scroll_bar.h"
23 #include "core/components/common/rotation/rotation_controller.h"
24 #include "core/components/list/list_element.h"
25 #include "core/components/list/list_item_component.h"
26 #include "core/components/list/list_theme.h"
27 #include "core/components/list/render_list.h"
28 #include "core/components/scroll/scroll_edge_effect.h"
29 #include "core/components/scroll/scroll_position_controller.h"
30 #include "core/pipeline/base/component_group.h"
31 
32 namespace OHOS::Ace {
33 
34 inline constexpr int32_t INDEX_EXPAND_ALL = -999;
35 
36 class ACE_EXPORT ListComponent : public ComponentGroup {
37     DECLARE_ACE_TYPE(ListComponent, ComponentGroup);
38 
39 public:
40     ListComponent() = default;
41     explicit ListComponent(const std::list<RefPtr<Component>>& children);
42     ~ListComponent() override = default;
43 
44     void InsertChild(int32_t position, const RefPtr<Component>& child) override;
45     void AppendChild(const RefPtr<Component>& child) override;
46     void RemoveChild(const RefPtr<Component>& child) override;
47     void SetGroupState(int32_t expandIndex, bool expand);
48 
49     RefPtr<Element> CreateElement() override;
50     RefPtr<RenderNode> CreateRenderNode() override;
51 
SetScrollPage(bool scrollPage)52     void SetScrollPage(bool scrollPage)
53     {
54         scrollPage_ = scrollPage;
55     }
56 
SetChainAnimation(bool chainAnimation)57     void SetChainAnimation(bool chainAnimation)
58     {
59         chainAnimation_ = chainAnimation;
60     }
61 
SetOnScroll(const EventMarker & onScroll)62     void SetOnScroll(const EventMarker& onScroll)
63     {
64         onScroll_ = onScroll;
65     }
66 
SetOnScrollBottom(const EventMarker & onScrollBottom)67     void SetOnScrollBottom(const EventMarker& onScrollBottom)
68     {
69         onScrollBottom_ = onScrollBottom;
70     }
71 
SetOnScrollTop(const EventMarker & onScrollTop)72     void SetOnScrollTop(const EventMarker& onScrollTop)
73     {
74         onScrollTop_ = onScrollTop;
75     }
76 
SetOnScrollEnd(const EventMarker & onScrollEnd)77     void SetOnScrollEnd(const EventMarker& onScrollEnd)
78     {
79         onScrollEnd_ = onScrollEnd;
80     }
81 
SetOnScrollTouchUp(const EventMarker & onScrollTouchUp)82     void SetOnScrollTouchUp(const EventMarker& onScrollTouchUp)
83     {
84         onScrollTouchUp_ = onScrollTouchUp;
85     }
86 
GetScrollPage()87     bool GetScrollPage() const
88     {
89         return scrollPage_;
90     }
91 
GetChainAnimation()92     bool GetChainAnimation() const
93     {
94         return chainAnimation_;
95     }
96 
GetDirection()97     FlexDirection GetDirection() const
98     {
99         return direction_;
100     }
101 
SetDirection(FlexDirection direction)102     void SetDirection(FlexDirection direction)
103     {
104         direction_ = direction;
105     }
106 
GetTotalCount()107     int32_t GetTotalCount() const
108     {
109         return totalCount_;
110     }
111 
GetItemsCount()112     int32_t GetItemsCount() const
113     {
114         return GetChildren().size();
115     }
116 
SetTotalCount(int32_t totalCount)117     void SetTotalCount(int32_t totalCount)
118     {
119         if (totalCount < 0) {
120             LOGE("Invalid TotalCount %{public}d", totalCount);
121             return;
122         }
123         LOGD("SetTotalCount to %{public}d.", totalCount);
124         totalCount_ = totalCount;
125     }
126 
GetOnRequestItem()127     const EventMarker& GetOnRequestItem() const
128     {
129         return onRequestItem_;
130     }
131 
SetOnRequestItem(const EventMarker & onRequestItem)132     void SetOnRequestItem(const EventMarker& onRequestItem)
133     {
134         onRequestItem_ = onRequestItem;
135     }
136 
GetCachedCount()137     int32_t GetCachedCount() const
138     {
139         return cachedCount_;
140     }
141 
GetBeginIndex()142     int32_t GetBeginIndex() const
143     {
144         return beginIndex_;
145     }
146 
GetEndIndex()147     int32_t GetEndIndex() const
148     {
149         return endIndex_;
150     }
151 
GetRepeatedLength()152     int32_t GetRepeatedLength() const
153     {
154         return repeatedLength_;
155     }
156 
GetIndexOffset()157     int32_t GetIndexOffset() const
158     {
159         return indexOffset_;
160     }
161 
SetCachedCount(int32_t cachedCount)162     void SetCachedCount(int32_t cachedCount)
163     {
164         if (cachedCount <= 0) {
165             LOGE("Invalid CachedCount %{public}d", cachedCount);
166             return;
167         }
168         LOGD("SetCachedCount to %{public}d.", cachedCount);
169         cachedCount_ = cachedCount;
170     }
171 
SetBeginIndex(int32_t beginIndex)172     void SetBeginIndex(int32_t beginIndex)
173     {
174         beginIndex_ = beginIndex;
175     }
176 
SetEndIndex(int32_t endIndex)177     void SetEndIndex(int32_t endIndex)
178     {
179         endIndex_ = endIndex;
180     }
181 
SetRepeatedLength(int32_t repeatedLength)182     void SetRepeatedLength(int32_t repeatedLength)
183     {
184         repeatedLength_ = repeatedLength;
185     }
186 
SetIndexOffset(int32_t indexOffset)187     void SetIndexOffset(int32_t indexOffset)
188     {
189         indexOffset_ = indexOffset;
190     }
191 
GetPositionController()192     RefPtr<ScrollPositionController> GetPositionController() const
193     {
194         return positionController_;
195     }
196 
SetPositionController(const RefPtr<ScrollPositionController> & controller)197     void SetPositionController(const RefPtr<ScrollPositionController>& controller)
198     {
199         positionController_ = controller;
200     }
201 
GetScrollEffect()202     const RefPtr<ScrollEdgeEffect>& GetScrollEffect() const
203     {
204         return scrollEffect_;
205     }
206 
SetScrollEffect(const RefPtr<ScrollEdgeEffect> & scrollEffect)207     void SetScrollEffect(const RefPtr<ScrollEdgeEffect>& scrollEffect)
208     {
209         scrollEffect_ = scrollEffect;
210     }
211 
GetOnScroll()212     const EventMarker& GetOnScroll() const
213     {
214         return onScroll_;
215     }
216 
GetOnScrollBottom()217     const EventMarker& GetOnScrollBottom() const
218     {
219         return onScrollBottom_;
220     }
221 
GetOnScrollTop()222     const EventMarker& GetOnScrollTop() const
223     {
224         return onScrollTop_;
225     }
226 
GetOnScrollEnd()227     const EventMarker& GetOnScrollEnd() const
228     {
229         return onScrollEnd_;
230     }
231 
GetOnScrollTouchUp()232     const EventMarker& GetOnScrollTouchUp() const
233     {
234         return onScrollTouchUp_;
235     }
236 
237     void InitStyle(const RefPtr<ListTheme>& theme);
238 
GetGradientWidth()239     const Dimension& GetGradientWidth() const
240     {
241         return gradientWidth_;
242     }
243 
SetGradientWidth(const Dimension & gradientWidth)244     void SetGradientWidth(const Dimension& gradientWidth)
245     {
246         if (gradientWidth.Value() < 0.0) {
247             LOGE("Invalid GradientWidth %{public}lf", gradientWidth_.Value());
248             return;
249         }
250         LOGD("SetGradientWidth to %{public}lf.", gradientWidth_.Value());
251         gradientWidth_ = gradientWidth;
252     }
253 
GetBackgroundColor()254     const Color& GetBackgroundColor() const
255     {
256         return backgroundColor_;
257     }
258 
SetBackgroundColor(const Color & backgroundColor)259     void SetBackgroundColor(const Color& backgroundColor)
260     {
261         backgroundColor_ = backgroundColor;
262     }
263 
264     void SetFlexAlign(FlexAlign flexAlign);
265 
266     void SetColumnCount(int32_t count);
267 
268     void SetColumnExtent(int32_t extent);
269 
270     void SetItemExtent(const Dimension& itemExtent);
271 
272     void SetWidth(double width);
273 
274     void SetHeight(double height);
275 
SetInRefresh(bool inRefresh)276     void SetInRefresh(bool inRefresh)
277     {
278         inRefresh_ = inRefresh;
279     }
280 
SetSupportItemCenter(bool center)281     void SetSupportItemCenter(bool center)
282     {
283         supportItemCenter_ = center;
284     }
285 
GetSupportItemCenter()286     bool GetSupportItemCenter() const
287     {
288         return supportItemCenter_;
289     }
290 
GetFlexAlign()291     FlexAlign GetFlexAlign() const
292     {
293         return flexAlign_;
294     }
295 
GetColumnCount()296     int32_t GetColumnCount() const
297     {
298         return columnCount_;
299     }
300 
GetColumnExtent()301     int32_t GetColumnExtent() const
302     {
303         return columnExtent_;
304     }
305 
GetItemExtent()306     const Dimension& GetItemExtent() const
307     {
308         return itemExtent_;
309     }
310 
GetWidth()311     double GetWidth() const
312     {
313         return width_;
314     }
315 
GetHeight()316     double GetHeight() const
317     {
318         return height_;
319     }
320 
NeedPreBuild()321     bool NeedPreBuild() const
322     {
323         return needPreBuild_;
324     }
325 
NeedUpdateElement()326     bool NeedUpdateElement() const
327     {
328         return needUpdateElement_;
329     }
330 
IsInRefresh()331     bool IsInRefresh() const
332     {
333         return inRefresh_;
334     }
335 
MarkNeedUpdateElement(bool needUpdateElement)336     void MarkNeedUpdateElement(bool needUpdateElement)
337     {
338         needUpdateElement_ = needUpdateElement;
339     }
340 
SetScrollBar(const RefPtr<ScrollBar> & scrollBar)341     void SetScrollBar(const RefPtr<ScrollBar>& scrollBar)
342     {
343         scrollBar_ = scrollBar;
344     }
345 
GetScrollBar()346     RefPtr<ScrollBar> GetScrollBar() const
347     {
348         return scrollBar_;
349     }
350 
SetRightToLeft(bool rightToLeft)351     void SetRightToLeft(bool rightToLeft)
352     {
353         rightToLeft_ = rightToLeft;
354     }
355 
GetRightToLeft()356     bool GetRightToLeft() const
357     {
358         return rightToLeft_;
359     }
360 
SetUpdateEffect(bool updateEffect)361     void SetUpdateEffect(bool updateEffect)
362     {
363         updateEffect_ = updateEffect;
364     }
365 
GetUpdateEffect()366     bool GetUpdateEffect() const
367     {
368         return updateEffect_;
369     }
370 
UpdateListItemIndex()371     void UpdateListItemIndex()
372     {
373         int32_t index = 0; // Update the item index by the order in list.
374         for (const auto& iter : GetChildren()) {
375             auto listItemComponent = ListItemComponent::GetListItem(iter);
376             if (listItemComponent) {
377                 listItemComponent->SetIndex(index++);
378             }
379         }
380     }
381 
SetPageReady(bool pageReady)382     void SetPageReady(bool pageReady)
383     {
384         pageReady_ = pageReady;
385     }
386 
GetPageReady()387     bool GetPageReady() const
388     {
389         return pageReady_;
390     }
391 
IsCenterLayout()392     bool IsCenterLayout() const
393     {
394         return isCenterLayout_;
395     }
396 
MarkCenterLayout(bool isCenter)397     void MarkCenterLayout(bool isCenter)
398     {
399         isCenterLayout_ = isCenter;
400     }
401 
IsItemScale()402     bool IsItemScale() const
403     {
404         return itemScale_;
405     }
406 
SetItemScale(bool itemScale)407     void SetItemScale(bool itemScale)
408     {
409         itemScale_ = itemScale;
410     }
411 
GetRotationController()412     const RefPtr<RotationController>& GetRotationController() const
413     {
414         return rotationController_;
415     }
416 
SetChainProperty(const SpringChainProperty & property)417     void SetChainProperty(const SpringChainProperty& property)
418     {
419         chainProperty_ = property;
420     }
421 
GetChainProperty()422     const SpringChainProperty& GetChainProperty() const
423     {
424         return chainProperty_;
425     }
426 
SetOverSpringProperty(const RefPtr<SpringProperty> & property)427     void SetOverSpringProperty(const RefPtr<SpringProperty>& property)
428     {
429         if (property && property->IsValid()) {
430             overSpringProperty_ = property;
431         }
432     }
433 
SetOverSpringProperty(double mass,double stiffness,double damping)434     void SetOverSpringProperty(double mass, double stiffness, double damping)
435     {
436         overSpringProperty_ = AceType::MakeRefPtr<SpringProperty>(mass, stiffness, damping);
437     }
438 
OverSpringProperty()439     const RefPtr<SpringProperty>& OverSpringProperty()
440     {
441         return overSpringProperty_;
442     }
443 
NeedDivider()444     bool NeedDivider() const
445     {
446         return needDivider_;
447     }
448 
MarkNeedDivider(bool needDivider)449     void MarkNeedDivider(bool needDivider)
450     {
451         needDivider_ = needDivider;
452     }
453 
GetDividerOrigin()454     const Dimension& GetDividerOrigin() const
455     {
456         return dividerOrigin_;
457     }
458 
SetDividerOrigin(const Dimension & origin)459     void SetDividerOrigin(const Dimension& origin)
460     {
461         dividerOrigin_ = origin;
462     }
463 
GetDividerLength()464     const Dimension& GetDividerLength() const
465     {
466         return dividerLength_;
467     }
468 
SetDividerLength(const Dimension & length)469     void SetDividerLength(const Dimension& length)
470     {
471         dividerLength_ = length;
472     }
473 
GetDividerColor()474     const Color& GetDividerColor() const
475     {
476         return dividerColor_;
477     }
478 
SetDividerColor(const Color & color)479     void SetDividerColor(const Color& color)
480     {
481         dividerColor_ = color;
482     }
483 
GetDividerHeight()484     const Dimension& GetDividerHeight() const
485     {
486         return dividerHeight_;
487     }
488 
SetDividerHeight(const Dimension & dividerHeight)489     void SetDividerHeight(const Dimension& dividerHeight)
490     {
491         dividerHeight_ = dividerHeight;
492     }
493 
GetOnRotateId()494     const EventMarker& GetOnRotateId() const
495     {
496         return onRotateId_;
497     }
498 
SetOnRotateId(const EventMarker & onRotateId)499     void SetOnRotateId(const EventMarker& onRotateId)
500     {
501         onRotateId_ = onRotateId;
502     }
503 
SetScrollVibrate(bool scrollVibrate)504     void SetScrollVibrate(bool scrollVibrate)
505     {
506         scrollVibrate_ = scrollVibrate;
507     }
508 
GetScrollVibrate()509     bool GetScrollVibrate()
510     {
511         return scrollVibrate_;
512     }
513 
IsRotationVibrate()514     bool IsRotationVibrate() const
515     {
516         return rotationVibrate_;
517     }
518 
MarkNeedRotationVibrate(bool needVibrate)519     void MarkNeedRotationVibrate(bool needVibrate)
520     {
521         rotationVibrate_ = needVibrate;
522     }
523 
SetAccessibilityDisabled(bool disable)524     void SetAccessibilityDisabled(bool disable)
525     {
526         accessibilityDisabled_ = disable;
527     }
528 
IsAccessibilityDisabled()529     bool IsAccessibilityDisabled() const
530     {
531         return accessibilityDisabled_;
532     }
533 
534 private:
535     EventMarker onRequestItem_;
536     EventMarker onScroll_;
537     EventMarker onScrollBottom_;
538     EventMarker onScrollTop_;
539     EventMarker onScrollEnd_;
540     EventMarker onScrollTouchUp_;
541     EventMarker onRotateId_;
542     Dimension gradientWidth_;
543     Color backgroundColor_ = Color::WHITE;
544     FlexDirection direction_ = FlexDirection::COLUMN;
545     bool scrollPage_ { false };
546     bool chainAnimation_ { false };
547     int32_t totalCount_ { 0 };
548     int32_t cachedCount_ { 1 }; // the default value of cached child number.
549     int32_t beginIndex_ { LIST_PARAM_INVAID };
550     int32_t endIndex_ { LIST_PARAM_INVAID };
551     int32_t repeatedLength_ { 0 };
552     int32_t indexOffset_ { 0 };
553     RefPtr<ScrollPositionController> positionController_;
554     RefPtr<RotationController> rotationController_ = AceType::MakeRefPtr<RotationController>();
555     RefPtr<ScrollEdgeEffect> scrollEffect_;
556     FlexAlign flexAlign_ { FlexAlign::STRETCH };
557     double width_ { DEFAULT_GRID_WIDTH };
558     double height_ { DEFAULT_GRID_HEIGHT };
559     int32_t columnCount_ { DEFAULT_COLUMN_COUNT };
560     int32_t columnExtent_ { DEFAULT_COLUMN_EXTENT };
561     Dimension itemExtent_;
562     bool needPreBuild_ = false;
563     bool needUpdateElement_ = false;
564     bool rightToLeft_ = false;
565     RefPtr<ScrollBar> scrollBar_;
566     bool inRefresh_ = false;
567     bool supportItemCenter_ = false;
568     bool updateEffect_ = false;
569     bool pageReady_ = false;
570     bool itemScale_ = false;
571     bool isCenterLayout_ = false;
572     SpringChainProperty chainProperty_;
573     RefPtr<SpringProperty> overSpringProperty_;
574     WeakPtr<ListElement> listElement_;
575 
576     // divider style
577     Dimension dividerOrigin_;
578     Dimension dividerLength_;
579     Dimension dividerHeight_ = DIVIDER_DEFAULT_HEIGHT;
580     Color dividerColor_;
581     bool needDivider_ = false;
582 
583     bool scrollVibrate_ = true;
584     bool rotationVibrate_ = false;
585     bool accessibilityDisabled_ = false;
586 };
587 
588 } // namespace OHOS::Ace
589 
590 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_COMPONENT_H
591