• 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_picker.h
28  *
29  * @brief Defines the attributes and functions of the <b>UIPicker</b> class.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef UI_PICKER_H
36 #define UI_PICKER_H
37 
38 #include "components/text_adapter.h"
39 #include "components/ui_list.h"
40 
41 namespace OHOS {
42 class UIPicker;
43 
44 class PickerListScrollListener : public ListScrollListener {
45 public:
46     PickerListScrollListener(UIPicker* picker, UIList* list);
~PickerListScrollListener()47     virtual ~PickerListScrollListener() {}
48 
49     void OnItemSelected(int16_t index, UIView* view) override;
50 
51     void OnScrollEnd(int16_t index, UIView* view) override;
52 
SetSelectView(UIView * view)53     void SetSelectView(UIView* view)
54     {
55         selectView_ = view;
56         lastSelectView_ = view;
57     }
58 
GetSelectView()59     const UIView* GetSelectView() const
60     {
61         return selectView_;
62     }
63 
SetSelectIndex(uint16_t index)64     void SetSelectIndex(uint16_t index)
65     {
66         selectIndex_ = index;
67     }
68 
GetSelectIndex()69     uint16_t GetSelectIndex() const
70     {
71         return selectIndex_;
72     }
73 
SetInitStatus(bool status)74     void SetInitStatus(bool status)
75     {
76         isInitted_ = status;
77     }
78 
79 private:
80     UIList* listView_;
81     UIPicker* pickerView_;
82     UIView* selectView_;
83     UIView* lastSelectView_;
84     uint16_t selectIndex_;
85     bool isInitted_;
86 };
87 
88 /**
89  * @brief Defines a picker. Multiple texts or numbers can be put into a sliding list for selection.
90  *        The selected text or numbers are highlighted.
91  *
92  * @since 1.0
93  * @version 1.0
94  */
95 class UIPicker : public UIViewGroup {
96 public:
97     /**
98      * @brief A constructor used to create a <b>UIPicker</b> instance.
99      *
100      * @since 1.0
101      * @version 1.0
102      */
103     UIPicker();
104 
105     /**
106      * @brief A destructor used to delete the <b>UIPicker</b> instance.
107      *
108      * @since 1.0
109      * @version 1.0
110      */
111     virtual ~UIPicker();
112 
113     /**
114      * @brief Obtains the view type.
115      *
116      * @return Returns the view type. For details, see {@link UIViewType}.
117      * @since 1.0
118      * @version 1.0
119      */
GetViewType()120     UIViewType GetViewType() const override
121     {
122         return UI_PICKER;
123     }
124 
OnPreDraw(Rect & invalidatedArea)125     bool OnPreDraw(Rect& invalidatedArea) const override
126     {
127         return false;
128     }
129 
130     /**
131      * @brief Sets dynamic text data in the picker by using a string array.
132      *
133      * @param value[] Indicates the array of text data.
134      * @param count   Indicates the array size.
135      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
136      * @since 1.0
137      * @version 1.0
138      */
139     virtual bool SetValues(const char* value[], uint16_t count);
140 
141     /**
142      * @brief Sets the numeric data in the picker by using a given numeric range.
143      *
144      * All integers in the range are automatically generated based on the start value and end value and placed in
145      * the picker in sequence. The start value must be smaller or equal to the end value.
146      *
147      * @param start Indicates the start integer.
148      * @param end   Indicates the end integer.
149      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
150      * @since 1.0
151      * @version 1.0
152      */
153     virtual bool SetValues(int16_t start, int16_t end);
154 
155     /**
156      * @brief Clears all values in the picker.
157      *
158      * @since 1.0
159      * @version 1.0
160      */
161     virtual void ClearValues();
162 
163     /**
164      * @brief Sets the font IDs of dynamic text, which is the string array set through {@link SetValues}.
165      *
166      * @param backgroundFontId Indicates the font ID of the background text.
167      * @param highlightFontId  Indicates the font ID of the highlighted text.
168      * @since 1.0
169      * @version 1.0
170      */
171     void SetFontId(uint8_t backgroundFontId, uint8_t highlightFontId);
172 
173     /**
174      * @brief Sets the font name and size for the background text.
175      *
176      * @param name Indicates the pointer to the font name to set.
177      * @param size Indicates the font size to set.
178      * @since 1.0
179      * @version 1.0
180      */
181     void SetBackgroundFont(const char* name, uint8_t size);
182 
183     /**
184      * @brief Sets the font name and size for the highlighted text.
185      *
186      * @param name Indicates the pointer to the font name to set.
187      * @param size Indicates the font size to set.
188      * @since 1.0
189      * @version 1.0
190      */
191     void SetHighlightFont(const char* name, uint8_t size);
192 
193     /**
194      * @brief Obtains the font ID of the background text.
195      *
196      * @return Returns the font ID.
197      * @since 1.0
198      * @version 1.0
199      */
GetBackgroundFontId()200     uint16_t GetBackgroundFontId() const
201     {
202         return backgroundFontId_;
203     }
204 
205     /**
206      * @brief Obtains the font ID of the highlighted text.
207      *
208      * @return Returns the font ID.
209      * @since 1.0
210      * @version 1.0
211      */
GetHighlightFontId()212     uint16_t GetHighlightFontId() const
213     {
214         return highlightFontId_;
215     }
216 
217     /**
218      * @brief Sets the text color.
219      *
220      * @param backgroundColor Indicates the color of the background text.
221      * @param highlightColor  Indicates the color of the highlighted text.
222      * @since 1.0
223      * @version 1.0
224      */
225     void SetTextColor(ColorType backgroundColor, ColorType highlightColor);
226 
227     /**
228      * @brief Obtains the color of the background text.
229      *
230      * @return Returns the color.
231      * @since 1.0
232      * @version 1.0
233      */
GetBackgroundTextColor()234     ColorType GetBackgroundTextColor() const
235     {
236         return backgroundColor_;
237     }
238 
239     /**
240      * @brief Obtains the color of the highlighted text.
241      *
242      * @return Returns the color of the highlighted text.
243      * @since 1.0
244      * @version 1.0
245      */
GetHighlightTextColor()246     ColorType GetHighlightTextColor() const
247     {
248         return highlightColor_;
249     }
250 
251     /**
252      * @brief Sets the index of the item currently selected in the picker.
253      *
254      * @param index Indicates the index to set.
255      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
256      * @since 1.0
257      * @version 1.0
258      */
259     bool SetSelected(uint16_t index);
260 
261     /**
262      * @brief Obtains the index of the item currently selected in the picker.
263      *
264      * @return Returns the index.
265      * @since 1.0
266      * @version 1.0
267      */
268     uint16_t GetSelected() const;
269 
270     /**
271      * @brief Sets the height of each item in the picker.
272      *
273      * @param height Indicates the height to set.
274      * @since 1.0
275      * @version 1.0
276      */
277     void SetItemHeight(int16_t height);
278 
279     /**
280      * @brief Sets the width for this component.
281      *
282      * @param width Indicates the width to set.
283      * @since 1.0
284      * @version 1.0
285      */
286     void SetWidth(int16_t width) override;
287 
288     /**
289      * @brief Sets the height for this component.
290      *
291      * @param height Indicates the height to set.
292      * @since 1.0
293      * @version 1.0
294      */
295     void SetHeight(int16_t height) override;
296 
297     /**
298      * @brief Sets whether a picker can slide cyclically.
299      *
300      * @param state Specifies whether the picker can slide cyclically. Value <b>true</b> indicates that the picker
301      *              can slide cyclically, and value <b>false</b> indicates that the picker cannot
302      *              slide cyclically. The default value is <b>false</b>.
303      * @since 1.0
304      * @version 1.0
305      */
306     void SetLoopState(bool state);
307 
308     /**
309      * @brief Defines the listener used by a picker. This listener is triggered when an item
310      *        is selected after sliding stops.
311      *
312      * @since 1.0
313      * @version 1.0
314      */
315     class SelectedListener : public HeapBase {
316     public:
317         /**
318          * @brief A constructor used to create a <b>SelectedListener</b> instance.
319          *
320          * @since 1.0
321          * @version 1.0
322          */
SelectedListener()323         SelectedListener() {}
324 
325         /**
326          * @brief A destructor used to delete the <b>SelectedListener</b> instance.
327          *
328          * @since 1.0
329          * @version 1.0
330          */
~SelectedListener()331         virtual ~SelectedListener() {}
332 
333         /**
334          * @brief Called when an item is selected after sliding stops. This function is implemented by applications.
335          *
336          * @param picker Indicates the picker instance.
337          * @since 1.0
338          * @version 1.0
339          */
OnPickerStoped(UIPicker & picker)340         virtual void OnPickerStoped(UIPicker& picker) {}
341     };
342 
343     /**
344      * @brief Registers a listener for a selected event.
345      *
346      * @param pickerListener Indicates the listener for a selected event in the picker. For details,
347      *                       see {@link SelectedListener}.
348      *
349      * @since 1.0
350      * @version 1.0
351      */
RegisterSelectedListener(SelectedListener * pickerListener)352     void RegisterSelectedListener(SelectedListener* pickerListener)
353     {
354         pickerListener_ = pickerListener;
355     }
356 
357     /**
358      * @brief Sets the text direction.
359      *
360      * @param direct Indicates the text direction to set. For details, see {@link UITextLanguageDirect}.
361      *
362      * @since 1.0
363      * @version 1.0
364      */
365     void SetDirect(UITextLanguageDirect direct);
366 
367     /**
368      * @brief Sets the text formatter.
369      *
370      * @param formatter Indicates the pointer to the text formatter. For details, see {@link TextFormatter}.
371      *
372      * @since 1.0
373      * @version 1.0
374      */
375     void SetTextFormatter(TextFormatter* formatter);
376 
377     /**
378      * @brief Sets the easing function that specifies a scroll animation after a finger lifts the screen.
379      *
380      * @param func Indicates the easing function to set. The default function is {@link EasingEquation::CubicEaseOut}.
381      *             For details, see {@link EasingEquation}.
382      * @since 5.0
383      * @version 3.0
384      */
SetDragFunc(EasingFunc func)385     void SetDragFunc(EasingFunc func)
386     {
387         list_.SetDragFunc(func);
388     }
389 
390     /**
391      * @brief Sets the rebound size, which is the distance a knob moves after being released when it reaches the end of
392      *        a scrollbar.
393      *
394      * @param size Indicates the rebound size to set.
395      * @since 5.0
396      * @version 3.0
397      */
SetReboundSize(uint16_t size)398     void SetReboundSize(uint16_t size)
399     {
400         list_.SetReboundSize(size);
401     }
402 
403     /**
404      * @brief 设置自动对齐动画时长,单位为毫秒,默认为100毫秒。该功能依赖EnableAutoAlign()方法,自动对齐设置为true情况下才生效。
405      *
406      * @param value 自动对齐动画时长,0表示无动画。
407      * @since 5.0
408      * @version 3.0
409      */
SetAutoAlignTime(uint16_t time)410     void SetAutoAlignTime(uint16_t time)
411     {
412         list_.SetAutoAlignTime(time);
413     }
414 
415     /**
416      * @brief Sets the drag acceleration.
417      *
418      * @param value Indicates the drag acceleration to set. The default value is <b>10</b>. A larger drag acceleration
419      *              indicates a higher inertial scroll velocity.
420      * @since 5.0
421      * @version 3.0
422      */
SetDragACCLevel(uint16_t value)423     void SetDragACCLevel(uint16_t value)
424     {
425         list_.SetDragACCLevel(value);
426     }
427 
428     /**
429      * @brief Sets the compensation distance after a finger lifts the screen.
430      *
431      * @param value Indicates the compensation distance to set. The default value is <b>0</b>.
432      * @since 5.0
433      * @version 3.0
434      */
SetSwipeACCLevel(uint16_t value)435     void SetSwipeACCLevel(uint16_t value)
436     {
437         list_.SetSwipeACCLevel(value);
438     }
439 
440     /**
441      * @brief Sets the blank size for this scroll view.
442      *
443      *
444      * @param value Indicates the blank size to set. The default value is <b>0</b>. Taking a vertical scroll as an
445      *              example, the value <b>0</b> indicates that the head node can only scroll downwards the top of the
446      *              view and the tail node scroll upwards the bottom; the value <b>10</b> indicates that the head node
447      *              can continue scrolling down by 10 pixels after it reaches the top of the view.
448      * @since 5.0
449      * @version 3.0
450      */
SetScrollBlankSize(uint16_t size)451     void SetScrollBlankSize(uint16_t size)
452     {
453         scrollBlankSize_ = size;
454         isScrollBlankSizeSet_ = true;
455         Refresh();
456     }
457 
458 #if ENABLE_ROTATE_INPUT
459     /**
460      * @brief Requests the focus on the view.
461      *
462      * @since 5.0
463      * @version 3.0
464      */
RequestFocus()465     void RequestFocus() override
466     {
467         list_.RequestFocus();
468     }
469 
470     /**
471      * @brief Clears the focus on the view.
472      *
473      * @since 5.0
474      * @version 3.0
475      */
ClearFocus()476     void ClearFocus() override
477     {
478         list_.ClearFocus();
479     }
480 
481     /**
482      * @brief Sets the rotation factor.
483      *
484      * @param factor Indicates the rotation factor to set.
485      * @since 5.0
486      * @version 3.0
487      */
SetRotateFactor(float factor)488     void SetRotateFactor(float factor)
489     {
490         list_.SetRotateFactor(factor);
491     }
492 
493     /**
494      * @brief Obtains the rotation factor.
495      *
496      * @return Returns the rotation factor.
497      * @since 5.0
498      * @version 3.0
499      */
GetRotateFactor()500     float GetRotateFactor()
501     {
502         return list_.GetRotateFactor();
503     }
504 
505     /**
506      * @brief 设置触发惯性滑动的组件大小比例阈值.
507      *
508      * @param threshold 设置触发惯性滑动的比例阈值.
509      *                  旋转表冠结束,如果最后一次旋转位移数值大于组件的宽或高除以threshold,则触发惯性滑动.
510      * @since 6
511      */
SetRotateThrowThreshold(uint8_t threshold)512     void SetRotateThrowThreshold(uint8_t threshold)
513     {
514         list_.SetRotateThrowThreshold(threshold);
515     }
516 #endif
517 
518 protected:
519     bool RefreshSelected(uint16_t index);
520     void RefreshList();
521     virtual void ClearList();
522     virtual void Refresh();
InitTextAdapter()523     virtual void InitTextAdapter()
524     {
525         if (textAdapter_ == nullptr) {
526             textAdapter_ = new TextAdapter();
527             if (textAdapter_ == nullptr) {
528                 GRAPHIC_LOGE("new TextAdapter fail");
529                 return;
530             }
531         }
532     }
533 
534     bool isWidthSet_ : 1;
535     bool isHeightSet_ : 1;
536     TextAdapter* textAdapter_;
537     uint16_t maxCount_;
538     PickerListScrollListener* listListener_;
539 private:
540     friend class PickerListScrollListener;
541     bool RefreshValues(const char* value[], uint16_t count);
542     bool RefreshValues(int16_t start, int16_t end);
543 
544     bool isScrollBlankSizeSet_ : 1;
545     uint16_t scrollBlankSize_;
546 
547     uint16_t backgroundFontId_;
548     uint16_t highlightFontId_;
549     uint8_t backgroundFontSize_;
550     uint8_t highlightFontSize_;
551     char* backgroundFontName_;
552     char* highlightFontName_;
553 
554     uint16_t itemsWidth_;
555     uint16_t itemsHeight_;
556     const char** rangeValue_;
557     uint16_t rangeValueCount_;
558     int16_t startValue_;
559     int16_t endValue_;
560     ColorType backgroundColor_;
561     ColorType highlightColor_;
562     List<const char*> dataList_;
563     bool isSetAdaptered_ : 1;
564     UIList list_;
565 
566     SelectedListener* pickerListener_;
567     UITextLanguageDirect direct_;
568 };
569 } // namespace OHOS
570 #endif
571