1 /*
2  * Copyright (c) 2021-2024 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_SCROLL_SCROLL_CONTROLLER_BASE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_CONTROLLER_BASE_H
18 
19 #include "base/geometry/axis.h"
20 #include "base/geometry/dimension.h"
21 #include "base/geometry/offset.h"
22 #include "base/geometry/rect.h"
23 #include "base/memory/ace_type.h"
24 #include "core/animation/curve.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components_ng/pattern/scrollable/scrollable_properties.h"
27 #include "core/components_ng/pattern/scrollable/scroller_observer_manager.h"
28 #include "core/event/ace_events.h"
29 
30 namespace OHOS::Ace {
31 
32 constexpr uint32_t POSITION_MIDDLE = 0;
33 constexpr uint32_t POSITION_TOP = 1 << 0;
34 constexpr uint32_t POSITION_BOTTOM = 1 << 1;
35 
36 enum class ScrollEvent : size_t {
37     SCROLL_TOP = 0,
38     SCROLL_BOTTOM,
39     SCROLL_TOUCHUP,
40     SCROLL_END,
41     SCROLL_POSITION,
42     SCROLL_EDGE,
43     SCROLL_LEFT,
44     SCROLL_RIGHT,
45     UNKNOWN,
46 };
47 
48 enum class ScrollEdgeType : size_t {
49     SCROLL_TOP = 0,
50     SCROLL_BOTTOM,
51     SCROLL_LEFT,
52     SCROLL_RIGHT,
53     SCROLL_NONE,
54 };
55 
56 enum class ScrollAlign {
57     START = 0,
58     CENTER,
59     END,
60     AUTO,
61     NONE,
62 };
63 
64 struct ListItemGroupIndex {
65     int32_t index = -1;
66     int32_t area = -1;
67     int32_t indexInGroup = -1;
68 };
69 
70 using OnFinishFunc = std::function<void()>;
71 class ACE_EXPORT ScrollControllerBase : public AceType {
72     DECLARE_ACE_TYPE(ScrollControllerBase, AceType);
73 
74 public:
75     ScrollControllerBase() = default;
76     ~ScrollControllerBase() override = default;
77 
78     virtual void ScrollToIndex(int32_t index, bool smooth = false, ScrollAlign align = ScrollAlign::NONE,
79         std::optional<float> extraOffset = std::nullopt)
80     {}
81 
82     virtual void JumpToItemInGroup(int32_t index, int32_t indexInGroup, bool smooth = false,
83         ScrollAlign align = ScrollAlign::NONE, int32_t source = 3) {} // 3 is SCROLL_FROM_JUMP
84 
GetScrollDirection()85     virtual Axis GetScrollDirection() const
86     {
87         return Axis::NONE;
88     }
89 
90     virtual bool AnimateTo(
91         const Dimension& position, float duration, const RefPtr<Curve>& curve, bool smooth, bool canOverScroll = false)
92     {
93         return true;
94     }
ScrollBy(double pixelX,double pixelY,bool smooth)95     virtual void ScrollBy(double pixelX, double pixelY, bool smooth) {}
ScrollToEdge(ScrollEdgeType scrollEdgeType,float velocity)96     virtual void ScrollToEdge(ScrollEdgeType scrollEdgeType, float velocity) {}
ScrollToEdge(ScrollEdgeType scrollEdgeType,bool smooth)97     virtual void ScrollToEdge(ScrollEdgeType scrollEdgeType, bool smooth) {}
Fling(double flingVelocity)98     virtual void Fling(double flingVelocity) {}
ScrollPage(bool reverse,bool smooth)99     virtual void ScrollPage(bool reverse, bool smooth) {}
GetCurrentOffset()100     virtual Offset GetCurrentOffset() const
101     {
102         return Offset();
103     }
IsAtEnd()104     virtual bool IsAtEnd() const
105     {
106         return true;
107     }
GetItemRect(int32_t index)108     virtual Rect GetItemRect(int32_t index) const
109     {
110         return Rect();
111     }
112 
GetItemIndex(double x,double y)113     virtual int32_t GetItemIndex(double x, double y) const
114     {
115         return -1;
116     }
117 
GetItemRectInGroup(int32_t index,int32_t indexInGroup)118     virtual Rect GetItemRectInGroup(int32_t index, int32_t indexInGroup) const
119     {
120         return Rect();
121     }
122 
GetItemIndexInGroup(double x,double y)123     virtual ListItemGroupIndex GetItemIndexInGroup(double x, double y) const
124     {
125         return ListItemGroupIndex();
126     }
127 
CloseAllSwipeActions(OnFinishFunc && onFinishCallback)128     virtual void CloseAllSwipeActions(OnFinishFunc&& onFinishCallback) {}
129 
SetObserver(const ScrollerObserver & observer)130     virtual void SetObserver(const ScrollerObserver& observer) {}
131 
SetObserverManager(const RefPtr<ScrollerObserverManager> & mgr)132     virtual void SetObserverManager(const RefPtr<ScrollerObserverManager>& mgr)
133     {
134         observerMgr_ = mgr;
135     }
136 
GetObserverManager()137     virtual RefPtr<ScrollerObserverManager> GetObserverManager() const
138     {
139         return observerMgr_;
140     }
141 
142 protected:
143     RefPtr<ScrollerObserverManager> observerMgr_ = nullptr;
144 };
145 } // namespace OHOS::Ace
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_CONTROLLER_BASE_H
148