ui_picker.cpp 13.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "components/ui_picker.h"
Y
YueBiang 已提交
17
#include "dock/vibrator_manager.h"
M
mamingshuai 已提交
18 19 20 21
#include "draw/draw_line.h"
#include "draw/draw_rect.h"
#include "themes/theme_manager.h"

Y
YueBiang 已提交
22 23 24 25 26 27
namespace {
#if ENABLE_ROTATE_INPUT
constexpr float DEFAULT_PICKER_ROTATE_FACTOR = 1.668;
#endif
}

M
mamingshuai 已提交
28 29 30 31
namespace OHOS {
class PickerListScrollListener : public ListScrollListener {
public:
    PickerListScrollListener(UIPicker* picker, UIList* list)
32 33 34 35 36 37 38 39
        : listView_(list),
          pickerView_(picker),
          selectView_(nullptr),
          lastSelectView_(nullptr),
          selectIndex_(0),
          isInitted_(false)
    {
    }
M
mamingshuai 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    virtual ~PickerListScrollListener() {}

    void OnItemSelected(int16_t index, UIView* view) override
    {
        if (!isInitted_) {
            return;
        }

        if ((lastSelectView_ != nullptr) && (listView_ != nullptr) && (pickerView_ != nullptr) && (view != nullptr)) {
            lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
            if (pickerView_->backgroundFontName_ == nullptr) {
                static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
            } else {
                static_cast<UILabel*>(lastSelectView_)
                    ->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
            }
            view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
            if (pickerView_->highlightFontName_ == nullptr) {
                static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
            } else {
61
                static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
M
mamingshuai 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            }
            lastSelectView_ = view;
            selectIndex_ = index;
            listView_->Invalidate();
        }
    }

    void OnScrollEnd(int16_t index, UIView* view) override
    {
        if ((view == nullptr) || (listView_ == nullptr) || (pickerView_ == nullptr)) {
            return;
        }

        if (lastSelectView_ != nullptr) {
            lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
            if (pickerView_->backgroundFontName_ == nullptr) {
                static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
            } else {
                static_cast<UILabel*>(lastSelectView_)
                    ->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
            }
            lastSelectView_ = view;
        }

        view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
        if (pickerView_->highlightFontName_ == nullptr) {
            static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
        } else {
90
            static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
M
mamingshuai 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        }

        listView_->Invalidate();
        selectView_ = view;
        selectIndex_ = index;

        if (pickerView_->pickerListener_) {
            pickerView_->pickerListener_->OnPickerStoped(*pickerView_);
        }
    }

    void SetSelectView(UIView* view)
    {
        selectView_ = view;
        lastSelectView_ = view;
    }

    const UIView* GetSelectView() const
    {
        return selectView_;
    }

    void SetSelectIndex(uint16_t index)
    {
        selectIndex_ = index;
    }

    uint16_t GetSelectIndex() const
    {
        return selectIndex_;
    }

    void SetInitStatus(bool status)
    {
        isInitted_ = status;
    }

private:
    UIList* listView_;
    UIPicker* pickerView_;
    UIView* selectView_;
    UIView* lastSelectView_;
    uint16_t selectIndex_;
    bool isInitted_;
};

UIPicker::UIPicker()
    : isWidthSet_(false),
      isHeightSet_(false),
      textAdapter_(nullptr),
      maxCount_(0),
      setSelectedIndex_(0),
      backgroundFontSize_(0),
      highlightFontSize_(0),
      backgroundFontName_(nullptr),
      highlightFontName_(nullptr),
      itemsWidth_(0),
      itemsHeight_(0),
      rangeValue_(nullptr),
      rangeValueCount_(0),
      startValue_(0),
      endValue_(0),
      isSetAdaptered_(false),
      pickerListener_(nullptr)
{
    Theme* theme = ThemeManager::GetInstance().GetCurrent();
    if (theme != nullptr) {
        style_ = &(theme->GetPickerBackgroundStyle());
    } else {
        style_ = &(StyleDefault::GetPickerBackgroundStyle());
    }
    backgroundFontId_ = style_->font_;
    backgroundColor_ = style_->textColor_;
    direct_ = UITextLanguageDirect::TEXT_DIRECT_LTR;

    if (theme != nullptr) {
        style_ = &(theme->GetPickerHighlightStyle());
    } else {
        style_ = &(StyleDefault::GetPickerHighlightStyle());
    }
    highlightFontId_ = style_->font_;
    highlightColor_ = style_->textColor_;

    list_.SetThrowDrag(true);
#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
176
    list_.rotateFactor_ = DEFAULT_PICKER_ROTATE_FACTOR;
M
mamingshuai 已提交
177
#endif
Y
YueBiang 已提交
178 179
#if ENABLE_VIBRATOR
    list_.SetMotorType(VibratorType::VIBRATOR_TYPE_TWO);
S
suyue 已提交
180 181 182
#endif
#if ENABLE_FOCUS_MANAGER
    focusable_ = true;
M
mamingshuai 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196
#endif
    list_.SetLoopState(false);
    list_.EnableAutoAlign(true);
    PickerListScrollListener* listener = new PickerListScrollListener(this, &list_);
    list_.SetScrollStateListener(listener);
    listListener_ = static_cast<void*>(listener);
    Add(&list_);
}

UIPicker::~UIPicker()
{
    ClearValues();
    Remove(&list_);
    if (listListener_ != nullptr) {
197
        delete static_cast<PickerListScrollListener*>(listListener_);
M
mamingshuai 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        listListener_ = nullptr;
    }

    if (backgroundFontName_ != nullptr) {
        UIFree(backgroundFontName_);
        backgroundFontName_ = nullptr;
    }

    if (highlightFontName_ != nullptr) {
        UIFree(highlightFontName_);
        highlightFontName_ = nullptr;
    }

    if (textAdapter_ != nullptr) {
        delete textAdapter_;
        textAdapter_ = nullptr;
    }
}

bool UIPicker::SetValues(int16_t start, int16_t end)
{
    if (start > end) {
        return false;
    }

    startValue_ = start;
    endValue_ = end;
    return RefreshValues(start, end);
}

bool UIPicker::SetValues(const char* value[], uint16_t count)
{
    if (value == nullptr) {
        return false;
    }

    rangeValue_ = value;
    rangeValueCount_ = count;
    return RefreshValues(value, count);
}

void UIPicker::Refresh()
{
    if (rangeValue_) {
        RefreshValues(rangeValue_, rangeValueCount_);
    } else if ((startValue_ != 0) || (endValue_ != 0)) {
        RefreshValues(startValue_, endValue_);
    }
}

bool UIPicker::RefreshValues(int16_t start, int16_t end)
{
    if (!isWidthSet_ || !isHeightSet_ || !itemsHeight_ || ((start == 0) && (end == 0))) {
        return false;
    }
Z
zhangguyuan 已提交
253
    uint16_t userSelectInndex = static_cast<PickerListScrollListener*>(listListener_)->GetSelectIndex();
M
mamingshuai 已提交
254 255 256 257 258
    ClearList();
    InitTextAdapter();
    textAdapter_->SetData(start, end);
    maxCount_ = end - start + 1;
    RefreshList();
Z
zhangguyuan 已提交
259 260 261
    if (userSelectInndex) {
        RefreshSelected(userSelectInndex);
    } else if (setSelectedIndex_) {
M
mamingshuai 已提交
262 263 264 265 266 267 268 269 270 271
        RefreshSelected(setSelectedIndex_);
    }
    return true;
}

bool UIPicker::RefreshValues(const char* value[], uint16_t count)
{
    if (value == nullptr || !isWidthSet_ || !isHeightSet_ || !itemsHeight_) {
        return false;
    }
Z
zhangguyuan 已提交
272
    uint16_t userSelectInndex = static_cast<PickerListScrollListener*>(listListener_)->GetSelectIndex();
M
mamingshuai 已提交
273 274 275 276 277 278 279 280
    ClearList();
    for (uint16_t i = 0; i < count; i++) {
        dataList_.PushBack(value[i]);
    }
    InitTextAdapter();
    textAdapter_->SetData(&dataList_);
    maxCount_ = count;
    RefreshList();
Z
zhangguyuan 已提交
281 282 283
    if (userSelectInndex != 0) {
        RefreshSelected(userSelectInndex);
    } else if (setSelectedIndex_ != 0) {
M
mamingshuai 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        RefreshSelected(setSelectedIndex_);
    }

    return true;
}

void UIPicker::RefreshList()
{
    int16_t height = GetHeight();
    itemsWidth_ = GetWidth();
    textAdapter_->SetWidth(itemsWidth_);
    textAdapter_->SetHeight(itemsHeight_);
    textAdapter_->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
    if (backgroundFontName_ == nullptr) {
        textAdapter_->SetFontId(backgroundFontId_);
    } else {
        textAdapter_->SetFont(backgroundFontName_, backgroundFontSize_);
    }
    textAdapter_->GetStyle().textColor_ = backgroundColor_;
    textAdapter_->SetDirect(direct_);
    list_.SetHeight(height);
    list_.SetWidth(itemsWidth_);
306
    list_.LayoutCenterOfParent();
M
mamingshuai 已提交
307
    list_.SetScrollBlankSize((height - itemsHeight_) / 2); // 2: half
308
    list_.SetSelectPosition(height / 2);                   // 2: half
M
mamingshuai 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    if (!isSetAdaptered_) {
        list_.SetAdapter(textAdapter_);
        isSetAdaptered_ = true;
    }

    list_.RefreshList();
    RefreshSelected(0);
}

void UIPicker::ClearValues()
{
    rangeValue_ = nullptr;
    rangeValueCount_ = 0;
    setSelectedIndex_ = 0;
    ClearList();
}

void UIPicker::ClearList()
{
    maxCount_ = 0;
    itemsWidth_ = 0;
    if (listListener_) {
        PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
        listListener->SetSelectView(nullptr);
        listListener->SetSelectIndex(0);
        listListener->SetInitStatus(false);
    }
    dataList_.Clear();
}

bool UIPicker::SetSelected(uint16_t index)
{
    setSelectedIndex_ = index;
    return RefreshSelected(index);
}

bool UIPicker::RefreshSelected(uint16_t index)
{
    if (itemsHeight_ && (maxCount_ > index) && (list_.GetChildrenHead() != nullptr) && isWidthSet_ && isHeightSet_) {
        PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
        listListener->SetInitStatus(false);
        // 2: half
        int16_t yOffset = (list_.GetHeight() - itemsHeight_) / 2 -
352
                          itemsHeight_ * (index - list_.GetChildrenHead()->GetViewIndex());
M
mamingshuai 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        list_.SetScrollStateListener(nullptr);
        list_.ScrollBy(yOffset - list_.GetChildrenHead()->GetY());
        list_.SetScrollStateListener(listListener);
        listListener->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
        UIView* childView = static_cast<UIView*>(list_.GetChildrenHead());
        uint16_t lastSelectIndex = listListener->GetSelectIndex();

        int16_t viewIndex;
        while (childView != nullptr) {
            viewIndex = childView->GetViewIndex();
            if (viewIndex == lastSelectIndex) {
                childView->SetStyle(STYLE_TEXT_COLOR, GetBackgroundTextColor().full);
                if (backgroundFontName_ == nullptr) {
                    static_cast<UILabel*>(childView)->SetFontId(backgroundFontId_);
                } else {
                    static_cast<UILabel*>(childView)->SetFont(backgroundFontName_, backgroundFontSize_);
                }
            }
            if (viewIndex == index) {
                childView->SetStyle(STYLE_TEXT_COLOR, GetHighlightTextColor().full);
                if (highlightFontName_ == nullptr) {
                    static_cast<UILabel*>(childView)->SetFontId(highlightFontId_);
                } else {
                    static_cast<UILabel*>(childView)->SetFont(highlightFontName_, highlightFontSize_);
                }
                listListener->SetSelectView(childView);
                listListener->SetSelectIndex(index);
                listListener->SetInitStatus(true);
            }
            childView = childView->GetNextSibling();
        }
        list_.Invalidate();
        return true;
    }
    return false;
}

uint16_t UIPicker::GetSelected() const
{
    PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
    return listListener->GetSelectIndex();
}

void UIPicker::SetFontId(uint8_t backgroundFontId, uint8_t highlightFontId)
{
    backgroundFontId_ = backgroundFontId;
    if (backgroundFontName_ != nullptr) {
        UIFree(backgroundFontName_);
        backgroundFontName_ = nullptr;
    }

    highlightFontId_ = highlightFontId;
    if (highlightFontName_ != nullptr) {
        UIFree(highlightFontName_);
        highlightFontName_ = nullptr;
    }

    Refresh();
}

void UIPicker::SetBackgroundFont(const char* name, uint8_t size)
{
    Text::SetFont(name, size, backgroundFontName_, backgroundFontSize_);
    Refresh();
}

void UIPicker::SetHighlightFont(const char* name, uint8_t size)
{
    Text::SetFont(name, size, highlightFontName_, highlightFontSize_);
    Refresh();
}

void UIPicker::SetTextColor(ColorType backgroundColor, ColorType highlightColor)
{
    backgroundColor_ = backgroundColor;
    highlightColor_ = highlightColor;
    Refresh();
}

void UIPicker::SetItemHeight(int16_t height)
{
    if (height > 0) {
        itemsHeight_ = height;
        Refresh();
    }
}

void UIPicker::SetWidth(int16_t width)
{
    if (width > 0) {
        UIView::SetWidth(width);
        isWidthSet_ = true;
        Refresh();
    }
}

void UIPicker::SetHeight(int16_t height)
{
    if (height > 0) {
        UIView::SetHeight(height);
        isHeightSet_ = true;
        Refresh();
    }
}

void UIPicker::SetLoopState(bool state)
{
    list_.SetLoopState(state);
W
wangtiantian 已提交
461
    Refresh();
M
mamingshuai 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

void UIPicker::SetDirect(UITextLanguageDirect direct)
{
    direct_ = direct;
    Refresh();
}

void UIPicker::SetTextFormatter(TextFormatter* formatter)
{
    InitTextAdapter();
    textAdapter_->SetTextFormatter(formatter);
    Refresh();
}
476
} // namespace OHOS