-
Notifications
You must be signed in to change notification settings - Fork 153
chore: 增加滚动区域滚轮滚动到顶部或底部的回弹效果 #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #include "dbounceanimation.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #ifndef DBOUNCEANIMATION_H | ||
| #define DBOUNCEANIMATION_H | ||
|
|
||
| #include <DObject> | ||
| #include <QObject> | ||
|
|
||
| class QPropertyAnimation; | ||
| class QAbstractScrollArea; | ||
| class DBounceAnimationPrivate; | ||
| class DBounceAnimation : public QObject, public DTK_CORE_NAMESPACE::DObject | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| explicit DBounceAnimation(QObject *parent = nullptr); | ||
|
|
||
| void setAnimationTarget(QAbstractScrollArea *w); | ||
| void setAniMationEnable(bool enable); | ||
|
|
||
| protected: | ||
| bool eventFilter(QObject *o, QEvent *e) override; | ||
| void bounceBack(Qt::Orientation orientation); | ||
|
|
||
| private: | ||
| D_DECLARE_PRIVATE(DBounceAnimation) | ||
|
|
||
| }; | ||
|
|
||
| #endif // DBOUNCEANIMATION_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #include "private/dbounceanimation_p.h" | ||
| #include <QPropertyAnimation> | ||
| #include <QEvent> | ||
| #include <QDebug> | ||
| #include <QAbstractScrollArea> | ||
| #include <QScrollBar> | ||
| #include <QWheelEvent> | ||
| #include <QTimer> | ||
|
|
||
| DBounceAnimationPrivate::DBounceAnimationPrivate(DBounceAnimation *qq) | ||
| : DObjectPrivate (qq) | ||
| , m_animation(nullptr) | ||
| , m_animationTarget(nullptr) | ||
| , m_deltaSum(0) | ||
| { | ||
| } | ||
|
|
||
| DBounceAnimation::DBounceAnimation(QObject *parent) | ||
| : QObject(parent) | ||
| , DObject(*new DBounceAnimationPrivate(this)) | ||
| { | ||
| } | ||
|
|
||
| void DBounceAnimation::setAnimationTarget(QAbstractScrollArea *w) | ||
| { | ||
| D_D(DBounceAnimation); | ||
| if (!w) | ||
| return; | ||
|
|
||
| if (d->m_animationTarget == w) | ||
| return; | ||
|
|
||
| d->m_animationTarget = w; | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空行 |
||
| void DBounceAnimation::setAniMationEnable(bool enable) | ||
| { | ||
| D_D(DBounceAnimation); | ||
| enable ? d->m_animationTarget->installEventFilter(this) | ||
| : d->m_animationTarget->removeEventFilter(this); | ||
| } | ||
|
|
||
| bool DBounceAnimation::eventFilter(QObject *o, QEvent *e) | ||
| { | ||
| D_D(DBounceAnimation); | ||
| if (e->type() == QEvent::Wheel) { | ||
| if (auto absscroll = dynamic_cast<QAbstractScrollArea *>(o)) { | ||
| if (auto wheelEvent = dynamic_cast<QWheelEvent *>(e)) { | ||
| if (absscroll->verticalScrollBar()->value() <= 0 || absscroll->verticalScrollBar()->value() >= absscroll->verticalScrollBar()->maximum()) { | ||
| d->m_deltaSum += wheelEvent->delta(); | ||
| bounceBack(wheelEvent->angleDelta().x() == 0 ? Qt::Vertical : Qt::Horizontal); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| void DBounceAnimation::bounceBack(Qt::Orientation orientation) | ||
| { | ||
| D_D(DBounceAnimation); | ||
| if (d->m_animation) | ||
| return; | ||
|
|
||
| if (orientation & Qt::Vertical && d->m_animationTarget->verticalScrollBar()->maximum() == d->m_animationTarget->verticalScrollBar()->minimum()) | ||
| return; | ||
|
|
||
| if (orientation & Qt::Horizontal && d->m_animationTarget->horizontalScrollBar()->maximum() == d->m_animationTarget->horizontalScrollBar()->minimum()) | ||
| return; | ||
|
|
||
| d->m_animation = new QPropertyAnimation(this); | ||
| d->m_animation->setTargetObject(d->m_animationTarget->viewport()); | ||
| d->m_animation->setPropertyName("pos"); | ||
| d->m_animation->setDuration(100); | ||
| d->m_animation->setEasingCurve(QEasingCurve::InQuart); | ||
| d->m_animation->setStartValue(QPoint(d->m_animationTarget->viewport()->x(), d->m_animationTarget->viewport()->y())); | ||
|
|
||
| QTimer::singleShot(100, this, [this, d, orientation]() { | ||
|
|
||
| if (orientation & Qt::Vertical) { | ||
| d->m_animation->setEndValue( | ||
| QPoint(d->m_animationTarget->viewport()->x(), d->m_animationTarget->viewport()->y() + d->m_deltaSum / 16)); | ||
| } else { | ||
| d->m_animation->setEndValue( | ||
| QPoint(d->m_animationTarget->viewport()->x() + d->m_deltaSum / 16, d->m_animationTarget->viewport()->y())); | ||
| } | ||
|
|
||
| d->m_animation->start(); | ||
|
|
||
| connect(d->m_animation, &QPropertyAnimation::finished, this, [d]() { | ||
| if (d->m_animation->direction() == QPropertyAnimation::Backward) { | ||
| delete d->m_animation; | ||
| d->m_animation = nullptr; | ||
| return; | ||
| } | ||
|
|
||
| d->m_animation->setDirection(QPropertyAnimation::Direction::Backward); | ||
| d->m_animation->setDuration(1000); | ||
| d->m_animation->start(QPropertyAnimation::DeleteWhenStopped); | ||
| d->m_deltaSum = 0; | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #ifndef DBOUNCEANIMATION_P_H | ||
| #define DBOUNCEANIMATION_P_H | ||
|
|
||
| #include "dbounceanimation.h" | ||
| #include <DObjectPrivate> | ||
|
|
||
| class DBounceAnimationPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate | ||
| { | ||
| public: | ||
| DBounceAnimationPrivate(DBounceAnimation *qq); | ||
|
|
||
| QPropertyAnimation *m_animation; | ||
| QAbstractScrollArea *m_animationTarget; | ||
| int m_deltaSum; | ||
|
|
||
| private: | ||
| D_DECLARE_PUBLIC(DBounceAnimation) | ||
| }; | ||
|
|
||
| #endif // DBOUNCEANIMATION_P_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以加一个
setEnable接口方便用户禁用启用