• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_scroll_view.h
28  *
29  * @brief Declares a view group that allows its child views to be displayed as scroll events occur.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_SCROLL_VIEW_H
36 #define GRAPHIC_LITE_UI_SCROLL_VIEW_H
37 
38 #include "animator/animator.h"
39 #include "components/ui_abstract_scroll.h"
40 
41 namespace OHOS {
42 /**
43  * @brief Supports horizontal or vertical scroll of child views. This class is inherited from {@link UIAbstractScroll}.
44  *
45  * Horizontal or vertical scroll occurs only when the width or height of the child view is greater than that of the
46  * <b>UIScrollView</b>.
47  *
48  * @since 1.0
49  * @version 1.0
50  */
51 class UIScrollView : public UIAbstractScroll {
52 public:
53     /**
54      * @brief Represents a listener that contains a callback to be invoked upon scroll state changes. The state can
55      *        either be {@link SCROLL_STATE_STOP} or {@link SCROLL_STATE_MOVE}.
56      * @since 1.0
57      * @version 1.0
58      */
59     class OnScrollListener : public HeapBase {
60     public:
61         /**
62          * @brief A constructor used to create an <b>OnScrollListener</b> instance with the default scroll state
63          *        {@link SCROLL_STATE_STOP}.
64          * @since 1.0
65          * @version 1.0
66          */
OnScrollListener()67         OnScrollListener() : state_(SCROLL_STATE_STOP) {}
68 
69         /**
70          * @brief A destructor used to delete the <b>OnScrollListener</b> instance.
71          * @since 1.0
72          * @version 1.0
73          */
~OnScrollListener()74         virtual ~OnScrollListener() {}
75 
76         /**
77          * @brief Called when a scroll starts.
78          *
79          * @since 1.0
80          * @version 1.0
81          */
OnScrollStart()82         virtual void OnScrollStart() {}
83 
84         /**
85          * @brief Called when a scroll ends.
86          *
87          * @since 1.0
88          * @version 1.0
89          */
OnScrollEnd()90         virtual void OnScrollEnd() {}
91 
92         /**
93          * @brief Obtains the scroll state of this view.
94          *
95          * @return Returns the scroll state, either {@link SCROLL_STATE_STOP} or {@link SCROLL_STATE_MOVE}.
96          * @since 1.0
97          * @version 1.0
98          */
GetScrollState()99         uint8_t GetScrollState() const
100         {
101             return state_;
102         }
103 
SetScrollState(uint8_t state)104         void SetScrollState(uint8_t state)
105         {
106             state_ = state;
107         }
108 
109         static constexpr uint8_t SCROLL_STATE_STOP = 0;
110         static constexpr uint8_t SCROLL_STATE_MOVE = 1;
111 
112     private:
113         uint8_t state_;
114     };
115 
116     /**
117      * @brief A constructor used to create a <b>UIScrollView</b> instance, with both horizontal and vertical scrolls
118      *        supported.
119      *
120      * @since 1.0
121      * @version 1.0
122      */
123     UIScrollView();
124 
125     /**
126      * @brief A destructor used to delete the <b>UIScrollView</b> instance.
127      *
128      * @since 1.0
129      * @version 1.0
130      */
~UIScrollView()131     virtual ~UIScrollView() {}
132 
133     /**
134      * @brief Obtains the view type.
135      * @return Returns the view type, as defined in {@link UIViewType}.
136      *
137      * @since 1.0
138      * @version 1.0
139      */
GetViewType()140     UIViewType GetViewType() const override
141     {
142         return UI_SCROLL_VIEW;
143     }
144 
145 #if ENABLE_ROTATE_INPUT
146     bool OnRotateEvent(const RotateEvent& event) override;
147 
148     bool OnRotateEndEvent(const RotateEvent& event) override;
149 #endif
150 
151     bool OnDragEvent(const DragEvent& event) override;
152 
153     bool OnDragEndEvent(const DragEvent& event) override;
154 
155     bool OnPressEvent(const PressEvent& event) override;
156 
157     /**
158      * @brief Scrolls the content of this view.
159      *
160      * @param xDistance Indicates the offset distance by which the content is scrolled on the x-axis.
161      * @param yDistance Indicates the offset distance by which the content is scrolled on the y-axis.
162      * @since 1.0
163      * @version 1.0
164      */
165     void ScrollBy(int16_t xDistance, int16_t yDistance);
166 
167     /**
168      * @brief Sets whether a horizontal scroll is enabled.
169      *
170      * @param state Specifies whether a horizontal scroll is enabled. <b>true</b> indicates a horizontal scroll is
171      *              enabled, and <b>false</b> indicates the opposite case.
172      * @since 1.0
173      * @version 1.0
174      */
SetHorizontalScrollState(bool state)175     void SetHorizontalScrollState(bool state)
176     {
177         if (direction_ == VERTICAL || direction_ == HORIZONTAL_AND_VERTICAL) {
178             direction_ = state ? HORIZONTAL_AND_VERTICAL : VERTICAL;
179         } else {
180             direction_ = state ? HORIZONTAL : HORIZONTAL_NOR_VERTICAL;
181         }
182     }
183 
184     /**
185      * @brief Checks whether a horizontal scroll is enabled.
186      *
187      * @return Returns <b>true</b> if a horizontal scroll is enabled; returns <b>false</b> otherwise.
188      * @since 1.0
189      * @version 1.0
190      */
GetHorizontalScrollState()191     bool GetHorizontalScrollState() const
192     {
193         return (direction_ == HORIZONTAL || direction_ == HORIZONTAL_AND_VERTICAL);
194     }
195 
196     /**
197      * @brief Sets whether a vertical scroll is enabled.
198      *
199      * @param state Specifies whether a vertical scroll is enabled. <b>true</b> indicates a vertical scroll is enabled,
200      *              and <b>false</b> indicates the opposite case.
201      * @since 1.0
202      * @version 1.0
203      */
SetVerticalScrollState(bool state)204     void SetVerticalScrollState(bool state)
205     {
206         if (direction_ == HORIZONTAL || direction_ == HORIZONTAL_AND_VERTICAL) {
207             direction_ = state ? HORIZONTAL_AND_VERTICAL : HORIZONTAL;
208         } else {
209             direction_ = state ? VERTICAL : HORIZONTAL_NOR_VERTICAL;
210         }
211     }
212 
213     /**
214      * @brief Checks whether a vertical scroll is enabled.
215      *
216      * @return Returns <b>true</b> if a vertical scroll is enabled, returns <b>false</b> otherwise.
217      * @since 1.0
218      * @version 1.0
219      */
GetVerticalScrollState()220     bool GetVerticalScrollState() const
221     {
222         return (direction_ == VERTICAL || direction_ == HORIZONTAL_AND_VERTICAL);
223     }
224 
225     /**
226      * @brief Registers a listener that contains a callback to be invoked upon scroll state changes.
227      *
228      * @param scrollListener Indicates the listener to register. For details, see {@link OnScrollListener}.
229      * @since 1.0
230      * @version 1.0
231      */
RegisterScrollListener(OnScrollListener * scrollListener)232     void RegisterScrollListener(OnScrollListener* scrollListener)
233     {
234         scrollListener_ = scrollListener;
235     }
236 
237 protected:
238     void StopAnimator() override;
239     bool DragXInner(int16_t distance) override;
240     bool DragYInner(int16_t distance) override;
241     bool MoveOffset(int16_t offsetX, int16_t offsetY);
242 
243 private:
244     void Drag(const DragEvent& event);
245     void CalculateReboundDistance(int16_t& dragDistanceX, int16_t& dragDistanceY) override;
246     void RefreshScrollBar();
247     OnScrollListener* scrollListener_;
248 #if ENABLE_VIBRATOR
249     int16_t totalRotateLen_;
250     int16_t lastVibratorRotateLen_;
251 #endif
252 };
253 } // namespace OHOS
254 #endif // GRAPHIC_LITE_UI_SCROLL_VIEW_H
255