-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy paththemeutils.cpp
More file actions
102 lines (90 loc) · 3.56 KB
/
themeutils.cpp
File metadata and controls
102 lines (90 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* OpenSpeedy - Open Source Game Speed Controller
* Copyright (C) 2025 Game1024
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "themeutils.h"
#include <QStyleFactory>
#include <QPalette>
#include <QColor>
#include <QSettings>
#include <QApplication>
#include <QStyle>
void ThemeUtils::applyTheme(Theme theme)
{
if (theme == System) {
if (isSystemDarkMode()) {
setDarkTheme();
} else {
setLightTheme();
}
} else if (theme == Dark) {
setDarkTheme();
} else {
setLightTheme();
}
}
void ThemeUtils::setDarkTheme()
{
QApplication* app = static_cast<QApplication*>(QApplication::instance());
if (!app) return;
// 深色模式必须使用 Fusion 风格,因为 WindowsVista 风格强制使用浅色控件
app->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
QColor darkColor = QColor(45, 45, 45);
QColor disabledColor = QColor(127, 127, 127);
darkPalette.setColor(QPalette::Window, darkColor);
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(18, 18, 18));
darkPalette.setColor(QPalette::AlternateBase, darkColor);
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Disabled, QPalette::Text, disabledColor);
darkPalette.setColor(QPalette::Button, darkColor);
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::Disabled, QPalette::ButtonText, disabledColor);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
darkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText, disabledColor);
app->setPalette(darkPalette);
app->setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }");
}
void ThemeUtils::setLightTheme()
{
QApplication* app = static_cast<QApplication*>(QApplication::instance());
if (!app) return;
// 浅色模式优先使用 WindowsVista (原生) 风格
QStyle* style = QStyleFactory::create("Fusion");
if (!style) {
style = QStyleFactory::create("Windows");
}
app->setStyle(style);
// 恢复默认调色板
app->setPalette(style->standardPalette());
// 清除全局样式表
app->setStyleSheet("");
}
bool ThemeUtils::isSystemDarkMode()
{
#ifdef Q_OS_WIN
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat);
return settings.value("AppsUseLightTheme", 1).toInt() == 0;
#else
return false;
#endif
}