Skip to content

Commit

Permalink
feat: 动效增加开关
Browse files Browse the repository at this point in the history
动效增加开关,通过环境变量DTK_DISABLE_ANIMATIONS和DTK_DISABLE_ANIMATION_***控制动效总开关和单个动效开关

Log: 动效增加开关
  • Loading branch information
Whale107 committed Oct 23, 2024
1 parent 87fd9f3 commit 5a8ce89
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 95 deletions.
7 changes: 7 additions & 0 deletions include/widgets/dstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ QT_BEGIN_NAMESPACE
class QTextLayout;
QT_END_NAMESPACE

#define ENABLE_ANIMATIONS (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATIONS"))
#define ENABLE_ANIMATION_BUTTONBOX (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_BUTTONBOX"))
#define ENABLE_ANIMATION_MESSAGE (!qEnvironmentVariableIsSet("DTK_DISABLE_MESSAGE"))
#define ENABLE_ANIMATION_LISTVIEWBOUNCE (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIREBOUNCE"))
#define ENABLE_ANIMATION_SEARCH (!qEnvironmentVariableIsSet("DTK_DISABLE_SEARCH"))
#define ENABLE_ANIMATION_SWITCHBUTTON (!qEnvironmentVariableIsSet("DTK_DISABLE_SWITCHBUTTON"))
#define ENABLE_ANIMATION_PROGRESSBAR (!qEnvironmentVariableIsSet("DTK_DISABLE_PROGRESSBAR"))

DWIDGET_BEGIN_NAMESPACE

Expand Down
45 changes: 32 additions & 13 deletions src/widgets/dbuttonbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ void DButtonBoxButton::paintEvent(QPaintEvent *e)
DStylePainter p(this);
DStyleOptionButtonBoxButton option;
initStyleOption(&option);
option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color());

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX)
option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color());

p.drawControl(DStyle::CE_ButtonBoxButton, option);
}

Expand Down Expand Up @@ -363,10 +366,13 @@ DButtonBoxPrivate::DButtonBoxPrivate(DButtonBox *qq)
, m_hoverId(-1)
, m_checkedId(-1)
, m_pressId(-1)
, m_hoverAnimation(new QVariantAnimation(qq))
, m_checkMoveAnimation(new QVariantAnimation(qq))
, m_hoverAnimation(nullptr)
, m_checkMoveAnimation(nullptr)
{

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) {
m_hoverAnimation = new QVariantAnimation(qq);
m_checkMoveAnimation = new QVariantAnimation(qq);
}
}

void DButtonBoxPrivate::init()
Expand All @@ -380,14 +386,17 @@ void DButtonBoxPrivate::init()
q->connect(group, SIGNAL(buttonPressed(QAbstractButton*)), q, SIGNAL(buttonPressed(QAbstractButton*)));
q->connect(group, SIGNAL(buttonReleased(QAbstractButton*)), q, SIGNAL(buttonReleased(QAbstractButton*)));
q->connect(group, SIGNAL(buttonToggled(QAbstractButton*, bool)), q, SIGNAL(buttonToggled(QAbstractButton*, bool)));
q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
m_hoverAnimation->setDuration(HOVER_ANI_DURATION);
m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) {
q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() {
q->update();
});
m_hoverAnimation->setDuration(HOVER_ANI_DURATION);
m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION);
}

layout = new QHBoxLayout(q);
layout->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -506,7 +515,9 @@ void DButtonBox::setButtonList(const QList<DButtonBoxButton *> &list, bool check
d->group->addButton(button);

button->setCheckable(checkable);
button->installEventFilter(this);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX)
button->installEventFilter(this);
}
}

Expand Down Expand Up @@ -616,6 +627,11 @@ void DButtonBox::paintEvent(QPaintEvent *e)
opt.state |= QStyle::State_Active;
}

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX) {
p.drawControl(QStyle::CE_PushButtonBevel, opt);
return;
}

bool isDarkType = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType;
int radius = DStyle::pixelMetric(style(), DStyle::PM_FrameRadius);
QColor background;
Expand Down Expand Up @@ -706,6 +722,9 @@ void DButtonBox::paintEvent(QPaintEvent *e)

bool DButtonBox::eventFilter(QObject *o, QEvent *e)
{
if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX)
return QWidget::eventFilter(o, e);;

D_D(DButtonBox);
for (int i = 0; i < buttonList().size(); ++i) {
if (o == buttonList().at(i)) {
Expand Down
31 changes: 22 additions & 9 deletions src/widgets/dfloatingmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ void DFloatingMessagePrivate::init()
iconButton->setIconSize(DSizeModeHelper::element(QSize(20, 20), QSize(30, 30)));

hBoxLayout->addWidget(iconButton);
hBoxLayout->addSpacing(10);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
hBoxLayout->addSpacing(10);

hBoxLayout->addWidget(labMessage);

if (notifyType == DFloatingMessage::MessageType::TransientType) { //临时消息
timer = new QTimer(q);
timer->setInterval(4000);
timer->setSingleShot(true);
q->connect(timer, &QTimer::timeout, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
q->connect(timer, &QTimer::timeout, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
else
q->connect(timer, &QTimer::timeout, q, &DFloatingMessage::close);
} else { //常驻消息
content = nullptr;
closeButton = new DDialogCloseButton(q);
Expand All @@ -74,12 +80,19 @@ void DFloatingMessagePrivate::init()

hBoxLayout->addWidget(closeButton);
q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::closeButtonClicked);
q->connect(closeButton, &DIconButton::clicked, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});

if(ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE)
q->connect(closeButton, &DIconButton::clicked, q, [q]() {
q->close();
Q_EMIT q->messageClosed();
});
else
q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::close);
}

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

auto effect = new QGraphicsDropShadowEffect(q);
effect->setColor(QColor(0, 0, 0, 0.1 * 255));
effect->setBlurRadius(20);
Expand Down
23 changes: 19 additions & 4 deletions src/widgets/dindeterminateprogressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ DIndeterminateProgressbarPrivate::DIndeterminateProgressbarPrivate(DIndeterminat
, m_sliderWidget(new QWidget(qq))
, m_timer(new QTimer(qq))
, m_leftToRight(true)
, m_spotWidget(new QWidget(qq))
, m_animation(new QPropertyAnimation(m_spotWidget, "pos", qq))
, m_spotWidget(nullptr)
, m_animation(nullptr)
{
if (!ENABLE_ANIMATIONS && !ENABLE_ANIMATION_PROGRESSBAR)
return;

m_spotWidget = new QWidget(qq);
m_animation = new QPropertyAnimation(m_spotWidget, "pos", qq);
}

DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent)
: QWidget(parent)
, DObject(*new DIndeterminateProgressbarPrivate(this))
{
D_D(DIndeterminateProgressbar);
d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());
d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0);

if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_PROGRESSBAR) {
d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());
d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0);
}

d->m_sliderWidget->setFixedWidth(150);
d->m_sliderWidget->move(0, 0);
Expand All @@ -60,6 +68,10 @@ void DIndeterminateProgressbar::resizeEvent(QResizeEvent *e)
{
D_D(DIndeterminateProgressbar);
d->m_sliderWidget->setFixedHeight(height());

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR)
QWidget::resizeEvent(e);

d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height());

d->m_animation->setStartValue(QPoint(-SPOT_WIDGET_WIDTH, 0));
Expand Down Expand Up @@ -111,6 +123,9 @@ void DIndeterminateProgressbar::paintEvent(QPaintEvent *e)
p.setPen(pen);
p.drawRoundedRect(d->m_sliderWidget->geometry(), radius, radius);

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR)
return;

if (d->m_sliderWidget->width() < d->m_spotWidget->width() / 2)
return;

Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ DListView::DListView(QWidget *parent) :
DObject(*new DListViewPrivate(this))
{
d_func()->init();
if (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIEW_ANIMATION")) {
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) {
auto ani = new DBounceAnimation(this);
ani->setAnimationTarget(this);
ani->setAniMationEnable(true);
Expand Down
84 changes: 64 additions & 20 deletions src/widgets/dmessagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ static void sendMessage_helper(DMessageManager *manager, QWidget *par, IconType

DMessageManagerPrivate::DMessageManagerPrivate(DMessageManager *qq)
: DObjectPrivate(qq)
, m_aniGeometry(new QPropertyAnimation(qq))
, m_aniOpacity(new QPropertyAnimation(qq))
, m_aniGroup(new QParallelAnimationGroup(qq))
, m_label(new ImageLabel)
, m_aniGeometry(nullptr)
, m_aniOpacity(nullptr)
, m_aniGroup(nullptr)
, m_label(nullptr)
{
if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

m_aniGeometry = new QPropertyAnimation(qq);
m_aniOpacity = new QPropertyAnimation(qq);
m_aniGroup = new QParallelAnimationGroup(qq);
m_label = new ImageLabel;

m_aniGeometry->setPropertyName("geometry");
m_aniGeometry->setDuration(ANIMATION_DURATION);
m_aniGeometry->setEasingCurve(QEasingCurve::OutCubic);
Expand Down Expand Up @@ -187,14 +195,20 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg)
layout->setContentsMargins(0, 0, 0, 0);
layout->setDirection(QBoxLayout::BottomToTop);
}

if (content->layout()->count() >= 1) {
content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide();
delete content->layout()->takeAt(content->layout()->count() - 1);
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) {
if (content->layout()->count() >= 1) {
content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide();
delete content->layout()->takeAt(content->layout()->count() - 1);
}
} else {
content->show();
}

static_cast<QBoxLayout*>(content->layout())->addWidget(floMsg, 0, Qt::AlignHCenter);

if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE)
return;

// 限制通知消息的最大宽度
for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
Expand All @@ -214,7 +228,10 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg)
d->m_label->setParent(par);
d->m_label->setAlignment(Qt::AlignCenter);
d->m_label->setContentsMargins(MARGIN, 0, MARGIN, 0);
d->m_label->setPixmap(floMsg->grab());

if (floMsg && !floMsg->grab().isNull())
d->m_label->setPixmap(floMsg->grab());

d->m_label->setScaledContents(true);
d->m_label->show();
d->m_aniGeometry->setTargetObject(d->m_label);
Expand Down Expand Up @@ -287,20 +304,47 @@ bool DMessageManager::setContentMargens(QWidget *par, const QMargins &margins)
*/
bool DMessageManager::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::Resize) {
if (auto content = watched->findChild<QWidget *>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) {
bool isOnlyResizeEvent = ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE ? event->type() == QEvent::Resize : event->type() == QEvent::LayoutRequest || event->type() == QEvent::Resize;

auto par = qobject_cast<QWidget *>(watched);
if (isOnlyResizeEvent) {
if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) {
if (auto content = watched->findChild<QWidget *>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) {

for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}
auto par = qobject_cast<QWidget *>(watched);

QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT);
content->setGeometry(geometry);
for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}

QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT);
content->setGeometry(geometry);
}
} else {
if (QWidget *widget = qobject_cast<QWidget *>(watched)) {
QWidget *content;

if (widget->objectName() == D_MESSAGE_MANAGER_CONTENT) {
content = widget;
} else {
content = widget->findChild<QWidget*>(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly);
}

QWidget *par = content->parentWidget();

// 限制通知消息的最大宽度
for (DFloatingMessage *message : content->findChildren<DFloatingMessage*>(QString(), Qt::FindDirectChildrenOnly)) {
message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width());
message->setMinimumHeight(message->sizeHint().height());
}

QRect geometry(QPoint(0, 0), content->sizeHint());
geometry.moveCenter(par->rect().center());
geometry.moveBottom(par->rect().bottom());
content->setGeometry(geometry);
}
}
} else if (event->type() == QEvent::ChildRemoved) {
// 如果是通知消息被删除的事件
Expand Down
Loading

0 comments on commit 5a8ce89

Please sign in to comment.