ui_swipe_view.cpp 14.9 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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_swipe_view.h"
#include "dock/focus_manager.h"
Y
YueBiang 已提交
18
#include "dock/vibrator_manager.h"
Y
YueBiang 已提交
19 20 21 22 23 24 25
#include "gfx_utils/graphic_log.h"

namespace {
#if ENABLE_ROTATE_INPUT
constexpr float DEFAULT_SWIPE_ROTATE_FACTOR = 5;
#endif
} // namespace
M
mamingshuai 已提交
26 27 28

namespace OHOS {
UISwipeView::UISwipeView(uint8_t direction)
Y
YueBiang 已提交
29
    : swipeListener_(nullptr), curIndex_(0), blankSize_(DEFAULT_BLANK_SIZE), curView_(nullptr), loop_(false)
M
mamingshuai 已提交
30 31
{
#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
32 33 34 35 36 37
    rotateFactor_ = DEFAULT_SWIPE_ROTATE_FACTOR;
    lastRotateLen_ = 0;
#endif
#if ENABLE_VIBRATOR
    lastIndex_ = 0;
    needVibration_ = false;
M
mamingshuai 已提交
38 39 40 41 42 43
#endif
    direction_ = direction;
    tickTime_ = ANIMATOR_TIME;
    swipeAccCoefficient_ = DRAG_ACC_FACTOR;
}

P
pssea 已提交
44
UISwipeView::~UISwipeView() {}
M
mamingshuai 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 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 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 176 177 178

void UISwipeView::Add(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    view->SetDragParentInstead(true);
    UIViewGroup::Add(view);
    SortChild();
    Invalidate();
}

void UISwipeView::Insert(UIView* prevView, UIView* insertView)
{
    if (insertView == nullptr) {
        return;
    }
    insertView->SetDragParentInstead(true);
    UIViewGroup::Insert(prevView, insertView);
    SortChild();
    Invalidate();
}

void UISwipeView::Remove(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    UIViewGroup::Remove(view);
    SortChild();
    Invalidate();
}

void UISwipeView::SetCurrentPage(uint16_t index, bool needAnimator)
{
    SwitchToPage(index, needAnimator);
    Invalidate();
}

bool UISwipeView::DragXInner(int16_t distance)
{
    if (distance == 0) {
        return true;
    }
    if (!loop_) {
        if ((distance > 0) && (childrenHead_ != nullptr)) {
            if (childrenHead_->GetX() >= blankSize_) {
                distance = 0;
            } else if (childrenHead_ && (childrenHead_->GetX() + distance > blankSize_)) {
                distance = blankSize_ - childrenHead_->GetX();
            }
        } else if (childrenTail_ != nullptr) {
            int16_t width = GetWidth();
            if (childrenTail_->GetRelativeRect().GetRight() < width - blankSize_) {
                distance = 0;
            } else if (width - (childrenTail_->GetX() + childrenTail_->GetWidth() + distance) > blankSize_) {
                distance = width - blankSize_ - childrenTail_->GetX() - childrenTail_->GetWidth();
            }
        }
    }
    CalculateInvalidate();
    MoveChildByOffset(distance, 0);
    CalculateInvalidate();
    return true;
}

bool UISwipeView::DragYInner(int16_t distance)
{
    if (distance == 0) {
        return true;
    }
    if (!loop_) {
        if ((distance > 0) && (childrenHead_ != nullptr)) {
            if (childrenHead_->GetY() >= blankSize_) {
                distance = 0;
            } else if ((childrenHead_ != nullptr) && (childrenHead_->GetY() + distance > blankSize_)) {
                distance = blankSize_ - childrenHead_->GetY();
            }
        } else if (childrenTail_ != nullptr) {
            int16_t height = GetHeight();
            if (childrenTail_->GetRelativeRect().GetBottom() < height - blankSize_) {
                distance = 0;
            } else if (height - (childrenTail_->GetY() + childrenTail_->GetHeight() + distance) > blankSize_) {
                distance = height - blankSize_ - childrenTail_->GetY() - childrenTail_->GetHeight();
            }
        }
    }
    CalculateInvalidate();
    MoveChildByOffset(0, distance);
    CalculateInvalidate();
    return true;
}

bool UISwipeView::OnDragEvent(const DragEvent& event)
{
    UIView* currentView = GetViewByIndex(curIndex_);
    if (currentView == nullptr) {
        return UIView::OnDragEvent(event);
    }
    if (scrollAnimator_.GetState() != Animator::STOP) {
        UIAbstractScroll::StopAnimator();
    }

    if (direction_ == HORIZONTAL) {
        DragXInner(event.GetDeltaX());
        RefreshDeltaY(event.GetDeltaX());
    } else {
        DragYInner(event.GetDeltaY());
        RefreshDeltaY(event.GetDeltaY());
    }
    return UIView::OnDragEvent(event);
}

bool UISwipeView::OnDragEndEvent(const DragEvent& event)
{
    int16_t distance = 0;
    if (direction_ == HORIZONTAL) {
        distance = event.GetCurrentPos().x - event.GetPreLastPoint().x;
    } else {
        distance = event.GetCurrentPos().y - event.GetPreLastPoint().y;
    }
    RefreshCurrentView(distance);

    if (curView_ == nullptr) {
        return UIView::OnDragEndEvent(event);
    }

    SwitchToPage(curIndex_);

    Invalidate();
    return UIView::OnDragEndEvent(event);
}

#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
179
bool UISwipeView::OnRotateStartEvent(const RotateEvent& event)
M
mamingshuai 已提交
180
{
Y
YueBiang 已提交
181 182
    if (scrollAnimator_.GetState() != Animator::STOP) {
        UIAbstractScroll::StopAnimator();
183
    }
Y
YueBiang 已提交
184 185 186 187 188 189 190 191 192 193 194
#if ENABLE_VIBRATOR
    needVibration_ = true;
#endif
    return UIView::OnRotateStartEvent(event);
}

bool UISwipeView::OnRotateEvent(const RotateEvent& event)
{
    lastRotateLen_ =  event.GetRotate() * rotateFactor_;
    if (direction_ == HORIZONTAL) {
        DragXInner(lastRotateLen_);
M
mamingshuai 已提交
195
    } else {
Y
YueBiang 已提交
196
        DragYInner(lastRotateLen_);
M
mamingshuai 已提交
197
    }
Y
YueBiang 已提交
198 199 200 201 202 203 204 205
    RefreshCurrentView(lastRotateLen_);
    return UIView::OnRotateEvent(event);
}

bool UISwipeView::OnRotateEndEvent(const RotateEvent& event)
{
    SwitchToPage(curIndex_);
    lastRotateLen_ = 0;
Y
YueBiang 已提交
206
#if ENABLE_VIBRATOR
Y
YueBiang 已提交
207
    needVibration_ = false;
Y
YueBiang 已提交
208
#endif
Y
YueBiang 已提交
209
    return UIView::OnRotateEndEvent(event);
M
mamingshuai 已提交
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 253 254 255
}
#endif

UIView* UISwipeView::GetViewByIndex(uint16_t index) const
{
    UIView* child = childrenHead_;
    while (child != nullptr) {
        if (child->GetViewIndex() == index) {
            return child;
        }
        child = child->GetNextSibling();
    }
    return nullptr;
}

void UISwipeView::SetAnimatorTime(uint16_t time)
{
    tickTime_ = time / DEFAULT_TASK_PERIOD;
    if (tickTime_ == 0) {
        tickTime_ = 1;
    }
    animatorCallback_.SetDragTimes(tickTime_);
}

void UISwipeView::SwitchToPage(int16_t dst, bool needAnimator)
{
    if (isNeedLoop()) {
        dst = (dst + childrenNum_) % childrenNum_;
    } else if (dst < 0) {
        dst = 0;
    } else if (dst >= childrenNum_) {
        dst = childrenNum_ - 1;
    }

    UIView* dstView = GetViewByIndex(dst);
    if (dstView == nullptr) {
        return;
    }
    curIndex_ = dst;
    int16_t xOffset = 0;
    int16_t yOffset = 0;

    if (direction_ == HORIZONTAL) {
        if (alignMode_ == ALIGN_LEFT) {
            xOffset = -dstView->GetX();
        } else if (alignMode_ == ALIGN_RIGHT) {
256
            xOffset = GetWidth() - (dstView->GetX() + dstView->GetWidthWithMargin());
M
mamingshuai 已提交
257
        } else {
258
            xOffset = (GetWidth() >> 1) - (dstView->GetX() + (dstView->GetWidthWithMargin() >> 1));
M
mamingshuai 已提交
259 260
        }
    } else {
261
        yOffset = (GetHeight() >> 1) - (dstView->GetY() + (dstView->GetHeightWithMargin() >> 1));
M
mamingshuai 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    }

    if ((xOffset != 0) || (yOffset != 0)) {
        if (scrollAnimator_.GetState() != Animator::STOP) {
            scrollAnimator_.Stop();
        }
        if (needAnimator) {
            animatorCallback_.SetDragTimes(tickTime_);
            animatorCallback_.SetDragStartValue(0, 0);
            animatorCallback_.SetDragEndValue(xOffset, yOffset);
            scrollAnimator_.Start();
        } else {
            MoveChildByOffset(xOffset, yOffset);
        }
    }
}

void UISwipeView::StopAnimator()
{
    UIAbstractScroll::StopAnimator();
    if (swipeListener_ != nullptr) {
        swipeListener_->OnSwipe(*this);
    }
}

void UISwipeView::SortChild()
{
    if (childrenHead_ == nullptr) {
        return;
    }
    int16_t index = 0;
    UIView* pre = childrenHead_;
    UIView* next = childrenHead_->GetNextSibling();
    if (direction_ == HORIZONTAL) {
        pre->SetX(0);
    } else {
        pre->SetY(0);
    }
    pre->SetViewIndex(index);
    index++;

    while (next != nullptr) {
        if (direction_ == HORIZONTAL) {
305
            next->SetX(pre->GetRelativeRect().GetRight() + pre->GetStyle(STYLE_MARGIN_RIGHT));
M
mamingshuai 已提交
306
        } else {
307
            next->SetY(pre->GetRelativeRect().GetBottom() + pre->GetStyle(STYLE_MARGIN_BOTTOM));
M
mamingshuai 已提交
308 309 310 311 312 313 314 315 316 317 318 319
        }
        pre = next;
        next->SetViewIndex(index);
        next = next->GetNextSibling();
        index++;
    }
    bool tmpLoop = loop_;
    loop_ = false;
    SwitchToPage(curIndex_, false);
    loop_ = tmpLoop;
}

Y
YueBiang 已提交
320 321 322
void UISwipeView::RefreshCurrentViewInner(int16_t distance,
                                          int16_t (UIView::*pfnGetXOrY)() const,
                                          int16_t (UIView::*pfnGetWidthOrHeight)())
M
mamingshuai 已提交
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
    if (childrenHead_ == nullptr) {
        curIndex_ = 0;
        curView_ = nullptr;
        return;
    }

    curIndex_ = 0;
    curView_ = nullptr;

    uint16_t swipeMid;
    if (alignMode_ == ALIGN_LEFT) {
        swipeMid = 0;
    } else if (alignMode_ == ALIGN_RIGHT) {
        swipeMid = (this->*pfnGetWidthOrHeight)();
    } else {
        swipeMid = (this->*pfnGetWidthOrHeight)() >> 1;
    }
    UIView* view = childrenHead_;

    if ((childrenHead_->*pfnGetXOrY)() > swipeMid) {
        curIndex_ = childrenHead_->GetViewIndex();
        curView_ = childrenHead_;
    } else if ((childrenTail_->*pfnGetXOrY)() + (childrenHead_->*pfnGetWidthOrHeight)() < swipeMid) {
        curIndex_ = childrenTail_->GetViewIndex();
        curView_ = childrenTail_;
    } else {
        while (view != nullptr) {
            if ((swipeMid >= (view->*pfnGetXOrY)()) &&
                (swipeMid <= (view->*pfnGetXOrY)() + (view->*pfnGetWidthOrHeight)())) {
                curIndex_ = view->GetViewIndex();
                curView_ = view;
                break;
            }
            view = view->GetNextSibling();
        }
    }
    if (curView_ == nullptr) {
        return;
    }

    int16_t accelerationOffset = GetMaxDeltaY() * GetSwipeACCLevel() / DRAG_ACC_FACTOR;
    if (distance < 0) {
        /*
         * 7, 10 : Check whether the current view is dragged by more than 1/5,
         * that is, the x or y coordinate plus 7/10 width or height.
         */
        if (((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() >> 1) < swipeMid) &&
Y
YueBiang 已提交
371
            ((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() * 7 / 10) - accelerationOffset <
G
guyuanzhang 已提交
372
            swipeMid)) {
M
mamingshuai 已提交
373 374 375 376 377 378 379 380
            curIndex_++;
        }
    } else if (distance > 0) {
        /*
         * 3, 10 : Check whether the current view is dragged by more than 1/5,
         * that is, the x or y coordinate plus 3/10 width or height.
         */
        if (((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() >> 1) > swipeMid) &&
Y
YueBiang 已提交
381
            ((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() * 3 / 10) + accelerationOffset >
G
guyuanzhang 已提交
382
            swipeMid)) {
M
mamingshuai 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395
            curIndex_--;
        }
    } else {
        if (alignMode_ == ALIGN_LEFT) {
            if (((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() >> 1) < swipeMid)) {
                curIndex_++;
            }
        } else if (alignMode_ == ALIGN_RIGHT) {
            if ((curView_->*pfnGetXOrY)() + ((curView_->*pfnGetWidthOrHeight)() >> 1) > swipeMid) {
                curIndex_--;
            }
        }
    }
Y
YueBiang 已提交
396 397 398
#if ENABLE_VIBRATOR
    VibratorFunc vibratorFunc = VibratorManager::GetInstance()->GetVibratorFunc();
    if (vibratorFunc != nullptr && needVibration_ && curIndex_ != lastIndex_) {
399
        GRAPHIC_LOGI("UISwipeView::RefreshCurrentViewInner Call vibrator function");
Y
YueBiang 已提交
400 401 402 403
        vibratorFunc(VibratorType::VIBRATOR_TYPE_ONE);
        lastIndex_ = curIndex_;
    }
#endif
M
mamingshuai 已提交
404 405 406 407 408
}

void UISwipeView::RefreshCurrentView(int16_t distance)
{
    if (direction_ == HORIZONTAL) {
409
        RefreshCurrentViewInner(distance, &UIView::GetX, &UIView::GetWidthWithMargin);
M
mamingshuai 已提交
410
    } else {
411
        RefreshCurrentViewInner(distance, &UIView::GetY, &UIView::GetHeightWithMargin);
M
mamingshuai 已提交
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 461 462 463 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 503 504
    }
}

void UISwipeView::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
{
    UIViewGroup::MoveChildByOffset(xOffset, yOffset);
    if (direction_ == HORIZONTAL) {
        while (isNeedLoop() && (childrenHead_->GetX() > 0)) {
            MoveLastChildToFirst();
        }
        while (isNeedLoop() && (childrenTail_->GetX() + childrenTail_->GetWidth() < GetWidth())) {
            MoveFirstChildToLast();
        }
    } else {
        while (isNeedLoop() && (childrenHead_->GetY() > 0)) {
            MoveLastChildToFirst();
        }
        while (isNeedLoop() && (childrenTail_->GetY() + childrenTail_->GetHeight() < GetHeight())) {
            MoveFirstChildToLast();
        }
    }
}

bool UISwipeView::isNeedLoop()
{
    if (!loop_ || (childrenHead_ == nullptr) || (childrenTail_ == nullptr)) {
        return false;
    }
    Rect childRect = GetAllChildRelativeRect();
    if (direction_ == HORIZONTAL) {
        if ((childRect.GetWidth() - childrenHead_->GetWidth() >= GetWidth()) &&
            (childRect.GetWidth() - childrenTail_->GetWidth() >= GetWidth())) {
            return true;
        }
    } else {
        if ((childRect.GetHeight() - childrenHead_->GetHeight() >= GetHeight()) &&
            (childRect.GetHeight() - childrenTail_->GetHeight() >= GetHeight())) {
            return true;
        }
    }
    return false;
}

void UISwipeView::MoveFirstChildToLast()
{
    if ((childrenTail_ == nullptr) || (childrenHead_ == nullptr)) {
        return;
    }
    if (direction_ == HORIZONTAL) {
        childrenHead_->SetX(childrenTail_->GetX() + childrenTail_->GetWidth());
    } else {
        childrenHead_->SetY(childrenTail_->GetY() + childrenTail_->GetHeight());
    }
    UIView* head = childrenHead_;
    UIViewGroup::Remove(childrenHead_);
    UIViewGroup::Add(head);
}

void UISwipeView::MoveLastChildToFirst()
{
    if ((childrenTail_ == nullptr) || (childrenHead_ == nullptr)) {
        return;
    }
    if (direction_ == HORIZONTAL) {
        childrenTail_->SetX(childrenHead_->GetX() - childrenTail_->GetWidth());
    } else {
        childrenTail_->SetY(childrenHead_->GetY() - childrenTail_->GetHeight());
    }
    UIView* last = childrenTail_;
    UIViewGroup::Remove(childrenTail_);
    UIViewGroup::Insert(nullptr, last);
}

void UISwipeView::CalculateInvalidate()
{
    Rect swipeRect(0, 0, GetRelativeRect().GetWidth() - 1, GetRelativeRect().GetHeight() - 1);
    UIView* view = childrenHead_;
    bool isFound = false;
    while (view != nullptr) {
        Rect rect = view->GetRelativeRect();
        if (rect.IsIntersect(swipeRect)) {
            if (view->IsVisible() && (view->GetOpaScale() != OPA_TRANSPARENT)) {
                view->Invalidate();
            }
            isFound = true;
        } else if (isFound) {
            return;
        }

        view = view->GetNextSibling();
    }
}
} // namespace OHOS