ui_list.cpp 29.9 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_list.h"
L
liqiang 已提交
17

Y
YueBiang 已提交
18
#include "components/ui_abstract_scroll_bar.h"
Y
YueBiang 已提交
19 20
#include "gfx_utils/graphic_log.h"

M
mamingshuai 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
namespace OHOS {
UIList::Recycle::~Recycle()
{
    ListNode<UIView*>* node = scrapView_.Begin();
    while (node != scrapView_.End()) {
        if (node->data_) {
            UIView* deleteView = node->data_;
            if (deleteView != nullptr) {
                delete deleteView;
                deleteView = nullptr;
                node->data_ = nullptr;
            }
        }
        node = node->next_;
    }
    scrapView_.Clear();
}

B
benb365 已提交
39
void UIList::Recycle::MeasureAdapterRelativeRect()
Y
YueBiang 已提交
40 41
{
    uint16_t i = 0;
Y
YueBiang 已提交
42 43 44
    if (listView_ == nullptr) {
        return;
    }
Y
YueBiang 已提交
45
    UIView* childHead = listView_->childrenHead_;
Y
YueBiang 已提交
46 47 48
    if (childHead == nullptr) {
        return;
    }
Y
YueBiang 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    uint16_t idx = childHead->GetViewIndex();
    if (listView_->direction_ == VERTICAL) {
        int32_t height = 0;
        for (; i < idx; i++) {
            height += adapter_->GetItemHeightWithMargin(i);
        }
        int16_t y = childHead->GetRelativeRect().GetTop() - height - childHead->GetStyle(STYLE_MARGIN_TOP);
        for (; i < adapter_->GetCount(); i++) {
            height += adapter_->GetItemHeightWithMargin(i);
        }
        adapterRelativeRect_.SetRect(0, y, listView_->GetWidth() - 1, y + height - 1);
    } else {
        int32_t width = 0;
        for (; i < idx; i++) {
            width += adapter_->GetItemWidthWithMargin(i);
        }
        int16_t x = childHead->GetRelativeRect().GetLeft() - width - childHead->GetStyle(STYLE_MARGIN_LEFT);
        for (; i < adapter_->GetCount(); i++) {
            width += adapter_->GetItemWidthWithMargin(i);
        }
        adapterRelativeRect_.SetRect(x, 0, x + width - 1, listView_->GetHeight() - 1);
    }
}

M
mamingshuai 已提交
73 74 75 76 77 78 79
void UIList::Recycle::InitRecycle()
{
    if ((adapter_ == nullptr) || (listView_ == nullptr)) {
        return;
    }
    FillActiveView();
    listView_->Invalidate();
Y
YueBiang 已提交
80
    hasInitialiszed_ = true;
Y
YueBiang 已提交
81
    if (listView_->xScrollBarVisible_ || listView_->yScrollBarVisible_) {
B
benb365 已提交
82
        MeasureAdapterRelativeRect();
Y
YueBiang 已提交
83
    }
M
mamingshuai 已提交
84 85 86 87 88 89 90 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
}

UIView* UIList::Recycle::GetView(int16_t index)
{
    if (adapter_ == nullptr) {
        return nullptr;
    }
    UIView* inView = nullptr;
    UIView* retView = nullptr;

    if (scrapView_.Size() != 0) {
        inView = scrapView_.Back();
    }

    retView = adapter_->GetView(inView, index);
    if (retView != nullptr) {
        retView->SetViewIndex(index);
        scrapView_.PopBack();
    }
    return retView;
}

void UIList::Recycle::FillActiveView()
{
    uint16_t index = listView_->GetStartIndex();
    if (listView_->GetDirection() == UIList::VERTICAL) {
        int16_t childBottom = 0;
        while ((index < adapter_->GetCount()) && (childBottom < listView_->GetHeight())) {
            UIView* view = GetView(index);
            if (view == nullptr) {
                break;
            }
            listView_->PushBack(view);
            if (listView_->childrenTail_) {
                childBottom =
                    listView_->childrenTail_->GetY() + listView_->childrenTail_->GetRelativeRect().GetHeight();
            } else {
                break;
            }
            index++;
        }
    } else {
        int16_t childRight = 0;
        while ((index < adapter_->GetCount()) && (childRight < listView_->GetWidth())) {
            UIView* view = GetView(index);
            listView_->PushBack(view);
            if (listView_->childrenTail_) {
                childRight = listView_->childrenTail_->GetX() + listView_->childrenTail_->GetRelativeRect().GetWidth();
            } else {
                break;
            }
            index++;
        }
    }
}

B
benb365 已提交
140
Rect32 UIList::Recycle::GetAdapterItemsReletiveRect()
Y
YueBiang 已提交
141 142 143 144 145 146
{
    return adapterRelativeRect_;
}

void UIList::Recycle::MoveAdapterItemsRelativeRect(int16_t x, int16_t y)
{
B
benb365 已提交
147
    auto& rect = adapterRelativeRect_;
Y
YueBiang 已提交
148 149 150
    rect.SetPosition(rect.GetX() + x, rect.GetY() + y);
}

151
UIList::UIList() : UIList(VERTICAL) {}
M
mamingshuai 已提交
152 153 154 155 156 157

UIList::UIList(uint8_t direction)
    : onSelectedView_(nullptr),
      isLoopList_(false),
      isReCalculateDragEnd_(true),
      autoAlign_(false),
L
liqiang 已提交
158
      alignTime_(DEFAULT_ALIGN_TIMES),
M
mamingshuai 已提交
159 160 161 162 163 164 165 166 167
      startIndex_(0),
      topIndex_(0),
      bottomIndex_(0),
      selectPosition_(0),
      onSelectedIndex_(0),
      recycle_(this),
      scrollListener_(nullptr)
{
#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
168
    rotateFactor_ = DEFAULT_LIST_ROTATE_FACTOR;
169 170
    rotateThrowthreshold_ = LIST_ROTATE_THROW_THRESHOLD;
    rotateAccCoefficient_ = LIST_ROTATE_DISTANCE_COEFF;
M
mamingshuai 已提交
171
#endif
S
suyue 已提交
172 173
#if ENABLE_FOCUS_MANAGER
    focusable_ = true;
M
mamingshuai 已提交
174 175 176 177 178 179 180 181 182 183 184 185
#endif
    direction_ = direction;
    touchable_ = true;
    draggable_ = true;
    dragParentInstead_ = false;
}

UIList::~UIList()
{
    UIView* view = GetChildrenHead();
    while (view != nullptr) {
        UIView* tmp = view->GetNextSibling();
W
wangtiantian 已提交
186
        UIViewGroup::Remove(view);
M
mamingshuai 已提交
187 188 189 190 191 192 193 194 195 196
        delete view;
        view = tmp;
    }
}

bool UIList::OnDragEvent(const DragEvent& event)
{
    if (scrollAnimator_.GetState() != Animator::STOP) {
        UIAbstractScroll::StopAnimator();
    }
L
liqiang 已提交
197
    isReCalculateDragEnd_ = true;
M
mamingshuai 已提交
198 199 200
    int16_t xDistance = event.GetDeltaX();
    int16_t yDistance = event.GetDeltaY();
    if (direction_ == VERTICAL) {
L
liqiang 已提交
201
        RefreshDelta(yDistance);
M
mamingshuai 已提交
202 203
        DragYInner(yDistance);
    } else {
L
liqiang 已提交
204
        RefreshDelta(xDistance);
M
mamingshuai 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
        DragXInner(xDistance);
    }
    return UIView::OnDragEvent(event);
}

bool UIList::OnDragEndEvent(const DragEvent& event)
{
    Point last = event.GetPreLastPoint();
    Point current = event.GetLastPoint();
    if ((last.x == current.x) && (last.y == current.y)) {
        last = current;
        current = event.GetCurrentPos();
    }
    isReCalculateDragEnd_ = false;
L
liqiang 已提交
219
    if (!DragThrowAnimator(current, last, event.GetDragDirection(), dragBack_)) {
M
mamingshuai 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        if (scrollListener_ && (scrollListener_->GetScrollState() == ListScrollListener::SCROLL_STATE_MOVE)) {
            scrollListener_->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
            scrollListener_->OnScrollEnd(onSelectedIndex_, onSelectedView_);
        }
    }
    return UIView::OnDragEndEvent(event);
}

bool UIList::OnPressEvent(const PressEvent& event)
{
    StopAnimator();
    return UIView::OnPressEvent(event);
}

#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
235 236 237
bool UIList::OnRotateStartEvent(const RotateEvent& event)
{
    isReCalculateDragEnd_ = true;
Y
YueBiang 已提交
238
    return UIAbstractScroll::OnRotateStartEvent(event);
Y
YueBiang 已提交
239 240 241 242 243
}

bool UIList::OnRotateEndEvent(const RotateEvent& event)
{
    isReCalculateDragEnd_ = false;
244
    return UIAbstractScroll::OnRotateEndEvent(event);
M
mamingshuai 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}
#endif

void UIList::ScrollBy(int16_t distance)
{
    if (direction_ == VERTICAL) {
        DragYInner(distance);
    } else {
        DragXInner(distance);
    }
    if (scrollListener_ && (scrollListener_->GetScrollState() == ListScrollListener::SCROLL_STATE_MOVE)) {
        scrollListener_->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
        scrollListener_->OnScrollEnd(onSelectedIndex_, onSelectedView_);
    }
}

bool UIList::DragXInner(int16_t distance)
{
    if (IsNeedReCalculateDragEnd()) {
        return false;
    }
    int16_t listWidth = GetWidth();
    if (distance == 0) {
        return true;
    }
    int16_t reboundSize = reboundSize_;
    if (isLoopList_ || (scrollAnimator_.GetState() != Animator::STOP)) {
        reboundSize = 0;
    }
    bool ret = 0;
    do {
        ret = MoveChildStep(distance);
    } while (ret);

    if (isLoopList_) {
Y
YueBiang 已提交
280
        return MoveOffset(distance, 0);
M
mamingshuai 已提交
281 282
    }
    if (distance > 0) {
283 284
        if (childrenHead_ && ((childrenHead_->GetX() + distance) > (scrollBlankSize_ + reboundSize))) {
            distance = scrollBlankSize_ + reboundSize - childrenHead_->GetX();
M
mamingshuai 已提交
285 286 287
        }
    } else {
        if (childrenTail_) {
288 289
            if (childrenTail_->GetRelativeRect().GetRight() + childrenTail_->GetStyle(STYLE_MARGIN_RIGHT) <
                listWidth - scrollBlankSize_ - reboundSize) {
M
mamingshuai 已提交
290
                distance = 0;
291 292 293 294
            } else if ((childrenTail_->GetRelativeRect().GetRight() + childrenTail_->GetStyle(STYLE_MARGIN_RIGHT) +
                        distance) <= (listWidth - scrollBlankSize_ - reboundSize)) {
                distance = listWidth - scrollBlankSize_ - reboundSize - childrenTail_->GetRelativeRect().GetRight() -
                           childrenTail_->GetStyle(STYLE_MARGIN_RIGHT) - 1;
M
mamingshuai 已提交
295 296 297
            }
        }
    }
Y
YueBiang 已提交
298
    return MoveOffset(distance, 0);
M
mamingshuai 已提交
299 300 301 302 303 304 305
}

bool UIList::DragYInner(int16_t distance)
{
    if (IsNeedReCalculateDragEnd()) {
        return false;
    }
Y
YueBiang 已提交
306
    int16_t listHeight = GetHeight();
M
mamingshuai 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319
    if (distance == 0) {
        return true;
    }
    int16_t reboundSize = reboundSize_;
    if (isLoopList_ || (scrollAnimator_.GetState() != Animator::STOP)) {
        reboundSize = 0;
    }
    bool ret = 0;
    do {
        ret = MoveChildStep(distance);
    } while (ret);

    if (isLoopList_) {
Y
YueBiang 已提交
320
        return MoveOffset(0, distance);
M
mamingshuai 已提交
321 322
    }
    if (distance > 0) {
323 324
        if (childrenHead_ && ((childrenHead_->GetY() + distance) > (scrollBlankSize_ + reboundSize))) {
            distance = scrollBlankSize_ + reboundSize - childrenHead_->GetY();
M
mamingshuai 已提交
325 326 327
        }
    } else {
        if (childrenTail_) {
328 329
            if (childrenTail_->GetRelativeRect().GetBottom() + childrenTail_->GetStyle(STYLE_MARGIN_BOTTOM) <
                listHeight - scrollBlankSize_ - reboundSize) {
M
mamingshuai 已提交
330
                distance = 0;
331 332 333 334
            } else if ((childrenTail_->GetRelativeRect().GetBottom() + childrenTail_->GetStyle(STYLE_MARGIN_BOTTOM) +
                        distance) <= (listHeight - scrollBlankSize_ - reboundSize)) {
                distance = listHeight - scrollBlankSize_ - reboundSize - childrenTail_->GetRelativeRect().GetBottom() -
                           childrenTail_->GetStyle(STYLE_MARGIN_BOTTOM) - 1;
M
mamingshuai 已提交
335 336 337
            }
        }
    }
Y
YueBiang 已提交
338
    return MoveOffset(0, distance);
M
mamingshuai 已提交
339 340
}

Y
YueBiang 已提交
341
bool UIList::MoveOffset(int16_t x, int16_t y)
M
mamingshuai 已提交
342
{
Y
YueBiang 已提交
343
    if ((x == 0) && (y == 0)) {
M
mamingshuai 已提交
344 345
        return false;
    }
Y
YueBiang 已提交
346 347 348 349
    MoveChildByOffset(x, y);
    if (xScrollBarVisible_ || yScrollBarVisible_) {
        recycle_.MoveAdapterItemsRelativeRect(x, y);
        UpdateScrollBar();
M
mamingshuai 已提交
350 351 352 353 354 355 356
    }
    Invalidate();
    if (scrollListener_ && (scrollListener_->GetScrollState() == ListScrollListener::SCROLL_STATE_STOP)) {
        scrollListener_->SetScrollState(ListScrollListener::SCROLL_STATE_MOVE);
        scrollListener_->OnScrollStart(onSelectedIndex_, onSelectedView_);
    }

357 358
    if (!isLoopList_ && scrollListener_) {
        if (direction_ == VERTICAL) {
359 360
            if (childrenHead_ && (childrenHead_->GetViewIndex() == 0) &&
                childrenHead_->GetRelativeRect().GetTop() >= 0 && childrenHead_->GetRelativeRect().GetTop() - y < 0) {
361 362
                scrollListener_->OnScrollTop(childrenHead_->GetViewIndex(), childrenHead_);
            }
363
            if (childrenTail_ && (childrenTail_->GetViewIndex() == recycle_.GetAdapterItemCount() - 1) &&
364 365
                (childrenTail_->GetRelativeRect().GetBottom() <= GetContentRect().GetHeight() - 1) &&
                (childrenTail_->GetRelativeRect().GetBottom() - y > GetContentRect().GetHeight() - 1)) {
366
                scrollListener_->OnScrollBottom(childrenTail_->GetViewIndex(), childrenTail_);
367
            }
L
lancer 已提交
368
        } else {
369 370
            if (childrenHead_ && (childrenHead_->GetViewIndex() == 0) &&
                childrenHead_->GetRelativeRect().GetLeft() >= 0 && childrenHead_->GetRelativeRect().GetLeft() - x < 0) {
371 372
                scrollListener_->OnScrollTop(childrenHead_->GetViewIndex(), childrenHead_);
            }
373
            if (childrenTail_ && (childrenTail_->GetViewIndex() == recycle_.GetAdapterItemCount() - 1) &&
374 375
                (childrenTail_->GetRelativeRect().GetRight() <= GetContentRect().GetWidth() - 1) &&
                (childrenTail_->GetRelativeRect().GetRight() - x > GetContentRect().GetWidth() - 1)) {
376 377 378 379
                scrollListener_->OnScrollBottom(childrenTail_->GetViewIndex(), childrenTail_);
            }
        }
    }
M
mamingshuai 已提交
380 381 382
    return true;
}

Y
YueBiang 已提交
383 384
void UIList::UpdateScrollBar()
{
B
benb365 已提交
385
    auto allItemsRect = recycle_.GetAdapterItemsReletiveRect();
L
liuyongchang 已提交
386
    float totalHeight = allItemsRect.GetHeight() + 2.0f * scrollBlankSize_; // 2: two blank spaces on both sides
Y
YueBiang 已提交
387
    int16_t height = GetHeight();
L
liuyongchang 已提交
388 389
    yScrollBar_->SetForegroundProportion(height / totalHeight);
    yScrollBar_->SetScrollProgress((scrollBlankSize_ - allItemsRect.GetTop()) /
Y
YueBiang 已提交
390 391 392 393
                                   (totalHeight - height));
    RefreshAnimator();
}

M
mamingshuai 已提交
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
bool UIList::IsNeedReCalculateDragEnd()
{
    if (!autoAlign_ || isReCalculateDragEnd_ || (onSelectedView_ == nullptr)) {
        return false;
    }
    int16_t animationLess = 0;
    if (direction_ == VERTICAL) {
        animationLess = animatorCallback_.endValueY_ - animatorCallback_.previousValueY_;
    } else {
        animationLess = animatorCallback_.endValueX_ - animatorCallback_.previousValueX_;
    }
    if (!isDragging_ || (MATH_ABS(animationLess) > RECALCULATE_DRAG_DISTANCE)) {
        return false;
    }
    return true;
}
bool UIList::ReCalculateDragEnd()
{
    if ((onSelectedView_ == nullptr) || isReCalculateDragEnd_ || !autoAlign_) {
        return false;
    }

    int16_t offsetX = 0;
    int16_t offsetY = 0;
    if (direction_ == VERTICAL) {
        // 2: half
        offsetY = selectPosition_ - (onSelectedView_->GetY() + (onSelectedView_->GetRelativeRect().GetHeight() / 2));
    } else {
        // 2: half
        offsetX = selectPosition_ - (onSelectedView_->GetX() + (onSelectedView_->GetRelativeRect().GetWidth() / 2));
    }
L
liqiang 已提交
425
    animatorCallback_.ResetCallback();
M
mamingshuai 已提交
426 427
    animatorCallback_.SetDragStartValue(0, 0);
    animatorCallback_.SetDragEndValue(offsetX, offsetY);
428
    animatorCallback_.SetDragTimes(GetAutoAlignTime() / DEFAULT_TASK_PERIOD);
M
mamingshuai 已提交
429 430 431 432 433
    scrollAnimator_.Start();
    isReCalculateDragEnd_ = true;
    return true;
}

434
bool UIList::MoveChildStepVertical(int16_t distance)
M
mamingshuai 已提交
435 436 437 438
{
    bool popRet = false;
    bool pushRet = false;
    if (distance > 0) {
439
        if ((childrenHead_ == nullptr) || (childrenHead_->GetRelativeRect().GetTop() + distance > 0)) {
M
mamingshuai 已提交
440 441 442 443 444 445 446 447 448 449 450
            uint16_t index = GetIndexDec(topIndex_);
            if (index == topIndex_) {
                return false;
            }
            UIView* newView = recycle_.GetView(index);
            if (newView == nullptr) {
                return false;
            }
            PushFront(newView);
            pushRet = true;
        }
451
        if (childrenTail_ != nullptr && (childrenTail_->GetRelativeRect().GetTop() + distance > GetHeight())) {
M
mamingshuai 已提交
452 453 454 455
            PopItem(childrenTail_);
            popRet = true;
        }
    } else {
456
        if (childrenTail_ == nullptr || (childrenTail_->GetRelativeRect().GetBottom() + distance < GetHeight())) {
M
mamingshuai 已提交
457 458 459 460 461 462 463
            UIView* newView = recycle_.GetView(GetIndexInc(bottomIndex_));
            if (newView == nullptr) {
                return false;
            }
            PushBack(newView);
            pushRet = true;
        }
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        if (childrenHead_ && (childrenHead_->GetRelativeRect().GetBottom() + distance < 0)) {
            PopItem(childrenHead_);
            popRet = true;
        }
    }
    return (popRet || pushRet);
}

bool UIList::MoveChildStepHorizontal(int16_t distance)
{
    bool popRet = false;
    bool pushRet = false;
    if (distance > 0) {
        if ((childrenHead_ == nullptr) || (childrenHead_->GetRelativeRect().GetLeft() + distance > 0)) {
            uint16_t index = GetIndexDec(topIndex_);
            if (index == topIndex_) {
                return false;
            }
            UIView* newView = recycle_.GetView(index);
            if (newView == nullptr) {
                return false;
            }
            PushFront(newView);
            pushRet = true;
        }
        if (childrenTail_ != nullptr && (childrenTail_->GetRelativeRect().GetLeft() + distance > GetWidth())) {
            PopItem(childrenTail_);
            popRet = true;
        }
    } else {
        if (childrenTail_ == nullptr || (childrenTail_->GetRelativeRect().GetRight() + distance < GetWidth())) {
            UIView* newView = recycle_.GetView(GetIndexInc(bottomIndex_));
            if (newView == nullptr) {
                return false;
            }
            PushBack(newView);
            pushRet = true;
        }
        if (childrenHead_ && (childrenHead_->GetRelativeRect().GetRight() + distance < 0)) {
M
mamingshuai 已提交
503 504 505 506 507 508 509 510 511 512
            PopItem(childrenHead_);
            popRet = true;
        }
    }
    return (popRet || pushRet);
}

bool UIList::MoveChildStep(int16_t distance)
{
    if (direction_ == VERTICAL) {
513
        return MoveChildStepVertical(distance);
M
mamingshuai 已提交
514
    } else {
515
        return MoveChildStepHorizontal(distance);
M
mamingshuai 已提交
516 517 518 519 520
    }
}

void UIList::SetAdapter(AbstractAdapter* adapter)
{
521 522
    recycle_.SetAdapter(adapter);
    recycle_.InitRecycle();
M
mamingshuai 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
}

UIView* UIList::GetSelectView()
{
    if (onSelectedView_ != nullptr) {
        return onSelectedView_;
    }
    if ((childrenHead_ == nullptr) || (selectPosition_ == 0)) {
        return nullptr;
    }
    UIView* child = childrenHead_;
    while (child != nullptr) {
        if (direction_ == VERTICAL) {
            if ((child->GetY() <= selectPosition_) &&
                (child->GetY() + child->GetRelativeRect().GetHeight() >= selectPosition_)) {
                if (scrollListener_ != nullptr) {
                    scrollListener_->OnItemSelected(child->GetViewIndex(), child);
                }
                return child;
            }
        } else {
            if ((child->GetX() <= selectPosition_) &&
                (child->GetX() + child->GetRelativeRect().GetWidth() >= selectPosition_)) {
                if (scrollListener_ != nullptr) {
                    scrollListener_->OnItemSelected(child->GetViewIndex(), child);
                }
                return child;
            }
        }
        child = child->GetNextSibling();
    }
    return nullptr;
}

void UIList::PushBack(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    if (childrenTail_ == nullptr) {
        SetHead(view);
    } else {
        if (direction_ == VERTICAL) {
566
            view->SetPosition(0, childrenTail_->GetY() + childrenTail_->GetHeightWithMargin());
M
mamingshuai 已提交
567
        } else {
Y
YueBiang 已提交
568
            view->SetPosition(childrenTail_->GetX() + childrenTail_->GetWidthWithMargin(), 0);
M
mamingshuai 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
        }
        bottomIndex_ = GetIndexInc(bottomIndex_);
    }

    view->SetDragParentInstead(true);
    UIViewGroup::Add(view);
}

void UIList::PushFront(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    if (GetChildrenHead() == nullptr) {
        SetHead(view);
    } else {
        if (direction_ == VERTICAL) {
586
            view->SetPosition(0, GetChildrenHead()->GetY() - view->GetHeightWithMargin());
M
mamingshuai 已提交
587
        } else {
588
            view->SetPosition(GetChildrenHead()->GetX() - view->GetWidthWithMargin(), 0);
M
mamingshuai 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        }
        topIndex_ = GetIndexDec(topIndex_);
    }
    view->SetDragParentInstead(true);
    UIViewGroup::Insert(nullptr, view);
}

void UIList::PopItem(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    recycle_.AddScrapView(view);
    if (view == GetChildrenHead()) {
        topIndex_ = GetIndexInc(topIndex_);
    }

    if (view == childrenTail_) {
        bottomIndex_ = GetIndexDec(bottomIndex_);
    }
    UIViewGroup::Remove(view);
}

void UIList::SetHead(UIView* view)
{
    if (view != nullptr) {
615
        view->SetPosition(0, 0);
M
mamingshuai 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
        topIndex_ = startIndex_;
        bottomIndex_ = startIndex_;
    }
}

void UIList::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
{
    UIView* view = GetChildrenHead();
    if (view == nullptr) {
        return;
    }
    int16_t x;
    int16_t y;
    int16_t height;
    int16_t width;

    if ((onSelectedIndex_ != NULL_SELECT_INDEX) && (selectPosition_ != 0)) {
Y
YueBiang 已提交
633
        if (yOffset != 0) {
M
mamingshuai 已提交
634
            height = view->GetRelativeRect().GetHeight();
Y
YueBiang 已提交
635
            if ((view->GetY() + yOffset > selectPosition_) ||
636
                (childrenTail_->GetY() + height + childrenTail_->GetStyle(STYLE_MARGIN_BOTTOM) + yOffset <
637
                 selectPosition_)) {
M
mamingshuai 已提交
638 639 640 641 642 643
                onSelectedIndex_ = NULL_SELECT_INDEX;
                onSelectedView_ = nullptr;
                if (scrollListener_ != nullptr) {
                    scrollListener_->OnItemSelected(onSelectedIndex_, onSelectedView_);
                }
            }
Y
YueBiang 已提交
644 645
        }
        if (xOffset != 0) {
M
mamingshuai 已提交
646
            width = view->GetRelativeRect().GetWidth();
Y
YueBiang 已提交
647
            if ((view->GetX() + xOffset > selectPosition_) ||
648
                (childrenTail_->GetX() + width + childrenTail_->GetStyle(STYLE_MARGIN_RIGHT) < selectPosition_)) {
M
mamingshuai 已提交
649 650 651 652 653 654 655 656 657
                onSelectedIndex_ = NULL_SELECT_INDEX;
                onSelectedView_ = nullptr;
                if (scrollListener_ != nullptr) {
                    scrollListener_->OnItemSelected(onSelectedIndex_, onSelectedView_);
                }
            }
        }
    }
    bool isSelectViewFind = false;
Y
YueBiang 已提交
658
    do {
M
mamingshuai 已提交
659 660 661 662 663 664 665
        x = view->GetX() + xOffset;
        y = view->GetY() + yOffset;
        view->SetPosition(x, y);
        if ((selectPosition_ != 0) && !isSelectViewFind) {
            if (direction_ == VERTICAL) {
                height = view->GetRelativeRect().GetHeight();
                /* Views may be the same but have different indexes because of view recycling. */
666 667
                if ((y - view->GetStyle(STYLE_PADDING_TOP) <= selectPosition_) &&
                    (y + view->GetStyle(STYLE_MARGIN_BOTTOM) + height >= selectPosition_) &&
M
mamingshuai 已提交
668 669 670 671 672 673 674 675 676 677
                    ((onSelectedView_ != view) || (onSelectedIndex_ != view->GetViewIndex()))) {
                    onSelectedIndex_ = view->GetViewIndex();
                    onSelectedView_ = view;
                    if (scrollListener_ != nullptr) {
                        scrollListener_->OnItemSelected(onSelectedIndex_, onSelectedView_);
                    }
                    isSelectViewFind = true;
                }
            } else {
                width = view->GetRelativeRect().GetWidth();
678 679
                if ((x - view->GetStyle(STYLE_MARGIN_LEFT) <= selectPosition_) &&
                    (x + width + view->GetStyle(STYLE_MARGIN_RIGHT) >= selectPosition_) &&
M
mamingshuai 已提交
680 681 682 683 684 685 686
                    ((onSelectedView_ != view) || (onSelectedIndex_ != view->GetViewIndex()))) {
                    onSelectedIndex_ = view->GetViewIndex();
                    onSelectedView_ = view;
                    if (scrollListener_ != nullptr) {
                        scrollListener_->OnItemSelected(onSelectedIndex_, onSelectedView_);
                    }
                    isSelectViewFind = true;
687 688
                }
            }
Y
YueBiang 已提交
689
#if ENABLE_VIBRATOR
690
            VibratorFunc vibratorFunc = VibratorManager::GetInstance()->GetVibratorFunc();
Y
YueBiang 已提交
691
            if (isSelectViewFind && isRotating_ && vibratorFunc != nullptr) {
692 693 694 695 696 697
                if (!isLoopList_ && (onSelectedIndex_ == 0 || onSelectedIndex_ == recycle_.adapter_->GetCount() - 1)) {
                    vibratorFunc(VibratorType::VIBRATOR_TYPE_THREE);
                    GRAPHIC_LOGI("UIList::MoveChildByOffset calls TYPE_THREE vibrator");
                } else {
                    vibratorFunc(VibratorType::VIBRATOR_TYPE_TWO);
                    GRAPHIC_LOGI("UIList::MoveChildByOffset calls TYPE_TWO vibrator");
M
mamingshuai 已提交
698 699
                }
            }
700
#endif
M
mamingshuai 已提交
701 702
        }
        view = view->GetNextSibling();
Y
YueBiang 已提交
703
    } while (view != nullptr);
M
mamingshuai 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
}

void UIList::StopAnimator()
{
    UIAbstractScroll::StopAnimator();
    if (!ReCalculateDragEnd()) {
        if ((scrollListener_ != nullptr) &&
            (scrollListener_->GetScrollState() == ListScrollListener::SCROLL_STATE_MOVE)) {
            scrollListener_->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
            scrollListener_->OnScrollEnd(onSelectedIndex_, onSelectedView_);
        }
    }
}

uint16_t UIList::GetIndexInc(uint16_t index)
{
    uint16_t ret = index + 1;
    if (isLoopList_ && (recycle_.GetAdapterItemCount() != 0)) {
        ret = ret % recycle_.GetAdapterItemCount();
    }
    return ret;
}

uint16_t UIList::GetIndexDec(uint16_t index)
{
    if (index == 0) {
        if (isLoopList_) {
            return recycle_.GetAdapterItemCount() - 1;
        } else {
            return 0;
        }
    } else {
        return index - 1;
    }
}

void UIList::ScrollTo(uint16_t index)
{
    UIView* child = GetChildrenHead();
    UIView* tmp = nullptr;
    while (child != nullptr) {
        tmp = child;
        child = child->GetNextSibling();
        PopItem(tmp);
    }
    onSelectedView_ = nullptr;
    SetStartIndex(index);
    recycle_.InitRecycle();
}

void UIList::RefreshList()
{
    int16_t topIndex = topIndex_;
    UIView* child = GetChildrenHead();
    UIView* tmp = nullptr;
    int16_t offset = 0;
    if (child != nullptr) {
        if (direction_ == VERTICAL) {
            offset = child->GetY();
        } else {
            offset = child->GetX();
        }
    }

    while (child != nullptr) {
        tmp = child;
        child = child->GetNextSibling();
        PopItem(tmp);
    }
    onSelectedView_ = nullptr;

    uint16_t tmpStartIndex = startIndex_;
    if (topIndex > recycle_.GetAdapterItemCount() - 1) {
        startIndex_ = 0;
        offset = 0;
    } else {
        startIndex_ = topIndex;
    }
    recycle_.InitRecycle();
    startIndex_ = tmpStartIndex;

    if (direction_ == VERTICAL) {
        DragYInner(offset);
    } else {
        DragXInner(offset);
    }
    Invalidate();
}

void UIList::RemoveAll()
{
    UIViewGroup::RemoveAll();
    recycle_.ClearScrapView();
}

Y
YueBiang 已提交
799 800
void UIList::SetXScrollBarVisible(bool visible)
{
Y
YueBiang 已提交
801
    bool lastVisible = xScrollBarVisible_;
Y
YueBiang 已提交
802
    UIAbstractScroll::SetXScrollBarVisible(visible);
Y
YueBiang 已提交
803 804
    if (!lastVisible && xScrollBarVisible_) {
        if (recycle_.HasInitialiszed()) {
B
benb365 已提交
805
            recycle_.MeasureAdapterRelativeRect();
Y
YueBiang 已提交
806 807 808
        } else {
            recycle_.InitRecycle();
        }
Y
YueBiang 已提交
809 810 811 812 813
    }
}

void UIList::SetYScrollBarVisible(bool visible)
{
Y
YueBiang 已提交
814
    bool lastVisible = yScrollBarVisible_;
Y
YueBiang 已提交
815
    UIAbstractScroll::SetYScrollBarVisible(visible);
Y
YueBiang 已提交
816 817
    if (!lastVisible && yScrollBarVisible_) {
        if (recycle_.HasInitialiszed()) {
B
benb365 已提交
818
            recycle_.MeasureAdapterRelativeRect();
Y
YueBiang 已提交
819 820 821
        } else {
            recycle_.InitRecycle();
        }
Y
YueBiang 已提交
822 823 824
    }
}

M
mamingshuai 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
void UIList::CalculateReboundDistance(int16_t& dragDistanceX, int16_t& dragDistanceY)
{
    if (isLoopList_) {
        return;
    }
    Rect rect = GetAllChildRelativeRect();
    int16_t top = rect.GetTop();
    int16_t bottom = rect.GetBottom();
    int16_t scrollHeight = GetHeight();
    int16_t left = rect.GetLeft();
    int16_t right = rect.GetRight();
    int16_t scrollWidth = GetWidth();
    if ((direction_ == VERTICAL) || (direction_ == HORIZONTAL_AND_VERTICAL)) {
        if (top > scrollBlankSize_) {
            if ((dragDistanceY + top) > (scrollBlankSize_ + reboundSize_)) {
                dragDistanceY = 0;
            }
            dragDistanceY += scrollBlankSize_ - (top + dragDistanceY);
        }
        if (bottom < (scrollHeight - scrollBlankSize_ - 1)) {
            if ((dragDistanceY + bottom) < (scrollHeight - scrollBlankSize_ - reboundSize_ - 1)) {
                dragDistanceY = 0;
            }
            dragDistanceY += scrollHeight - scrollBlankSize_ - 1 - (bottom + dragDistanceY);
        }
    } else {
        if (left > scrollBlankSize_) {
            if ((dragDistanceX + left) > (scrollBlankSize_ + reboundSize_)) {
                dragDistanceX = 0;
            }
            dragDistanceX += scrollBlankSize_ - (left + dragDistanceX);
        }
        if (right < (scrollWidth - scrollBlankSize_ - 1)) {
            if ((dragDistanceX + right) < (scrollWidth - scrollBlankSize_ - reboundSize_ - 1)) {
                dragDistanceX = 0;
            }
            dragDistanceX += scrollWidth - scrollBlankSize_ - 1 - (right + dragDistanceX);
        }
    }
}

866
/* this is a temporary implementation just used for list and will be replaced later,
L
liqiang 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
   we assume size of all items in scroll are equal for now. */
void UIList::FixDistance(int16_t& distanceX, int16_t& distanceY)
{
    if (childrenHead_ == nullptr) {
        GRAPHIC_LOGW("cannot fix drag distance without children!");
        return;
    }

    if (direction_ == VERTICAL) {
        FixVerDistance(distanceY);
    } else {
        FixHorDistance(distanceX);
    }
}

void UIList::FixHorDistance(int16_t& distanceX)
{
    UIView* targetView = childrenHead_;
    while (targetView != nullptr) {
        int16_t pos = targetView->GetX();
        int16_t width = targetView->GetRelativeRect().GetWidth();
L
lancer 已提交
888 889 890
        if (width == 0) {
            return;
        }
L
liqiang 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
        if (pos <= selectPosition_ && pos + width >= selectPosition_) {
            int16_t offset = selectPosition_ - (pos + width / 2); // 2 : half of width
            if ((distanceX < 0) && (offset > 0)) {
                offset = offset - width;
            } else if ((distanceX > 0) && (offset < 0)) {
                offset = offset + width;
            }
            distanceX = (distanceX / width) * width + offset;
            return;
        }
        targetView = targetView->GetNextSibling();
    }
}

void UIList::FixVerDistance(int16_t& distanceY)
{
    UIView* targetView = childrenHead_;
    while (targetView != nullptr) {
        int16_t pos = targetView->GetY();
        int16_t height = targetView->GetRelativeRect().GetHeight();
L
lancer 已提交
911 912 913
        if (height == 0) {
            return;
        }
L
liqiang 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926
        if (pos <= selectPosition_ && pos + height >= selectPosition_) {
            int16_t offset = selectPosition_ - (pos + height / 2); // 2 : half of height
            if ((distanceY < 0) && (offset > 0)) {
                offset = offset - height;
            } else if ((distanceY > 0) && (offset < 0)) {
                offset = offset + height;
            }
            distanceY = (distanceY / height) * height + offset;
            return;
        }
        targetView = targetView->GetNextSibling();
    }
}
M
mamingshuai 已提交
927
} // namespace OHOS