1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "components/ui_picker.h"
17a3e0fd82Sopenharmony_ci#include "dock/vibrator_manager.h"
18a3e0fd82Sopenharmony_ci#include "draw/draw_line.h"
19a3e0fd82Sopenharmony_ci#include "draw/draw_rect.h"
20a3e0fd82Sopenharmony_ci#include "themes/theme_manager.h"
21a3e0fd82Sopenharmony_ci
22a3e0fd82Sopenharmony_cinamespace OHOS {
23a3e0fd82Sopenharmony_ciPickerListScrollListener::PickerListScrollListener(UIPicker* picker, UIList* list)
24a3e0fd82Sopenharmony_ci    : listView_(list),
25a3e0fd82Sopenharmony_ci      pickerView_(picker),
26a3e0fd82Sopenharmony_ci      selectView_(nullptr),
27a3e0fd82Sopenharmony_ci      lastSelectView_(nullptr),
28a3e0fd82Sopenharmony_ci      selectIndex_(0),
29a3e0fd82Sopenharmony_ci      isInitted_(false) {}
30a3e0fd82Sopenharmony_ci
31a3e0fd82Sopenharmony_civoid PickerListScrollListener::OnItemSelected(int16_t index, UIView* view)
32a3e0fd82Sopenharmony_ci{
33a3e0fd82Sopenharmony_ci    if (!isInitted_) {
34a3e0fd82Sopenharmony_ci        return;
35a3e0fd82Sopenharmony_ci    }
36a3e0fd82Sopenharmony_ci
37a3e0fd82Sopenharmony_ci    if ((lastSelectView_ != nullptr) && (listView_ != nullptr) && (pickerView_ != nullptr) && (view != nullptr)) {
38a3e0fd82Sopenharmony_ci        lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
39a3e0fd82Sopenharmony_ci        if (pickerView_->backgroundFontName_ == nullptr) {
40a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
41a3e0fd82Sopenharmony_ci        } else {
42a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(lastSelectView_)
43a3e0fd82Sopenharmony_ci                ->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
44a3e0fd82Sopenharmony_ci        }
45a3e0fd82Sopenharmony_ci        view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
46a3e0fd82Sopenharmony_ci        if (pickerView_->highlightFontName_ == nullptr) {
47a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
48a3e0fd82Sopenharmony_ci        } else {
49a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
50a3e0fd82Sopenharmony_ci        }
51a3e0fd82Sopenharmony_ci        lastSelectView_ = view;
52a3e0fd82Sopenharmony_ci        selectIndex_ = index;
53a3e0fd82Sopenharmony_ci        listView_->Invalidate();
54a3e0fd82Sopenharmony_ci        if (pickerView_->pickerListener_) {
55a3e0fd82Sopenharmony_ci            pickerView_->pickerListener_->OnPickerChanged(*pickerView_);
56a3e0fd82Sopenharmony_ci        }
57a3e0fd82Sopenharmony_ci    }
58a3e0fd82Sopenharmony_ci}
59a3e0fd82Sopenharmony_ci
60a3e0fd82Sopenharmony_civoid PickerListScrollListener::OnScrollEnd(int16_t index, UIView* view)
61a3e0fd82Sopenharmony_ci{
62a3e0fd82Sopenharmony_ci    if ((view == nullptr) || (listView_ == nullptr) || (pickerView_ == nullptr)) {
63a3e0fd82Sopenharmony_ci        return;
64a3e0fd82Sopenharmony_ci    }
65a3e0fd82Sopenharmony_ci
66a3e0fd82Sopenharmony_ci    if (lastSelectView_ != nullptr) {
67a3e0fd82Sopenharmony_ci        lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
68a3e0fd82Sopenharmony_ci        if (pickerView_->backgroundFontName_ == nullptr) {
69a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
70a3e0fd82Sopenharmony_ci        } else {
71a3e0fd82Sopenharmony_ci            static_cast<UILabel*>(lastSelectView_)
72a3e0fd82Sopenharmony_ci                ->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
73a3e0fd82Sopenharmony_ci        }
74a3e0fd82Sopenharmony_ci        lastSelectView_ = view;
75a3e0fd82Sopenharmony_ci    }
76a3e0fd82Sopenharmony_ci
77a3e0fd82Sopenharmony_ci    view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
78a3e0fd82Sopenharmony_ci    if (pickerView_->highlightFontName_ == nullptr) {
79a3e0fd82Sopenharmony_ci        static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
80a3e0fd82Sopenharmony_ci    } else {
81a3e0fd82Sopenharmony_ci        static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
82a3e0fd82Sopenharmony_ci    }
83a3e0fd82Sopenharmony_ci
84a3e0fd82Sopenharmony_ci    listView_->Invalidate();
85a3e0fd82Sopenharmony_ci    selectView_ = view;
86a3e0fd82Sopenharmony_ci    selectIndex_ = index;
87a3e0fd82Sopenharmony_ci
88a3e0fd82Sopenharmony_ci    if (pickerView_->pickerListener_) {
89a3e0fd82Sopenharmony_ci        pickerView_->pickerListener_->OnPickerStoped(*pickerView_);
90a3e0fd82Sopenharmony_ci    }
91a3e0fd82Sopenharmony_ci}
92a3e0fd82Sopenharmony_ci
93a3e0fd82Sopenharmony_ciUIPicker::UIPicker()
94a3e0fd82Sopenharmony_ci    : isWidthSet_(false),
95a3e0fd82Sopenharmony_ci      isHeightSet_(false),
96a3e0fd82Sopenharmony_ci      textAdapter_(nullptr),
97a3e0fd82Sopenharmony_ci      maxCount_(0),
98a3e0fd82Sopenharmony_ci      isScrollBlankSizeSet_(false),
99a3e0fd82Sopenharmony_ci      scrollBlankSize_(0),
100a3e0fd82Sopenharmony_ci      backgroundFontSize_(0),
101a3e0fd82Sopenharmony_ci      highlightFontSize_(0),
102a3e0fd82Sopenharmony_ci      backgroundFontName_(nullptr),
103a3e0fd82Sopenharmony_ci      highlightFontName_(nullptr),
104a3e0fd82Sopenharmony_ci      itemsWidth_(0),
105a3e0fd82Sopenharmony_ci      itemsHeight_(0),
106a3e0fd82Sopenharmony_ci      rangeValue_(nullptr),
107a3e0fd82Sopenharmony_ci      rangeValueCount_(0),
108a3e0fd82Sopenharmony_ci      startValue_(0),
109a3e0fd82Sopenharmony_ci      endValue_(0),
110a3e0fd82Sopenharmony_ci      isSetAdaptered_(false),
111a3e0fd82Sopenharmony_ci      pickerListener_(nullptr)
112a3e0fd82Sopenharmony_ci{
113a3e0fd82Sopenharmony_ci    Theme* theme = ThemeManager::GetInstance().GetCurrent();
114a3e0fd82Sopenharmony_ci    if (theme != nullptr) {
115a3e0fd82Sopenharmony_ci        style_ = &(theme->GetPickerBackgroundStyle());
116a3e0fd82Sopenharmony_ci    } else {
117a3e0fd82Sopenharmony_ci        style_ = &(StyleDefault::GetPickerBackgroundStyle());
118a3e0fd82Sopenharmony_ci    }
119a3e0fd82Sopenharmony_ci    backgroundFontId_ = style_->font_;
120a3e0fd82Sopenharmony_ci    backgroundColor_ = style_->textColor_;
121a3e0fd82Sopenharmony_ci    direct_ = UITextLanguageDirect::TEXT_DIRECT_LTR;
122a3e0fd82Sopenharmony_ci
123a3e0fd82Sopenharmony_ci    if (theme != nullptr) {
124a3e0fd82Sopenharmony_ci        style_ = &(theme->GetPickerHighlightStyle());
125a3e0fd82Sopenharmony_ci    } else {
126a3e0fd82Sopenharmony_ci        style_ = &(StyleDefault::GetPickerHighlightStyle());
127a3e0fd82Sopenharmony_ci    }
128a3e0fd82Sopenharmony_ci    highlightFontId_ = style_->font_;
129a3e0fd82Sopenharmony_ci    highlightColor_ = style_->textColor_;
130a3e0fd82Sopenharmony_ci
131a3e0fd82Sopenharmony_ci    list_.SetThrowDrag(true);
132a3e0fd82Sopenharmony_ci    list_.SetStyle(StyleDefault::GetBackgroundTransparentStyle());
133a3e0fd82Sopenharmony_ci#if ENABLE_ROTATE_INPUT
134a3e0fd82Sopenharmony_ci    list_.rotateFactor_ = DEFAULT_PICKER_ROTATE_FACTOR;
135a3e0fd82Sopenharmony_ci    list_.rotateThrowthreshold_ = PICKERVIEW_ROTATE_THROW_THRESHOLD;
136a3e0fd82Sopenharmony_ci    list_.rotateAccCoefficient_ = PICKERVIEW_ROTATE_DISTANCE_COEFF;
137a3e0fd82Sopenharmony_ci#endif
138a3e0fd82Sopenharmony_ci#if ENABLE_FOCUS_MANAGER
139a3e0fd82Sopenharmony_ci    focusable_ = true;
140a3e0fd82Sopenharmony_ci#endif
141a3e0fd82Sopenharmony_ci    list_.SetLoopState(false);
142a3e0fd82Sopenharmony_ci    list_.EnableAutoAlign(true);
143a3e0fd82Sopenharmony_ci    listListener_ = new PickerListScrollListener(this, &list_);
144a3e0fd82Sopenharmony_ci    list_.SetScrollStateListener(listListener_);
145a3e0fd82Sopenharmony_ci    Add(&list_);
146a3e0fd82Sopenharmony_ci}
147a3e0fd82Sopenharmony_ci
148a3e0fd82Sopenharmony_ciUIPicker::~UIPicker()
149a3e0fd82Sopenharmony_ci{
150a3e0fd82Sopenharmony_ci    ClearValues();
151a3e0fd82Sopenharmony_ci    Remove(&list_);
152a3e0fd82Sopenharmony_ci    if (listListener_ != nullptr) {
153a3e0fd82Sopenharmony_ci        delete listListener_;
154a3e0fd82Sopenharmony_ci        listListener_ = nullptr;
155a3e0fd82Sopenharmony_ci    }
156a3e0fd82Sopenharmony_ci
157a3e0fd82Sopenharmony_ci    if (backgroundFontName_ != nullptr) {
158a3e0fd82Sopenharmony_ci        UIFree(backgroundFontName_);
159a3e0fd82Sopenharmony_ci        backgroundFontName_ = nullptr;
160a3e0fd82Sopenharmony_ci    }
161a3e0fd82Sopenharmony_ci
162a3e0fd82Sopenharmony_ci    if (highlightFontName_ != nullptr) {
163a3e0fd82Sopenharmony_ci        UIFree(highlightFontName_);
164a3e0fd82Sopenharmony_ci        highlightFontName_ = nullptr;
165a3e0fd82Sopenharmony_ci    }
166a3e0fd82Sopenharmony_ci
167a3e0fd82Sopenharmony_ci    if (textAdapter_ != nullptr) {
168a3e0fd82Sopenharmony_ci        delete textAdapter_;
169a3e0fd82Sopenharmony_ci        textAdapter_ = nullptr;
170a3e0fd82Sopenharmony_ci    }
171a3e0fd82Sopenharmony_ci}
172a3e0fd82Sopenharmony_ci
173a3e0fd82Sopenharmony_cibool UIPicker::SetValues(int16_t start, int16_t end)
174a3e0fd82Sopenharmony_ci{
175a3e0fd82Sopenharmony_ci    if (start > end) {
176a3e0fd82Sopenharmony_ci        return false;
177a3e0fd82Sopenharmony_ci    }
178a3e0fd82Sopenharmony_ci
179a3e0fd82Sopenharmony_ci    startValue_ = start;
180a3e0fd82Sopenharmony_ci    endValue_ = end;
181a3e0fd82Sopenharmony_ci    return RefreshValues(start, end);
182a3e0fd82Sopenharmony_ci}
183a3e0fd82Sopenharmony_ci
184a3e0fd82Sopenharmony_cibool UIPicker::SetValues(const char* value[], uint16_t count)
185a3e0fd82Sopenharmony_ci{
186a3e0fd82Sopenharmony_ci    if (value == nullptr) {
187a3e0fd82Sopenharmony_ci        return false;
188a3e0fd82Sopenharmony_ci    }
189a3e0fd82Sopenharmony_ci
190a3e0fd82Sopenharmony_ci    rangeValue_ = value;
191a3e0fd82Sopenharmony_ci    rangeValueCount_ = count;
192a3e0fd82Sopenharmony_ci    return RefreshValues(value, count);
193a3e0fd82Sopenharmony_ci}
194a3e0fd82Sopenharmony_ci
195a3e0fd82Sopenharmony_civoid UIPicker::Refresh()
196a3e0fd82Sopenharmony_ci{
197a3e0fd82Sopenharmony_ci    if (rangeValue_) {
198a3e0fd82Sopenharmony_ci        RefreshValues(rangeValue_, rangeValueCount_);
199a3e0fd82Sopenharmony_ci    } else if ((startValue_ != 0) || (endValue_ != 0)) {
200a3e0fd82Sopenharmony_ci        RefreshValues(startValue_, endValue_);
201a3e0fd82Sopenharmony_ci    }
202a3e0fd82Sopenharmony_ci}
203a3e0fd82Sopenharmony_ci
204a3e0fd82Sopenharmony_cibool UIPicker::RefreshValues(int16_t start, int16_t end)
205a3e0fd82Sopenharmony_ci{
206a3e0fd82Sopenharmony_ci    if ((start == 0) && (end == 0)) {
207a3e0fd82Sopenharmony_ci        return false;
208a3e0fd82Sopenharmony_ci    }
209a3e0fd82Sopenharmony_ci    maxCount_ = end - start + 1;
210a3e0fd82Sopenharmony_ci    if (!isWidthSet_ || !isHeightSet_ || !itemsHeight_) {
211a3e0fd82Sopenharmony_ci        return false;
212a3e0fd82Sopenharmony_ci    }
213a3e0fd82Sopenharmony_ci    uint16_t userSelectIndex = listListener_->GetSelectIndex();
214a3e0fd82Sopenharmony_ci    ClearList();
215a3e0fd82Sopenharmony_ci    InitTextAdapter();
216a3e0fd82Sopenharmony_ci    textAdapter_->SetData(start, end);
217a3e0fd82Sopenharmony_ci    RefreshList();
218a3e0fd82Sopenharmony_ci    RefreshSelected(userSelectIndex);
219a3e0fd82Sopenharmony_ci    return true;
220a3e0fd82Sopenharmony_ci}
221a3e0fd82Sopenharmony_ci
222a3e0fd82Sopenharmony_cibool UIPicker::RefreshValues(const char* value[], uint16_t count)
223a3e0fd82Sopenharmony_ci{
224a3e0fd82Sopenharmony_ci    if (value == nullptr) {
225a3e0fd82Sopenharmony_ci        return false;
226a3e0fd82Sopenharmony_ci    }
227a3e0fd82Sopenharmony_ci    maxCount_ = count;
228a3e0fd82Sopenharmony_ci    if (!isWidthSet_ || !isHeightSet_ || !itemsHeight_) {
229a3e0fd82Sopenharmony_ci        return false;
230a3e0fd82Sopenharmony_ci    }
231a3e0fd82Sopenharmony_ci    uint16_t userSelectIndex = listListener_->GetSelectIndex();
232a3e0fd82Sopenharmony_ci    ClearList();
233a3e0fd82Sopenharmony_ci    for (uint16_t i = 0; i < count; i++) {
234a3e0fd82Sopenharmony_ci        dataList_.PushBack(value[i]);
235a3e0fd82Sopenharmony_ci    }
236a3e0fd82Sopenharmony_ci    InitTextAdapter();
237a3e0fd82Sopenharmony_ci    textAdapter_->SetData(&dataList_);
238a3e0fd82Sopenharmony_ci    RefreshList();
239a3e0fd82Sopenharmony_ci    RefreshSelected(userSelectIndex);
240a3e0fd82Sopenharmony_ci
241a3e0fd82Sopenharmony_ci    return true;
242a3e0fd82Sopenharmony_ci}
243a3e0fd82Sopenharmony_ci
244a3e0fd82Sopenharmony_civoid UIPicker::RefreshList()
245a3e0fd82Sopenharmony_ci{
246a3e0fd82Sopenharmony_ci    int16_t height = GetHeight();
247a3e0fd82Sopenharmony_ci    itemsWidth_ = GetWidth();
248a3e0fd82Sopenharmony_ci    textAdapter_->SetWidth(itemsWidth_);
249a3e0fd82Sopenharmony_ci    textAdapter_->SetHeight(itemsHeight_);
250a3e0fd82Sopenharmony_ci    textAdapter_->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
251a3e0fd82Sopenharmony_ci    if (backgroundFontName_ == nullptr) {
252a3e0fd82Sopenharmony_ci        textAdapter_->SetFontId(backgroundFontId_);
253a3e0fd82Sopenharmony_ci    } else {
254a3e0fd82Sopenharmony_ci        textAdapter_->SetFont(backgroundFontName_, backgroundFontSize_);
255a3e0fd82Sopenharmony_ci    }
256a3e0fd82Sopenharmony_ci    textAdapter_->GetStyle().textColor_ = backgroundColor_;
257a3e0fd82Sopenharmony_ci    textAdapter_->SetDirect(direct_);
258a3e0fd82Sopenharmony_ci    list_.SetHeight(height);
259a3e0fd82Sopenharmony_ci    list_.SetWidth(itemsWidth_);
260a3e0fd82Sopenharmony_ci    list_.LayoutCenterOfParent();
261a3e0fd82Sopenharmony_ci    list_.SetSelectPosition(height / 2);                   // 2: half
262a3e0fd82Sopenharmony_ci    if (isScrollBlankSizeSet_) {
263a3e0fd82Sopenharmony_ci        list_.SetScrollBlankSize(scrollBlankSize_);
264a3e0fd82Sopenharmony_ci    } else {
265a3e0fd82Sopenharmony_ci        list_.SetScrollBlankSize((height - itemsHeight_) / 2); // 2: half
266a3e0fd82Sopenharmony_ci    }
267a3e0fd82Sopenharmony_ci    if (!isSetAdaptered_) {
268a3e0fd82Sopenharmony_ci        list_.SetAdapter(textAdapter_);
269a3e0fd82Sopenharmony_ci        isSetAdaptered_ = true;
270a3e0fd82Sopenharmony_ci    }
271a3e0fd82Sopenharmony_ci
272a3e0fd82Sopenharmony_ci    list_.RefreshList();
273a3e0fd82Sopenharmony_ci    RefreshSelected(0);
274a3e0fd82Sopenharmony_ci}
275a3e0fd82Sopenharmony_ci
276a3e0fd82Sopenharmony_civoid UIPicker::ClearValues()
277a3e0fd82Sopenharmony_ci{
278a3e0fd82Sopenharmony_ci    rangeValue_ = nullptr;
279a3e0fd82Sopenharmony_ci    rangeValueCount_ = 0;
280a3e0fd82Sopenharmony_ci    maxCount_ = 0;
281a3e0fd82Sopenharmony_ci    ClearList();
282a3e0fd82Sopenharmony_ci    ClearTextAdapter();
283a3e0fd82Sopenharmony_ci}
284a3e0fd82Sopenharmony_ci
285a3e0fd82Sopenharmony_civoid UIPicker::ClearList()
286a3e0fd82Sopenharmony_ci{
287a3e0fd82Sopenharmony_ci    itemsWidth_ = 0;
288a3e0fd82Sopenharmony_ci    if (listListener_) {
289a3e0fd82Sopenharmony_ci        listListener_->SetSelectView(nullptr);
290a3e0fd82Sopenharmony_ci        listListener_->SetSelectIndex(0);
291a3e0fd82Sopenharmony_ci        listListener_->SetInitStatus(false);
292a3e0fd82Sopenharmony_ci    }
293a3e0fd82Sopenharmony_ci    dataList_.Clear();
294a3e0fd82Sopenharmony_ci}
295a3e0fd82Sopenharmony_ci
296a3e0fd82Sopenharmony_civoid UIPicker::ClearTextAdapter()
297a3e0fd82Sopenharmony_ci{
298a3e0fd82Sopenharmony_ci    if (textAdapter_ != nullptr) {
299a3e0fd82Sopenharmony_ci        delete textAdapter_;
300a3e0fd82Sopenharmony_ci        textAdapter_ = nullptr;
301a3e0fd82Sopenharmony_ci    }
302a3e0fd82Sopenharmony_ci    list_.SetAdapter(textAdapter_);
303a3e0fd82Sopenharmony_ci    list_.RefreshList();
304a3e0fd82Sopenharmony_ci    isSetAdaptered_ = false;
305a3e0fd82Sopenharmony_ci}
306a3e0fd82Sopenharmony_ci
307a3e0fd82Sopenharmony_cibool UIPicker::SetSelected(uint16_t index)
308a3e0fd82Sopenharmony_ci{
309a3e0fd82Sopenharmony_ci    return RefreshSelected(index);
310a3e0fd82Sopenharmony_ci}
311a3e0fd82Sopenharmony_ci
312a3e0fd82Sopenharmony_cibool UIPicker::RefreshSelected(uint16_t index)
313a3e0fd82Sopenharmony_ci{
314a3e0fd82Sopenharmony_ci    if (maxCount_ <= index) {
315a3e0fd82Sopenharmony_ci        GRAPHIC_LOGW("Failed to refresh selected since index is beyond range!");
316a3e0fd82Sopenharmony_ci        return false;
317a3e0fd82Sopenharmony_ci    }
318a3e0fd82Sopenharmony_ci    if (itemsHeight_ && (list_.GetChildrenHead() != nullptr) && isWidthSet_ && isHeightSet_) {
319a3e0fd82Sopenharmony_ci        listListener_->SetInitStatus(false);
320a3e0fd82Sopenharmony_ci        // 2: half
321a3e0fd82Sopenharmony_ci        int16_t yOffset = (list_.GetHeight() - itemsHeight_) / 2 -
322a3e0fd82Sopenharmony_ci                          itemsHeight_ * (index - list_.GetChildrenHead()->GetViewIndex());
323a3e0fd82Sopenharmony_ci        list_.SetScrollStateListener(nullptr);
324a3e0fd82Sopenharmony_ci        list_.ScrollBy(yOffset - list_.GetChildrenHead()->GetY());
325a3e0fd82Sopenharmony_ci        list_.SetScrollStateListener(listListener_);
326a3e0fd82Sopenharmony_ci        listListener_->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
327a3e0fd82Sopenharmony_ci        UIView* childView = static_cast<UIView*>(list_.GetChildrenHead());
328a3e0fd82Sopenharmony_ci        uint16_t lastSelectIndex = listListener_->GetSelectIndex();
329a3e0fd82Sopenharmony_ci
330a3e0fd82Sopenharmony_ci        while (childView != nullptr) {
331a3e0fd82Sopenharmony_ci            int16_t viewIndex = childView->GetViewIndex();
332a3e0fd82Sopenharmony_ci            if (viewIndex == lastSelectIndex) {
333a3e0fd82Sopenharmony_ci                childView->SetStyle(STYLE_TEXT_COLOR, GetBackgroundTextColor().full);
334a3e0fd82Sopenharmony_ci                if (backgroundFontName_ == nullptr) {
335a3e0fd82Sopenharmony_ci                    static_cast<UILabel*>(childView)->SetFontId(backgroundFontId_);
336a3e0fd82Sopenharmony_ci                } else {
337a3e0fd82Sopenharmony_ci                    static_cast<UILabel*>(childView)->SetFont(backgroundFontName_, backgroundFontSize_);
338a3e0fd82Sopenharmony_ci                }
339a3e0fd82Sopenharmony_ci            }
340a3e0fd82Sopenharmony_ci            if (viewIndex == index) {
341a3e0fd82Sopenharmony_ci                childView->SetStyle(STYLE_TEXT_COLOR, GetHighlightTextColor().full);
342a3e0fd82Sopenharmony_ci                if (highlightFontName_ == nullptr) {
343a3e0fd82Sopenharmony_ci                    static_cast<UILabel*>(childView)->SetFontId(highlightFontId_);
344a3e0fd82Sopenharmony_ci                } else {
345a3e0fd82Sopenharmony_ci                    static_cast<UILabel*>(childView)->SetFont(highlightFontName_, highlightFontSize_);
346a3e0fd82Sopenharmony_ci                }
347a3e0fd82Sopenharmony_ci                listListener_->SetSelectView(childView);
348a3e0fd82Sopenharmony_ci                listListener_->SetInitStatus(true);
349a3e0fd82Sopenharmony_ci            }
350a3e0fd82Sopenharmony_ci            childView = childView->GetNextSibling();
351a3e0fd82Sopenharmony_ci        }
352a3e0fd82Sopenharmony_ci        listListener_->SetSelectIndex(index);
353a3e0fd82Sopenharmony_ci        list_.Invalidate();
354a3e0fd82Sopenharmony_ci        return true;
355a3e0fd82Sopenharmony_ci    }
356a3e0fd82Sopenharmony_ci    listListener_->SetSelectIndex(index);
357a3e0fd82Sopenharmony_ci    return false;
358a3e0fd82Sopenharmony_ci}
359a3e0fd82Sopenharmony_ci
360a3e0fd82Sopenharmony_ciuint16_t UIPicker::GetSelected() const
361a3e0fd82Sopenharmony_ci{
362a3e0fd82Sopenharmony_ci    return listListener_->GetSelectIndex();
363a3e0fd82Sopenharmony_ci}
364a3e0fd82Sopenharmony_ci
365a3e0fd82Sopenharmony_civoid UIPicker::SetFontId(uint16_t backgroundFontId, uint16_t highlightFontId)
366a3e0fd82Sopenharmony_ci{
367a3e0fd82Sopenharmony_ci    if ((backgroundFontId == backgroundFontId_) && (highlightFontId == highlightFontId_)) {
368a3e0fd82Sopenharmony_ci        return;
369a3e0fd82Sopenharmony_ci    }
370a3e0fd82Sopenharmony_ci    backgroundFontId_ = backgroundFontId;
371a3e0fd82Sopenharmony_ci    if (backgroundFontName_ != nullptr) {
372a3e0fd82Sopenharmony_ci        UIFree(backgroundFontName_);
373a3e0fd82Sopenharmony_ci        backgroundFontName_ = nullptr;
374a3e0fd82Sopenharmony_ci    }
375a3e0fd82Sopenharmony_ci
376a3e0fd82Sopenharmony_ci    highlightFontId_ = highlightFontId;
377a3e0fd82Sopenharmony_ci    if (highlightFontName_ != nullptr) {
378a3e0fd82Sopenharmony_ci        UIFree(highlightFontName_);
379a3e0fd82Sopenharmony_ci        highlightFontName_ = nullptr;
380a3e0fd82Sopenharmony_ci    }
381a3e0fd82Sopenharmony_ci
382a3e0fd82Sopenharmony_ci    Refresh();
383a3e0fd82Sopenharmony_ci}
384a3e0fd82Sopenharmony_ci
385a3e0fd82Sopenharmony_civoid UIPicker::SetBackgroundFont(const char* name, uint8_t size)
386a3e0fd82Sopenharmony_ci{
387a3e0fd82Sopenharmony_ci    if ((name != nullptr) && (backgroundFontName_ != nullptr)) {
388a3e0fd82Sopenharmony_ci        if (strcmp(name, backgroundFontName_) == 0 && size == backgroundFontSize_) {
389a3e0fd82Sopenharmony_ci            return;
390a3e0fd82Sopenharmony_ci        }
391a3e0fd82Sopenharmony_ci    }
392a3e0fd82Sopenharmony_ci    Text::SetFont(name, size, backgroundFontName_, backgroundFontSize_);
393a3e0fd82Sopenharmony_ci    Refresh();
394a3e0fd82Sopenharmony_ci}
395a3e0fd82Sopenharmony_ci
396a3e0fd82Sopenharmony_civoid UIPicker::SetHighlightFont(const char* name, uint8_t size)
397a3e0fd82Sopenharmony_ci{
398a3e0fd82Sopenharmony_ci    if ((name != nullptr) && (highlightFontName_ != nullptr)) {
399a3e0fd82Sopenharmony_ci        if (strcmp(name, highlightFontName_) == 0 && size == highlightFontSize_) {
400a3e0fd82Sopenharmony_ci            return;
401a3e0fd82Sopenharmony_ci        }
402a3e0fd82Sopenharmony_ci    }
403a3e0fd82Sopenharmony_ci    Text::SetFont(name, size, highlightFontName_, highlightFontSize_);
404a3e0fd82Sopenharmony_ci    Refresh();
405a3e0fd82Sopenharmony_ci}
406a3e0fd82Sopenharmony_ci
407a3e0fd82Sopenharmony_civoid UIPicker::SetTextColor(ColorType backgroundColor, ColorType highlightColor)
408a3e0fd82Sopenharmony_ci{
409a3e0fd82Sopenharmony_ci    if ((backgroundColor.full == backgroundColor_.full) && (highlightColor.full == highlightColor_.full)) {
410a3e0fd82Sopenharmony_ci        return;
411a3e0fd82Sopenharmony_ci    }
412a3e0fd82Sopenharmony_ci    backgroundColor_ = backgroundColor;
413a3e0fd82Sopenharmony_ci    highlightColor_ = highlightColor;
414a3e0fd82Sopenharmony_ci    Refresh();
415a3e0fd82Sopenharmony_ci}
416a3e0fd82Sopenharmony_ci
417a3e0fd82Sopenharmony_civoid UIPicker::SetItemHeight(int16_t height)
418a3e0fd82Sopenharmony_ci{
419a3e0fd82Sopenharmony_ci    if (height == itemsHeight_) {
420a3e0fd82Sopenharmony_ci        return;
421a3e0fd82Sopenharmony_ci    }
422a3e0fd82Sopenharmony_ci    if (height > 0) {
423a3e0fd82Sopenharmony_ci        itemsHeight_ = height;
424a3e0fd82Sopenharmony_ci        Refresh();
425a3e0fd82Sopenharmony_ci    }
426a3e0fd82Sopenharmony_ci}
427a3e0fd82Sopenharmony_ci
428a3e0fd82Sopenharmony_civoid UIPicker::SetWidth(int16_t width)
429a3e0fd82Sopenharmony_ci{
430a3e0fd82Sopenharmony_ci    if (width == UIView::GetWidth()) {
431a3e0fd82Sopenharmony_ci        return;
432a3e0fd82Sopenharmony_ci    }
433a3e0fd82Sopenharmony_ci    if (width > 0) {
434a3e0fd82Sopenharmony_ci        UIView::SetWidth(width);
435a3e0fd82Sopenharmony_ci        isWidthSet_ = true;
436a3e0fd82Sopenharmony_ci        Refresh();
437a3e0fd82Sopenharmony_ci    }
438a3e0fd82Sopenharmony_ci}
439a3e0fd82Sopenharmony_ci
440a3e0fd82Sopenharmony_civoid UIPicker::SetHeight(int16_t height)
441a3e0fd82Sopenharmony_ci{
442a3e0fd82Sopenharmony_ci    if (height == UIView::GetHeight()) {
443a3e0fd82Sopenharmony_ci        return;
444a3e0fd82Sopenharmony_ci    }
445a3e0fd82Sopenharmony_ci    if (height > 0) {
446a3e0fd82Sopenharmony_ci        UIView::SetHeight(height);
447a3e0fd82Sopenharmony_ci        isHeightSet_ = true;
448a3e0fd82Sopenharmony_ci        Refresh();
449a3e0fd82Sopenharmony_ci    }
450a3e0fd82Sopenharmony_ci}
451a3e0fd82Sopenharmony_ci
452a3e0fd82Sopenharmony_civoid UIPicker::SetLoopState(bool state)
453a3e0fd82Sopenharmony_ci{
454a3e0fd82Sopenharmony_ci    if (state == list_.GetLoopState()) {
455a3e0fd82Sopenharmony_ci        return;
456a3e0fd82Sopenharmony_ci    }
457a3e0fd82Sopenharmony_ci    list_.SetLoopState(state);
458a3e0fd82Sopenharmony_ci    Refresh();
459a3e0fd82Sopenharmony_ci}
460a3e0fd82Sopenharmony_ci
461a3e0fd82Sopenharmony_civoid UIPicker::SetDirect(UITextLanguageDirect direct)
462a3e0fd82Sopenharmony_ci{
463a3e0fd82Sopenharmony_ci    if (direct == direct_) {
464a3e0fd82Sopenharmony_ci        return;
465a3e0fd82Sopenharmony_ci    }
466a3e0fd82Sopenharmony_ci    direct_ = direct;
467a3e0fd82Sopenharmony_ci    Refresh();
468a3e0fd82Sopenharmony_ci}
469a3e0fd82Sopenharmony_ci
470a3e0fd82Sopenharmony_civoid UIPicker::SetTextFormatter(TextFormatter* formatter)
471a3e0fd82Sopenharmony_ci{
472a3e0fd82Sopenharmony_ci    InitTextAdapter();
473a3e0fd82Sopenharmony_ci    textAdapter_->SetTextFormatter(formatter);
474a3e0fd82Sopenharmony_ci    Refresh();
475a3e0fd82Sopenharmony_ci}
476a3e0fd82Sopenharmony_ci} // namespace OHOS
477