Fixed: Title bar theme issue in Thor Mode#527
Conversation
Log: Optimized the logic for triggering updates pms: bug-355415
deepin pr auto review这段代码主要实现了标题栏(TitleBar)在 DTK(Deepin Tool Kit)环境下对主题切换和调色板变化的响应,通过重绘来确保背景色正确。以下是对这段代码的详细审查,包括语法逻辑、代码质量、性能和安全性方面的改进意见: 1. 语法逻辑与代码结构优点:
改进建议:
2. 代码质量优点:
改进建议:
3. 代码性能改进建议:
4. 代码安全改进建议:
总结修改建议综合以上分析,以下是优化后的代码片段建议: titlebar.cpp 修改建议: // ... includes ...
TitleBar::TitleBar(QWidget *parent) : QWidget(parent), m_layout(new QHBoxLayout(this))
{
// ... 初始化代码 ...
// 初始化背景色缓存
updateBackgroundColor();
// 监听 DTK 主题类型变化
QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this](){
updateBackgroundColor();
update();
});
// 监听 QApplication 全局调色板变化
// 使用 qobject_cast 进行更安全的类型转换
if (auto *guiApp = qobject_cast<QGuiApplication *>(qApp)) {
QObject::connect(guiApp, &QGuiApplication::paletteChanged, this, [this](const QPalette &) {
updateBackgroundColor();
update();
});
}
// ...
}
void TitleBar::updateBackgroundColor()
{
if (auto *helper = DGuiApplicationHelper::instance()) {
m_bgColor = helper->applicationPalette().color(QPalette::Base);
}
}
void TitleBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter p(this);
// 使用成员变量中缓存的背景色,避免频繁查询
p.fillRect(rect(), m_bgColor);
}
void TitleBar::changeEvent(QEvent *event)
{
// 只需要捕获 PaletteChange 即可,ApplicationPaletteChange 通常包含在其中或由信号处理
if (event->type() == QEvent::PaletteChange) {
updateBackgroundColor();
update();
}
QWidget::changeEvent(event);
}titlebar.h 修改建议: class TitleBar : public QWidget
{
Q_OBJECT
public:
// ...
protected:
void paintEvent(QPaintEvent *event) override;
void changeEvent(QEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private:
void updateBackgroundColor(); // 新增辅助函数
// ... 其他成员变量 ...
QColor m_bgColor; // 新增缓存背景色
};这些改进主要提升了代码的类型安全性、运行时性能(通过缓存减少调用开销)以及逻辑的清晰度。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: JWWTSL, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
TAG Bot New tag: 6.5.34 |
|
TAG Bot New tag: 6.5.35 |
Log: Optimized the logic for triggering updates
pms: bug-355415