• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
95 
96     /* Free Scroll mode */
97     struct ScrollToParam {
98         Dimension xOffset;
99         Dimension yOffset;
100         float duration = 0.0f;
101         RefPtr<Curve> curve;
102         bool smooth = false;
103         bool canOverScroll = false;
104     };
FreeScrollTo(const ScrollToParam & param)105     virtual bool FreeScrollTo(const ScrollToParam& param)
106     {
107         return false;
108     }
109 
SetCanStayOverScroll(bool canStayOverScroll)110     virtual void SetCanStayOverScroll(bool canStayOverScroll) {}
ScrollBy(double pixelX,double pixelY,bool smooth)111     virtual void ScrollBy(double pixelX, double pixelY, bool smooth) {}
ScrollToEdge(ScrollEdgeType scrollEdgeType,float velocity)112     virtual void ScrollToEdge(ScrollEdgeType scrollEdgeType, float velocity) {}
ScrollToEdge(ScrollEdgeType scrollEdgeType,bool smooth)113     virtual void ScrollToEdge(ScrollEdgeType scrollEdgeType, bool smooth) {}
Fling(double flingVelocity)114     virtual void Fling(double flingVelocity) {}
ScrollPage(bool reverse,bool smooth)115     virtual void ScrollPage(bool reverse, bool smooth) {}
GetCurrentOffset()116     virtual Offset GetCurrentOffset() const
117     {
118         return Offset();
119     }
IsAtEnd()120     virtual bool IsAtEnd() const
121     {
122         return true;
123     }
GetItemRect(int32_t index)124     virtual Rect GetItemRect(int32_t index) const
125     {
126         return Rect();
127     }
128 
GetItemIndex(double x,double y)129     virtual int32_t GetItemIndex(double x, double y) const
130     {
131         return -1;
132     }
133 
GetItemRectInGroup(int32_t index,int32_t indexInGroup)134     virtual Rect GetItemRectInGroup(int32_t index, int32_t indexInGroup) const
135     {
136         return Rect();
137     }
138 
GetItemIndexInGroup(double x,double y)139     virtual ListItemGroupIndex GetItemIndexInGroup(double x, double y) const
140     {
141         return ListItemGroupIndex();
142     }
143 
CloseAllSwipeActions(OnFinishFunc && onFinishCallback)144     virtual void CloseAllSwipeActions(OnFinishFunc&& onFinishCallback) {}
145 
SetObserver(const ScrollerObserver & observer)146     virtual void SetObserver(const ScrollerObserver& observer) {}
147 
SetObserverManager(const RefPtr<ScrollerObserverManager> & mgr)148     virtual void SetObserverManager(const RefPtr<ScrollerObserverManager>& mgr)
149     {
150         observerMgr_ = mgr;
151     }
152 
GetObserverManager()153     virtual RefPtr<ScrollerObserverManager> GetObserverManager() const
154     {
155         return observerMgr_;
156     }
157 
StopAnimate()158     virtual void StopAnimate() {}
159 
160 protected:
161     RefPtr<ScrollerObserverManager> observerMgr_ = nullptr;
162 };
163 } // namespace OHOS::Ace
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_CONTROLLER_BASE_H
166