• 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_swipe_view.h
28  *
29  * @brief Defines the attributes and common functions of a swipe view.
30  *
31  * Each swipe view consists of multiple child views, which can be navigated through swiping. The child views can be
32  * either horizontal or vertical.
33  *
34  * @since 1.0
35  * @version 1.0
36  */
37 
38 #ifndef GRAPHIC_LITE_UI_SWIPE_VIEW_H
39 #define GRAPHIC_LITE_UI_SWIPE_VIEW_H
40 
41 #include "animator/animator.h"
42 #include "components/ui_abstract_scroll.h"
43 
44 namespace OHOS {
45 /**
46  * @brief Represents a swipe view.
47  *
48  * Each swipe view consists of multiple child views, which can be navigated through swiping. The child views can be
49  * either horizontal or vertical.
50  *
51  * @see UIAbstractScroll
52  * @since 1.0
53  * @version 1.0
54  */
55 class UISwipeView : public UIAbstractScroll {
56 public:
57     /**
58      * @brief Represents a listener for changes of the swipe view.
59      *
60      * This is an inner class of <b>UISwipeView</b>. It contains a callback function to be invoked when the swipe view
61      * state changes.
62      *
63      * @since 1.0
64      * @version 1.0
65      */
66     class OnSwipeListener : public HeapBase {
67     public:
68         virtual void OnSwipe(UISwipeView& view) = 0;
~OnSwipeListener()69         virtual ~OnSwipeListener() {}
70     };
71 
72     enum AlignMode : uint8_t { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT };
73 
74     /**
75      * @brief A constructor used to create a <b>UISwipeView</b> instance.
76      *
77      * @since 1.0
78      * @version 1.0
79      */
80     UISwipeView(uint8_t direction = HORIZONTAL);
81 
82     /**
83      * @brief A destructor used to delete the <b>UISwipeView</b> instance.
84      *
85      * @since 1.0
86      * @version 1.0
87      */
88     virtual ~UISwipeView();
89 
90     /**
91      * @brief Obtains the component type.
92      *
93      * @return Returns the component type, as defined in {@link UIViewType}.
94      * @since 1.0
95      * @version 1.0
96      */
GetViewType()97     UIViewType GetViewType() const override
98     {
99         return UI_SWIPE_VIEW;
100     }
101 
102     /**
103      * @brief Sets the dragging direction.
104      *
105      * @param direction Indicates the dragging direction, either {@link HORIZONTAL} or {@link VERTICAL}.
106      * @since 1.0
107      * @version 1.0
108      */
SetDirection(uint8_t direction)109     void SetDirection(uint8_t direction)
110     {
111         direction_ = direction;
112     }
113 
114     /**
115      * @brief Obtains the dragging direction.
116      *
117      * @return Returns the dragging direction.
118      * @since 1.0
119      * @version 1.0
120      */
GetDirection()121     uint8_t GetDirection() const
122     {
123         return direction_;
124     }
125 
126     /**
127      * @brief Adds a view.
128      *
129      * @param view Indicates the view to add.
130      * @since 1.0
131      * @version 1.0
132      */
133     void Add(UIView* view) override;
134 
135     /**
136      * @brief Inserts a view.
137      *
138      * @param prevView Indicates the previous view.
139      * @param insertView Indicates the view to insert.
140      * @since 1.0
141      * @version 1.0
142      */
143     void Insert(UIView* prevView, UIView* insertView) override;
144 
145     /**
146      * @brief Deletes a view.
147      *
148      * @param view Indicates the view to delete.
149      * @since 1.0
150      * @version 1.0
151      */
152     void Remove(UIView* view) override;
153 
154     /**
155      * @brief Sets the index for the current tab.
156      *
157      * @param index Indicates the index of a view.
158      * @param needAnimator Specifies whether a flip animation is needed. <b>false</b> (default value) indicates a flip
159      * animation is not needed, and <b>true</b> indicates the opposite case.
160      * @since 1.0
161      * @version 1.0
162      */
163     void SetCurrentPage(uint16_t index, bool needAnimator = false);
164 
165     /**
166      * @brief Obtains the current tab index.
167      *
168      * @return Returns the current tab index.
169      * @since 1.0
170      * @version 1.0
171      */
GetCurrentPage()172     uint16_t GetCurrentPage() const
173     {
174         return curIndex_;
175     }
176 
177     /**
178      * @brief Obtains the current view.
179      *
180      * @return Returns the current view.
181      * @since 1.0
182      * @version 1.0
183      */
GetCurrentView()184     UIView* GetCurrentView() const
185     {
186         return curView_;
187     }
188 
189     /**
190      * @brief Sets a blank size, as defined in {@link DEFAULT_BLANK_SIZE}
191      *
192      * @param size Indicates the blank size to set.
193      * @since 1.0
194      * @version 1.0
195      */
SetBlankSize(uint16_t size)196     void SetBlankSize(uint16_t size)
197     {
198         blankSize_ = size;
199     }
200 
GetBlankSize()201     uint16_t GetBlankSize() const
202     {
203         return blankSize_;
204     }
205 
206     /**
207      * @fn void OnDragEvent(const DragEvent& event) override
208      *
209      * @brief revice drag event, Switch to specified view when drag
210      *
211      * @param event The drag event
212      */
213     bool OnDragEvent(const DragEvent& event) override;
214 
215     bool OnDragEndEvent(const DragEvent& event) override;
216 
217 #if defined(ENABLE_ROTATE_INPUT) && ENABLE_ROTATE_INPUT
218     bool OnRotateEvent(const RotateEvent& event) override;
219 
220     bool OnRotateEndEvent(const RotateEvent& event) override;
221 #endif
222 
223     /**
224      * @brief Sets the time for the page being animated. The page will go beyond the blank during this time.
225      *
226      * @param time Indicates the time of the page being animated.
227      * @since 1.0
228      * @version 1.0
229      */
230     void SetAnimatorTime(uint16_t time);
231 
232     /**
233      * @brief Sets whether the swipe view supports a cycle swipe.
234      *
235      * @param loop Indicates the cycle swipe flag. <b>true</b> indicates the cycle swipe is supported, and <b>false</b>
236      * indicates the opposite case.
237      * @since 1.0
238      * @version 1.0
239      */
SetLoopState(bool loop)240     void SetLoopState(bool loop)
241     {
242         loop_ = loop;
243     }
244 
245     /**
246      * @brief Obtains a view based on its index.
247      *
248      * @param Indicates the index of a view.
249      * @return Returns the view.
250      * @since 1.0
251      * @version 1.0
252      */
253     UIView* GetViewByIndex(uint16_t index) const;
254 
255     /**
256      * @brief Obtains the listener set for swipe events.
257      *
258      * @return Returns the swipe event listener.
259      * @since 1.0
260      * @version 1.0
261      */
GetOnSwipeListener()262     OnSwipeListener*& GetOnSwipeListener()
263     {
264         return swipeListener_;
265     }
266 
267     /**
268      * @brief Sets the listener that contains a callback to be invoked upon a swipe event.
269      *
270      * @param onSwipeListener Indicates the listener to set.
271      * @since 1.0
272      * @version 1.0
273      */
SetOnSwipeListener(OnSwipeListener * onSwipeListener)274     void SetOnSwipeListener(OnSwipeListener* onSwipeListener)
275     {
276         swipeListener_ = onSwipeListener;
277     }
278 
279     /**
280      * @brief Sets the alignment mode for child components of <b>UISwipeView</b>.
281      *
282      * @param alignMode Indicates the alignment mode to set, as enumerated in {@link AlignMode}.
283      * The default value is <b>ALIGN_CENTER</b>.
284      * @since 1.0
285      * @version 1.0
286      */
287     void SetAlignMode(AlignMode alignMode = ALIGN_CENTER)
288     {
289         alignMode_ = alignMode;
290     }
291 
292     /**
293      * @brief Obtains the alignment mode of child components of <b>UISwipeView</b>.
294      *
295      * @return Returns the alignment mode. For details, see {@link AlignMode}.
296      * @since 1.0
297      * @version 1.0
298      */
GetAlignMode()299     AlignMode GetAlignMode()
300     {
301         return alignMode_;
302     }
303 
304     /**
305      * @brief Indicates the horizontal direction.
306      *
307      * @since 1.0
308      * @version 1.0
309      */
310     static constexpr uint8_t HORIZONTAL = 0;
311 
312     /**
313      * @brief Indicates the vertical direction.
314      *
315      * @since 1.0
316      * @version 1.0
317      */
318     static constexpr uint8_t VERTICAL = 1;
319 
320     void SetXScrollBarVisible(bool visible) = delete;
321 
322     void SetYScrollBarVisible(bool visible) = delete;
323 
324     void SetScrollBarSide(uint8_t side) = delete;
325 
326     void SetScrollBarCenter(Point center) = delete;
327 
328 protected:
329     bool DragXInner(int16_t distance) override;
330     bool DragYInner(int16_t distance) override;
331     void SortChild();
332     void StopAnimator() override;
333     virtual void SwitchToPage(int16_t dst, bool needAnimator = true);
334     void MoveChildByOffset(int16_t xOffset, int16_t yOffset) override;
335     void MoveHeadOrTailChild();
336 
337     /**
338      * @brief Indicates that the animation duration is 12 ticks.
339      *
340      * @since 1.0
341      * @version 1.0
342      */
343     constexpr static uint16_t ANIMATOR_TIME = 12;
344 
345     /**
346      * @brief Indicates the maximum distance of an invalid dragging. Dragging is not triggered if the distance is less
347      * than this value.
348      *
349      * @since 1.0
350      * @version 1.0
351      */
352     constexpr static uint16_t STOP_DISTANCE = 5;
353 
354     /**
355      * @brief Indicates the maximum distance between the first and the last tab when the current view is not in a cycle
356      * swipe mode. The page can be rebound after the setting.
357      *
358      * @since 1.0
359      * @version 1.0
360      */
361     constexpr static uint16_t DEFAULT_BLANK_SIZE = 30;
362     uint16_t tickTime_;
363     OnSwipeListener* swipeListener_;
364     uint16_t curIndex_;
365     uint16_t blankSize_;
366     UIView* curView_;
367     AlignMode alignMode_ = ALIGN_CENTER;
368     bool loop_;
369 
370 private:
371     void RefreshCurrentViewByPosition(int16_t (UIView::*pfnGetXOrY)() const, int16_t (UIView::*pfnGetWidthOrHeight)());
372     void RefreshCurrentViewByThrow(int16_t distance,
373                                    uint8_t dragDirection,
374                                    int16_t (UIView::*pfnGetXOrY)() const,
375                                    int16_t (UIView::*pfnGetWidthOrHeight)());
376 
377     bool IsNeedLoop();
378     void MoveFirstChildToLast();
379     void MoveLastChildToFirst();
380     void CalculateInvalidate();
381     void CurrentIndexInc();
382     void CurrentIndexDec();
383     void Vibrator();
384 };
385 } // namespace OHOS
386 #endif // GRAPHIC_LITE_UI_SWIPE_VIEW_H
387