From d200dd3cd0d608b527927c2542115f7ec3914704 Mon Sep 17 00:00:00 2001 From: swq Date: Thu, 29 Aug 2024 14:49:16 +0800 Subject: [PATCH] fix: Modify the taskbar preview close button to a perfect circle log: Change the toolbutton control to a diconbutton control and change the color in the Hover event issue: https://github.com/linuxdeepin/developer-center/issues/9813 --- panels/dock/taskmanager/x11preview.cpp | 13 ++++++--- panels/dock/taskmanager/x11preview.h | 38 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/panels/dock/taskmanager/x11preview.cpp b/panels/dock/taskmanager/x11preview.cpp index 5f7d8a41b..6611fc1a6 100644 --- a/panels/dock/taskmanager/x11preview.cpp +++ b/panels/dock/taskmanager/x11preview.cpp @@ -347,7 +347,7 @@ class AppItemWindowDeletegate : public QAbstractItemDelegate { auto closeButton = new DIconButton(parent); closeButton->setIcon(DDciIcon::fromTheme("close")); - + closeButton->setEnabledCircle(true); QPalette palette = closeButton->palette(); QColor lightColor = palette.color(QPalette::Normal, QPalette::Light); @@ -409,7 +409,7 @@ X11WindowPreviewContainer::X11WindowPreviewContainer(X11WindowMonitor* monitor, connect(m_hideTimer, &QTimer::timeout, this, &X11WindowPreviewContainer::callHide); - connect(m_closeAllButton, &DToolButton::clicked, this, [this](){ + connect(m_closeAllButton, &DIconButton::clicked, this, [this](){ if (m_previewItem.isNull()) return; for (auto window : m_previewItem->getAppendWindows()) { window->close(); @@ -575,10 +575,15 @@ void X11WindowPreviewContainer::initUI() m_previewIcon->setFixedSize(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT); m_previewIcon->setScaledContents(true); - m_closeAllButton = new DToolButton(this); + m_closeAllButton = new DIconButton(this); + m_closeAllButton->setIconSize(QSize(16, 16)); - m_closeAllButton->setIcon(QIcon::fromTheme("close")); + m_closeAllButton->setIcon(DDciIcon::fromTheme("close")); m_closeAllButton->setFixedSize(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT); + m_closeAllButton->setEnabledCircle(true); + + DIconButtonHoverFilter *hoverHandler = new DIconButtonHoverFilter(this); + m_closeAllButton->installEventFilter(hoverHandler); m_previewIcon->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); m_previewTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); diff --git a/panels/dock/taskmanager/x11preview.h b/panels/dock/taskmanager/x11preview.h index 81f74d56b..3cba3ac42 100644 --- a/panels/dock/taskmanager/x11preview.h +++ b/panels/dock/taskmanager/x11preview.h @@ -15,6 +15,8 @@ #include #include #include +#include +#include DWIDGET_USE_NAMESPACE @@ -24,6 +26,40 @@ class AppItemWindowModel; class AppItemWindowDeletegate; class PreviewsListView; +class DIconButtonHoverFilter : public QObject +{ + Q_OBJECT public: + explicit DIconButtonHoverFilter(QObject* parent = nullptr) : QObject(parent) + { + } + + void setButtonColor(QObject* watched, QColor color) + { + DIconButton* button = qobject_cast(watched); + if (button) { + QPalette pt = button->palette(); + pt.setColor(QPalette::Button, color); + button->setPalette(pt); + } + } + +protected: + bool eventFilter(QObject* watched, QEvent* event) override + { + if (event->type() == QEvent::HoverEnter) { + // 处理 HoverEnter 事件 + QColor color = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType ? Qt::white : Qt::black; + color.setAlphaF(0.1); + setButtonColor(watched, color); + } else if (event->type() == QEvent::HoverLeave || event->type() == QEvent::Show) { + // 处理 HoverLeave 和 show事件 + setButtonColor(watched, Qt::transparent); + } + return QObject::eventFilter(watched, event); + } +}; + + class X11WindowPreviewContainer: public DBlurEffectWidget { Q_OBJECT @@ -66,7 +102,7 @@ private Q_SLOTS: QLabel* m_previewIcon; DLabel* m_previewTitle; - DToolButton* m_closeAllButton; + DIconButton* m_closeAllButton; QTimer* m_hideTimer;