From 53f15e614d9a8982ff57138f3fa2fe763c97cb0e Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 09:58:36 +0800 Subject: [PATCH 01/17] feat(anim): add hover transitions for button widgets Add elaMixColor helper in ElaDef.h for animated color interpolation. Animate hover fill for ElaPushButton, ElaToggleButton, ElaToolButton and window titlebar buttons (150ms/175ms fades, press stays instant). --- .../ElaToolButtonStyle.cpp | 53 ++++++++++++++----- .../DeveloperComponents/ElaToolButtonStyle.h | 4 ++ .../DeveloperComponents/ElaWindowStyle.cpp | 47 +++++++++++++--- .../DeveloperComponents/ElaWindowStyle.h | 9 ++++ ElaWidgetTools/ElaDef.h | 13 +++++ ElaWidgetTools/ElaPushButton.cpp | 36 ++++++++++++- ElaWidgetTools/ElaPushButton.h | 1 + ElaWidgetTools/ElaToggleButton.cpp | 17 +++++- ElaWidgetTools/private/ElaPushButtonPrivate.h | 1 + .../private/ElaToggleButtonPrivate.h | 1 + 10 files changed, 160 insertions(+), 22 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp index 4e3401a..d69aa21 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include #include "ElaTheme.h" @@ -64,21 +66,30 @@ void ElaToolButtonStyle::drawComplexControl(ComplexControl control, const QStyle } else { - if (bopt->state.testFlag(QStyle::State_MouseOver) || bopt->state.testFlag(QStyle::State_On)) + // 悬停过渡 + bool isHovered = bopt->state.testFlag(QStyle::State_MouseOver) || bopt->state.testFlag(QStyle::State_On); + if (_firstPaint) { - painter->setBrush(_pIsTransparent ? ElaThemeColor(_themeMode, BasicHoverAlpha) : ElaThemeColor(_themeMode, BasicHover)); - painter->drawRoundedRect(toolButtonRect, _pBorderRadius, _pBorderRadius); + _firstPaint = false; + _lastHovered = isHovered; + _hoverRatio = isHovered ? 1 : 0; } - else + else if (isHovered != _lastHovered) { - if (!_pIsTransparent) - { - painter->setBrush(ElaThemeColor(_themeMode, BasicBase)); - painter->drawRoundedRect(toolButtonRect, _pBorderRadius, _pBorderRadius); - // 底边线绘制 - painter->setPen(ElaThemeColor(_themeMode, BasicBaseLine)); - painter->drawLine(toolButtonRect.x() + _pBorderRadius, toolButtonRect.y() + toolButtonRect.height(), toolButtonRect.x() + toolButtonRect.width() - _pBorderRadius, toolButtonRect.y() + toolButtonRect.height()); - } + _startHoverAnimation(isHovered ? 1 : 0, widget); + _lastHovered = isHovered; + } + QColor hoverColor = _pIsTransparent ? ElaThemeColor(_themeMode, BasicHoverAlpha) : ElaThemeColor(_themeMode, BasicHover); + QColor normalColor = _pIsTransparent ? QColor(Qt::transparent) : ElaThemeColor(_themeMode, BasicBase); + painter->setBrush(elaMixColor(normalColor, hoverColor, _hoverRatio)); + painter->drawRoundedRect(toolButtonRect, _pBorderRadius, _pBorderRadius); + if (!_pIsTransparent) + { + // 底边线绘制(随悬停淡出) + QColor lineColor = ElaThemeColor(_themeMode, BasicBaseLine); + lineColor.setAlphaF(lineColor.alphaF() * (1.0 - _hoverRatio)); + painter->setPen(lineColor); + painter->drawLine(toolButtonRect.x() + _pBorderRadius, toolButtonRect.y() + toolButtonRect.height(), toolButtonRect.x() + toolButtonRect.width() - _pBorderRadius, toolButtonRect.y() + toolButtonRect.height()); } } } @@ -360,4 +371,22 @@ qreal ElaToolButtonStyle::_calculateExpandIndicatorWidth(const QStyleOptionToolB int indicatorWidth = painter->fontMetrics().horizontalAdvance(QChar((unsigned short) ElaIconType::AngleDown)); painter->restore(); return indicatorWidth; +} + +void ElaToolButtonStyle::_startHoverAnimation(qreal endRatio, const QWidget *widget) const +{ + QVariantAnimation *hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard) + { + widgetGuard->update(); + } + }); + hoverAnimation->setDuration(150); + hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + hoverAnimation->setStartValue(_hoverRatio); + hoverAnimation->setEndValue(endRatio); + hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); } \ No newline at end of file diff --git a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h index 043bbce..90d620a 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h @@ -23,6 +23,10 @@ class ElaToolButtonStyle : public QProxyStyle ElaThemeType::ThemeMode _themeMode; int _contentMargin{6}; int _hasMenuIndicatorWidth{30}; + mutable qreal _hoverRatio{0}; + mutable bool _lastHovered{false}; + mutable bool _firstPaint{true}; + void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; void _drawIndicator(QPainter* painter, const QStyleOptionToolButton* bopt, const QWidget* widget) const; void _drawIcon(QPainter* painter, QRectF iconRect, const QStyleOptionToolButton* bopt, const QWidget* widget) const; void _drawText(QPainter* painter, QRect contentRect, const QStyleOptionToolButton* bopt) const; diff --git a/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.cpp index c2610b8..b400f9b 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include #include "ElaTheme.h" @@ -35,16 +37,28 @@ void ElaWindowStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* if (option->state.testFlag(QStyle::State_Sunken)) { painter->setBrush(ElaThemeColor(_themeMode, BasicHoverAlpha)); - } - else if (option->state.testFlag(QStyle::State_MouseOver)) - { - painter->setBrush(ElaThemeColor(_themeMode, BasicPressAlpha)); + painter->drawRect(option->rect); } else { - painter->setBrush(Qt::transparent); + // 悬停过渡(按 widget 键控) + bool isHovered = option->state.testFlag(QStyle::State_MouseOver); + const QWidget* targetWidget = qobject_cast(option->styleObject ? option->styleObject : widget); + if (targetWidget) + { + ButtonHoverState& state = _hoverStates[targetWidget]; + if (state.hovered != isHovered) + { + _startHoverAnimation(targetWidget, isHovered ? 1 : 0); + state.hovered = isHovered; + } + if (state.ratio > 0) + { + painter->setBrush(elaMixColor(QColor(Qt::transparent), ElaThemeColor(_themeMode, BasicPressAlpha), state.ratio)); + painter->drawRect(option->rect); + } + } } - painter->drawRect(option->rect); painter->restore(); } return; @@ -209,3 +223,24 @@ void ElaWindowStyle::drawControl(ControlElement element, const QStyleOption* opt } QProxyStyle::drawControl(element, option, painter, widget); } + +void ElaWindowStyle::_startHoverAnimation(const QWidget* targetWidget, qreal endRatio) const +{ + QVariantAnimation* hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(targetWidget); + connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (!widgetGuard) + { + return; + } + ButtonHoverState state = _hoverStates.value(widgetGuard.data()); + state.ratio = value.toReal(); + _hoverStates[widgetGuard.data()] = state; + widgetGuard->update(); + }); + hoverAnimation->setDuration(150); + hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + hoverAnimation->setStartValue(_hoverStates.value(targetWidget).ratio); + hoverAnimation->setEndValue(endRatio); + hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.h b/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.h index 4b05f0c..69ab7aa 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaWindowStyle.h @@ -1,6 +1,7 @@ #ifndef ELAWINDOWSTYLE_H #define ELAWINDOWSTYLE_H +#include #include #include "ElaDef.h" @@ -14,7 +15,15 @@ class ElaWindowStyle : public QProxyStyle void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = nullptr) const override; private: + // 同一样式实例服务多个标题栏按钮,悬停状态按 widget 分别跟踪 + struct ButtonHoverState + { + qreal ratio{0}; + bool hovered{false}; + }; ElaThemeType::ThemeMode _themeMode; + mutable QHash _hoverStates; + void _startHoverAnimation(const QWidget* targetWidget, qreal endRatio) const; }; #endif // ELAWINDOWSTYLE_H diff --git a/ElaWidgetTools/ElaDef.h b/ElaWidgetTools/ElaDef.h index 19e294e..568ae73 100755 --- a/ElaWidgetTools/ElaDef.h +++ b/ElaWidgetTools/ElaDef.h @@ -1,10 +1,23 @@ #ifndef ELADEF_H #define ELADEF_H +#include #include #include "ElaProperty.h" #define ElaDefVersion "2.0.2" +// 状态过渡动画取色:在 from、to 之间按 ratio(0~1) 做通道级线性插值 +inline QColor elaMixColor(const QColor& from, const QColor& to, qreal ratio) +{ + ratio = qBound(0.0, ratio, 1.0); + QColor mixed; + mixed.setRedF(from.redF() + (to.redF() - from.redF()) * ratio); + mixed.setGreenF(from.greenF() + (to.greenF() - from.greenF()) * ratio); + mixed.setBlueF(from.blueF() + (to.blueF() - from.blueF()) * ratio); + mixed.setAlphaF(from.alphaF() + (to.alphaF() - from.alphaF()) * ratio); + return mixed; +} + #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) #define Q_BEGIN_ENUM_CREATE(CLASS) \ namespace CLASS \ diff --git a/ElaWidgetTools/ElaPushButton.cpp b/ElaWidgetTools/ElaPushButton.cpp index 80b16fb..8c8a579 100755 --- a/ElaWidgetTools/ElaPushButton.cpp +++ b/ElaWidgetTools/ElaPushButton.cpp @@ -1,7 +1,9 @@ #include "ElaPushButton.h" +#include #include #include +#include #include "ElaIcon.h" #include "ElaTheme.h" @@ -26,6 +28,7 @@ ElaPushButton::ElaPushButton(QWidget* parent) d->_pDarkHoverColor = ElaThemeColor(ElaThemeType::Dark, BasicHover); d->_pLightPressColor = ElaThemeColor(ElaThemeType::Light, BasicPress); d->_pDarkPressColor = ElaThemeColor(ElaThemeType::Dark, BasicPress); + d->_pHoverAlpha = 0; d->_lightTextColor = ElaThemeColor(ElaThemeType::Light, BasicText); d->_darkTextColor = ElaThemeColor(ElaThemeType::Dark, BasicText); setMouseTracking(true); @@ -104,6 +107,35 @@ bool ElaPushButton::isHoverEnabled() const return d->_isHoverEnabled; } +bool ElaPushButton::event(QEvent* event) +{ + Q_D(ElaPushButton); + switch (event->type()) + { + case QEvent::Enter: + case QEvent::Leave: + { + if (isEnabled() && d->_isHoverEnabled) + { + QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); + connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + update(); + }); + alphaAnimation->setDuration(175); + alphaAnimation->setStartValue(d->_pHoverAlpha); + alphaAnimation->setEndValue(event->type() == QEvent::Enter ? 255 : 0); + alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + } + break; + } + default: + { + break; + } + } + return QPushButton::event(event); +} + void ElaPushButton::mousePressEvent(QMouseEvent* event) { Q_D(ElaPushButton); @@ -132,12 +164,12 @@ void ElaPushButton::paintEvent(QPaintEvent* event) if (d->_themeMode == ElaThemeType::Light) { painter.setPen(ElaThemeColor(ElaThemeType::Light, BasicBorder)); - painter.setBrush(isEnabled() ? d->_isPressed ? d->_pLightPressColor : ((d->_isHoverEnabled && underMouse()) ? d->_pLightHoverColor : d->_pLightDefaultColor) : ElaThemeColor(d->_themeMode, BasicDisable)); + painter.setBrush(isEnabled() ? d->_isPressed ? d->_pLightPressColor : elaMixColor(d->_pLightDefaultColor, d->_pLightHoverColor, d->_isHoverEnabled ? d->_pHoverAlpha / 255.0 : 0) : ElaThemeColor(d->_themeMode, BasicDisable)); } else { painter.setPen(Qt::NoPen); - painter.setBrush(isEnabled() ? d->_isPressed ? d->_pDarkPressColor : ((d->_isHoverEnabled && underMouse()) ? d->_pDarkHoverColor : d->_pDarkDefaultColor) : ElaThemeColor(d->_themeMode, BasicDisable)); + painter.setBrush(isEnabled() ? d->_isPressed ? d->_pDarkPressColor : elaMixColor(d->_pDarkDefaultColor, d->_pDarkHoverColor, d->_isHoverEnabled ? d->_pHoverAlpha / 255.0 : 0) : ElaThemeColor(d->_themeMode, BasicDisable)); } painter.drawRoundedRect(foregroundRect, d->_pBorderRadius, d->_pBorderRadius); // 底边线绘制 diff --git a/ElaWidgetTools/ElaPushButton.h b/ElaWidgetTools/ElaPushButton.h index 40c03cf..9d9c91c 100755 --- a/ElaWidgetTools/ElaPushButton.h +++ b/ElaWidgetTools/ElaPushButton.h @@ -35,6 +35,7 @@ class ELA_EXPORT ElaPushButton : public QPushButton bool isHoverEnabled() const; protected: + virtual bool event(QEvent* event) override; virtual void mousePressEvent(QMouseEvent* event) override; virtual void mouseReleaseEvent(QMouseEvent* event) override; virtual void paintEvent(QPaintEvent* event) override; diff --git a/ElaWidgetTools/ElaToggleButton.cpp b/ElaWidgetTools/ElaToggleButton.cpp index cad7fb7..ba88c92 100755 --- a/ElaWidgetTools/ElaToggleButton.cpp +++ b/ElaWidgetTools/ElaToggleButton.cpp @@ -17,6 +17,7 @@ ElaToggleButton::ElaToggleButton(QWidget* parent) d->_pBorderRadius = 3; d->_themeMode = eTheme->getThemeMode(); d->_pToggleAlpha = 0; + d->_pHoverAlpha = 0; setMouseTracking(true); setFixedSize(80, 32); QFont font = this->font(); @@ -58,11 +59,23 @@ bool ElaToggleButton::getIsToggled() const bool ElaToggleButton::event(QEvent* event) { + Q_D(ElaToggleButton); switch (event->type()) { case QEvent::Enter: case QEvent::Leave: { + if (isEnabled()) + { + QPropertyAnimation* hoverAnimation = new QPropertyAnimation(d, "pHoverAlpha"); + connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + update(); + }); + hoverAnimation->setDuration(175); + hoverAnimation->setStartValue(d->_pHoverAlpha); + hoverAnimation->setEndValue(event->type() == QEvent::Enter ? 255 : 0); + hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); + } update(); break; } @@ -123,12 +136,12 @@ void ElaToggleButton::paintEvent(QPaintEvent* event) if (d->_isToggled) { painter.setPen(ElaThemeColor(d->_themeMode, BasicBorder)); - painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, PrimaryPress) : (underMouse() ? ElaThemeColor(d->_themeMode, PrimaryHover) : ElaThemeColor(d->_themeMode, PrimaryNormal)) : ElaThemeColor(d->_themeMode, BasicDisable)); + painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, PrimaryPress) : elaMixColor(ElaThemeColor(d->_themeMode, PrimaryNormal), ElaThemeColor(d->_themeMode, PrimaryHover), d->_pHoverAlpha / 255.0) : ElaThemeColor(d->_themeMode, BasicDisable)); } else { painter.setPen(ElaThemeColor(d->_themeMode, BasicBorder)); - painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, BasicPress) : (underMouse() ? ElaThemeColor(d->_themeMode, BasicHover) : ElaThemeColor(d->_themeMode, BasicBase)) : ElaThemeColor(d->_themeMode, BasicDisable)); + painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, BasicPress) : elaMixColor(ElaThemeColor(d->_themeMode, BasicBase), ElaThemeColor(d->_themeMode, BasicHover), d->_pHoverAlpha / 255.0) : ElaThemeColor(d->_themeMode, BasicDisable)); } } else diff --git a/ElaWidgetTools/private/ElaPushButtonPrivate.h b/ElaWidgetTools/private/ElaPushButtonPrivate.h index c3ed0f2..3f87bbe 100755 --- a/ElaWidgetTools/private/ElaPushButtonPrivate.h +++ b/ElaWidgetTools/private/ElaPushButtonPrivate.h @@ -17,6 +17,7 @@ class ElaPushButtonPrivate : public QObject Q_PROPERTY_CREATE_D(QColor, DarkHoverColor) Q_PROPERTY_CREATE_D(QColor, LightPressColor) Q_PROPERTY_CREATE_D(QColor, DarkPressColor) + Q_PROPERTY_CREATE(int, HoverAlpha) public: explicit ElaPushButtonPrivate(QObject* parent = nullptr); ~ElaPushButtonPrivate(); diff --git a/ElaWidgetTools/private/ElaToggleButtonPrivate.h b/ElaWidgetTools/private/ElaToggleButtonPrivate.h index 9f20da0..ef52101 100755 --- a/ElaWidgetTools/private/ElaToggleButtonPrivate.h +++ b/ElaWidgetTools/private/ElaToggleButtonPrivate.h @@ -12,6 +12,7 @@ class ElaToggleButtonPrivate : public QObject Q_PROPERTY_CREATE_D(int, BorderRadius) Q_PROPERTY_CREATE_D(QString, Text) Q_PROPERTY_CREATE(int, ToggleAlpha) + Q_PROPERTY_CREATE(int, HoverAlpha) public: explicit ElaToggleButtonPrivate(QObject* parent = nullptr); ~ElaToggleButtonPrivate() override; From e621c191854219427a070361d259dabc413b7f8d Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 09:58:48 +0800 Subject: [PATCH 02/17] feat(anim): animate checkbox and radio hover and check states Hover fill fades over 150ms (OutCubic); the check mark and radio dot pop in over 200ms (OutBack) with scale and opacity. --- .../DeveloperComponents/ElaCheckBoxStyle.cpp | 85 ++++++++++++------- .../DeveloperComponents/ElaCheckBoxStyle.h | 7 ++ .../ElaRadioButtonStyle.cpp | 62 +++++++++++--- .../DeveloperComponents/ElaRadioButtonStyle.h | 7 ++ 4 files changed, 119 insertions(+), 42 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp index 2cd6f1d..cb5007d 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include "ElaTheme.h" ElaCheckBoxStyle::ElaCheckBoxStyle(QStyle* style) @@ -28,6 +30,30 @@ void ElaCheckBoxStyle::drawControl(ControlElement element, const QStyleOption* o if (const QStyleOptionButton* bopt = qstyleoption_cast(option)) { bool isEnabled = bopt->state.testFlag(QStyle::State_Enabled); + // 悬停与勾选过渡 + bool isHovered = bopt->state.testFlag(QStyle::State_MouseOver); + bool isChecked = bopt->state.testFlag(QStyle::State_On) || bopt->state.testFlag(QStyle::State_NoChange); + if (_firstPaint) + { + _firstPaint = false; + _lastHovered = isHovered; + _lastChecked = isChecked; + _hoverRatio = isHovered ? 1 : 0; + _checkRatio = isChecked ? 1 : 0; + } + else + { + if (isHovered != _lastHovered) + { + _startRatioAnimation(&_hoverRatio, isHovered ? 1 : 0, 150, QEasingCurve::OutCubic, widget); + _lastHovered = isHovered; + } + if (isChecked != _lastChecked) + { + _startRatioAnimation(&_checkRatio, isChecked ? 1 : 0, 200, QEasingCurve::OutBack, widget); + _lastChecked = isChecked; + } + } painter->save(); painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); QRect checkBoxRect = bopt->rect; @@ -36,42 +62,17 @@ void ElaCheckBoxStyle::drawControl(ControlElement element, const QStyleOption* o checkRect.adjust(1, 1, -1, -1); //复选框绘制 painter->setPen(Qt::NoPen); - if (bopt->state.testFlag(QStyle::State_On) || bopt->state.testFlag(QStyle::State_NoChange)) + if (isChecked) { painter->setPen(Qt::NoPen); - if (bopt->state.testFlag(QStyle::State_Sunken)) - { - painter->setBrush(ElaThemeColor(_themeMode, PrimaryPress)); - } - else - { - if (bopt->state.testFlag(QStyle::State_MouseOver)) - { - painter->setBrush(ElaThemeColor(_themeMode, PrimaryHover)); - } - else - { - painter->setBrush(ElaThemeColor(_themeMode, PrimaryNormal)); - } - } + painter->setBrush(bopt->state.testFlag(QStyle::State_Sunken) ? ElaThemeColor(_themeMode, PrimaryPress) : elaMixColor(ElaThemeColor(_themeMode, PrimaryNormal), ElaThemeColor(_themeMode, PrimaryHover), _hoverRatio)); } else { - if (bopt->state.testFlag(QStyle::State_Sunken)) + painter->setPen(ElaThemeColor(_themeMode, BasicBorderDeep)); + if (!bopt->state.testFlag(QStyle::State_Sunken)) { - painter->setPen(ElaThemeColor(_themeMode, BasicBorderDeep)); - } - else - { - painter->setPen(ElaThemeColor(_themeMode, BasicBorderDeep)); - if (bopt->state.testFlag(QStyle::State_MouseOver)) - { - painter->setBrush(ElaThemeColor(_themeMode, BasicHover)); - } - else - { - painter->setBrush(ElaThemeColor(_themeMode, BasicBase)); - } + painter->setBrush(elaMixColor(ElaThemeColor(_themeMode, BasicBase), ElaThemeColor(_themeMode, BasicHover), _hoverRatio)); } } painter->drawRoundedRect(checkRect, 2, 2); @@ -83,6 +84,12 @@ void ElaCheckBoxStyle::drawControl(ControlElement element, const QStyleOption* o QFont iconFont = QFont("ElaAwesome"); iconFont.setPixelSize(_pCheckIndicatorWidth * 0.75); painter->setFont(iconFont); + // 勾选弹出(缩放+透明度) + qreal scale = 0.6 + 0.4 * _checkRatio; + painter->translate(checkRect.center()); + painter->scale(scale, scale); + painter->translate(-checkRect.center()); + painter->setOpacity(qBound(0.0, _checkRatio, 1.0)); painter->drawText(checkRect, Qt::AlignCenter, QChar((unsigned short)ElaIconType::Check)); painter->restore(); } @@ -132,3 +139,21 @@ int ElaCheckBoxStyle::pixelMetric(PixelMetric metric, const QStyleOption* option } return QProxyStyle::pixelMetric(metric, option, widget); } + +void ElaCheckBoxStyle::_startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const +{ + QVariantAnimation* ratioAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + *targetRatio = value.toReal(); + if (widgetGuard) + { + widgetGuard->update(); + } + }); + ratioAnimation->setDuration(duration); + ratioAnimation->setEasingCurve(QEasingCurve(curve)); + ratioAnimation->setStartValue(*targetRatio); + ratioAnimation->setEndValue(endRatio); + ratioAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h index dfb8150..11c2aea 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h @@ -1,6 +1,7 @@ #ifndef ELACHECKBOXSTYLE_H #define ELACHECKBOXSTYLE_H +#include #include #include "ElaDef.h" @@ -16,6 +17,12 @@ class ElaCheckBoxStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; + mutable qreal _hoverRatio{0}; + mutable qreal _checkRatio{0}; + mutable bool _lastHovered{false}; + mutable bool _lastChecked{false}; + mutable bool _firstPaint{true}; + void _startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const; }; #endif // ELACHECKBOXSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp index ec938ab..4bcfc3b 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include "ElaTheme.h" ElaRadioButtonStyle::ElaRadioButtonStyle(QStyle* style) @@ -26,6 +28,30 @@ void ElaRadioButtonStyle::drawPrimitive(PrimitiveElement element, const QStyleOp { break; } + // 悬停与勾选过渡 + bool isHovered = bopt->state.testFlag(QStyle::State_MouseOver); + bool isChecked = !bopt->state.testFlag(QStyle::State_Off); + if (_firstPaint) + { + _firstPaint = false; + _lastHovered = isHovered; + _lastChecked = isChecked; + _hoverRatio = isHovered ? 1 : 0; + _checkRatio = isChecked ? 1 : 0; + } + else + { + if (isHovered != _lastHovered) + { + _startRatioAnimation(&_hoverRatio, isHovered ? 1 : 0, 150, QEasingCurve::OutCubic, widget); + _lastHovered = isHovered; + } + if (isChecked != _lastChecked) + { + _startRatioAnimation(&_checkRatio, isChecked ? 1 : 0, 200, QEasingCurve::OutBack, widget); + _lastChecked = isChecked; + } + } QRect buttonRect = bopt->rect; buttonRect.adjust(1, 1, -1, -1); painter->save(); @@ -34,14 +60,7 @@ void ElaRadioButtonStyle::drawPrimitive(PrimitiveElement element, const QStyleOp if (bopt->state & QStyle::State_Off) { painter->setPen(QPen(ElaThemeColor(_themeMode, BasicBorder), 1.5)); - if (bopt->state & QStyle::State_MouseOver) - { - painter->setBrush(ElaThemeColor(_themeMode, BasicHover)); - } - else - { - painter->setBrush(ElaThemeColor(_themeMode, BasicBase)); - } + painter->setBrush(elaMixColor(ElaThemeColor(_themeMode, BasicBase), ElaThemeColor(_themeMode, BasicHover), _hoverRatio)); painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), 8.5, 8.5); } else @@ -50,24 +69,25 @@ void ElaRadioButtonStyle::drawPrimitive(PrimitiveElement element, const QStyleOp // 外圆形 painter->setBrush(ElaThemeColor(_themeMode, PrimaryNormal)); painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 2, buttonRect.width() / 2); - // 内圆形 + // 内圆形(勾选弹出) painter->setBrush(ElaThemeColor(_themeMode, BasicTextInvert)); + qreal dotScale = 0.6 + 0.4 * _checkRatio; if (bopt->state & QStyle::State_Sunken) { if (bopt->state & QStyle::State_MouseOver) { - painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 4.5, buttonRect.width() / 4.5); + painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 4.5 * dotScale, buttonRect.width() / 4.5 * dotScale); } } else { if (bopt->state & QStyle::State_MouseOver) { - painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 3.5, buttonRect.width() / 3.5); + painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 3.5 * dotScale, buttonRect.width() / 3.5 * dotScale); } else { - painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 4, buttonRect.width() / 4); + painter->drawEllipse(QPointF(buttonRect.center().x() + 1, buttonRect.center().y() + 1), buttonRect.width() / 4 * dotScale, buttonRect.width() / 4 * dotScale); } } } @@ -102,3 +122,21 @@ int ElaRadioButtonStyle::pixelMetric(PixelMetric metric, const QStyleOption* opt } return QProxyStyle::pixelMetric(metric, option, widget); } + +void ElaRadioButtonStyle::_startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const +{ + QVariantAnimation* ratioAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + *targetRatio = value.toReal(); + if (widgetGuard) + { + widgetGuard->update(); + } + }); + ratioAnimation->setDuration(duration); + ratioAnimation->setEasingCurve(QEasingCurve(curve)); + ratioAnimation->setStartValue(*targetRatio); + ratioAnimation->setEndValue(endRatio); + ratioAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h index 485f3f3..331ceed 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h @@ -1,6 +1,7 @@ #ifndef ELARADIOBUTTONSTYLE_H #define ELARADIOBUTTONSTYLE_H +#include #include #include "ElaDef.h" @@ -15,6 +16,12 @@ class ElaRadioButtonStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; + mutable qreal _hoverRatio{0}; + mutable qreal _checkRatio{0}; + mutable bool _lastHovered{false}; + mutable bool _lastChecked{false}; + mutable bool _firstPaint{true}; + void _startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const; }; #endif // ELARADIOBUTTONSTYLE_H From edec66becde91672ebe8f49b729fd636b4cc7785 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 09:58:48 +0800 Subject: [PATCH 03/17] feat(anim): animate combobox and spinbox body hover Body background now interpolates between base and hover colors over 150ms instead of switching instantly. --- .../DeveloperComponents/ElaComboBoxStyle.cpp | 37 ++++++++++++++-- .../DeveloperComponents/ElaComboBoxStyle.h | 4 ++ .../DeveloperComponents/ElaSpinBoxStyle.cpp | 42 +++++++++++++++---- .../DeveloperComponents/ElaSpinBoxStyle.h | 4 ++ 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp index 40f0d7e..50fe1bc 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include "ElaTheme.h" ElaComboBoxStyle::ElaComboBoxStyle(QStyle* style) @@ -148,12 +150,23 @@ void ElaComboBoxStyle::drawComplexControl(ComplexControl control, const QStyleOp painter->setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing); //背景绘制 bool isEnabled = copt->state.testFlag(QStyle::State_Enabled); + // 悬停过渡 + bool isHovered = copt->state.testFlag(QStyle::State_MouseOver); + if (_firstPaint) + { + _firstPaint = false; + _lastHovered = isHovered; + _hoverRatio = isHovered ? 1 : 0; + } + else if (isHovered != _lastHovered) + { + _startHoverAnimation(isHovered ? 1 : 0, widget); + _lastHovered = isHovered; + } painter->setPen(ElaThemeColor(_themeMode, BasicBorder)); painter->setBrush(isEnabled ? (copt->state.testFlag(QStyle::State_HasFocus) && copt->editable) ? ElaThemeColor(_themeMode, DialogBase) - : copt->state.testFlag(QStyle::State_MouseOver) - ? ElaThemeColor(_themeMode, BasicHover) - : ElaThemeColor(_themeMode, BasicBase) + : elaMixColor(ElaThemeColor(_themeMode, BasicBase), ElaThemeColor(_themeMode, BasicHover), _hoverRatio) : ElaThemeColor(_themeMode, BasicDisable)); QRect comboBoxRect = copt->rect; comboBoxRect.adjust(_shadowBorderWidth, 1, -_shadowBorderWidth, -1); @@ -260,3 +273,21 @@ QSize ElaComboBoxStyle::sizeFromContents(ContentsType type, const QStyleOption* } return QProxyStyle::sizeFromContents(type, option, size, widget); } + +void ElaComboBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widget) const +{ + QVariantAnimation* hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard) + { + widgetGuard->update(); + } + }); + hoverAnimation->setDuration(150); + hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + hoverAnimation->setStartValue(_hoverRatio); + hoverAnimation->setEndValue(endRatio); + hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h index 9500892..b8a8a27 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h @@ -22,6 +22,10 @@ class ElaComboBoxStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; int _shadowBorderWidth{6}; + mutable qreal _hoverRatio{0}; + mutable bool _lastHovered{false}; + mutable bool _firstPaint{true}; + void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; }; #endif // ELACOMBOBOXSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp index 2cf7adf..3272cda 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include #include ElaSpinBoxStyle::ElaSpinBoxStyle(QStyle* style) { @@ -36,16 +38,22 @@ void ElaSpinBoxStyle::drawComplexControl(ComplexControl control, const QStyleOpt QRect spinBoxRect = sopt->rect.adjusted(1, 1, -1, -1); painter->setPen(ElaThemeColor(_themeMode, BasicBorder)); bool isEnable = sopt->state.testFlag(QStyle::State_Enabled); + // 悬停过渡 + bool isHovered = sopt->state.testFlag(QStyle::State_MouseOver); + if (_firstPaint) + { + _firstPaint = false; + _lastHovered = isHovered; + _hoverRatio = isHovered ? 1 : 0; + } + else if (isHovered != _lastHovered) + { + _startHoverAnimation(isHovered ? 1 : 0, widget); + _lastHovered = isHovered; + } if (isEnable) { - if (sopt->state & QStyle::State_MouseOver) - { - painter->setBrush(ElaThemeColor(_themeMode, BasicHover)); - } - else - { - painter->setBrush(ElaThemeColor(_themeMode, BasicBase)); - } + painter->setBrush(elaMixColor(ElaThemeColor(_themeMode, BasicBase), ElaThemeColor(_themeMode, BasicHover), _hoverRatio)); } else { @@ -209,3 +217,21 @@ QRect ElaSpinBoxStyle::subControlRect(ComplexControl cc, const QStyleOptionCompl } return rect; } + +void ElaSpinBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widget) const +{ + QVariantAnimation* hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard) + { + widgetGuard->update(); + } + }); + hoverAnimation->setDuration(150); + hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + hoverAnimation->setStartValue(_hoverRatio); + hoverAnimation->setEndValue(endRatio); + hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h index eaff1f6..1805e42 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h @@ -16,6 +16,10 @@ class ElaSpinBoxStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; + mutable qreal _hoverRatio{0}; + mutable bool _lastHovered{false}; + mutable bool _firstPaint{true}; + void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; }; #endif // ELASPINBOXSTYLE_H From f5f3c871fbec8369ae7be8496120c2667da91254 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 09:58:48 +0800 Subject: [PATCH 04/17] feat(anim): animate row hover in list, table and tree views Track hovered row per index (or per hover row for tables) and fade the row highlight in and out over 150ms; selected rows blend between selected and selected-hover colors. --- .../DeveloperComponents/ElaListViewStyle.cpp | 82 ++++++++++++++---- .../DeveloperComponents/ElaListViewStyle.h | 7 ++ .../DeveloperComponents/ElaTableViewStyle.cpp | 56 ++++++++++++- .../DeveloperComponents/ElaTableViewStyle.h | 5 ++ .../DeveloperComponents/ElaTreeViewStyle.cpp | 84 +++++++++++++++---- .../DeveloperComponents/ElaTreeViewStyle.h | 7 ++ 6 files changed, 206 insertions(+), 35 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp index beb2cfa..7f2072a 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include "ElaListView.h" #include "ElaTheme.h" @@ -29,6 +31,38 @@ void ElaListViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio // Item背景 if (const QStyleOptionViewItem* vopt = qstyleoption_cast(option)) { + // 行悬停过渡(按行键控,旧行淡出、新行淡入) + bool isHovered = vopt->state.testFlag(QStyle::State_MouseOver); + QPersistentModelIndex rowKey = vopt->index.sibling(vopt->index.row(), 0); + if (_firstPaint) + { + _firstPaint = false; + _hoverIndex = isHovered ? rowKey : QPersistentModelIndex(); + _hoverInRatio = isHovered ? 1 : 0; + } + else if (rowKey.isValid() && isHovered && rowKey != _hoverIndex) + { + // 悬停行切换 + _fadeOutIndex = _hoverIndex; + if (_fadeOutIndex.isValid()) + { + _startRowHoverAnimation(false, widget); + } + _hoverIndex = rowKey; + _startRowHoverAnimation(true, widget); + } + else if (rowKey.isValid() && !isHovered && rowKey == _hoverIndex) + { + // 悬停移出行外 + _fadeOutIndex = _hoverIndex; + _hoverIndex = QPersistentModelIndex(); + _startRowHoverAnimation(false, widget); + } + qreal hoverRatio = 0; + if (rowKey.isValid()) + { + hoverRatio = rowKey == _hoverIndex ? _hoverInRatio : rowKey == _fadeOutIndex ? _hoverOutRatio : 0; + } painter->save(); painter->setRenderHint(QPainter::Antialiasing); QRect itemRect = vopt->rect; @@ -37,24 +71,15 @@ void ElaListViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio path.addRoundedRect(itemRect, 4, 4); if (vopt->state & QStyle::State_Selected) { - if (vopt->state & QStyle::State_MouseOver) - { - // 选中时覆盖 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicSelectedHoverAlpha)); - } - else - { - // 选中 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicSelectedAlpha)); - } + // 选中(悬停时覆盖高亮) + painter->fillPath(path, elaMixColor(ElaThemeColor(_themeMode, BasicSelectedAlpha), ElaThemeColor(_themeMode, BasicSelectedHoverAlpha), hoverRatio)); } - else + else if (hoverRatio > 0) { - if (vopt->state & QStyle::State_MouseOver) - { - // 覆盖时颜色 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicHoverAlpha)); - } + // 覆盖时颜色 + QColor hoverColor = ElaThemeColor(_themeMode, BasicHoverAlpha); + hoverColor.setAlphaF(hoverColor.alphaF() * hoverRatio); + painter->fillPath(path, hoverColor); } painter->restore(); } @@ -192,3 +217,28 @@ QSize ElaListViewStyle::sizeFromContents(ContentsType type, const QStyleOption* } return QProxyStyle::sizeFromContents(type, option, size, widget); } + +void ElaListViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const +{ + QVariantAnimation* rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard) + { + widgetGuard->update(); + } + }); + rowAnimation->setDuration(150); + rowAnimation->setEasingCurve(QEasingCurve::OutCubic); + rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); + rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); + rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h index bf1c734..db097f7 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h @@ -1,6 +1,7 @@ #ifndef ELALISTVIEWSTYLE_H #define ELALISTVIEWSTYLE_H +#include #include #include "ElaDef.h" @@ -19,6 +20,12 @@ class ElaListViewStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; int _leftPadding{11}; + mutable QPersistentModelIndex _hoverIndex; + mutable QPersistentModelIndex _fadeOutIndex; + mutable qreal _hoverInRatio{0}; + mutable qreal _hoverOutRatio{0}; + mutable bool _firstPaint{true}; + void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; #endif // ELALISTVIEWSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp index de4a197..108f0f5 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include "ElaTableView.h" #include "ElaTheme.h" @@ -35,15 +37,40 @@ void ElaTableViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOpti { return; } + // 行悬停过渡(按行号键控,旧行淡出、新行淡入) + int hoverRow = _pCurrentHoverRow; + if (hoverRow != _lastHoverRow) + { + _fadeOutRow = _lastHoverRow; + if (_fadeOutRow >= 0) + { + _startRowHoverAnimation(false, widget); + } + _lastHoverRow = hoverRow; + if (hoverRow >= 0) + { + _startRowHoverAnimation(true, widget); + } + } painter->save(); painter->setRenderHint(QPainter::Antialiasing); QAbstractItemView::SelectionBehavior selectionBehavior = tabView->selectionBehavior(); if (selectionBehavior == QAbstractItemView::SelectRows) { - if (vopt->index.row() == _pCurrentHoverRow) + if (hoverRow >= 0 && vopt->index.row() == hoverRow) { painter->setPen(Qt::NoPen); - painter->setBrush(ElaThemeColor(_themeMode, BasicHoverAlpha)); + QColor hoverColor = ElaThemeColor(_themeMode, BasicHoverAlpha); + hoverColor.setAlphaF(hoverColor.alphaF() * _hoverInRatio); + painter->setBrush(hoverColor); + painter->drawRect(vopt->rect); + } + else if (vopt->index.row() >= 0 && vopt->index.row() == _fadeOutRow && _hoverOutRatio > 0) + { + painter->setPen(Qt::NoPen); + QColor hoverColor = ElaThemeColor(_themeMode, BasicHoverAlpha); + hoverColor.setAlphaF(hoverColor.alphaF() * _hoverOutRatio); + painter->setBrush(hoverColor); painter->drawRect(vopt->rect); } } @@ -100,6 +127,31 @@ void ElaTableViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOpti QProxyStyle::drawPrimitive(element, option, painter, widget); } +void ElaTableViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const +{ + QVariantAnimation* rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard) + { + widgetGuard->update(); + } + }); + rowAnimation->setDuration(150); + rowAnimation->setEasingCurve(QEasingCurve::OutCubic); + rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); + rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); + rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} + void ElaTableViewStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { //qDebug() << element << option->rect; diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h index f52fec7..c542cc6 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h @@ -20,6 +20,11 @@ class ElaTableViewStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; int _horizontalPadding{11}; + mutable int _lastHoverRow{-1}; + mutable int _fadeOutRow{-1}; + mutable qreal _hoverInRatio{0}; + mutable qreal _hoverOutRatio{0}; + void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; #endif // ELATABLEVIEWSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp index 8fdee05..c37b3cd 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include "ElaTheme.h" @@ -31,33 +33,56 @@ void ElaTreeViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio // Item背景 if (const QStyleOptionViewItem *vopt = qstyleoption_cast(option)) { + // 行悬停过渡(按行键控,旧行淡出、新行淡入) + bool isEnable = vopt->state.testFlag(QStyle::State_Enabled); + bool isHovered = vopt->state.testFlag(QStyle::State_MouseOver) && isEnable; + QPersistentModelIndex rowKey = vopt->index.sibling(vopt->index.row(), 0); + if (_firstPaint) + { + _firstPaint = false; + _hoverIndex = isHovered ? rowKey : QPersistentModelIndex(); + _hoverInRatio = isHovered ? 1 : 0; + } + else if (rowKey.isValid() && isHovered && rowKey != _hoverIndex) + { + // 悬停行切换 + _fadeOutIndex = _hoverIndex; + if (_fadeOutIndex.isValid()) + { + _startRowHoverAnimation(false, widget); + } + _hoverIndex = rowKey; + _startRowHoverAnimation(true, widget); + } + else if (rowKey.isValid() && !isHovered && rowKey == _hoverIndex) + { + // 悬停移出行外 + _fadeOutIndex = _hoverIndex; + _hoverIndex = QPersistentModelIndex(); + _startRowHoverAnimation(false, widget); + } + qreal hoverRatio = 0; + if (rowKey.isValid() && isEnable) + { + hoverRatio = rowKey == _hoverIndex ? _hoverInRatio : rowKey == _fadeOutIndex ? _hoverOutRatio : 0; + } painter->save(); painter->setRenderHint(QPainter::Antialiasing); QRect itemRect = vopt->rect; itemRect.adjust(0, 2, 0, -2); QPainterPath path; path.addRoundedRect(itemRect, 4, 4); - bool isEnable = vopt->state.testFlag(QStyle::State_Enabled); if (vopt->state & QStyle::State_Selected) { - if (vopt->state & QStyle::State_MouseOver && isEnable) - { - // 选中时覆盖 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicSelectedHoverAlpha)); - } - else - { - // 选中 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicSelectedAlpha)); - } + // 选中(悬停时覆盖高亮) + painter->fillPath(path, elaMixColor(ElaThemeColor(_themeMode, BasicSelectedAlpha), ElaThemeColor(_themeMode, BasicSelectedHoverAlpha), hoverRatio)); } - else + else if (hoverRatio > 0) { - if (vopt->state & QStyle::State_MouseOver && isEnable) - { - // 覆盖时颜色 - painter->fillPath(path, ElaThemeColor(_themeMode, BasicHoverAlpha)); - } + // 覆盖时颜色 + QColor hoverColor = ElaThemeColor(_themeMode, BasicHoverAlpha); + hoverColor.setAlphaF(hoverColor.alphaF() * hoverRatio); + painter->fillPath(path, hoverColor); } painter->restore(); } @@ -331,4 +356,29 @@ QRect ElaTreeViewStyle::subElementRect(SubElement element, const QStyleOption *o } } return QProxyStyle::subElementRect(element, option, widget); +} + +void ElaTreeViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget *widget) const +{ + QVariantAnimation *rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard) + { + widgetGuard->update(); + } + }); + rowAnimation->setDuration(150); + rowAnimation->setEasingCurve(QEasingCurve::OutCubic); + rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); + rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); + rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); } \ No newline at end of file diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h index d59fc08..f184fb9 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h @@ -1,6 +1,7 @@ #ifndef ELATREEVIEWSTYLE_H #define ELATREEVIEWSTYLE_H +#include #include #include "ElaDef.h" @@ -21,6 +22,12 @@ class ElaTreeViewStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; int _leftPadding{11}; + mutable QPersistentModelIndex _hoverIndex; + mutable QPersistentModelIndex _fadeOutIndex; + mutable qreal _hoverInRatio{0}; + mutable qreal _hoverOutRatio{0}; + mutable bool _firstPaint{true}; + void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; #endif // ELATREEVIEWSTYLE_H From 0ef0515d237d003f15f1a936a8f18691c7709639 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 09:58:48 +0800 Subject: [PATCH 05/17] feat(anim): animate tab and menu item hover Tab and menu item hover overlays fade in and out over 150ms, keyed by item rect since one style instance serves many items. --- .../DeveloperComponents/ElaMenuStyle.cpp | 59 ++++++++++++++++++- .../DeveloperComponents/ElaMenuStyle.h | 6 ++ .../DeveloperComponents/ElaTabBarStyle.cpp | 59 ++++++++++++++++++- .../DeveloperComponents/ElaTabBarStyle.h | 6 ++ 4 files changed, 125 insertions(+), 5 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp index 3743a83..348f6a7 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include "ElaMenu.h" #include "ElaTheme.h" @@ -81,13 +83,39 @@ void ElaMenuStyle::drawControl(ControlElement element, const QStyleOption* optio qreal textLeftSpacing = 8; painter->save(); painter->setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing); - //覆盖效果 - if (mopt->state.testFlag(QStyle::State_Enabled) && (mopt->state.testFlag(QStyle::State_MouseOver) || mopt->state.testFlag(QStyle::State_Selected))) + //覆盖效果(悬停过渡,按菜单项区域键控) + bool isHovered = mopt->state.testFlag(QStyle::State_Enabled) && (mopt->state.testFlag(QStyle::State_MouseOver) || mopt->state.testFlag(QStyle::State_Selected)); + if (_firstPaint) + { + _firstPaint = false; + _hoverRect = isHovered ? menuRect : QRect(); + _hoverInRatio = isHovered ? 1 : 0; + } + else if (isHovered && menuRect != _hoverRect) + { + _fadeRect = _hoverRect; + if (_fadeRect.isValid()) + { + _startItemHoverAnimation(false, widget); + } + _hoverRect = menuRect; + _startItemHoverAnimation(true, widget); + } + else if (!isHovered && menuRect == _hoverRect) + { + _fadeRect = _hoverRect; + _hoverRect = QRect(); + _startItemHoverAnimation(false, widget); + } + qreal hoverRatio = menuRect == _hoverRect ? _hoverInRatio : menuRect == _fadeRect ? _hoverOutRatio : 0; + if (hoverRatio > 0) { QRect hoverRect = menuRect; hoverRect.adjust(0, 2, 0, -2); painter->setPen(Qt::NoPen); - painter->setBrush(ElaThemeColor(_themeMode, PopupHover)); + QColor hoverColor = ElaThemeColor(_themeMode, PopupHover); + hoverColor.setAlphaF(hoverColor.alphaF() * hoverRatio); + painter->setBrush(hoverColor); painter->drawRoundedRect(hoverRect, 5, 5); } //Icon绘制 @@ -245,3 +273,28 @@ QSize ElaMenuStyle::sizeFromContents(ContentsType type, const QStyleOption* opti } return QProxyStyle::sizeFromContents(type, option, size, widget); } + +void ElaMenuStyle::_startItemHoverAnimation(bool isFadeIn, const QWidget* widget) const +{ + QVariantAnimation* itemAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(itemAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard) + { + widgetGuard->update(); + } + }); + itemAnimation->setDuration(150); + itemAnimation->setEasingCurve(QEasingCurve::OutCubic); + itemAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); + itemAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); + itemAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h index b9e5a5d..b589e90 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h @@ -23,6 +23,12 @@ class ElaMenuStyle : public QProxyStyle int _shadowBorderWidth{6}; ElaThemeType::ThemeMode _themeMode; mutable int _iconWidth{0}; + mutable QRect _hoverRect; + mutable QRect _fadeRect; + mutable qreal _hoverInRatio{0}; + mutable qreal _hoverOutRatio{0}; + mutable bool _firstPaint{true}; + void _startItemHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; #endif // ELAMENUSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp index b1c517f..d4d940e 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include "ElaTheme.h" ElaTabBarStyle::ElaTabBarStyle(QStyle* style) @@ -122,9 +124,37 @@ void ElaTabBarStyle::drawControl(ControlElement element, const QStyleOption* opt } else { - if (topt->state.testFlag(QStyle::State_MouseOver)) + // 悬停过渡(按 tab 区域键控,旧 tab 淡出、新 tab 淡入) + bool isHovered = topt->state.testFlag(QStyle::State_MouseOver); + QRect hoverRect = topt->rect; + if (_firstPaint) { - painter->setBrush(ElaThemeColor(_themeMode, BasicHoverAlpha)); + _firstPaint = false; + _hoverRect = isHovered ? hoverRect : QRect(); + _hoverInRatio = isHovered ? 1 : 0; + } + else if (isHovered && hoverRect != _hoverRect) + { + _fadeRect = _hoverRect; + if (_fadeRect.isValid()) + { + _startTabHoverAnimation(false, widget); + } + _hoverRect = hoverRect; + _startTabHoverAnimation(true, widget); + } + else if (!isHovered && hoverRect == _hoverRect) + { + _fadeRect = _hoverRect; + _hoverRect = QRect(); + _startTabHoverAnimation(false, widget); + } + qreal hoverRatio = hoverRect == _hoverRect ? _hoverInRatio : hoverRect == _fadeRect ? _hoverOutRatio : 0; + if (hoverRatio > 0) + { + QColor hoverColor = ElaThemeColor(_themeMode, BasicHoverAlpha); + hoverColor.setAlphaF(hoverColor.alphaF() * hoverRatio); + painter->setBrush(hoverColor); } else { @@ -218,3 +248,28 @@ QRect ElaTabBarStyle::subElementRect(SubElement element, const QStyleOption* opt } return QProxyStyle::subElementRect(element, option, widget); } + +void ElaTabBarStyle::_startTabHoverAnimation(bool isFadeIn, const QWidget* widget) const +{ + QVariantAnimation* tabAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(tabAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard) + { + widgetGuard->update(); + } + }); + tabAnimation->setDuration(150); + tabAnimation->setEasingCurve(QEasingCurve::OutCubic); + tabAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); + tabAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); + tabAnimation->start(QAbstractAnimation::DeleteWhenStopped); +} diff --git a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h index 0a1890e..1c774b4 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h @@ -18,6 +18,12 @@ class ElaTabBarStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; + mutable QRect _hoverRect; + mutable QRect _fadeRect; + mutable qreal _hoverInRatio{0}; + mutable qreal _hoverOutRatio{0}; + mutable bool _firstPaint{true}; + void _startTabHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; #endif // ELATABBARSTYLE_H From ededb4c0af07052b4590f630f49794d6d18063af Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 10:36:08 +0800 Subject: [PATCH 06/17] chore: ignore local example run script --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 89a9550..d4668b0 100755 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,9 @@ __pycache__/ /Qt6.10.3/ /Qt/ +# 本地运行脚本(个人用) +/run_example_local.sh + # 本地资源(体积大,不入库) /PySide6Example/Resource/ /ElaWidgetToolsExample/Resource/ From 318e3a1cd2adda0f58046ac2a805342d51fab63d Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 10:54:43 +0800 Subject: [PATCH 07/17] perf(widget): pause infinite animations when widgets are hidden - ElaSkeleton: pause shimmer animation on hideEvent, resume on showEvent, and skip valueChanged updates while not visible - ElaProgressRing: same pause/resume for the two busy animations, plus a visibility guard in the valueChanged handler - setIsBusying(false) semantics unchanged (stop + reset) No visual change: durations, easing and shimmer/busy rendering untouched. --- ElaWidgetTools/ElaProgressRing.cpp | 34 ++++++++++++++++++++++++++++++ ElaWidgetTools/ElaProgressRing.h | 2 ++ ElaWidgetTools/ElaSkeleton.cpp | 26 +++++++++++++++++++++++ ElaWidgetTools/ElaSkeleton.h | 2 ++ 4 files changed, 64 insertions(+) diff --git a/ElaWidgetTools/ElaProgressRing.cpp b/ElaWidgetTools/ElaProgressRing.cpp index 864b01b..556421b 100755 --- a/ElaWidgetTools/ElaProgressRing.cpp +++ b/ElaWidgetTools/ElaProgressRing.cpp @@ -34,6 +34,10 @@ ElaProgressRing::ElaProgressRing(QWidget* parent) d->_busyStartDegAnimation = new QPropertyAnimation(d, "pBusyStartDeg"); connect(d->_busyStartDegAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); d->_busyStartDegAnimation->setEasingCurve(QEasingCurve::Linear); @@ -61,6 +65,36 @@ ElaProgressRing::~ElaProgressRing() { } +void ElaProgressRing::showEvent(QShowEvent* event) +{ + QWidget::showEvent(event); + Q_D(ElaProgressRing); + // 恢复 busy 动画;对未暂停/未运行的动画 resume() 是无害的空操作,不会违反 setIsBusying 语义 + if (d->_busyStartDegAnimation) + { + d->_busyStartDegAnimation->resume(); + } + if (d->_busyContentDegAnimation) + { + d->_busyContentDegAnimation->resume(); + } +} + +void ElaProgressRing::hideEvent(QHideEvent* event) +{ + QWidget::hideEvent(event); + Q_D(ElaProgressRing); + // 暂停而非停止,保留进度;setIsBusying(false) 的停止语义不受影响 + if (d->_busyStartDegAnimation) + { + d->_busyStartDegAnimation->pause(); + } + if (d->_busyContentDegAnimation) + { + d->_busyContentDegAnimation->pause(); + } +} + void ElaProgressRing::setIsBusying(bool isBusying) { Q_D(ElaProgressRing); diff --git a/ElaWidgetTools/ElaProgressRing.h b/ElaWidgetTools/ElaProgressRing.h index 62dbfc7..f602186 100755 --- a/ElaWidgetTools/ElaProgressRing.h +++ b/ElaWidgetTools/ElaProgressRing.h @@ -31,6 +31,8 @@ class ELA_EXPORT ElaProgressRing : public QWidget protected: virtual void paintEvent(QPaintEvent* event) override; + virtual void showEvent(QShowEvent* event) override; + virtual void hideEvent(QHideEvent* event) override; }; #endif //ELAFRAMEWORK_ELAWIDGETTOOLS_INCLUDE_ELAPROGRESSRING_H_ diff --git a/ElaWidgetTools/ElaSkeleton.cpp b/ElaWidgetTools/ElaSkeleton.cpp index e038f61..55ca35b 100644 --- a/ElaWidgetTools/ElaSkeleton.cpp +++ b/ElaWidgetTools/ElaSkeleton.cpp @@ -36,6 +36,10 @@ void ElaSkeletonPrivate::_startShimmerAnimation() _shimmerAnimation->setLoopCount(-1); connect(_shimmerAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { + if (!q->isVisible()) + { + return; + } _pShimmerPosition = value.toReal(); q->update(); }); @@ -77,6 +81,28 @@ ElaSkeleton::~ElaSkeleton() d->_stopShimmerAnimation(); } +void ElaSkeleton::showEvent(QShowEvent* event) +{ + QWidget::showEvent(event); + Q_D(ElaSkeleton); + // 恢复微光动画;对未暂停的动画 resume() 是无害的空操作 + if (d->_shimmerAnimation) + { + d->_shimmerAnimation->resume(); + } +} + +void ElaSkeleton::hideEvent(QHideEvent* event) +{ + QWidget::hideEvent(event); + Q_D(ElaSkeleton); + // 暂停而非停止,保留进度,重新显示时无跳变 + if (d->_shimmerAnimation) + { + d->_shimmerAnimation->pause(); + } +} + void ElaSkeleton::setSkeletonType(SkeletonType type) { Q_D(ElaSkeleton); diff --git a/ElaWidgetTools/ElaSkeleton.h b/ElaWidgetTools/ElaSkeleton.h index 51eb1b0..612bb33 100644 --- a/ElaWidgetTools/ElaSkeleton.h +++ b/ElaWidgetTools/ElaSkeleton.h @@ -29,6 +29,8 @@ class ELA_EXPORT ElaSkeleton : public QWidget protected: virtual void paintEvent(QPaintEvent* event) override; + virtual void showEvent(QShowEvent* event) override; + virtual void hideEvent(QHideEvent* event) override; virtual QSize sizeHint() const override; }; From e6d51cfe1ad706d3388549d278173ec858d51989 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 11:05:07 +0800 Subject: [PATCH 08/17] perf(anim): skip repaints while widgets are not visible Add a visibility guard to every animation valueChanged callback that marks its widget dirty (~56 sites across widgets, private impls, developer components and styles). State updates stay outside the guard so the final animation value is never lost; only the repaint is skipped. Style/delegate callbacks now check widgetGuard/owner view visibility as well. No visual change for visible widgets. --- .../ElaCentralStackedWidget.cpp | 16 +++++++++ .../DeveloperComponents/ElaCheckBoxStyle.cpp | 2 +- .../ElaColorValueSliderStyle.cpp | 7 +++- .../DeveloperComponents/ElaComboBoxStyle.cpp | 2 +- .../ElaDrawerContainer.cpp | 4 +++ .../DeveloperComponents/ElaDrawerHeader.cpp | 4 +++ .../DeveloperComponents/ElaFooterDelegate.cpp | 20 ++++++++--- .../DeveloperComponents/ElaMaskWidget.cpp | 4 +++ .../ElaNavigationStyle.cpp | 35 +++++++++++++++---- .../DeveloperComponents/ElaPivotView.cpp | 8 +++++ .../ElaPopularCardFloater.cpp | 8 +++++ .../ElaRadioButtonStyle.cpp | 2 +- .../ElaRollerPickerContainer.cpp | 4 +++ .../DeveloperComponents/ElaScrollBarStyle.cpp | 10 ++++-- .../DeveloperComponents/ElaSliderStyle.cpp | 7 +++- .../DeveloperComponents/ElaSpinBoxStyle.cpp | 2 +- .../ElaThemeAnimationWidget.cpp | 4 +++ .../ElaToolButtonStyle.cpp | 2 +- ElaWidgetTools/ElaComboBox.cpp | 8 +++++ ElaWidgetTools/ElaCopyButton.cpp | 10 ++++-- ElaWidgetTools/ElaDashboardGauge.cpp | 2 +- ElaWidgetTools/ElaDoubleSpinBox.cpp | 8 +++++ ElaWidgetTools/ElaFloatButton.cpp | 4 +-- ElaWidgetTools/ElaIconButton.cpp | 8 +++++ ElaWidgetTools/ElaLineEdit.cpp | 8 +++++ ElaWidgetTools/ElaMenu.cpp | 4 +++ ElaWidgetTools/ElaMultiSelectComboBox.cpp | 8 +++++ ElaWidgetTools/ElaPasswordBox.cpp | 8 +++++ ElaWidgetTools/ElaPlainTextEdit.cpp | 8 +++++ ElaWidgetTools/ElaPopularCard.cpp | 8 +++++ ElaWidgetTools/ElaProgressBar.cpp | 5 ++- ElaWidgetTools/ElaPromotionCard.cpp | 4 +++ ElaWidgetTools/ElaPushButton.cpp | 4 +++ ElaWidgetTools/ElaRoller.cpp | 4 +++ ElaWidgetTools/ElaSpinBox.cpp | 8 +++++ ElaWidgetTools/ElaSpotlight.cpp | 7 ++-- ElaWidgetTools/ElaToggleButton.cpp | 8 +++++ ElaWidgetTools/ElaToolButton.cpp | 8 +++++ ElaWidgetTools/private/ElaAppBarPrivate.cpp | 5 ++- ElaWidgetTools/private/ElaCalendarPrivate.cpp | 10 ++++-- .../private/ElaCountdownPrivate.cpp | 2 +- .../private/ElaMessageBarPrivate.cpp | 10 ++++-- .../private/ElaMultiSelectComboBoxPrivate.cpp | 5 ++- .../private/ElaPromotionCardPrivate.cpp | 5 ++- .../private/ElaScrollBarPrivate.cpp | 5 ++- .../private/ElaSpotlightPrivate.cpp | 5 ++- .../private/ElaToggleSwitchPrivate.cpp | 4 +-- .../private/ElaTreeSelectPrivate.cpp | 10 ++++-- 48 files changed, 292 insertions(+), 42 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp b/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp index d7ce2a6..3a30785 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp @@ -109,6 +109,10 @@ void ElaCentralStackedWidget::doWindowStackSwitch(ElaWindowType::StackSwitchMode targetWidget->setVisible(false); QPropertyAnimation* popupAnimation = new QPropertyAnimation(this, "pPopupAnimationYOffset"); connect(popupAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(popupAnimation, &QPropertyAnimation::finished, this, [=]() { @@ -135,12 +139,20 @@ void ElaCentralStackedWidget::doWindowStackSwitch(ElaWindowType::StackSwitchMode _isDrawNewPix = false; QPropertyAnimation* currentPixZoomAnimation = new QPropertyAnimation(this, "pScaleAnimationRatio"); connect(currentPixZoomAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(currentPixZoomAnimation, &QPropertyAnimation::finished, this, [=]() { _isDrawNewPix = true; QPropertyAnimation* targetPixZoomAnimation = new QPropertyAnimation(this, "pScaleAnimationRatio"); connect(targetPixZoomAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(targetPixZoomAnimation, &QPropertyAnimation::finished, this, [=]() { @@ -201,6 +213,10 @@ void ElaCentralStackedWidget::doWindowStackSwitch(ElaWindowType::StackSwitchMode targetWidget->setVisible(false); QPropertyAnimation* flipAnimation = new QPropertyAnimation(this, "pFlipAnimationRatio"); connect(flipAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(flipAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp index cb5007d..d46570c 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp @@ -146,7 +146,7 @@ void ElaCheckBoxStyle::_startRatioAnimation(qreal* targetRatio, qreal endRatio, QPointer widgetGuard = const_cast(widget); connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { *targetRatio = value.toReal(); - if (widgetGuard) + if (widgetGuard && widgetGuard->isVisible()) { widgetGuard->update(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp index 440ff97..fae1f24 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp @@ -1,6 +1,7 @@ #include "ElaColorValueSliderStyle.h" #include +#include #include #include @@ -149,9 +150,13 @@ void ElaColorValueSliderStyle::_startRadiusAnimation(qreal startRadius, qreal en { ElaColorValueSliderStyle* style = const_cast(this); QPropertyAnimation* circleRadiusAnimation = new QPropertyAnimation(style, "circleRadius"); + QPointer widgetGuard = widget; connect(circleRadiusAnimation, &QPropertyAnimation::valueChanged, style, [=](const QVariant& value) { this->_circleRadius = value.toReal(); - widget->update(); }); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } }); circleRadiusAnimation->setEasingCurve(QEasingCurve::InOutSine); circleRadiusAnimation->setStartValue(startRadius); circleRadiusAnimation->setEndValue(endRadius); diff --git a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp index 50fe1bc..2bd2fbe 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp @@ -280,7 +280,7 @@ void ElaComboBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widge QPointer widgetGuard = const_cast(widget); connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { this->_hoverRatio = value.toReal(); - if (widgetGuard) + if (widgetGuard && widgetGuard->isVisible()) { widgetGuard->update(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaDrawerContainer.cpp b/ElaWidgetTools/DeveloperComponents/ElaDrawerContainer.cpp index ffa7b5b..de227ac 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaDrawerContainer.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaDrawerContainer.cpp @@ -73,6 +73,10 @@ void ElaDrawerContainer::doDrawerAnimation(bool isExpand) _pContainerPix = grab(rect()); QPropertyAnimation* opacityAnimation = new QPropertyAnimation(this, "pOpacity"); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(opacityAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/DeveloperComponents/ElaDrawerHeader.cpp b/ElaWidgetTools/DeveloperComponents/ElaDrawerHeader.cpp index 3b2cbe3..50ad3c7 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaDrawerHeader.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaDrawerHeader.cpp @@ -48,6 +48,10 @@ void ElaDrawerHeader::doExpandOrCollapseAnimation() { QPropertyAnimation* rotateAnimation = new QPropertyAnimation(this, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); diff --git a/ElaWidgetTools/DeveloperComponents/ElaFooterDelegate.cpp b/ElaWidgetTools/DeveloperComponents/ElaFooterDelegate.cpp index 4150514..1316a86 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaFooterDelegate.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaFooterDelegate.cpp @@ -24,7 +24,10 @@ ElaFooterDelegate::ElaFooterDelegate(QObject* parent) _lastSelectMarkTopAnimation = new QPropertyAnimation(this, "lastSelectMarkTop"); connect(_lastSelectMarkTopAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { _lastSelectMarkTop = value.toReal(); - _pElaListView->viewport()->update(); + if (_pElaListView && _pElaListView->isVisible()) + { + _pElaListView->viewport()->update(); + } }); _lastSelectMarkTopAnimation->setDuration(300); _lastSelectMarkTopAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -32,7 +35,10 @@ ElaFooterDelegate::ElaFooterDelegate(QObject* parent) _selectMarkBottomAnimation = new QPropertyAnimation(this, "selectMarkBottom"); connect(_selectMarkBottomAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { _selectMarkBottom = value.toReal(); - _pElaListView->viewport()->update(); + if (_pElaListView && _pElaListView->isVisible()) + { + _pElaListView->viewport()->update(); + } }); _selectMarkBottomAnimation->setDuration(300); _selectMarkBottomAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -48,7 +54,10 @@ ElaFooterDelegate::ElaFooterDelegate(QObject* parent) _lastSelectMarkBottomAnimation = new QPropertyAnimation(this, "lastSelectMarkBottom"); connect(_lastSelectMarkBottomAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { _lastSelectMarkBottom = value.toReal(); - _pElaListView->viewport()->update(); + if (_pElaListView && _pElaListView->isVisible()) + { + _pElaListView->viewport()->update(); + } }); _lastSelectMarkBottomAnimation->setDuration(300); _lastSelectMarkBottomAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -56,7 +65,10 @@ ElaFooterDelegate::ElaFooterDelegate(QObject* parent) _selectMarkTopAnimation = new QPropertyAnimation(this, "selectMarkTop"); connect(_selectMarkTopAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { _selectMarkTop = value.toReal(); - _pElaListView->viewport()->update(); + if (_pElaListView && _pElaListView->isVisible()) + { + _pElaListView->viewport()->update(); + } }); _selectMarkTopAnimation->setDuration(300); _selectMarkTopAnimation->setEasingCurve(QEasingCurve::InOutSine); diff --git a/ElaWidgetTools/DeveloperComponents/ElaMaskWidget.cpp b/ElaWidgetTools/DeveloperComponents/ElaMaskWidget.cpp index 81f62be..abfdcb9 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaMaskWidget.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaMaskWidget.cpp @@ -18,6 +18,10 @@ void ElaMaskWidget::doMaskAnimation(int endValue) { QPropertyAnimation* opacityAnimation = new QPropertyAnimation(this, "pMaskAlpha"); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(opacityAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/DeveloperComponents/ElaNavigationStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaNavigationStyle.cpp index 22919ff..1dfe952 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaNavigationStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaNavigationStyle.cpp @@ -21,14 +21,20 @@ ElaNavigationStyle::ElaNavigationStyle(QStyle* style) // Mark向上 _lastSelectMarkTopAnimation = new QPropertyAnimation(this, "pLastSelectMarkTop"); connect(_lastSelectMarkTopAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); _lastSelectMarkTopAnimation->setDuration(300); _lastSelectMarkTopAnimation->setEasingCurve(QEasingCurve::InOutSine); _selectMarkBottomAnimation = new QPropertyAnimation(this, "pSelectMarkBottom"); connect(_selectMarkBottomAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); _selectMarkBottomAnimation->setDuration(300); _selectMarkBottomAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -43,14 +49,20 @@ ElaNavigationStyle::ElaNavigationStyle(QStyle* style) // Mark向下 _lastSelectMarkBottomAnimation = new QPropertyAnimation(this, "pLastSelectMarkBottom"); connect(_lastSelectMarkBottomAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); _lastSelectMarkBottomAnimation->setDuration(300); _lastSelectMarkBottomAnimation->setEasingCurve(QEasingCurve::InOutSine); _selectMarkTopAnimation = new QPropertyAnimation(this, "pSelectMarkTop"); connect(_selectMarkTopAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); _selectMarkTopAnimation->setDuration(300); _selectMarkTopAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -353,7 +365,10 @@ void ElaNavigationStyle::navigationNodeStateChange(QVariantMap data) _opacityAnimationTargetNode = nullptr; }); connect(nodeOpacityAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); nodeOpacityAnimation->setDuration(480); nodeOpacityAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -367,7 +382,10 @@ void ElaNavigationStyle::navigationNodeStateChange(QVariantMap data) _expandAnimationTargetNode = nullptr; }); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); rotateAnimation->setDuration(300); rotateAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -395,7 +413,10 @@ void ElaNavigationStyle::navigationNodeStateChange(QVariantMap data) _expandAnimationTargetNode = nullptr; }); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - _pNavigationView->viewport()->update(); + if (_pNavigationView && _pNavigationView->isVisible()) + { + _pNavigationView->viewport()->update(); + } }); rotateAnimation->setDuration(300); rotateAnimation->setEasingCurve(QEasingCurve::InOutSine); diff --git a/ElaWidgetTools/DeveloperComponents/ElaPivotView.cpp b/ElaWidgetTools/DeveloperComponents/ElaPivotView.cpp index 86fffe7..f342a45 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaPivotView.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaPivotView.cpp @@ -36,6 +36,10 @@ void ElaPivotView::doCurrentIndexChangedAnimation(const QModelIndex& index) _pCurrentIndexRect = visualRect(index); QPropertyAnimation* markAnimation = new QPropertyAnimation(this, "pMarkX"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(markAnimation, &QPropertyAnimation::finished, this, [=]() { @@ -68,6 +72,10 @@ void ElaPivotView::doCurrentIndexChangedAnimation(const QModelIndex& index) _pCurrentIndexRect = visualRect(model()->index(_pPivotStyle->getCurrentIndex(), 0)); QPropertyAnimation* markAnimation = new QPropertyAnimation(this, "pMarkX"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(markAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/DeveloperComponents/ElaPopularCardFloater.cpp b/ElaWidgetTools/DeveloperComponents/ElaPopularCardFloater.cpp index ebebd76..a5e8fba 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaPopularCardFloater.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaPopularCardFloater.cpp @@ -56,6 +56,10 @@ void ElaPopularCardFloater::showFloater() QPropertyAnimation* geometryAnimation = new QPropertyAnimation(this, "geometry"); connect(geometryAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); geometryAnimation->setEasingCurve(QEasingCurve::OutQuad); @@ -110,6 +114,10 @@ void ElaPopularCardFloater::hideFloater() //内容调整动画 QPropertyAnimation* hoverAnimation = new QPropertyAnimation(this, "pHoverYOffset"); connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); hoverAnimation->setDuration(300); diff --git a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp index 4bcfc3b..c439aba 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp @@ -129,7 +129,7 @@ void ElaRadioButtonStyle::_startRatioAnimation(qreal* targetRatio, qreal endRati QPointer widgetGuard = const_cast(widget); connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { *targetRatio = value.toReal(); - if (widgetGuard) + if (widgetGuard && widgetGuard->isVisible()) { widgetGuard->update(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaRollerPickerContainer.cpp b/ElaWidgetTools/DeveloperComponents/ElaRollerPickerContainer.cpp index 7ef7b7d..7e86bc5 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRollerPickerContainer.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaRollerPickerContainer.cpp @@ -44,6 +44,10 @@ void ElaRollerPickerContainer::doPickerAnimation() update(); }); connect(offsetAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); offsetAnimation->setEasingCurve(QEasingCurve::OutCubic); diff --git a/ElaWidgetTools/DeveloperComponents/ElaScrollBarStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaScrollBarStyle.cpp index 4ac8a8d..40516b1 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaScrollBarStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaScrollBarStyle.cpp @@ -152,7 +152,10 @@ void ElaScrollBarStyle::startExpandAnimation(bool isExpand) _pIsExpand = true; QPropertyAnimation* opacityAnimation = new QPropertyAnimation(this, "pOpacity"); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=]() { - _pScrollBar->update(); + if (_pScrollBar && _pScrollBar->isVisible()) + { + _pScrollBar->update(); + } }); opacityAnimation->setDuration(250); opacityAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -174,7 +177,10 @@ void ElaScrollBarStyle::startExpandAnimation(bool isExpand) _pIsExpand = false; }); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=]() { - _pScrollBar->update(); + if (_pScrollBar && _pScrollBar->isVisible()) + { + _pScrollBar->update(); + } }); opacityAnimation->setDuration(250); opacityAnimation->setEasingCurve(QEasingCurve::InOutSine); diff --git a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp index 4008ed0..0383be5 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp @@ -1,6 +1,7 @@ #include "ElaSliderStyle.h" #include +#include #include #include @@ -154,9 +155,13 @@ void ElaSliderStyle::_startRadiusAnimation(qreal startRadius, qreal endRadius, Q { ElaSliderStyle* style = const_cast(this); QPropertyAnimation* circleRadiusAnimation = new QPropertyAnimation(style, "circleRadius"); + QPointer widgetGuard = widget; connect(circleRadiusAnimation, &QPropertyAnimation::valueChanged, style, [=](const QVariant& value) { this->_circleRadius = value.toReal(); - widget->update(); }); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } }); circleRadiusAnimation->setEasingCurve(QEasingCurve::InOutSine); circleRadiusAnimation->setStartValue(startRadius); circleRadiusAnimation->setEndValue(endRadius); diff --git a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp index 3272cda..c7d5a44 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp @@ -224,7 +224,7 @@ void ElaSpinBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widget QPointer widgetGuard = const_cast(widget); connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { this->_hoverRatio = value.toReal(); - if (widgetGuard) + if (widgetGuard && widgetGuard->isVisible()) { widgetGuard->update(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaThemeAnimationWidget.cpp b/ElaWidgetTools/DeveloperComponents/ElaThemeAnimationWidget.cpp index 9d100b6..77bfaf5 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaThemeAnimationWidget.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaThemeAnimationWidget.cpp @@ -23,6 +23,10 @@ void ElaThemeAnimationWidget::startAnimation(int msec) this->deleteLater(); }); connect(themeChangeAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); themeChangeAnimation->setStartValue(0); diff --git a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp index d69aa21..9119de3 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp @@ -379,7 +379,7 @@ void ElaToolButtonStyle::_startHoverAnimation(qreal endRatio, const QWidget *wid QPointer widgetGuard = const_cast(widget); connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { this->_hoverRatio = value.toReal(); - if (widgetGuard) + if (widgetGuard && widgetGuard->isVisible()) { widgetGuard->update(); } diff --git a/ElaWidgetTools/ElaComboBox.cpp b/ElaWidgetTools/ElaComboBox.cpp index 05151b1..f588afd 100644 --- a/ElaWidgetTools/ElaComboBox.cpp +++ b/ElaWidgetTools/ElaComboBox.cpp @@ -185,6 +185,10 @@ void ElaComboBox::showPopup() QPropertyAnimation *rotateAnimation = new QPropertyAnimation(d->_comboBoxStyle, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); @@ -211,6 +215,10 @@ void ElaComboBox::_resetIndicatorAnimations() QPropertyAnimation *rotateAnimation = new QPropertyAnimation(d->_comboBoxStyle, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaCopyButton.cpp b/ElaWidgetTools/ElaCopyButton.cpp index d943edd..3876a57 100644 --- a/ElaWidgetTools/ElaCopyButton.cpp +++ b/ElaWidgetTools/ElaCopyButton.cpp @@ -40,7 +40,10 @@ ElaCopyButton::ElaCopyButton(QWidget *parent) : QPushButton(parent), d_ptr(new E connect(fadeAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { d->_iconOpacity = value.toReal(); - update(); + if (isVisible()) + { + update(); + } }); fadeAnimation->setStartValue(0.0); fadeAnimation->setEndValue(1.0); @@ -122,7 +125,10 @@ void ElaCopyButton::mouseReleaseEvent(QMouseEvent *event) connect(fadeAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { d->_iconOpacity = value.toReal(); - update(); + if (isVisible()) + { + update(); + } }); fadeAnimation->setStartValue(0.0); fadeAnimation->setEndValue(1.0); diff --git a/ElaWidgetTools/ElaDashboardGauge.cpp b/ElaWidgetTools/ElaDashboardGauge.cpp index 88d8c6a..ae0002e 100644 --- a/ElaWidgetTools/ElaDashboardGauge.cpp +++ b/ElaWidgetTools/ElaDashboardGauge.cpp @@ -62,7 +62,7 @@ void ElaDashboardGauge::setValue(qreal value) animation->setStartValue(d->_animatedValue); animation->setEndValue(value); animation->setEasingCurve(QEasingCurve::OutCubic); - connect(animation, &QPropertyAnimation::valueChanged, this, [this]() { update(); }); + connect(animation, &QPropertyAnimation::valueChanged, this, [this]() { if (isVisible()) { update(); } }); animation->start(QAbstractAnimation::DeleteWhenStopped); } else diff --git a/ElaWidgetTools/ElaDoubleSpinBox.cpp b/ElaWidgetTools/ElaDoubleSpinBox.cpp index 0e35843..22a7dad 100755 --- a/ElaWidgetTools/ElaDoubleSpinBox.cpp +++ b/ElaWidgetTools/ElaDoubleSpinBox.cpp @@ -91,6 +91,10 @@ void ElaDoubleSpinBox::focusInEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); @@ -109,6 +113,10 @@ void ElaDoubleSpinBox::focusOutEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaFloatButton.cpp b/ElaWidgetTools/ElaFloatButton.cpp index 6f7a239..0d136af 100644 --- a/ElaWidgetTools/ElaFloatButton.cpp +++ b/ElaWidgetTools/ElaFloatButton.cpp @@ -208,7 +208,7 @@ bool ElaFloatButton::event(QEvent *event) { d->_isAlphaAnimationFinished = false; QPropertyAnimation *alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { update(); }); + connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { if (isVisible()) { update(); } }); connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { d->_isAlphaAnimationFinished = true; }); alphaAnimation->setDuration(200); alphaAnimation->setStartValue(d->_pHoverAlpha); @@ -220,7 +220,7 @@ bool ElaFloatButton::event(QEvent *event) { d->_isAlphaAnimationFinished = false; QPropertyAnimation *alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { update(); }); + connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { if (isVisible()) { update(); } }); connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { d->_isAlphaAnimationFinished = true; }); alphaAnimation->setDuration(200); alphaAnimation->setStartValue(d->_pHoverAlpha); diff --git a/ElaWidgetTools/ElaIconButton.cpp b/ElaWidgetTools/ElaIconButton.cpp index 65245f3..652649c 100755 --- a/ElaWidgetTools/ElaIconButton.cpp +++ b/ElaWidgetTools/ElaIconButton.cpp @@ -164,6 +164,10 @@ bool ElaIconButton::event(QEvent* event) d->_isAlphaAnimationFinished = false; QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { @@ -183,6 +187,10 @@ bool ElaIconButton::event(QEvent* event) d->_isAlphaAnimationFinished = false; QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/ElaLineEdit.cpp b/ElaWidgetTools/ElaLineEdit.cpp index bd3fbf3..a0685b9 100755 --- a/ElaWidgetTools/ElaLineEdit.cpp +++ b/ElaWidgetTools/ElaLineEdit.cpp @@ -71,6 +71,10 @@ void ElaLineEdit::focusInEvent(QFocusEvent* event) } QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); @@ -94,6 +98,10 @@ void ElaLineEdit::focusOutEvent(QFocusEvent* event) } QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaMenu.cpp b/ElaWidgetTools/ElaMenu.cpp index 82efdcc..c332535 100755 --- a/ElaWidgetTools/ElaMenu.cpp +++ b/ElaWidgetTools/ElaMenu.cpp @@ -152,6 +152,10 @@ void ElaMenu::showEvent(QShowEvent* event) update(); }); connect(posAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); posAnimation->setEasingCurve(QEasingCurve::OutCubic); diff --git a/ElaWidgetTools/ElaMultiSelectComboBox.cpp b/ElaWidgetTools/ElaMultiSelectComboBox.cpp index 5581c32..12bac62 100755 --- a/ElaWidgetTools/ElaMultiSelectComboBox.cpp +++ b/ElaWidgetTools/ElaMultiSelectComboBox.cpp @@ -305,6 +305,10 @@ void ElaMultiSelectComboBox::showPopup() QPropertyAnimation *rotateAnimation = new QPropertyAnimation(d, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); @@ -333,6 +337,10 @@ void ElaMultiSelectComboBox::_resetIndicatorAnimations() QPropertyAnimation *rotateAnimation = new QPropertyAnimation(d, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaPasswordBox.cpp b/ElaWidgetTools/ElaPasswordBox.cpp index 201494e..2d16674 100644 --- a/ElaWidgetTools/ElaPasswordBox.cpp +++ b/ElaWidgetTools/ElaPasswordBox.cpp @@ -90,6 +90,10 @@ void ElaPasswordBox::focusInEvent(QFocusEvent *event) QPropertyAnimation *markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); @@ -110,6 +114,10 @@ void ElaPasswordBox::focusOutEvent(QFocusEvent *event) QPropertyAnimation *markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaPlainTextEdit.cpp b/ElaWidgetTools/ElaPlainTextEdit.cpp index 563fbb1..3c4decd 100755 --- a/ElaWidgetTools/ElaPlainTextEdit.cpp +++ b/ElaWidgetTools/ElaPlainTextEdit.cpp @@ -51,6 +51,10 @@ void ElaPlainTextEdit::focusInEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d->_style, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); @@ -69,6 +73,10 @@ void ElaPlainTextEdit::focusOutEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d->_style, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaPopularCard.cpp b/ElaWidgetTools/ElaPopularCard.cpp index 31cbaa4..688d226 100755 --- a/ElaWidgetTools/ElaPopularCard.cpp +++ b/ElaWidgetTools/ElaPopularCard.cpp @@ -91,6 +91,10 @@ bool ElaPopularCard::event(QEvent* event) d->_floatTimer->start(450); QPropertyAnimation* hoverAnimation = new QPropertyAnimation(d, "pHoverYOffset"); connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); hoverAnimation->setDuration(130); @@ -109,6 +113,10 @@ bool ElaPopularCard::event(QEvent* event) d->_floatTimer->stop(); QPropertyAnimation* hoverAnimation = new QPropertyAnimation(d, "pHoverYOffset"); connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); hoverAnimation->setDuration(130); diff --git a/ElaWidgetTools/ElaProgressBar.cpp b/ElaWidgetTools/ElaProgressBar.cpp index 31c9e94..b1ffaf1 100755 --- a/ElaWidgetTools/ElaProgressBar.cpp +++ b/ElaWidgetTools/ElaProgressBar.cpp @@ -17,7 +17,10 @@ ElaProgressBar::ElaProgressBar(QWidget* parent) d->_busyAnimation = new QPropertyAnimation(d->_style, "busyStartValue"); connect(d->_busyAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { d->_style->setProperty("busyEndValue", value.toInt() + 75); - update(); + if (isVisible()) + { + update(); + } }); d->_busyAnimation->setDuration(2000); d->_busyAnimation->setLoopCount(-1); diff --git a/ElaWidgetTools/ElaPromotionCard.cpp b/ElaWidgetTools/ElaPromotionCard.cpp index ea7d07c..935a634 100755 --- a/ElaWidgetTools/ElaPromotionCard.cpp +++ b/ElaWidgetTools/ElaPromotionCard.cpp @@ -110,6 +110,10 @@ bool ElaPromotionCard::event(QEvent* event) QMouseEvent* mouseEvent = dynamic_cast(event); QPropertyAnimation* opacityAnimation = new QPropertyAnimation(d, "pPressOpacity"); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(opacityAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/ElaPushButton.cpp b/ElaWidgetTools/ElaPushButton.cpp index 8c8a579..adab5a5 100755 --- a/ElaWidgetTools/ElaPushButton.cpp +++ b/ElaWidgetTools/ElaPushButton.cpp @@ -119,6 +119,10 @@ bool ElaPushButton::event(QEvent* event) { QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); alphaAnimation->setDuration(175); diff --git a/ElaWidgetTools/ElaRoller.cpp b/ElaWidgetTools/ElaRoller.cpp index b79c61c..c26a1f6 100755 --- a/ElaWidgetTools/ElaRoller.cpp +++ b/ElaWidgetTools/ElaRoller.cpp @@ -34,6 +34,10 @@ ElaRoller::ElaRoller(QWidget* parent) d->_scrollAnimation = new QPropertyAnimation(d, "pScrollOffset"); connect(d->_scrollAnimation, &QPropertyAnimation::valueChanged, this, [=]() { + if (!isVisible()) + { + return; + } update(); }); connect(d->_scrollAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/ElaSpinBox.cpp b/ElaWidgetTools/ElaSpinBox.cpp index 9517236..874b43b 100755 --- a/ElaWidgetTools/ElaSpinBox.cpp +++ b/ElaWidgetTools/ElaSpinBox.cpp @@ -91,6 +91,10 @@ void ElaSpinBox::focusInEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); @@ -109,6 +113,10 @@ void ElaSpinBox::focusOutEvent(QFocusEvent* event) { QPropertyAnimation* markAnimation = new QPropertyAnimation(d, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); markAnimation->setDuration(300); diff --git a/ElaWidgetTools/ElaSpotlight.cpp b/ElaWidgetTools/ElaSpotlight.cpp index 63140db..84e209a 100644 --- a/ElaWidgetTools/ElaSpotlight.cpp +++ b/ElaWidgetTools/ElaSpotlight.cpp @@ -140,7 +140,7 @@ void ElaSpotlight::start() d->_pOpacity = 0.0; QPropertyAnimation *fadeIn = new QPropertyAnimation(d, "pOpacity"); - connect(fadeIn, &QPropertyAnimation::valueChanged, this, [=]() { update(); }); + connect(fadeIn, &QPropertyAnimation::valueChanged, this, [=]() { if (isVisible()) { update(); } }); fadeIn->setDuration(300); fadeIn->setStartValue(0.0); fadeIn->setEndValue(1.0); @@ -178,7 +178,10 @@ void ElaSpotlight::finish() connect(fadeOut, &QPropertyAnimation::valueChanged, this, [=]() { d->_tipWidget->setWindowOpacity(d->_pOpacity); - update(); + if (isVisible()) + { + update(); + } }); fadeOut->setDuration(250); fadeOut->setStartValue(d->_pOpacity); diff --git a/ElaWidgetTools/ElaToggleButton.cpp b/ElaWidgetTools/ElaToggleButton.cpp index ba88c92..8bc128c 100755 --- a/ElaWidgetTools/ElaToggleButton.cpp +++ b/ElaWidgetTools/ElaToggleButton.cpp @@ -69,6 +69,10 @@ bool ElaToggleButton::event(QEvent* event) { QPropertyAnimation* hoverAnimation = new QPropertyAnimation(d, "pHoverAlpha"); connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); hoverAnimation->setDuration(175); @@ -103,6 +107,10 @@ void ElaToggleButton::mouseReleaseEvent(QMouseEvent* event) d->_isToggled = !d->_isToggled; QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pToggleAlpha"); connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { diff --git a/ElaWidgetTools/ElaToolButton.cpp b/ElaWidgetTools/ElaToolButton.cpp index 88c8f10..d036fe4 100755 --- a/ElaWidgetTools/ElaToolButton.cpp +++ b/ElaWidgetTools/ElaToolButton.cpp @@ -104,6 +104,10 @@ bool ElaToolButton::eventFilter(QObject* watched, QEvent* event) //指示器动画 QPropertyAnimation* rotateAnimation = new QPropertyAnimation(d->_toolButtonStyle, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); @@ -118,6 +122,10 @@ bool ElaToolButton::eventFilter(QObject* watched, QEvent* event) //指示器动画 QPropertyAnimation* rotateAnimation = new QPropertyAnimation(d->_toolButtonStyle, "pExpandIconRotate"); connect(rotateAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { + if (!isVisible()) + { + return; + } update(); }); rotateAnimation->setDuration(300); diff --git a/ElaWidgetTools/private/ElaAppBarPrivate.cpp b/ElaWidgetTools/private/ElaAppBarPrivate.cpp index 5e585a5..1bf6e8d 100755 --- a/ElaWidgetTools/private/ElaAppBarPrivate.cpp +++ b/ElaWidgetTools/private/ElaAppBarPrivate.cpp @@ -98,7 +98,10 @@ void ElaAppBarPrivate::onStayTopButtonClicked() rotateAnimation->setEndValue(targetRotate); connect(rotateAnimation, &QPropertyAnimation::valueChanged, _stayTopButton, [=]() { - _stayTopButton->update(); + if (_stayTopButton->isVisible()) + { + _stayTopButton->update(); + } }); rotateAnimation->start(QAbstractAnimation::DeleteWhenStopped); } diff --git a/ElaWidgetTools/private/ElaCalendarPrivate.cpp b/ElaWidgetTools/private/ElaCalendarPrivate.cpp index bfe74bc..da4dda4 100755 --- a/ElaWidgetTools/private/ElaCalendarPrivate.cpp +++ b/ElaWidgetTools/private/ElaCalendarPrivate.cpp @@ -149,13 +149,19 @@ void ElaCalendarPrivate::_doSwitchAnimation(bool isZoomIn) _calendarDelegate->setIsTransparent(true); QPropertyAnimation* oldPixZoomAnimation = new QPropertyAnimation(this, "pZoomRatio"); connect(oldPixZoomAnimation, &QPropertyAnimation::valueChanged, this, [=]() { - q->update(); + if (q->isVisible()) + { + q->update(); + } }); connect(oldPixZoomAnimation, &QPropertyAnimation::finished, this, [=]() { _isDrawNewPix = true; QPropertyAnimation* newPixZoomAnimation = new QPropertyAnimation(this, "pZoomRatio"); connect(newPixZoomAnimation, &QPropertyAnimation::valueChanged, this, [=]() { - q->update(); + if (q->isVisible()) + { + q->update(); + } }); connect(newPixZoomAnimation, &QPropertyAnimation::finished, this, [=]() { if (_calendarModel->getDisplayMode() == ElaCalendarType::DayMode) diff --git a/ElaWidgetTools/private/ElaCountdownPrivate.cpp b/ElaWidgetTools/private/ElaCountdownPrivate.cpp index b3fa11d..108251f 100644 --- a/ElaWidgetTools/private/ElaCountdownPrivate.cpp +++ b/ElaWidgetTools/private/ElaCountdownPrivate.cpp @@ -54,7 +54,7 @@ void ElaCountdownPrivate::_startFlipAnimation() Q_Q(ElaCountdown); _pFlipAngle = 0; QPropertyAnimation* flipAnim = new QPropertyAnimation(this, "pFlipAngle"); - connect(flipAnim, &QPropertyAnimation::valueChanged, q, [=]() { q->update(); }); + connect(flipAnim, &QPropertyAnimation::valueChanged, q, [=]() { if (q->isVisible()) { q->update(); } }); flipAnim->setDuration(300); flipAnim->setStartValue(0.0); flipAnim->setEndValue(180.0); diff --git a/ElaWidgetTools/private/ElaMessageBarPrivate.cpp b/ElaWidgetTools/private/ElaMessageBarPrivate.cpp index fdfbf6c..ee57dec 100755 --- a/ElaWidgetTools/private/ElaMessageBarPrivate.cpp +++ b/ElaWidgetTools/private/ElaMessageBarPrivate.cpp @@ -54,7 +54,10 @@ void ElaMessageBarPrivate::messageBarEnd() QPropertyAnimation* barFinishedOpacityAnimation = new QPropertyAnimation(this, "pOpacity"); connect(barFinishedOpacityAnimation, &QPropertyAnimation::valueChanged, this, [=]() { _closeButton->setOpacity(_pOpacity); - q->update(); + if (q->isVisible()) + { + q->update(); + } }); connect(barFinishedOpacityAnimation, &QPropertyAnimation::finished, this, [=]() { q->deleteLater(); @@ -125,7 +128,10 @@ void ElaMessageBarPrivate::_messageBarCreate(int displayMsec) }); QPropertyAnimation* timePercentAnimation = new QPropertyAnimation(this, "pTimePercent"); connect(timePercentAnimation, &QPropertyAnimation::valueChanged, this, [=]() { - q->update(); + if (q->isVisible()) + { + q->update(); + } }); timePercentAnimation->setStartValue(100); timePercentAnimation->setEndValue(0); diff --git a/ElaWidgetTools/private/ElaMultiSelectComboBoxPrivate.cpp b/ElaWidgetTools/private/ElaMultiSelectComboBoxPrivate.cpp index e7fe9d4..951ba5b 100755 --- a/ElaWidgetTools/private/ElaMultiSelectComboBoxPrivate.cpp +++ b/ElaWidgetTools/private/ElaMultiSelectComboBoxPrivate.cpp @@ -42,7 +42,10 @@ void ElaMultiSelectComboBoxPrivate::onItemPressed(const QModelIndex& index) _refreshCurrentIndexs(); QPropertyAnimation* markAnimation = new QPropertyAnimation(this, "pExpandMarkWidth"); connect(markAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - q->update(); + if (q->isVisible()) + { + q->update(); + } }); markAnimation->setDuration(300); markAnimation->setEasingCurve(QEasingCurve::InOutSine); diff --git a/ElaWidgetTools/private/ElaPromotionCardPrivate.cpp b/ElaWidgetTools/private/ElaPromotionCardPrivate.cpp index bbc54f4..304e231 100755 --- a/ElaWidgetTools/private/ElaPromotionCardPrivate.cpp +++ b/ElaWidgetTools/private/ElaPromotionCardPrivate.cpp @@ -36,7 +36,10 @@ void ElaPromotionCardPrivate::_startHoverOpacityAnimation(bool isVisible) Q_Q(ElaPromotionCard); QPropertyAnimation* opacityAnimation = new QPropertyAnimation(this, "pHoverOpacity"); connect(opacityAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - q->update(); + if (q->isVisible()) + { + q->update(); + } }); opacityAnimation->setDuration(250); opacityAnimation->setStartValue(_pHoverOpacity); diff --git a/ElaWidgetTools/private/ElaScrollBarPrivate.cpp b/ElaWidgetTools/private/ElaScrollBarPrivate.cpp index abd77d4..c168c34 100755 --- a/ElaWidgetTools/private/ElaScrollBarPrivate.cpp +++ b/ElaWidgetTools/private/ElaScrollBarPrivate.cpp @@ -27,7 +27,10 @@ void ElaScrollBarPrivate::onRangeChanged(int min, int max) q->blockSignals(true); q->setMaximum(value.toUInt()); q->blockSignals(false); - q->update(); + if (q->isVisible()) + { + q->update(); + } }); rangeSmoothAnimation->setEasingCurve(QEasingCurve::OutSine); rangeSmoothAnimation->setDuration(250); diff --git a/ElaWidgetTools/private/ElaSpotlightPrivate.cpp b/ElaWidgetTools/private/ElaSpotlightPrivate.cpp index 1016c30..0a506d9 100644 --- a/ElaWidgetTools/private/ElaSpotlightPrivate.cpp +++ b/ElaWidgetTools/private/ElaSpotlightPrivate.cpp @@ -47,7 +47,10 @@ void ElaSpotlightPrivate::_showStep(int index, bool animate) { QPropertyAnimation* moveAnimation = new QPropertyAnimation(this, "pSpotlightRect"); connect(moveAnimation, &QPropertyAnimation::valueChanged, q, [=]() { - q->update(); + if (q->isVisible()) + { + q->update(); + } _updateTipPosition(); }); moveAnimation->setDuration(350); diff --git a/ElaWidgetTools/private/ElaToggleSwitchPrivate.cpp b/ElaWidgetTools/private/ElaToggleSwitchPrivate.cpp index 6c2cc7b..6be9620 100755 --- a/ElaWidgetTools/private/ElaToggleSwitchPrivate.cpp +++ b/ElaWidgetTools/private/ElaToggleSwitchPrivate.cpp @@ -18,7 +18,7 @@ void ElaToggleSwitchPrivate::_startPosAnimation(qreal startX, qreal endX, bool i QPropertyAnimation* circleAnimation = new QPropertyAnimation(q, "circleCenterX"); connect(circleAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant& value) { this->_circleCenterX = value.toReal(); - q->update(); }); + if (q->isVisible()) { q->update(); } }); circleAnimation->setStartValue(startX); circleAnimation->setEndValue(endX); circleAnimation->setEasingCurve(QEasingCurve::InOutSine); @@ -33,7 +33,7 @@ void ElaToggleSwitchPrivate::_startRadiusAnimation(qreal startRadius, qreal endR QPropertyAnimation* circleRadiusAnimation = new QPropertyAnimation(q, "circleRadius"); connect(circleRadiusAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant& value) { this->_circleRadius = value.toReal(); - q->update(); }); + if (q->isVisible()) { q->update(); } }); circleRadiusAnimation->setEasingCurve(QEasingCurve::InOutSine); circleRadiusAnimation->setStartValue(startRadius); circleRadiusAnimation->setEndValue(endRadius); diff --git a/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp b/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp index 981c966..ca81ddf 100644 --- a/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp +++ b/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp @@ -176,7 +176,10 @@ void ElaTreeSelectPrivate::_showPopup() connect(rotateAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant &value) { _expandIconRotate = value.toReal(); - q->update(); + if (q->isVisible()) + { + q->update(); + } }); rotateAnimation->setStartValue(_expandIconRotate); rotateAnimation->setEndValue(-180.0); @@ -222,7 +225,10 @@ void ElaTreeSelectPrivate::_hidePopup() connect(rotateAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant &value) { _expandIconRotate = value.toReal(); - q->update(); + if (q->isVisible()) + { + q->update(); + } }); rotateAnimation->setStartValue(_expandIconRotate); rotateAnimation->setEndValue(0.0); From b61e7e8f18d8c49f334b40146b9aedf3ac7edfff Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 11:12:53 +0800 Subject: [PATCH 09/17] perf(anim): reuse animation objects across state transitions Buttons (IconButton/FloatButton/ToggleButton) and the style animation helpers now keep one long-lived animation object per purpose and only reset start/end values on re-entry, instead of allocating a new QPropertyAnimation/QVariantAnimation and re-connecting on every Enter/Leave/check/toggle. DeleteWhenStopped removed on the cached objects; in/out dual purposes use separate cached slots. Also covers the row/tab/menu hover helpers whose callbacks previously had no visibility guard: the cached connections now include the isVisible() check. ElaWindowStyle per-widget keyed animations intentionally left as is. No visual change: durations, easing and end states unchanged. --- .../DeveloperComponents/ElaCheckBoxStyle.cpp | 25 ++++++----- .../DeveloperComponents/ElaCheckBoxStyle.h | 3 ++ .../DeveloperComponents/ElaComboBoxStyle.cpp | 32 +++++++------ .../DeveloperComponents/ElaComboBoxStyle.h | 2 + .../DeveloperComponents/ElaListViewStyle.cpp | 39 +++++++++------- .../DeveloperComponents/ElaListViewStyle.h | 3 ++ .../DeveloperComponents/ElaMenuStyle.cpp | 39 +++++++++------- .../DeveloperComponents/ElaMenuStyle.h | 3 ++ .../ElaRadioButtonStyle.cpp | 25 ++++++----- .../DeveloperComponents/ElaRadioButtonStyle.h | 3 ++ .../DeveloperComponents/ElaSpinBoxStyle.cpp | 32 +++++++------ .../DeveloperComponents/ElaSpinBoxStyle.h | 2 + .../DeveloperComponents/ElaTabBarStyle.cpp | 39 +++++++++------- .../DeveloperComponents/ElaTabBarStyle.h | 3 ++ .../DeveloperComponents/ElaTableViewStyle.cpp | 39 +++++++++------- .../DeveloperComponents/ElaTableViewStyle.h | 3 ++ .../ElaToolButtonStyle.cpp | 32 +++++++------ .../DeveloperComponents/ElaToolButtonStyle.h | 2 + .../DeveloperComponents/ElaTreeViewStyle.cpp | 39 +++++++++------- .../DeveloperComponents/ElaTreeViewStyle.h | 3 ++ ElaWidgetTools/ElaFloatButton.cpp | 32 +++++++------ ElaWidgetTools/ElaIconButton.cpp | 32 +------------ ElaWidgetTools/ElaToggleButton.cpp | 36 +-------------- .../private/ElaFloatButtonPrivate.h | 4 ++ .../private/ElaIconButtonPrivate.cpp | 27 +++++++++++ ElaWidgetTools/private/ElaIconButtonPrivate.h | 4 ++ .../private/ElaToggleButtonPrivate.cpp | 45 +++++++++++++++++++ .../private/ElaToggleButtonPrivate.h | 6 +++ 28 files changed, 329 insertions(+), 225 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp index d46570c..999ee40 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.cpp @@ -142,18 +142,23 @@ int ElaCheckBoxStyle::pixelMetric(PixelMetric metric, const QStyleOption* option void ElaCheckBoxStyle::_startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const { - QVariantAnimation* ratioAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - *targetRatio = value.toReal(); - if (widgetGuard && widgetGuard->isVisible()) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& ratioAnimation = targetRatio == &_hoverRatio ? _hoverRatioAnimation : _checkRatioAnimation; + if (!ratioAnimation) + { + ratioAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + *targetRatio = value.toReal(); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + ratioAnimation->stop(); ratioAnimation->setDuration(duration); ratioAnimation->setEasingCurve(QEasingCurve(curve)); ratioAnimation->setStartValue(*targetRatio); ratioAnimation->setEndValue(endRatio); - ratioAnimation->start(QAbstractAnimation::DeleteWhenStopped); + ratioAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h index 11c2aea..2a2abda 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaCheckBoxStyle.h @@ -5,6 +5,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaCheckBoxStyle : public QProxyStyle { Q_OBJECT @@ -19,6 +20,8 @@ class ElaCheckBoxStyle : public QProxyStyle ElaThemeType::ThemeMode _themeMode; mutable qreal _hoverRatio{0}; mutable qreal _checkRatio{0}; + mutable QVariantAnimation* _hoverRatioAnimation{nullptr}; + mutable QVariantAnimation* _checkRatioAnimation{nullptr}; mutable bool _lastHovered{false}; mutable bool _lastChecked{false}; mutable bool _firstPaint{true}; diff --git a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp index 2bd2fbe..6cda7bc 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.cpp @@ -276,18 +276,22 @@ QSize ElaComboBoxStyle::sizeFromContents(ContentsType type, const QStyleOption* void ElaComboBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widget) const { - QVariantAnimation* hoverAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - this->_hoverRatio = value.toReal(); - if (widgetGuard && widgetGuard->isVisible()) - { - widgetGuard->update(); - } - }); - hoverAnimation->setDuration(150); - hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); - hoverAnimation->setStartValue(_hoverRatio); - hoverAnimation->setEndValue(endRatio); - hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); + if (!_hoverAnimation) + { + _hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(_hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + _hoverAnimation->stop(); + _hoverAnimation->setDuration(150); + _hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + _hoverAnimation->setStartValue(_hoverRatio); + _hoverAnimation->setEndValue(endRatio); + _hoverAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h index b8a8a27..eeb134a 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaComboBoxStyle.h @@ -4,6 +4,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaComboBoxStyle : public QProxyStyle { Q_OBJECT @@ -23,6 +24,7 @@ class ElaComboBoxStyle : public QProxyStyle ElaThemeType::ThemeMode _themeMode; int _shadowBorderWidth{6}; mutable qreal _hoverRatio{0}; + mutable QVariantAnimation* _hoverAnimation{nullptr}; mutable bool _lastHovered{false}; mutable bool _firstPaint{true}; void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp index 7f2072a..6c29d3e 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp @@ -220,25 +220,30 @@ QSize ElaListViewStyle::sizeFromContents(ContentsType type, const QStyleOption* void ElaListViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const { - QVariantAnimation* rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - if (isFadeIn) - { - this->_hoverInRatio = value.toReal(); - } - else - { - this->_hoverOutRatio = value.toReal(); - } - if (widgetGuard) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; + if (!rowAnimation) + { + rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + rowAnimation->stop(); rowAnimation->setDuration(150); rowAnimation->setEasingCurve(QEasingCurve::OutCubic); rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); - rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); + rowAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h index db097f7..c96b05c 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h @@ -5,6 +5,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaListViewStyle : public QProxyStyle { Q_OBJECT @@ -24,6 +25,8 @@ class ElaListViewStyle : public QProxyStyle mutable QPersistentModelIndex _fadeOutIndex; mutable qreal _hoverInRatio{0}; mutable qreal _hoverOutRatio{0}; + mutable QVariantAnimation* _hoverInAnimation{nullptr}; + mutable QVariantAnimation* _hoverOutAnimation{nullptr}; mutable bool _firstPaint{true}; void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; diff --git a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp index 348f6a7..a2bf509 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.cpp @@ -276,25 +276,30 @@ QSize ElaMenuStyle::sizeFromContents(ContentsType type, const QStyleOption* opti void ElaMenuStyle::_startItemHoverAnimation(bool isFadeIn, const QWidget* widget) const { - QVariantAnimation* itemAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(itemAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - if (isFadeIn) - { - this->_hoverInRatio = value.toReal(); - } - else - { - this->_hoverOutRatio = value.toReal(); - } - if (widgetGuard) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& itemAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; + if (!itemAnimation) + { + itemAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(itemAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + itemAnimation->stop(); itemAnimation->setDuration(150); itemAnimation->setEasingCurve(QEasingCurve::OutCubic); itemAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); itemAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); - itemAnimation->start(QAbstractAnimation::DeleteWhenStopped); + itemAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h index b589e90..e68b0b7 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaMenuStyle.h @@ -5,6 +5,7 @@ #include "ElaDef.h" +class QVariantAnimation; class ElaMenuStyle : public QProxyStyle { Q_OBJECT @@ -27,6 +28,8 @@ class ElaMenuStyle : public QProxyStyle mutable QRect _fadeRect; mutable qreal _hoverInRatio{0}; mutable qreal _hoverOutRatio{0}; + mutable QVariantAnimation* _hoverInAnimation{nullptr}; + mutable QVariantAnimation* _hoverOutAnimation{nullptr}; mutable bool _firstPaint{true}; void _startItemHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; diff --git a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp index c439aba..ebd9404 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.cpp @@ -125,18 +125,23 @@ int ElaRadioButtonStyle::pixelMetric(PixelMetric metric, const QStyleOption* opt void ElaRadioButtonStyle::_startRatioAnimation(qreal* targetRatio, qreal endRatio, int duration, QEasingCurve::Type curve, const QWidget* widget) const { - QVariantAnimation* ratioAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - *targetRatio = value.toReal(); - if (widgetGuard && widgetGuard->isVisible()) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& ratioAnimation = targetRatio == &_hoverRatio ? _hoverRatioAnimation : _checkRatioAnimation; + if (!ratioAnimation) + { + ratioAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(ratioAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + *targetRatio = value.toReal(); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + ratioAnimation->stop(); ratioAnimation->setDuration(duration); ratioAnimation->setEasingCurve(QEasingCurve(curve)); ratioAnimation->setStartValue(*targetRatio); ratioAnimation->setEndValue(endRatio); - ratioAnimation->start(QAbstractAnimation::DeleteWhenStopped); + ratioAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h index 331ceed..4f7beb4 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaRadioButtonStyle.h @@ -5,6 +5,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaRadioButtonStyle : public QProxyStyle { Q_OBJECT @@ -18,6 +19,8 @@ class ElaRadioButtonStyle : public QProxyStyle ElaThemeType::ThemeMode _themeMode; mutable qreal _hoverRatio{0}; mutable qreal _checkRatio{0}; + mutable QVariantAnimation* _hoverRatioAnimation{nullptr}; + mutable QVariantAnimation* _checkRatioAnimation{nullptr}; mutable bool _lastHovered{false}; mutable bool _lastChecked{false}; mutable bool _firstPaint{true}; diff --git a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp index c7d5a44..751e076 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.cpp @@ -220,18 +220,22 @@ QRect ElaSpinBoxStyle::subControlRect(ComplexControl cc, const QStyleOptionCompl void ElaSpinBoxStyle::_startHoverAnimation(qreal endRatio, const QWidget* widget) const { - QVariantAnimation* hoverAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - this->_hoverRatio = value.toReal(); - if (widgetGuard && widgetGuard->isVisible()) - { - widgetGuard->update(); - } - }); - hoverAnimation->setDuration(150); - hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); - hoverAnimation->setStartValue(_hoverRatio); - hoverAnimation->setEndValue(endRatio); - hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); + if (!_hoverAnimation) + { + _hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(_hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + _hoverAnimation->stop(); + _hoverAnimation->setDuration(150); + _hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + _hoverAnimation->setStartValue(_hoverRatio); + _hoverAnimation->setEndValue(endRatio); + _hoverAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h index 1805e42..9512378 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaSpinBoxStyle.h @@ -4,6 +4,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaSpinBoxStyle : public QProxyStyle { Q_OBJECT @@ -17,6 +18,7 @@ class ElaSpinBoxStyle : public QProxyStyle private: ElaThemeType::ThemeMode _themeMode; mutable qreal _hoverRatio{0}; + mutable QVariantAnimation* _hoverAnimation{nullptr}; mutable bool _lastHovered{false}; mutable bool _firstPaint{true}; void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; diff --git a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp index d4d940e..6139a11 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.cpp @@ -251,25 +251,30 @@ QRect ElaTabBarStyle::subElementRect(SubElement element, const QStyleOption* opt void ElaTabBarStyle::_startTabHoverAnimation(bool isFadeIn, const QWidget* widget) const { - QVariantAnimation* tabAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(tabAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - if (isFadeIn) - { - this->_hoverInRatio = value.toReal(); - } - else - { - this->_hoverOutRatio = value.toReal(); - } - if (widgetGuard) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& tabAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; + if (!tabAnimation) + { + tabAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(tabAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + tabAnimation->stop(); tabAnimation->setDuration(150); tabAnimation->setEasingCurve(QEasingCurve::OutCubic); tabAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); tabAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); - tabAnimation->start(QAbstractAnimation::DeleteWhenStopped); + tabAnimation->start(); } diff --git a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h index 1c774b4..42892dc 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTabBarStyle.h @@ -4,6 +4,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaTabBarStyle : public QProxyStyle { Q_OBJECT @@ -22,6 +23,8 @@ class ElaTabBarStyle : public QProxyStyle mutable QRect _fadeRect; mutable qreal _hoverInRatio{0}; mutable qreal _hoverOutRatio{0}; + mutable QVariantAnimation* _hoverInAnimation{nullptr}; + mutable QVariantAnimation* _hoverOutAnimation{nullptr}; mutable bool _firstPaint{true}; void _startTabHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp index 108f0f5..3b4a1b3 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp @@ -129,27 +129,32 @@ void ElaTableViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOpti void ElaTableViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const { - QVariantAnimation* rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { - if (isFadeIn) - { - this->_hoverInRatio = value.toReal(); - } - else - { - this->_hoverOutRatio = value.toReal(); - } - if (widgetGuard) - { - widgetGuard->update(); - } - }); + QVariantAnimation*& rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; + if (!rowAnimation) + { + rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + rowAnimation->stop(); rowAnimation->setDuration(150); rowAnimation->setEasingCurve(QEasingCurve::OutCubic); rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); - rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); + rowAnimation->start(); } void ElaTableViewStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h index c542cc6..aab9c81 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h @@ -5,6 +5,7 @@ #include "ElaDef.h" class QStyleOptionViewItem; +class QVariantAnimation; class ElaTableViewStyle : public QProxyStyle { Q_OBJECT @@ -24,6 +25,8 @@ class ElaTableViewStyle : public QProxyStyle mutable int _fadeOutRow{-1}; mutable qreal _hoverInRatio{0}; mutable qreal _hoverOutRatio{0}; + mutable QVariantAnimation* _hoverInAnimation{nullptr}; + mutable QVariantAnimation* _hoverOutAnimation{nullptr}; void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; diff --git a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp index 9119de3..1c58ff9 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.cpp @@ -375,18 +375,22 @@ qreal ElaToolButtonStyle::_calculateExpandIndicatorWidth(const QStyleOptionToolB void ElaToolButtonStyle::_startHoverAnimation(qreal endRatio, const QWidget *widget) const { - QVariantAnimation *hoverAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { - this->_hoverRatio = value.toReal(); - if (widgetGuard && widgetGuard->isVisible()) - { - widgetGuard->update(); - } - }); - hoverAnimation->setDuration(150); - hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); - hoverAnimation->setStartValue(_hoverRatio); - hoverAnimation->setEndValue(endRatio); - hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); + if (!_hoverAnimation) + { + _hoverAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(_hoverAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { + this->_hoverRatio = value.toReal(); + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + _hoverAnimation->stop(); + _hoverAnimation->setDuration(150); + _hoverAnimation->setEasingCurve(QEasingCurve::OutCubic); + _hoverAnimation->setStartValue(_hoverRatio); + _hoverAnimation->setEndValue(endRatio); + _hoverAnimation->start(); } \ No newline at end of file diff --git a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h index 90d620a..7710f41 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaToolButtonStyle.h @@ -5,6 +5,7 @@ #include "ElaDef.h" class QStyleOptionToolButton; +class QVariantAnimation; class ElaToolButtonStyle : public QProxyStyle { Q_OBJECT @@ -24,6 +25,7 @@ class ElaToolButtonStyle : public QProxyStyle int _contentMargin{6}; int _hasMenuIndicatorWidth{30}; mutable qreal _hoverRatio{0}; + mutable QVariantAnimation* _hoverAnimation{nullptr}; mutable bool _lastHovered{false}; mutable bool _firstPaint{true}; void _startHoverAnimation(qreal endRatio, const QWidget* widget) const; diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp index c37b3cd..0b3c413 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp @@ -360,25 +360,30 @@ QRect ElaTreeViewStyle::subElementRect(SubElement element, const QStyleOption *o void ElaTreeViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget *widget) const { - QVariantAnimation *rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); - connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { - if (isFadeIn) - { - this->_hoverInRatio = value.toReal(); - } - else - { - this->_hoverOutRatio = value.toReal(); - } - if (widgetGuard) - { - widgetGuard->update(); - } - }); + QVariantAnimation *&rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; + if (!rowAnimation) + { + rowAnimation = new QVariantAnimation; + QPointer widgetGuard = const_cast(widget); + connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { + if (isFadeIn) + { + this->_hoverInRatio = value.toReal(); + } + else + { + this->_hoverOutRatio = value.toReal(); + } + if (widgetGuard && widgetGuard->isVisible()) + { + widgetGuard->update(); + } + }); + } + rowAnimation->stop(); rowAnimation->setDuration(150); rowAnimation->setEasingCurve(QEasingCurve::OutCubic); rowAnimation->setStartValue(isFadeIn ? _hoverInRatio : _hoverOutRatio); rowAnimation->setEndValue(isFadeIn ? 1.0 : 0.0); - rowAnimation->start(QAbstractAnimation::DeleteWhenStopped); + rowAnimation->start(); } \ No newline at end of file diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h index f184fb9..06e4f3b 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h @@ -5,6 +5,7 @@ #include #include "ElaDef.h" +class QVariantAnimation; class ElaTreeViewStyle : public QProxyStyle { Q_OBJECT @@ -26,6 +27,8 @@ class ElaTreeViewStyle : public QProxyStyle mutable QPersistentModelIndex _fadeOutIndex; mutable qreal _hoverInRatio{0}; mutable qreal _hoverOutRatio{0}; + mutable QVariantAnimation* _hoverInAnimation{nullptr}; + mutable QVariantAnimation* _hoverOutAnimation{nullptr}; mutable bool _firstPaint{true}; void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; }; diff --git a/ElaWidgetTools/ElaFloatButton.cpp b/ElaWidgetTools/ElaFloatButton.cpp index 0d136af..c0974de 100644 --- a/ElaWidgetTools/ElaFloatButton.cpp +++ b/ElaWidgetTools/ElaFloatButton.cpp @@ -207,25 +207,13 @@ bool ElaFloatButton::event(QEvent *event) case QEvent::Enter: { d->_isAlphaAnimationFinished = false; - QPropertyAnimation *alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { if (isVisible()) { update(); } }); - connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { d->_isAlphaAnimationFinished = true; }); - alphaAnimation->setDuration(200); - alphaAnimation->setStartValue(d->_pHoverAlpha); - alphaAnimation->setEndValue(255); - alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startHoverAlphaAnimation(255); break; } case QEvent::Leave: { d->_isAlphaAnimationFinished = false; - QPropertyAnimation *alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=]() { if (isVisible()) { update(); } }); - connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { d->_isAlphaAnimationFinished = true; }); - alphaAnimation->setDuration(200); - alphaAnimation->setStartValue(d->_pHoverAlpha); - alphaAnimation->setEndValue(0); - alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startHoverAlphaAnimation(0); break; } default: @@ -243,6 +231,22 @@ ElaFloatButtonPrivate::~ElaFloatButtonPrivate() { } +void ElaFloatButtonPrivate::_startHoverAlphaAnimation(qreal endValue) +{ + Q_Q(ElaFloatButton); + if (!_hoverAlphaAnimation) + { + _hoverAlphaAnimation = new QPropertyAnimation(this, "pHoverAlpha", q); + connect(_hoverAlphaAnimation, &QPropertyAnimation::valueChanged, q, [=]() { if (q->isVisible()) { q->update(); } }); + connect(_hoverAlphaAnimation, &QPropertyAnimation::finished, q, [=]() { _isAlphaAnimationFinished = true; }); + } + _hoverAlphaAnimation->stop(); + _hoverAlphaAnimation->setDuration(200); + _hoverAlphaAnimation->setStartValue(_pHoverAlpha); + _hoverAlphaAnimation->setEndValue(endValue); + _hoverAlphaAnimation->start(); +} + void ElaFloatButtonPrivate::updatePosition() { Q_Q(ElaFloatButton); diff --git a/ElaWidgetTools/ElaIconButton.cpp b/ElaWidgetTools/ElaIconButton.cpp index 652649c..d0a2404 100755 --- a/ElaWidgetTools/ElaIconButton.cpp +++ b/ElaWidgetTools/ElaIconButton.cpp @@ -162,21 +162,7 @@ bool ElaIconButton::event(QEvent* event) if (isEnabled() && !d->_pIsSelected) { d->_isAlphaAnimationFinished = false; - QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - if (!isVisible()) - { - return; - } - update(); - }); - connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { - d->_isAlphaAnimationFinished = true; - }); - alphaAnimation->setDuration(175); - alphaAnimation->setStartValue(d->_pHoverAlpha); - alphaAnimation->setEndValue(d->_themeMode == ElaThemeType::Light ? d->_pLightHoverColor.alpha() : d->_pDarkHoverColor.alpha()); - alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startHoverAlphaAnimation(d->_themeMode == ElaThemeType::Light ? d->_pLightHoverColor.alpha() : d->_pDarkHoverColor.alpha()); } break; } @@ -185,21 +171,7 @@ bool ElaIconButton::event(QEvent* event) if (isEnabled() && !d->_pIsSelected) { d->_isAlphaAnimationFinished = false; - QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - if (!isVisible()) - { - return; - } - update(); - }); - connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { - d->_isAlphaAnimationFinished = true; - }); - alphaAnimation->setDuration(175); - alphaAnimation->setStartValue(d->_pHoverAlpha); - alphaAnimation->setEndValue(0); - alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startHoverAlphaAnimation(0); } break; } diff --git a/ElaWidgetTools/ElaToggleButton.cpp b/ElaWidgetTools/ElaToggleButton.cpp index 8bc128c..4728c10 100755 --- a/ElaWidgetTools/ElaToggleButton.cpp +++ b/ElaWidgetTools/ElaToggleButton.cpp @@ -67,18 +67,7 @@ bool ElaToggleButton::event(QEvent* event) { if (isEnabled()) { - QPropertyAnimation* hoverAnimation = new QPropertyAnimation(d, "pHoverAlpha"); - connect(hoverAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - if (!isVisible()) - { - return; - } - update(); - }); - hoverAnimation->setDuration(175); - hoverAnimation->setStartValue(d->_pHoverAlpha); - hoverAnimation->setEndValue(event->type() == QEvent::Enter ? 255 : 0); - hoverAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startHoverAlphaAnimation(event->type() == QEvent::Enter ? 255 : 0); } update(); break; @@ -105,28 +94,7 @@ void ElaToggleButton::mouseReleaseEvent(QMouseEvent* event) d->_isPressed = false; d->_isAlphaAnimationFinished = false; d->_isToggled = !d->_isToggled; - QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pToggleAlpha"); - connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) { - if (!isVisible()) - { - return; - } - update(); - }); - connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() { - d->_isAlphaAnimationFinished = true; - }); - alphaAnimation->setDuration(250); - alphaAnimation->setStartValue(d->_pToggleAlpha); - if (d->_isToggled) - { - alphaAnimation->setEndValue(255); - } - else - { - alphaAnimation->setEndValue(0); - } - alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped); + d->_startToggleAlphaAnimation(d->_isToggled ? 255 : 0); Q_EMIT this->toggled(d->_isToggled); QWidget::mouseReleaseEvent(event); } diff --git a/ElaWidgetTools/private/ElaFloatButtonPrivate.h b/ElaWidgetTools/private/ElaFloatButtonPrivate.h index a4135ed..bd1fc21 100644 --- a/ElaWidgetTools/private/ElaFloatButtonPrivate.h +++ b/ElaWidgetTools/private/ElaFloatButtonPrivate.h @@ -7,6 +7,7 @@ class ElaFloatButton; class ElaMenu; +class QPropertyAnimation; class ElaFloatButtonPrivate : public QObject { Q_OBJECT @@ -27,6 +28,9 @@ class ElaFloatButtonPrivate : public QObject bool _isPressed{false}; bool _isAlphaAnimationFinished{true}; ElaThemeType::ThemeMode _themeMode; + QPropertyAnimation* _hoverAlphaAnimation{nullptr}; + + void _startHoverAlphaAnimation(qreal endValue); }; #endif // ELAFLOATBUTTONPRIVATE_H diff --git a/ElaWidgetTools/private/ElaIconButtonPrivate.cpp b/ElaWidgetTools/private/ElaIconButtonPrivate.cpp index c20c2bf..41b8add 100755 --- a/ElaWidgetTools/private/ElaIconButtonPrivate.cpp +++ b/ElaWidgetTools/private/ElaIconButtonPrivate.cpp @@ -1,5 +1,9 @@ #include "ElaIconButtonPrivate.h" +#include + +#include "ElaIconButton.h" + ElaIconButtonPrivate::ElaIconButtonPrivate(QObject *parent) : QObject{parent} {} @@ -8,3 +12,26 @@ ElaIconButtonPrivate::~ElaIconButtonPrivate() { } + +void ElaIconButtonPrivate::_startHoverAlphaAnimation(qreal endValue) +{ + Q_Q(ElaIconButton); + if (!_hoverAlphaAnimation) + { + _hoverAlphaAnimation = new QPropertyAnimation(this, "pHoverAlpha", q); + connect(_hoverAlphaAnimation, &QPropertyAnimation::valueChanged, q, [=]() { + if (q->isVisible()) + { + q->update(); + } + }); + connect(_hoverAlphaAnimation, &QPropertyAnimation::finished, q, [=]() { + _isAlphaAnimationFinished = true; + }); + } + _hoverAlphaAnimation->stop(); + _hoverAlphaAnimation->setDuration(175); + _hoverAlphaAnimation->setStartValue(_pHoverAlpha); + _hoverAlphaAnimation->setEndValue(endValue); + _hoverAlphaAnimation->start(); +} diff --git a/ElaWidgetTools/private/ElaIconButtonPrivate.h b/ElaWidgetTools/private/ElaIconButtonPrivate.h index 0516c0d..f6dc20f 100755 --- a/ElaWidgetTools/private/ElaIconButtonPrivate.h +++ b/ElaWidgetTools/private/ElaIconButtonPrivate.h @@ -7,6 +7,7 @@ #include "ElaDef.h" class ElaIconButton; +class QPropertyAnimation; class ElaIconButtonPrivate : public QObject { Q_OBJECT @@ -30,6 +31,9 @@ class ElaIconButtonPrivate : public QObject QPixmap _iconPix; bool _isAlphaAnimationFinished{true}; ElaThemeType::ThemeMode _themeMode; + QPropertyAnimation* _hoverAlphaAnimation{nullptr}; + + void _startHoverAlphaAnimation(qreal endValue); }; #endif // ELAICONBUTTONPRIVATE_H diff --git a/ElaWidgetTools/private/ElaToggleButtonPrivate.cpp b/ElaWidgetTools/private/ElaToggleButtonPrivate.cpp index a6c2536..456ee95 100755 --- a/ElaWidgetTools/private/ElaToggleButtonPrivate.cpp +++ b/ElaWidgetTools/private/ElaToggleButtonPrivate.cpp @@ -1,5 +1,7 @@ #include "ElaToggleButtonPrivate.h" +#include + #include "ElaTheme.h" #include "ElaToggleButton.h" ElaToggleButtonPrivate::ElaToggleButtonPrivate(QObject* parent) @@ -10,3 +12,46 @@ ElaToggleButtonPrivate::ElaToggleButtonPrivate(QObject* parent) ElaToggleButtonPrivate::~ElaToggleButtonPrivate() { } + +void ElaToggleButtonPrivate::_startHoverAlphaAnimation(qreal endValue) +{ + Q_Q(ElaToggleButton); + if (!_hoverAlphaAnimation) + { + _hoverAlphaAnimation = new QPropertyAnimation(this, "pHoverAlpha", q); + connect(_hoverAlphaAnimation, &QPropertyAnimation::valueChanged, q, [=]() { + if (q->isVisible()) + { + q->update(); + } + }); + } + _hoverAlphaAnimation->stop(); + _hoverAlphaAnimation->setDuration(175); + _hoverAlphaAnimation->setStartValue(_pHoverAlpha); + _hoverAlphaAnimation->setEndValue(endValue); + _hoverAlphaAnimation->start(); +} + +void ElaToggleButtonPrivate::_startToggleAlphaAnimation(qreal endValue) +{ + Q_Q(ElaToggleButton); + if (!_toggleAlphaAnimation) + { + _toggleAlphaAnimation = new QPropertyAnimation(this, "pToggleAlpha", q); + connect(_toggleAlphaAnimation, &QPropertyAnimation::valueChanged, q, [=]() { + if (q->isVisible()) + { + q->update(); + } + }); + connect(_toggleAlphaAnimation, &QPropertyAnimation::finished, q, [=]() { + _isAlphaAnimationFinished = true; + }); + } + _toggleAlphaAnimation->stop(); + _toggleAlphaAnimation->setDuration(250); + _toggleAlphaAnimation->setStartValue(_pToggleAlpha); + _toggleAlphaAnimation->setEndValue(endValue); + _toggleAlphaAnimation->start(); +} diff --git a/ElaWidgetTools/private/ElaToggleButtonPrivate.h b/ElaWidgetTools/private/ElaToggleButtonPrivate.h index ef52101..1706fc7 100755 --- a/ElaWidgetTools/private/ElaToggleButtonPrivate.h +++ b/ElaWidgetTools/private/ElaToggleButtonPrivate.h @@ -5,6 +5,7 @@ #include "ElaDef.h" class ElaToggleButton; +class QPropertyAnimation; class ElaToggleButtonPrivate : public QObject { Q_OBJECT @@ -22,6 +23,11 @@ class ElaToggleButtonPrivate : public QObject bool _isToggled{false}; bool _isPressed{false}; ElaThemeType::ThemeMode _themeMode; + QPropertyAnimation* _hoverAlphaAnimation{nullptr}; + QPropertyAnimation* _toggleAlphaAnimation{nullptr}; + + void _startHoverAlphaAnimation(qreal endValue); + void _startToggleAlphaAnimation(qreal endValue); }; #endif // ELATOGGLEBUTTONPRIVATE_H From 47be0b744c5bc41c02c5ad6c3b3153143017bfbe Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 11:30:08 +0800 Subject: [PATCH 10/17] perf(style): repaint only the animated region for row hover and slider handle - List/Table/Tree view row hover fades now dirty a viewport row strip via viewport()->update(rect) (the same region Qt itself uses for its hover handling) instead of a full-widget update; the in/out strips are captured when the animation starts - Slider handle radius transitions (ElaSliderStyle, ElaColorValueSliderStyle) update only the handle rect expanded by 2px Visual behavior unchanged; row strip expansion (2px) covers the rounded highlight and antialiasing bleed. --- .../ElaColorValueSliderStyle.cpp | 14 ++++---- .../ElaColorValueSliderStyle.h | 2 +- .../DeveloperComponents/ElaListViewStyle.cpp | 29 ++++++++++++---- .../DeveloperComponents/ElaListViewStyle.h | 4 ++- .../DeveloperComponents/ElaSliderStyle.cpp | 14 ++++---- .../DeveloperComponents/ElaSliderStyle.h | 2 +- .../DeveloperComponents/ElaTableViewStyle.cpp | 26 ++++++++++++--- .../DeveloperComponents/ElaTableViewStyle.h | 4 ++- .../DeveloperComponents/ElaTreeViewStyle.cpp | 33 ++++++++++++++----- .../DeveloperComponents/ElaTreeViewStyle.h | 4 ++- 10 files changed, 96 insertions(+), 36 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp index fae1f24..b854407 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.cpp @@ -72,7 +72,7 @@ void ElaColorValueSliderStyle::drawComplexControl(ComplexControl control, const { if (!_lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(sliderHandleRect.width() / 2.8, sliderHandleRect.width() / 4.5, const_cast(widget)); + _startRadiusAnimation(sliderHandleRect.width() / 2.8, sliderHandleRect.width() / 4.5, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } painter->drawEllipse(QPointF(sliderHandleRect.center().x() + 1, sliderHandleRect.center().y() + 1), _circleRadius, _circleRadius); @@ -84,12 +84,12 @@ void ElaColorValueSliderStyle::drawComplexControl(ComplexControl control, const { if (!_lastState.testFlag(QStyle::State_MouseOver)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } if (_lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } painter->drawEllipse(QPointF(sliderHandleRect.center().x() + 1, sliderHandleRect.center().y() + 1), _circleRadius, _circleRadius); @@ -100,7 +100,7 @@ void ElaColorValueSliderStyle::drawComplexControl(ComplexControl control, const { if (_lastState.testFlag(QStyle::State_MouseOver) || _lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 3.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 3.8, sliderHandleRect, const_cast(widget)); _lastState &= ~QStyle::State_MouseOver; _lastState &= ~QStyle::State_Sunken; } @@ -146,16 +146,18 @@ int ElaColorValueSliderStyle::styleHint(StyleHint hint, const QStyleOption* opti return QProxyStyle::styleHint(hint, option, widget, returnData); } -void ElaColorValueSliderStyle::_startRadiusAnimation(qreal startRadius, qreal endRadius, QWidget* widget) const +void ElaColorValueSliderStyle::_startRadiusAnimation(qreal startRadius, qreal endRadius, const QRect& handleRect, QWidget* widget) const { ElaColorValueSliderStyle* style = const_cast(this); QPropertyAnimation* circleRadiusAnimation = new QPropertyAnimation(style, "circleRadius"); + // 手柄悬停过渡期间位置不变,脏区域即手柄矩形外扩 2px(含描边与抗锯齿溢出) + QRect animRect = handleRect.adjusted(-2, -2, 2, 2); QPointer widgetGuard = widget; connect(circleRadiusAnimation, &QPropertyAnimation::valueChanged, style, [=](const QVariant& value) { this->_circleRadius = value.toReal(); if (widgetGuard && widgetGuard->isVisible()) { - widgetGuard->update(); + widgetGuard->update(animRect); } }); circleRadiusAnimation->setEasingCurve(QEasingCurve::InOutSine); circleRadiusAnimation->setStartValue(startRadius); diff --git a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.h b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.h index 2ddd132..5243de6 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaColorValueSliderStyle.h @@ -21,7 +21,7 @@ class ElaColorValueSliderStyle : public QProxyStyle QLinearGradient* _baseGradient{nullptr}; mutable QStyle::State _lastState{QStyle::State_None}; mutable qreal _circleRadius{0}; - void _startRadiusAnimation(qreal startRadius, qreal endRadius, QWidget* widget) const; + void _startRadiusAnimation(qreal startRadius, qreal endRadius, const QRect& handleRect, QWidget* widget) const; }; #endif // ELACOLORVALUESLIDERSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp index 6c29d3e..ba7689b 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.cpp @@ -1,5 +1,6 @@ #include "ElaListViewStyle.h" +#include #include #include #include @@ -39,6 +40,7 @@ void ElaListViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio _firstPaint = false; _hoverIndex = isHovered ? rowKey : QPersistentModelIndex(); _hoverInRatio = isHovered ? 1 : 0; + _hoverInRect = isHovered ? vopt->rect : QRect(); } else if (rowKey.isValid() && isHovered && rowKey != _hoverIndex) { @@ -46,17 +48,17 @@ void ElaListViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio _fadeOutIndex = _hoverIndex; if (_fadeOutIndex.isValid()) { - _startRowHoverAnimation(false, widget); + _startRowHoverAnimation(false, _hoverInRect, widget); } _hoverIndex = rowKey; - _startRowHoverAnimation(true, widget); + _startRowHoverAnimation(true, vopt->rect, widget); } else if (rowKey.isValid() && !isHovered && rowKey == _hoverIndex) { // 悬停移出行外 _fadeOutIndex = _hoverIndex; _hoverIndex = QPersistentModelIndex(); - _startRowHoverAnimation(false, widget); + _startRowHoverAnimation(false, _hoverInRect, widget); } qreal hoverRatio = 0; if (rowKey.isValid()) @@ -218,13 +220,16 @@ QSize ElaListViewStyle::sizeFromContents(ContentsType type, const QStyleOption* return QProxyStyle::sizeFromContents(type, option, size, widget); } -void ElaListViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const +void ElaListViewStyle::_startRowHoverAnimation(bool isFadeIn, const QRect& rowRect, const QWidget* widget) const { QVariantAnimation*& rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; if (!rowAnimation) { rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); + const QAbstractScrollArea* scrollArea = dynamic_cast(widget); + // 行悬停过渡的脏区域是视口上的整行条带(同 QAbstractItemView 自身悬停处理), + // 必须走 viewport()->update(rect),rect 为视口坐标 + QPointer widgetGuard = scrollArea ? const_cast(scrollArea)->viewport() : const_cast(widget); connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { if (isFadeIn) { @@ -236,10 +241,22 @@ void ElaListViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* wid } if (widgetGuard && widgetGuard->isVisible()) { - widgetGuard->update(); + const QRect& animRect = isFadeIn ? this->_hoverInRect : this->_hoverOutRect; + if (animRect.isValid()) + { + widgetGuard->update(QRect(0, animRect.y(), widgetGuard->width(), animRect.height()).adjusted(0, -2, 0, 2)); + } } }); } + if (isFadeIn) + { + _hoverInRect = rowRect; + } + else + { + _hoverOutRect = rowRect; + } rowAnimation->stop(); rowAnimation->setDuration(150); rowAnimation->setEasingCurve(QEasingCurve::OutCubic); diff --git a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h index c96b05c..99f4aee 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaListViewStyle.h @@ -27,8 +27,10 @@ class ElaListViewStyle : public QProxyStyle mutable qreal _hoverOutRatio{0}; mutable QVariantAnimation* _hoverInAnimation{nullptr}; mutable QVariantAnimation* _hoverOutAnimation{nullptr}; + mutable QRect _hoverInRect; + mutable QRect _hoverOutRect; mutable bool _firstPaint{true}; - void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; + void _startRowHoverAnimation(bool isFadeIn, const QRect& rowRect, const QWidget* widget) const; }; #endif // ELALISTVIEWSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp index 0383be5..929babe 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.cpp @@ -77,7 +77,7 @@ void ElaSliderStyle::drawComplexControl(ComplexControl control, const QStyleOpti { if (!_lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(sliderHandleRect.width() / 2.8, sliderHandleRect.width() / 4.5, const_cast(widget)); + _startRadiusAnimation(sliderHandleRect.width() / 2.8, sliderHandleRect.width() / 4.5, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } painter->drawEllipse(QPointF(sliderHandleRect.center().x() + 1, sliderHandleRect.center().y() + 1), _circleRadius, _circleRadius); @@ -89,12 +89,12 @@ void ElaSliderStyle::drawComplexControl(ComplexControl control, const QStyleOpti { if (!_lastState.testFlag(QStyle::State_MouseOver)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } if (_lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 2.8, sliderHandleRect, const_cast(widget)); _lastState = sopt->state; } painter->drawEllipse(QPointF(sliderHandleRect.center().x() + 1, sliderHandleRect.center().y() + 1), _circleRadius, _circleRadius); @@ -105,7 +105,7 @@ void ElaSliderStyle::drawComplexControl(ComplexControl control, const QStyleOpti { if (_lastState.testFlag(QStyle::State_MouseOver) || _lastState.testFlag(QStyle::State_Sunken)) { - _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 3.8, const_cast(widget)); + _startRadiusAnimation(_circleRadius, sliderHandleRect.width() / 3.8, sliderHandleRect, const_cast(widget)); _lastState &= ~QStyle::State_MouseOver; _lastState &= ~QStyle::State_Sunken; } @@ -151,16 +151,18 @@ int ElaSliderStyle::styleHint(StyleHint hint, const QStyleOption* option, const return QProxyStyle::styleHint(hint, option, widget, returnData); } -void ElaSliderStyle::_startRadiusAnimation(qreal startRadius, qreal endRadius, QWidget* widget) const +void ElaSliderStyle::_startRadiusAnimation(qreal startRadius, qreal endRadius, const QRect& handleRect, QWidget* widget) const { ElaSliderStyle* style = const_cast(this); QPropertyAnimation* circleRadiusAnimation = new QPropertyAnimation(style, "circleRadius"); + // 手柄悬停过渡期间位置不变,脏区域即手柄矩形外扩 2px(含描边与抗锯齿溢出) + QRect animRect = handleRect.adjusted(-2, -2, 2, 2); QPointer widgetGuard = widget; connect(circleRadiusAnimation, &QPropertyAnimation::valueChanged, style, [=](const QVariant& value) { this->_circleRadius = value.toReal(); if (widgetGuard && widgetGuard->isVisible()) { - widgetGuard->update(); + widgetGuard->update(animRect); } }); circleRadiusAnimation->setEasingCurve(QEasingCurve::InOutSine); circleRadiusAnimation->setStartValue(startRadius); diff --git a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.h b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.h index 4145a51..eff665a 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaSliderStyle.h @@ -18,7 +18,7 @@ class ElaSliderStyle : public QProxyStyle ElaThemeType::ThemeMode _themeMode; mutable QStyle::State _lastState{QStyle::State_None}; mutable qreal _circleRadius{0}; - void _startRadiusAnimation(qreal startRadius, qreal endRadius, QWidget* widget) const; + void _startRadiusAnimation(qreal startRadius, qreal endRadius, const QRect& handleRect, QWidget* widget) const; }; #endif // ELASLIDERSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp index 3b4a1b3..5e82ffd 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.cpp @@ -1,5 +1,6 @@ #include "ElaTableViewStyle.h" +#include #include #include #include @@ -44,12 +45,12 @@ void ElaTableViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOpti _fadeOutRow = _lastHoverRow; if (_fadeOutRow >= 0) { - _startRowHoverAnimation(false, widget); + _startRowHoverAnimation(false, QRect(0, tabView->rowViewportPosition(_fadeOutRow), 1, tabView->rowHeight(_fadeOutRow)), widget); } _lastHoverRow = hoverRow; if (hoverRow >= 0) { - _startRowHoverAnimation(true, widget); + _startRowHoverAnimation(true, QRect(0, tabView->rowViewportPosition(hoverRow), 1, tabView->rowHeight(hoverRow)), widget); } } painter->save(); @@ -127,13 +128,16 @@ void ElaTableViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOpti QProxyStyle::drawPrimitive(element, option, painter, widget); } -void ElaTableViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const +void ElaTableViewStyle::_startRowHoverAnimation(bool isFadeIn, const QRect& rowRect, const QWidget* widget) const { QVariantAnimation*& rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; if (!rowAnimation) { rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); + const QAbstractScrollArea* scrollArea = dynamic_cast(widget); + // 行悬停过渡的脏区域是视口上的整行条带(同 QAbstractItemView 自身悬停处理), + // 必须走 viewport()->update(rect),rect 为视口坐标 + QPointer widgetGuard = scrollArea ? const_cast(scrollArea)->viewport() : const_cast(widget); connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant& value) { if (isFadeIn) { @@ -145,10 +149,22 @@ void ElaTableViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget* wi } if (widgetGuard && widgetGuard->isVisible()) { - widgetGuard->update(); + const QRect& animRect = isFadeIn ? this->_hoverInRect : this->_hoverOutRect; + if (animRect.isValid()) + { + widgetGuard->update(QRect(0, animRect.y(), widgetGuard->width(), animRect.height()).adjusted(0, -2, 0, 2)); + } } }); } + if (isFadeIn) + { + _hoverInRect = rowRect; + } + else + { + _hoverOutRect = rowRect; + } rowAnimation->stop(); rowAnimation->setDuration(150); rowAnimation->setEasingCurve(QEasingCurve::OutCubic); diff --git a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h index aab9c81..c9183af 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTableViewStyle.h @@ -27,7 +27,9 @@ class ElaTableViewStyle : public QProxyStyle mutable qreal _hoverOutRatio{0}; mutable QVariantAnimation* _hoverInAnimation{nullptr}; mutable QVariantAnimation* _hoverOutAnimation{nullptr}; - void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; + mutable QRect _hoverInRect; + mutable QRect _hoverOutRect; + void _startRowHoverAnimation(bool isFadeIn, const QRect& rowRect, const QWidget* widget) const; }; #endif // ELATABLEVIEWSTYLE_H diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp index 0b3c413..6c629de 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.cpp @@ -1,5 +1,6 @@ #include "ElaTreeViewStyle.h" +#include #include #include #include @@ -42,6 +43,7 @@ void ElaTreeViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio _firstPaint = false; _hoverIndex = isHovered ? rowKey : QPersistentModelIndex(); _hoverInRatio = isHovered ? 1 : 0; + _hoverInRect = isHovered ? vopt->rect : QRect(); } else if (rowKey.isValid() && isHovered && rowKey != _hoverIndex) { @@ -49,17 +51,17 @@ void ElaTreeViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOptio _fadeOutIndex = _hoverIndex; if (_fadeOutIndex.isValid()) { - _startRowHoverAnimation(false, widget); + _startRowHoverAnimation(false, _hoverInRect, widget); } _hoverIndex = rowKey; - _startRowHoverAnimation(true, widget); + _startRowHoverAnimation(true, vopt->rect, widget); } else if (rowKey.isValid() && !isHovered && rowKey == _hoverIndex) { // 悬停移出行外 _fadeOutIndex = _hoverIndex; _hoverIndex = QPersistentModelIndex(); - _startRowHoverAnimation(false, widget); + _startRowHoverAnimation(false, _hoverInRect, widget); } qreal hoverRatio = 0; if (rowKey.isValid() && isEnable) @@ -358,13 +360,16 @@ QRect ElaTreeViewStyle::subElementRect(SubElement element, const QStyleOption *o return QProxyStyle::subElementRect(element, option, widget); } -void ElaTreeViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget *widget) const +void ElaTreeViewStyle::_startRowHoverAnimation(bool isFadeIn, const QRect &rowRect, const QWidget *widget) const { QVariantAnimation *&rowAnimation = isFadeIn ? _hoverInAnimation : _hoverOutAnimation; if (!rowAnimation) { rowAnimation = new QVariantAnimation; - QPointer widgetGuard = const_cast(widget); + const QAbstractScrollArea *scrollArea = dynamic_cast(widget); + // 行悬停过渡的脏区域是视口上的整行条带(同 QAbstractItemView 自身悬停处理), + // 必须走 viewport()->update(rect),rect 为视口坐标 + QPointer widgetGuard = scrollArea ? const_cast(scrollArea)->viewport() : const_cast(widget); connect(rowAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { if (isFadeIn) { @@ -374,11 +379,23 @@ void ElaTreeViewStyle::_startRowHoverAnimation(bool isFadeIn, const QWidget *wid { this->_hoverOutRatio = value.toReal(); } - if (widgetGuard && widgetGuard->isVisible()) + if (widgetGuard && widgetGuard->isVisible()) + { + const QRect& animRect = isFadeIn ? this->_hoverInRect : this->_hoverOutRect; + if (animRect.isValid()) { - widgetGuard->update(); + widgetGuard->update(QRect(0, animRect.y(), widgetGuard->width(), animRect.height()).adjusted(0, -2, 0, 2)); } - }); + } + }); + } + if (isFadeIn) + { + _hoverInRect = rowRect; + } + else + { + _hoverOutRect = rowRect; } rowAnimation->stop(); rowAnimation->setDuration(150); diff --git a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h index 06e4f3b..4ff11e3 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h +++ b/ElaWidgetTools/DeveloperComponents/ElaTreeViewStyle.h @@ -29,8 +29,10 @@ class ElaTreeViewStyle : public QProxyStyle mutable qreal _hoverOutRatio{0}; mutable QVariantAnimation* _hoverInAnimation{nullptr}; mutable QVariantAnimation* _hoverOutAnimation{nullptr}; + mutable QRect _hoverInRect; + mutable QRect _hoverOutRect; mutable bool _firstPaint{true}; - void _startRowHoverAnimation(bool isFadeIn, const QWidget* widget) const; + void _startRowHoverAnimation(bool isFadeIn, const QRect& rowRect, const QWidget* widget) const; }; #endif // ELATREEVIEWSTYLE_H From c5cc88228b864e86fe69ad1181a10a3d4ace5ce0 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 11:31:46 +0800 Subject: [PATCH 11/17] perf(window): replace processEvents with a zero-delay timer for blur page switch QApplication::processEvents() inside the navigation path risks event re-entrancy (a re-entering navigation could restart animations or re-grab page pixmaps mid-flight). QTimer::singleShot(0) keeps the same ordering (one frame of the blurred state renders before the page switches) without re-entering the event loop. --- .../DeveloperComponents/ElaCentralStackedWidget.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp b/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp index 3a30785..5b59dd9 100755 --- a/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp +++ b/ElaWidgetTools/DeveloperComponents/ElaCentralStackedWidget.cpp @@ -247,8 +247,11 @@ void ElaCentralStackedWidget::doWindowStackSwitch(ElaWindowType::StackSwitchMode blurAnimation->setStartValue(40); blurAnimation->setEndValue(2); blurAnimation->start(QAbstractAnimation::DeleteWhenStopped); - QApplication::processEvents(); - _containerStackedWidget->setCurrentIndex(nodeIndex); + // 用零延迟单发定时器代替 processEvents():保留“先渲染一帧模糊、再切页”的时序, + // 同时避免事件重入(重入可能在动画/页面截取进行中重新触发导航) + QTimer::singleShot(0, this, [=]() { + _containerStackedWidget->setCurrentIndex(nodeIndex); + }); break; } } From 60380871a8e1c93fe9f71a3a4f13d7c6aee454f0 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 11:32:22 +0800 Subject: [PATCH 12/17] perf(button): drop empty-property QPropertyAnimation timer hacks The icon fade in ElaCopyButton and the expand-icon rotation in ElaTreeSelectPrivate used QPropertyAnimation(this, "") purely as a timer that drives repaints. Replace them with QVariantAnimation (same durations, easing and valueChanged handling) so no property lookup on an empty name happens per tick. --- ElaWidgetTools/ElaCopyButton.cpp | 10 +++++----- ElaWidgetTools/private/ElaTreeSelectPrivate.cpp | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ElaWidgetTools/ElaCopyButton.cpp b/ElaWidgetTools/ElaCopyButton.cpp index 3876a57..c0a19a2 100644 --- a/ElaWidgetTools/ElaCopyButton.cpp +++ b/ElaWidgetTools/ElaCopyButton.cpp @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include #include "ElaTheme.h" #include "private/ElaCopyButtonPrivate.h" @@ -36,8 +36,8 @@ ElaCopyButton::ElaCopyButton(QWidget *parent) : QPushButton(parent), d_ptr(new E connect(d->_resetTimer, &QTimer::timeout, this, [=]() { d->_isSuccess = false; - QPropertyAnimation *fadeAnimation = new QPropertyAnimation(this, ""); - connect(fadeAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) + QVariantAnimation *fadeAnimation = new QVariantAnimation(this); + connect(fadeAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { d->_iconOpacity = value.toReal(); if (isVisible()) @@ -121,8 +121,8 @@ void ElaCopyButton::mouseReleaseEvent(QMouseEvent *event) d->_isSuccess = true; d->_resetTimer->start(d->_pSuccessDuration); - QPropertyAnimation *fadeAnimation = new QPropertyAnimation(this, ""); - connect(fadeAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value) + QVariantAnimation *fadeAnimation = new QVariantAnimation(this); + connect(fadeAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) { d->_iconOpacity = value.toReal(); if (isVisible()) diff --git a/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp b/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp index ca81ddf..5604a04 100644 --- a/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp +++ b/ElaWidgetTools/private/ElaTreeSelectPrivate.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "ElaLineEdit.h" #include "ElaTheme.h" @@ -172,8 +173,8 @@ void ElaTreeSelectPrivate::_showPopup() heightAnimation->setDuration(400); heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); - QPropertyAnimation *rotateAnimation = new QPropertyAnimation(this, ""); - connect(rotateAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant &value) + QVariantAnimation *rotateAnimation = new QVariantAnimation(this); + connect(rotateAnimation, &QVariantAnimation::valueChanged, q, [=](const QVariant &value) { _expandIconRotate = value.toReal(); if (q->isVisible()) @@ -221,8 +222,8 @@ void ElaTreeSelectPrivate::_hidePopup() heightAnimation->setDuration(300); heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); - QPropertyAnimation *rotateAnimation = new QPropertyAnimation(this, ""); - connect(rotateAnimation, &QPropertyAnimation::valueChanged, q, [=](const QVariant &value) + QVariantAnimation *rotateAnimation = new QVariantAnimation(this); + connect(rotateAnimation, &QVariantAnimation::valueChanged, q, [=](const QVariant &value) { _expandIconRotate = value.toReal(); if (q->isVisible()) From 488b83b39c2513905fc15a3447179483babd3181 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 21:39:53 +0800 Subject: [PATCH 13/17] perf(blur): downsample before exponential blur for large-radius backgrounds doExponentialBlur now scales the image down to 1/4 side length, blurs with a proportionally reduced radius, then smooth-scales back up. The exponential decay length relative to the image size is unchanged, so the visual result is nearly identical while the pixel work drops ~16x for the Mica/acrylic background (blurRadius 500 on window resize). Small images and small radii keep the original full-resolution path. --- ElaWidgetTools/ElaExponentialBlur.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ElaWidgetTools/ElaExponentialBlur.cpp b/ElaWidgetTools/ElaExponentialBlur.cpp index 92da249..e4b33c0 100755 --- a/ElaWidgetTools/ElaExponentialBlur.cpp +++ b/ElaWidgetTools/ElaExponentialBlur.cpp @@ -22,6 +22,18 @@ ElaExponentialBlur::~ElaExponentialBlur() QPixmap ElaExponentialBlur::doExponentialBlur(QImage img, const quint16& blurRadius) { QImage shadowImage = img.convertToFormat(QImage::Format_ARGB32); + // 指数模糊的计算量随像素数线性增长。先按 1/4 边长降采样、用同步缩小的半径模糊, + // 再平滑放大回原尺寸:衰减长度与图像尺寸的比例不变,结果视觉几乎无差, + // 计算量降约 16 倍(Mica/亚克力背景在窗口 resize 时会反复全量调用) + const int kDownScale = 4; + if (blurRadius >= kDownScale && shadowImage.width() > kDownScale && shadowImage.height() > kDownScale) + { + QSize smallSize(shadowImage.width() / kDownScale, shadowImage.height() / kDownScale); + QImage smallImage = shadowImage.scaled(smallSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + ElaExponentialBlur::getInstance()->d_ptr->_drawExponentialBlur(smallImage, blurRadius / kDownScale); + shadowImage = smallImage.scaled(shadowImage.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + return QPixmap::fromImage(shadowImage); + } ElaExponentialBlur::getInstance()->d_ptr->_drawExponentialBlur(shadowImage, blurRadius); return QPixmap::fromImage(shadowImage); } From eb7c495b761e51d682a39f93e911784eff07224d Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 21:42:47 +0800 Subject: [PATCH 14/17] perf(popup): use window opacity instead of QGraphicsOpacityEffect ElaToast, ElaFlyout, ElaSnackbar, ElaPopconfirm and ElaTeachingTip are frameless top-level Qt::Tool windows; animating setWindowOpacity gives the same fade without the offscreen render+composite that a graphics opacity effect adds on every frame. Durations, easing and fade in/out sequencing unchanged. The unused _opacityEffect members in the ElaFlyout/ElaPopconfirm private classes are removed. ElaPopularCardFloater keeps its effect (child-widget opacity, no window to animate). --- ElaWidgetTools/ElaFlyout.cpp | 9 ++++----- ElaWidgetTools/ElaPopconfirm.cpp | 9 ++++----- ElaWidgetTools/ElaSnackbar.cpp | 12 +++++------- ElaWidgetTools/ElaTeachingTip.cpp | 12 +++--------- ElaWidgetTools/ElaToast.cpp | 11 ++++------- ElaWidgetTools/private/ElaFlyoutPrivate.h | 2 -- ElaWidgetTools/private/ElaPopconfirmPrivate.h | 2 -- 7 files changed, 20 insertions(+), 37 deletions(-) diff --git a/ElaWidgetTools/ElaFlyout.cpp b/ElaWidgetTools/ElaFlyout.cpp index 3190087..ee18f71 100644 --- a/ElaWidgetTools/ElaFlyout.cpp +++ b/ElaWidgetTools/ElaFlyout.cpp @@ -1,7 +1,6 @@ #include "ElaFlyout.h" #include -#include #include #include #include @@ -28,9 +27,8 @@ ElaFlyout::ElaFlyout(QWidget *parent) int margin = d->_shadowBorderWidth + 8; d->_mainLayout->setContentsMargins(margin, margin, margin, margin); - d->_opacityEffect = new QGraphicsOpacityEffect(this); - d->_opacityEffect->setOpacity(0); - setGraphicsEffect(d->_opacityEffect); + // 顶层窗口直接用窗口透明度做淡入,避免 QGraphicsOpacityEffect 每帧的离屏合成 + setWindowOpacity(0.0); d->_themeMode = eTheme->getThemeMode(); connect(eTheme, &ElaTheme::themeModeChanged, this, [=](ElaThemeType::ThemeMode themeMode) @@ -220,7 +218,8 @@ bool ElaFlyoutPrivate::eventFilter(QObject *watched, QEvent *event) void ElaFlyoutPrivate::_doShowAnimation() { - QPropertyAnimation *animation = new QPropertyAnimation(_opacityEffect, "opacity"); + Q_Q(ElaFlyout); + QPropertyAnimation *animation = new QPropertyAnimation(q, "windowOpacity"); animation->setDuration(250); animation->setStartValue(0.0); animation->setEndValue(1.0); diff --git a/ElaWidgetTools/ElaPopconfirm.cpp b/ElaWidgetTools/ElaPopconfirm.cpp index 83e8239..1f7e39f 100644 --- a/ElaWidgetTools/ElaPopconfirm.cpp +++ b/ElaWidgetTools/ElaPopconfirm.cpp @@ -1,7 +1,6 @@ #include "ElaPopconfirm.h" #include -#include #include #include #include @@ -35,9 +34,8 @@ ElaPopconfirm::ElaPopconfirm(QWidget *parent) : QWidget{nullptr}, d_ptr(new ElaP setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); setAttribute(Qt::WA_TranslucentBackground); - d->_opacityEffect = new QGraphicsOpacityEffect(this); - d->_opacityEffect->setOpacity(0); - setGraphicsEffect(d->_opacityEffect); + // 顶层窗口直接用窗口透明度做淡入,避免 QGraphicsOpacityEffect 每帧的离屏合成 + setWindowOpacity(0.0); d->_cancelButton = new ElaPushButton(d->_pCancelButtonText, this); d->_cancelButton->setFixedHeight(30); @@ -261,7 +259,8 @@ bool ElaPopconfirmPrivate::eventFilter(QObject *watched, QEvent *event) void ElaPopconfirmPrivate::_doShowAnimation() { - QPropertyAnimation *animation = new QPropertyAnimation(_opacityEffect, "opacity"); + Q_Q(ElaPopconfirm); + QPropertyAnimation *animation = new QPropertyAnimation(q, "windowOpacity"); animation->setDuration(250); animation->setStartValue(0.0); animation->setEndValue(1.0); diff --git a/ElaWidgetTools/ElaSnackbar.cpp b/ElaWidgetTools/ElaSnackbar.cpp index 3b30ac4..fb1ec1d 100644 --- a/ElaWidgetTools/ElaSnackbar.cpp +++ b/ElaWidgetTools/ElaSnackbar.cpp @@ -1,6 +1,5 @@ #include "ElaSnackbar.h" -#include #include #include #include @@ -133,11 +132,10 @@ ElaSnackbar::ElaSnackbar(SnackbarType type, const QString &text, const QString & move(s_centerX, targetY + 20); show(); - QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this); - opacityEffect->setOpacity(0); - setGraphicsEffect(opacityEffect); + // 顶层窗口直接用窗口透明度做淡入淡出,避免 QGraphicsOpacityEffect 每帧的离屏合成 + setWindowOpacity(0.0); - QPropertyAnimation *fadeIn = new QPropertyAnimation(opacityEffect, "opacity", this); + QPropertyAnimation *fadeIn = new QPropertyAnimation(this, "windowOpacity", this); fadeIn->setDuration(250); fadeIn->setStartValue(0.0); fadeIn->setEndValue(1.0); @@ -159,9 +157,9 @@ ElaSnackbar::ElaSnackbar(SnackbarType type, const QString &text, const QString & s_activeSnackbars.removeOne(this); _reflowAll(); - QPropertyAnimation *fadeOut = new QPropertyAnimation(opacityEffect, "opacity", this); + QPropertyAnimation *fadeOut = new QPropertyAnimation(this, "windowOpacity", this); fadeOut->setDuration(300); - fadeOut->setStartValue(opacityEffect->opacity()); + fadeOut->setStartValue(windowOpacity()); fadeOut->setEndValue(0.0); fadeOut->setEasingCurve(QEasingCurve::InCubic); connect(fadeOut, &QPropertyAnimation::finished, this, [=]() diff --git a/ElaWidgetTools/ElaTeachingTip.cpp b/ElaWidgetTools/ElaTeachingTip.cpp index e711ea1..2db161f 100644 --- a/ElaWidgetTools/ElaTeachingTip.cpp +++ b/ElaWidgetTools/ElaTeachingTip.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -119,20 +118,15 @@ ElaTeachingTip::TailPosition ElaTeachingTipPrivate::_calculateAutoTailPosition() void ElaTeachingTipPrivate::_doShowAnimation() { Q_Q(ElaTeachingTip); - QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(q); - q->setGraphicsEffect(effect); + // 顶层窗口直接用窗口透明度做淡入,避免 QGraphicsOpacityEffect 每帧的离屏合成 _pOpacity = 0.0; - effect->setOpacity(0); + q->setWindowOpacity(0.0); - QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity", q); + QPropertyAnimation *animation = new QPropertyAnimation(q, "windowOpacity", q); animation->setDuration(200); animation->setStartValue(0.0); animation->setEndValue(1.0); animation->setEasingCurve(QEasingCurve::OutCubic); - QObject::connect(animation, &QPropertyAnimation::finished, q, [=]() - { - q->setGraphicsEffect(nullptr); - }); animation->start(QAbstractAnimation::DeleteWhenStopped); } diff --git a/ElaWidgetTools/ElaToast.cpp b/ElaWidgetTools/ElaToast.cpp index 95b542c..d5aeaf6 100644 --- a/ElaWidgetTools/ElaToast.cpp +++ b/ElaWidgetTools/ElaToast.cpp @@ -1,6 +1,5 @@ #include "ElaToast.h" -#include #include #include #include @@ -68,20 +67,18 @@ ElaToast::ElaToast(ToastType type, const QString &text, int displayMsec, QWidget } move(targetPos); - // Opacity effect and animation - QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this); - opacityEffect->setOpacity(0); - setGraphicsEffect(opacityEffect); + // 顶层窗口直接用窗口透明度做淡入淡出,避免 QGraphicsOpacityEffect 每帧的离屏合成 + setWindowOpacity(0.0); // Fade in - QPropertyAnimation *fadeIn = new QPropertyAnimation(opacityEffect, "opacity", this); + QPropertyAnimation *fadeIn = new QPropertyAnimation(this, "windowOpacity", this); fadeIn->setDuration(200); fadeIn->setStartValue(0.0); fadeIn->setEndValue(1.0); fadeIn->setEasingCurve(QEasingCurve::OutCubic); // Fade out - QPropertyAnimation *fadeOut = new QPropertyAnimation(opacityEffect, "opacity", this); + QPropertyAnimation *fadeOut = new QPropertyAnimation(this, "windowOpacity", this); fadeOut->setDuration(300); fadeOut->setStartValue(1.0); fadeOut->setEndValue(0.0); diff --git a/ElaWidgetTools/private/ElaFlyoutPrivate.h b/ElaWidgetTools/private/ElaFlyoutPrivate.h index 3ff8bba..668557c 100644 --- a/ElaWidgetTools/private/ElaFlyoutPrivate.h +++ b/ElaWidgetTools/private/ElaFlyoutPrivate.h @@ -6,7 +6,6 @@ #include "ElaDef.h" class ElaFlyout; class QVBoxLayout; -class QGraphicsOpacityEffect; class ElaFlyoutPrivate : public QObject { Q_OBJECT @@ -26,7 +25,6 @@ class ElaFlyoutPrivate : public QObject private: QVBoxLayout* _mainLayout{nullptr}; QWidget* _contentWidget{nullptr}; - QGraphicsOpacityEffect* _opacityEffect{nullptr}; ElaThemeType::ThemeMode _themeMode; #if defined(Q_OS_WIN) && QT_VERSION == QT_VERSION_CHECK(6, 11, 0) int _shadowBorderWidth{0}; diff --git a/ElaWidgetTools/private/ElaPopconfirmPrivate.h b/ElaWidgetTools/private/ElaPopconfirmPrivate.h index 5495c2d..f8e2908 100644 --- a/ElaWidgetTools/private/ElaPopconfirmPrivate.h +++ b/ElaWidgetTools/private/ElaPopconfirmPrivate.h @@ -5,7 +5,6 @@ #include "ElaDef.h" class QVBoxLayout; -class QGraphicsOpacityEffect; class ElaPushButton; class ElaPopconfirm; class ElaPopconfirmPrivate : public QObject @@ -29,7 +28,6 @@ class ElaPopconfirmPrivate : public QObject private: ElaThemeType::ThemeMode _themeMode; QVBoxLayout* _mainLayout{nullptr}; - QGraphicsOpacityEffect* _opacityEffect{nullptr}; ElaPushButton* _confirmButton{nullptr}; ElaPushButton* _cancelButton{nullptr}; int _shadowBorderWidth{8}; From be0e2bd5e9954f7365ccb17ef260b8e4879ac2a4 Mon Sep 17 00:00:00 2001 From: CyberWon Date: Sat, 29 Aug 2026 21:43:03 +0800 Subject: [PATCH 15/17] chore(include): drop unused QGraphicsOpacityEffect includes ElaSplashScreen already animates windowOpacity directly; the effect header was never used in these three translation units. --- ElaWidgetTools/ElaSplashScreen.cpp | 1 - ElaWidgetTools/private/ElaMessageBarPrivate.cpp | 1 - ElaWidgetTools/private/ElaSplashScreenPrivate.cpp | 1 - 3 files changed, 3 deletions(-) diff --git a/ElaWidgetTools/ElaSplashScreen.cpp b/ElaWidgetTools/ElaSplashScreen.cpp index 504051a..6162bbe 100644 --- a/ElaWidgetTools/ElaSplashScreen.cpp +++ b/ElaWidgetTools/ElaSplashScreen.cpp @@ -1,7 +1,6 @@ #include "ElaSplashScreen.h" #include -#include #include #include #include diff --git a/ElaWidgetTools/private/ElaMessageBarPrivate.cpp b/ElaWidgetTools/private/ElaMessageBarPrivate.cpp index ee57dec..84540ed 100755 --- a/ElaWidgetTools/private/ElaMessageBarPrivate.cpp +++ b/ElaWidgetTools/private/ElaMessageBarPrivate.cpp @@ -4,7 +4,6 @@ #include "ElaMessageBar.h" #include #include -#include #include #include #include diff --git a/ElaWidgetTools/private/ElaSplashScreenPrivate.cpp b/ElaWidgetTools/private/ElaSplashScreenPrivate.cpp index b5db746..eda6a62 100644 --- a/ElaWidgetTools/private/ElaSplashScreenPrivate.cpp +++ b/ElaWidgetTools/private/ElaSplashScreenPrivate.cpp @@ -1,6 +1,5 @@ #include "ElaSplashScreenPrivate.h" -#include #include #include "ElaSplashScreen.h" From e0391e1805d2c63a005b710b961c57623f48930f Mon Sep 17 00:00:00 2001 From: CyberWon Date: Wed, 9 Sep 2026 11:05:09 +0800 Subject: [PATCH 16/17] fix(appbar): keep native NSWindow behaviors on macOS for real maximize On macOS, ElaAppBar applied Qt::FramelessWindowHint, which strips NSWindowStyleMaskTitled from the NSWindow. showMaximized() then degenerates into a plain geometry stretch filling the screen instead of a native zoom, losing restore geometry, fullscreen/split-view and Mission Control behaviors. Instead, keep the window titled and hide the title bar appearance via Cocoa APIs (titlebarAppearsTransparent, NSWindowTitleHidden, NSWindowStyleMaskFullSizeContentView, hide traffic-light buttons) so the system handles maximize/restore natively. Also sync the maximize button icon on macOS via QEvent::WindowStateChange (previously only synced on Windows through WM_SIZE). Fixes #9 --- ElaWidgetTools/ElaAppBar.cpp | 35 +++++++++++++++++ ElaWidgetTools/private/ElaAppBarPrivate.cpp | 7 ++++ ElaWidgetTools/private/ElaCocoaWindowHelper.h | 27 +++++++++++++ .../private/ElaCocoaWindowHelper.mm | 39 +++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 ElaWidgetTools/private/ElaCocoaWindowHelper.h create mode 100644 ElaWidgetTools/private/ElaCocoaWindowHelper.mm diff --git a/ElaWidgetTools/ElaAppBar.cpp b/ElaWidgetTools/ElaAppBar.cpp index 4b869a2..ce60d9a 100755 --- a/ElaWidgetTools/ElaAppBar.cpp +++ b/ElaWidgetTools/ElaAppBar.cpp @@ -25,6 +25,9 @@ #include "ElaIconButton.h" #include "ElaTheme.h" #include "private/ElaAppBarPrivate.h" +#ifdef Q_OS_MACOS +#include "private/ElaCocoaWindowHelper.h" +#endif Q_PROPERTY_CREATE_Q_CPP(ElaAppBar, bool, IsStayTop) Q_PROPERTY_CREATE_Q_CPP(ElaAppBar, bool, IsDefaultClosed) Q_PROPERTY_CREATE_Q_CPP(ElaAppBar, bool, IsOnlyAllowMinAndClose) @@ -54,8 +57,15 @@ ElaAppBar::ElaAppBar(QWidget* parent) #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 3) && QT_VERSION <= QT_VERSION_CHECK(6, 6, 1)) window()->setWindowFlags((window()->windowFlags()) | Qt::WindowMinimizeButtonHint | Qt::FramelessWindowHint); #endif +#else +#ifdef Q_OS_MACOS + // macOS: 不使用 Qt::FramelessWindowHint。它会移除 NSWindow 的 Titled mask, + // 导致 showMaximized() 退化为原地拉伸铺满屏幕而非原生最大化。 + // 这里保留原生标题栏,仅隐藏其外观,从而保住原生最大化/全屏/分屏行为(Issue #9)。 + // 无边框外观在窗口 Show 事件中通过 Cocoa API 应用,见 eventFilter。 #else window()->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint | Qt::WindowFullscreenButtonHint | Qt::WindowSystemMenuHint); +#endif #endif setMouseTracking(true); setObjectName("ElaAppBar"); @@ -333,6 +343,15 @@ void ElaAppBar::setIsFixedSize(bool isFixedSize) } #else bool isVisible = window()->isVisible(); +#ifdef Q_OS_MACOS + window()->setWindowFlag(Qt::WindowMaximizeButtonHint, !isFixedSize); + if (isVisible) + { + window()->show(); + } + // setWindowFlag 会重建原生窗口,重新应用无边框外观设置 + ElaCocoaWindowHelper::setupFramelessWindow(window()); +#else window()->setWindowFlags((window()->windowFlags()) | Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); if (!isFixedSize) { @@ -342,6 +361,7 @@ void ElaAppBar::setIsFixedSize(bool isFixedSize) { window()->show(); } +#endif #endif Q_EMIT pIsFixedSizeChanged(); } @@ -866,6 +886,21 @@ bool ElaAppBar::eventFilter(QObject* obj, QEvent* event) break; } #endif +#ifdef Q_OS_MACOS + case QEvent::Show: + { + // 通过 Cocoa API 应用无边框外观,保留原生窗口系统行为(Issue #9) + ElaCocoaWindowHelper::setupFramelessWindow(window()); + break; + } +#endif + case QEvent::WindowStateChange: + { + // 同步最大化/还原图标状态(Windows 由 nativeProc 的 WM_SIZE 同步, + // macOS 等平台在此同步,修复原生最大化后图标不失步) + d->_changeMaxButtonAwesome(window()->isMaximized()); + break; + } case QEvent::Close: { QCloseEvent* closeEvent = dynamic_cast(event); diff --git a/ElaWidgetTools/private/ElaAppBarPrivate.cpp b/ElaWidgetTools/private/ElaAppBarPrivate.cpp index 1bf6e8d..2521969 100755 --- a/ElaWidgetTools/private/ElaAppBarPrivate.cpp +++ b/ElaWidgetTools/private/ElaAppBarPrivate.cpp @@ -8,6 +8,9 @@ #include "ElaIconButton.h" #include "ElaNavigationBar.h" #include "ElaText.h" +#ifdef Q_OS_MACOS +#include "private/ElaCocoaWindowHelper.h" +#endif #include #include @@ -86,6 +89,10 @@ void ElaAppBarPrivate::onStayTopButtonClicked() { q->window()->setWindowFlag(Qt::WindowStaysOnTopHint, _pIsStayTop); } +#ifdef Q_OS_MACOS + // setWindowFlag 可能重建原生窗口,重新应用无边框外观设置 + ElaCocoaWindowHelper::setupFramelessWindow(q->window()); +#endif #endif _stayTopButton->setIsSelected(_pIsStayTop); diff --git a/ElaWidgetTools/private/ElaCocoaWindowHelper.h b/ElaWidgetTools/private/ElaCocoaWindowHelper.h new file mode 100644 index 0000000..6bd5719 --- /dev/null +++ b/ElaWidgetTools/private/ElaCocoaWindowHelper.h @@ -0,0 +1,27 @@ +#ifndef ELACOCAOWINDOWHELPER_H +#define ELACOCAOWINDOWHELPER_H + +#include + +#ifdef Q_OS_MACOS + +class QWidget; + +class ElaCocoaWindowHelper +{ +public: + /** + * @brief 将窗口设置为“原生无边框”样式。 + * + * 与 Qt::FramelessWindowHint 不同,本函数保留 NSWindow 的原生标题栏 + * (Titled style mask),仅将其透明化并隐藏标题文字,同时开启 + * FullSizeContentView 让内容延伸到标题栏区域。 + * + * 这样窗口仍然拥有完整的原生系统行为:原生最大化(zoom)/还原、 + * 全屏、拖拽到屏幕边缘分屏、Mission Control 等。 + */ + static void setupFramelessWindow(QWidget* window); +}; + +#endif // Q_OS_MACOS +#endif // ELACOCAOWINDOWHELPER_H diff --git a/ElaWidgetTools/private/ElaCocoaWindowHelper.mm b/ElaWidgetTools/private/ElaCocoaWindowHelper.mm new file mode 100644 index 0000000..3b706b8 --- /dev/null +++ b/ElaWidgetTools/private/ElaCocoaWindowHelper.mm @@ -0,0 +1,39 @@ +#include "ElaCocoaWindowHelper.h" + +#ifdef Q_OS_MACOS + +#include + +#import + +void ElaCocoaWindowHelper::setupFramelessWindow(QWidget* window) +{ + if (!window) + { + return; + } + NSView* view = reinterpret_cast(window->winId()); + if (!view) + { + return; + } + NSWindow* nsWindow = [view window]; + if (!nsWindow) + { + return; + } + // 保留 Titled mask,仅让内容视图延伸到标题栏下并隐藏标题栏外观。 + // 移除 Titled(即 Qt::FramelessWindowHint 的效果)会让 NSWindow 丢失 + // 原生最大化(zoom)/还原、全屏、分屏等系统行为。 + nsWindow.styleMask |= NSWindowStyleMaskFullSizeContentView; + nsWindow.titlebarAppearsTransparent = YES; + nsWindow.titleVisibility = NSWindowTitleHidden; + // 窗口拖拽由 ElaAppBar 自行处理,避免背景拖拽与拖拽逻辑冲突。 + nsWindow.movableByWindowBackground = NO; + // 隐藏原生红绿灯按钮(ElaAppBar 自绘最小化/最大化/关闭按钮)。 + [[nsWindow standardWindowButton:NSWindowCloseButton] setHidden:YES]; + [[nsWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; + [[nsWindow standardWindowButton:NSWindowZoomButton] setHidden:YES]; +} + +#endif // Q_OS_MACOS From ee4415df58418ff5cb7f0929a8553db556d366ad Mon Sep 17 00:00:00 2001 From: CyberWon Date: Wed, 9 Sep 2026 11:54:59 +0800 Subject: [PATCH 17/17] ci(example): normalize Windows paths in windeployqt install script QT_SDK_DIR / CMAKE_INSTALL_PREFIX may contain backslashes (e.g. D:\a\...\bin from install-qt-action). Interpolating them verbatim into install(CODE) writes invalid escape sequences into the generated cmake_install.cmake, failing 'cmake --install' with "Invalid character escape '\a'". Normalize WINDEPLOYQT_EXECUTABLE, QT_SDK_DIR and CMAKE_INSTALL_PREFIX with file(TO_CMAKE_PATH) before embedding them into the install script. --- ElaWidgetToolsExample/CMakeLists.txt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ElaWidgetToolsExample/CMakeLists.txt b/ElaWidgetToolsExample/CMakeLists.txt index e299b90..196ca1f 100755 --- a/ElaWidgetToolsExample/CMakeLists.txt +++ b/ElaWidgetToolsExample/CMakeLists.txt @@ -105,13 +105,20 @@ if (MSVC) endif () find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${QT_SDK_DIR}/bin") if (WIN32) + # 统一转换为正斜杠路径。QT_SDK_DIR 等可能携带 Windows 反斜杠 + # (如 D:\a\...\bin),直接内插进 install(CODE) 生成的 + # cmake_install.cmake 会被当作非法转义序列(Invalid character escape '\a'), + # 导致 cmake --install 报 Syntax error。 + file(TO_CMAKE_PATH "${WINDEPLOYQT_EXECUTABLE}" WINDEPLOYQT_EXECUTABLE) + file(TO_CMAKE_PATH "${QT_SDK_DIR}" QT_SDK_DIR_NORMALIZED) + file(TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}" INSTALL_PREFIX_NORMALIZED) install(CODE " - if(EXISTS \"${WINDEPLOYQT_EXECUTABLE}\" AND NOT EXISTS \"${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/platforms\") + if(EXISTS \"${WINDEPLOYQT_EXECUTABLE}\" AND NOT EXISTS \"${INSTALL_PREFIX_NORMALIZED}/${PROJECT_NAME}/platforms\") execute_process( - COMMAND \"${CMAKE_COMMAND}\" -E env PATH=\"${QT_SDK_DIR}/bin\" + COMMAND \"${CMAKE_COMMAND}\" -E env PATH=\"${QT_SDK_DIR_NORMALIZED}/bin\" \"${WINDEPLOYQT_EXECUTABLE}\" \"$/$\" - --dir \"${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}\" + --dir \"${INSTALL_PREFIX_NORMALIZED}/${PROJECT_NAME}\" --no-compiler-runtime --no-system-d3d-compiler --no-opengl-sw