Skip to content

Commit 9a6b4d7

Browse files
committed
Fixed: Title bar theme issue in Thor Mode
Log: Optimized the logic for triggering updates pms: bug-355415
1 parent 73d83c1 commit 9a6b4d7

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/views/titlebar.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
#include "utils.h"
88

99
#include <DApplication>
10+
#include <DGuiApplicationHelper>
1011
#include <DIconButton>
1112

13+
#include <QGuiApplication>
1214
#include <QIcon>
15+
#include <QPainter>
1316
#include <QLabel>
1417
#include <QDebug>
1518
#include <QMouseEvent>
@@ -37,6 +40,17 @@ TitleBar::TitleBar(QWidget *parent) : QWidget(parent), m_layout(new QHBoxLayout(
3740
// this->setPalette(palette);
3841
this->setBackgroundRole(DPalette::Base);
3942
this->setAutoFillBackground(true);
43+
44+
// 监听 DTK 主题类型变化,触发重绘(切换深/浅色时)
45+
QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this](){
46+
update();
47+
});
48+
49+
// 监听 QApplication 全局调色板变化:DTK 通过 D-Bus 异步完成 palette 初始化时
50+
QObject::connect(static_cast<QGuiApplication *>(qApp), &QGuiApplication::paletteChanged,
51+
this, [this](const QPalette &) {
52+
update();
53+
});
4054
/********************* Modify by m000714 daizhengwen End ************************/
4155
m_layout->setContentsMargins(0, 0, 0, 0);
4256

@@ -78,6 +92,25 @@ void TitleBar::setVerResized(bool resized)
7892
}
7993

8094

95+
void TitleBar::paintEvent(QPaintEvent *event)
96+
{
97+
Q_UNUSED(event)
98+
99+
QPainter p(this);
100+
const QColor bgColor = DGuiApplicationHelper::instance()->applicationPalette().color(QPalette::Base);
101+
p.fillRect(rect(), bgColor);
102+
}
103+
104+
void TitleBar::changeEvent(QEvent *event)
105+
{
106+
// 捕获系统/应用 palette 变化事件,触发重绘以保持颜色正确
107+
if (event->type() == QEvent::ApplicationPaletteChange
108+
|| event->type() == QEvent::PaletteChange) {
109+
update();
110+
}
111+
QWidget::changeEvent(event);
112+
}
113+
81114
void TitleBar::mousePressEvent(QMouseEvent *event)
82115
{
83116
// qCDebug(views) << "Enter TitleBar::mousePressEvent";

src/views/titlebar.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ class TitleBar : public QWidget
5050
int m_verResizedCurOff = 0;//当前垂直偏移量
5151
bool m_verResizedEnabled = false;//是否手动resize当前界面
5252
protected:
53-
void mousePressEvent(QMouseEvent *event);
54-
void mouseMoveEvent(QMouseEvent *event);
53+
void paintEvent(QPaintEvent *event) override;
54+
void changeEvent(QEvent *event) override;
55+
void mousePressEvent(QMouseEvent *event) override;
56+
void mouseMoveEvent(QMouseEvent *event) override;
5557
};
5658

5759
#endif // TITLEBAR_H

0 commit comments

Comments
 (0)