Skip to content

Commit 2fd4f63

Browse files
committed
Fix the theme to other color when the system theme is dark. See: KangLin/RabbitRemoteControl#68
1 parent 0fb8dfd commit 2fd4f63

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- QTextBrowser replace QTextEdit
88
- Package MacOS to end
99
- Fix Unable to change "Icon theme" after disabling it. See: https://github.com/KangLin/RabbitRemoteControl/issues/69
10+
- Fix the theme to other color when the system theme is dark. See: https://github.com/KangLin/RabbitRemoteControl/pull/68
1011

1112
### Version v2.3.3
1213
- CAdminAuthorization: Add pkexec in linux

ChangeLog_zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- 用 QTextBrowser 替换 QTextEdit
88
- 完成 MacOS 打包
99
- 修复禁用“图标主题”后,不能选择的错误。 See: https://github.com/KangLin/RabbitRemoteControl/issues/69
10+
- 修复在黑暗主题下系统托盘图标为白色. See: https://github.com/KangLin/RabbitRemoteControl/pull/68
1011

1112
### 版本: v2.3.3
1213
- 修改在 linux 下用管理员权限启动程序。增加 pkexec

Src/Style/FrmStyle.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <QSettings>
1010
#include <QLoggingCategory>
11+
#include <QPalette>
12+
#include <QIcon>
13+
#include <QStyleHints>
14+
#include <QApplication>
15+
#include <QMessageBox>
1116

1217
#include "FrmStyle.h"
1318
#include "ui_FrmStyle.h"
@@ -113,9 +118,27 @@ CFrmStyle::~CFrmStyle()
113118

114119
void CFrmStyle::on_pbOK_clicked()
115120
{
121+
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
122+
const auto colorScheme = QGuiApplication::styleHints()->colorScheme();
123+
QRegularExpression re("black|[Dd]ark");
124+
QRegularExpressionMatch match = re.match(ui->cbIconTheme->currentText());
125+
if (Qt::ColorScheme::Dark == colorScheme && match.hasMatch()) {
126+
QString szThemeName;
127+
QString szInfo = tr("Current system theme is dark, current select theme is ")
128+
+ QString("\"") + ui->cbIconTheme->currentText() + "\". \n";
129+
szInfo += tr("it's almost impossible to find the icon because it matches the color.") + "\n"
130+
+ tr("Are you sure you want to modify it?");
131+
auto ret = QMessageBox::information(nullptr, tr("Change theme"),
132+
szInfo,
133+
QMessageBox::Ok | QMessageBox::No,
134+
QMessageBox::No);
135+
if(QMessageBox::No == ret)
136+
return;
137+
}
138+
#endif // QT_VERSION
139+
116140
QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
117141
QSettings::IniFormat);
118-
119142
bool bIconTheme = ui->gpIconTheme->isChecked();
120143
set.setValue("Style/Icon/Theme/Enable", bIconTheme);
121144
if(bIconTheme) {

Src/Style/FrmStyle.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ private Q_SLOTS:
9999
void on_pbCancel_clicked();
100100
void on_pbBrowse_clicked();
101101
void on_pbDefault_clicked();
102-
103102
void on_gpIconTheme_clicked();
104-
103+
105104
private:
106105
Ui::CFrmStyle *ui;
107106
};

Src/Style/Style.cpp

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <QColor>
1414
#include <QPalette>
1515
#include <QIcon>
16+
#include <QStyleHints>
17+
#include <QMessageBox>
1618

1719
namespace RabbitCommon {
1820

@@ -48,6 +50,14 @@ CStyle::CStyle(QObject *parent) : QObject(parent)
4850
<< "Fallback search paths:" << QIcon::fallbackSearchPaths() << "\n"
4951
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
5052
;
53+
54+
bool check = false;
55+
if(QApplication::instance()) {
56+
check = connect(QApplication::styleHints(),
57+
SIGNAL(colorSchemeChanged(Qt::ColorScheme)),
58+
this, SLOT(slotColorSchemeChanged(Qt::ColorScheme)));
59+
Q_ASSERT(check);
60+
}
5161
}
5262

5363
CStyle* CStyle::Instance()
@@ -79,8 +89,27 @@ int CStyle::LoadStyle()
7989
QString szThemeName = QIcon::themeName();
8090
if(szThemeName.isEmpty())
8191
szThemeName = m_szDefaultIconTheme;
82-
QIcon::setThemeName(set.value("Style/Icon/Theme",
83-
szThemeName).toString());
92+
szThemeName = set.value("Style/Icon/Theme", szThemeName).toString();
93+
94+
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
95+
const auto scheme = QGuiApplication::styleHints()->colorScheme();
96+
//qDebug(log) << "Scheme:" << scheme;
97+
QRegularExpression re("black|[Dd]ark");
98+
QRegularExpressionMatch match = re.match(szThemeName);
99+
if (Qt::ColorScheme::Dark == scheme && match.hasMatch()) {
100+
QString szInfo = tr("Current system theme is dark, current theme is ") + QString("\"") + szThemeName + "\". ";
101+
szInfo += tr("it's almost impossible to find the icon because it matches the color.");
102+
#if defined(Q_OS_WIN)
103+
szThemeName = "rabbit-green";
104+
#else
105+
szThemeName = "rabbit-white";
106+
#endif
107+
szInfo += " " + tr("change to ") + szThemeName;
108+
qInfo(log) << szInfo;
109+
}
110+
#endif // QT_VERSION
111+
112+
QIcon::setThemeName(szThemeName);
84113

85114
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
86115
QIcon::setFallbackSearchPaths(
@@ -207,4 +236,32 @@ QString CStyle::GetStyleFile()
207236
return m_szFile;
208237
}
209238

239+
void CStyle::slotColorSchemeChanged(Qt::ColorScheme colorScheme)
240+
{
241+
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
242+
QRegularExpression re("black|[Dd]ark");
243+
QRegularExpressionMatch match = re.match(QIcon::themeName());
244+
if (Qt::ColorScheme::Dark == colorScheme && match.hasMatch()) {
245+
QString szThemeName;
246+
QString szInfo = tr("Current system theme is dark, current theme is ") + QString("\"") + QIcon::themeName() + "\". ";
247+
szInfo += tr("it's almost impossible to find the icon because it matches the color.");
248+
#if defined(Q_OS_WIN)
249+
szThemeName = "rabbit-green";
250+
#else
251+
szThemeName = "rabbit-white";
252+
#endif
253+
szInfo += " " + tr("change to ") + "\"" + szThemeName + "\"?";
254+
auto ret = QMessageBox::information(nullptr, tr("Change theme"), szInfo,
255+
QMessageBox::Ok | QMessageBox::No,
256+
QMessageBox::Ok);
257+
if(QMessageBox::Ok == ret) {
258+
QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
259+
QSettings::IniFormat);
260+
QIcon::setThemeName(szThemeName);
261+
szThemeName = set.value("Style/Icon/Theme", szThemeName).toString();
262+
}
263+
}
264+
#endif // QT_VERSION
265+
}
266+
210267
} //namespace RabbitCommon

Src/Style/Style.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class CStyle : public QObject
4242
void SetFile(const QString& file);
4343
QString GetStyleFile();
4444

45+
private Q_SLOTS:
46+
void slotColorSchemeChanged(Qt::ColorScheme colorScheme);
47+
4548
private:
4649
CStyle(QObject *parent = nullptr);
4750

0 commit comments

Comments
 (0)