Skip to content

Commit 65d6ecc

Browse files
committed
Qt: Don't use style hints as source of truth for dark mode
Apparently this doesn't work on Linux. Typical.
1 parent 598d393 commit 65d6ecc

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/duckstation-qt/qtthemes.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QtWidgets/QStyleFactory>
1616

1717
namespace QtHost {
18+
static void SetThemeAttributes(bool is_variable_color_theme, bool is_dark_theme);
1819
static void SetStyleFromSettings();
1920

2021
namespace {
@@ -23,6 +24,8 @@ struct State
2324
std::string current_theme_name;
2425
QString unthemed_style_name;
2526
QPalette unthemed_palette;
27+
bool is_variable_color_theme = false;
28+
bool is_dark_theme = false;
2629
bool unthemed_style_name_set = false;
2730
};
2831
} // namespace
@@ -49,31 +52,43 @@ void QtHost::UpdateApplicationTheme()
4952
SetIconThemeFromStyle();
5053
}
5154

55+
void QtHost::SetThemeAttributes(bool is_variable_color_theme, bool is_dark_theme)
56+
{
57+
s_state.is_variable_color_theme = is_variable_color_theme;
58+
s_state.is_dark_theme = is_dark_theme;
59+
60+
if (is_variable_color_theme)
61+
qApp->styleHints()->unsetColorScheme();
62+
else
63+
qApp->styleHints()->setColorScheme(is_dark_theme ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light);
64+
}
65+
5266
void QtHost::SetStyleFromSettings()
5367
{
5468
const TinyString theme = Host::GetBaseTinyStringSettingValue("UI", "Theme", QtHost::GetDefaultThemeName());
5569

5670
if (theme == "qdarkstyle")
5771
{
72+
SetThemeAttributes(false, true);
5873
qApp->setStyle(s_state.unthemed_style_name);
5974
qApp->setPalette(s_state.unthemed_palette);
6075
qApp->setStyleSheet(QString());
61-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
6276

6377
QFile f(QStringLiteral(":qdarkstyle/style.qss"));
6478
if (f.open(QFile::ReadOnly | QFile::Text))
6579
qApp->setStyleSheet(f.readAll());
6680
}
6781
else if (theme == "fusion")
6882
{
83+
SetThemeAttributes(true, false);
6984
qApp->setStyle(QStyleFactory::create("Fusion"));
7085
qApp->setPalette(s_state.unthemed_palette);
7186
qApp->setStyleSheet(QString());
72-
qApp->styleHints()->unsetColorScheme();
7387
}
7488
else if (theme == "darkfusion")
7589
{
7690
// adapted from https://gist.github.com/QuantumCD/6245215
91+
SetThemeAttributes(false, true);
7792
qApp->setStyle(QStyleFactory::create("Fusion"));
7893

7994
static constexpr QColor lighterGray(75, 75, 75);
@@ -105,11 +120,11 @@ void QtHost::SetStyleFromSettings()
105120

106121
qApp->setPalette(darkPalette);
107122
qApp->setStyleSheet(QString());
108-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
109123
}
110124
else if (theme == "darkfusionblue")
111125
{
112126
// adapted from https://gist.github.com/QuantumCD/6245215
127+
SetThemeAttributes(false, true);
113128
qApp->setStyle(QStyleFactory::create("Fusion"));
114129

115130
// static constexpr QColor lighterGray(75, 75, 75);
@@ -142,10 +157,10 @@ void QtHost::SetStyleFromSettings()
142157

143158
qApp->setPalette(darkPalette);
144159
qApp->setStyleSheet(QString());
145-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
146160
}
147161
else if (theme == "darkerfusion")
148162
{
163+
SetThemeAttributes(false, true);
149164
qApp->setStyle(QStyleFactory::create("Fusion"));
150165

151166
static constexpr QColor window_color(36, 36, 36);
@@ -181,7 +196,6 @@ void QtHost::SetStyleFromSettings()
181196

182197
qApp->setPalette(darkPalette);
183198
qApp->setStyleSheet(QString());
184-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
185199

186200
// menus are by far the ugliest part of fusion, so we style them manually
187201
const QString stylesheet = QStringLiteral(R"(
@@ -228,13 +242,13 @@ QToolBar {
228242
)");
229243

230244
qApp->setStyleSheet(stylesheet);
231-
232245
}
233246
else if (theme == "cobaltsky")
234247
{
235248
// Custom palette by KamFretoZ, A soothing deep royal blue
236249
// that are meant to be easy on the eyes as the main color.
237250
// Alternative dark theme.
251+
SetThemeAttributes(false, true);
238252
qApp->setStyle(QStyleFactory::create("Fusion"));
239253

240254
static constexpr QColor gray(150, 150, 150);
@@ -266,10 +280,10 @@ QToolBar {
266280

267281
qApp->setPalette(darkPalette);
268282
qApp->setStyleSheet(QString());
269-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
270283
}
271284
else if (theme == "greymatter")
272285
{
286+
SetThemeAttributes(false, true);
273287
qApp->setStyle(QStyleFactory::create("Fusion"));
274288

275289
static constexpr QColor darkGray(46, 52, 64);
@@ -300,12 +314,12 @@ QToolBar {
300314

301315
qApp->setPalette(darkPalette);
302316
qApp->setStyleSheet(QString());
303-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
304317
}
305318
else if (theme == "greengiant")
306319
{
307320
// Custom palette by RedDevilus, Tame (Light/Washed out) Green as main color and Grayish Blue as complimentary.
308321
// Alternative white theme.
322+
SetThemeAttributes(false, false);
309323
qApp->setStyle(QStyleFactory::create("Fusion"));
310324

311325
static constexpr QColor black(25, 25, 25);
@@ -335,10 +349,10 @@ QToolBar {
335349

336350
qApp->setPalette(greenGiantPalette);
337351
qApp->setStyleSheet(QString());
338-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
339352
}
340353
else if (theme == "pinkypals")
341354
{
355+
SetThemeAttributes(false, false);
342356
qApp->setStyle(QStyleFactory::create("Fusion"));
343357

344358
static constexpr QColor black(25, 25, 25);
@@ -369,13 +383,13 @@ QToolBar {
369383

370384
qApp->setPalette(PinkyPalsPalette);
371385
qApp->setStyleSheet(QString());
372-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
373386
}
374387
else if (theme == "AMOLED")
375388
{
376389
// Custom palette by KamFretoZ, A pure concentrated darkness
377390
// of a theme designed for maximum eye comfort and benefits
378391
// OLED screens.
392+
SetThemeAttributes(false, true);
379393
qApp->setStyle(QStyleFactory::create("Fusion"));
380394

381395
static constexpr QColor black(0, 0, 0);
@@ -406,10 +420,10 @@ QToolBar {
406420

407421
qApp->setPalette(AMOLEDPalette);
408422
qApp->setStyleSheet(QString());
409-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
410423
}
411424
else if (theme == "darkruby")
412425
{
426+
SetThemeAttributes(false, true);
413427
qApp->setStyle(QStyleFactory::create("Fusion"));
414428

415429
static constexpr QColor gray(128, 128, 128);
@@ -438,10 +452,10 @@ QToolBar {
438452

439453
qApp->setPalette(darkPalette);
440454
qApp->setStyleSheet(QString());
441-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
442455
}
443456
else if (theme == "purplerain")
444457
{
458+
SetThemeAttributes(false, true);
445459
qApp->setStyle(QStyleFactory::create("Fusion"));
446460

447461
static constexpr QColor darkPurple(73, 41, 121);
@@ -471,29 +485,29 @@ QToolBar {
471485

472486
qApp->setPalette(darkPalette);
473487
qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #505a70; border: 1px solid white; }");
474-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
475488
}
476489
#ifdef _WIN32
477490
else if (theme == "windowsvista")
478491
{
492+
SetThemeAttributes(false, false);
479493
qApp->setStyle(QStyleFactory::create("windowsvista"));
480494
qApp->setPalette(s_state.unthemed_palette);
481495
qApp->setStyleSheet(QString());
482-
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
483496
}
484497
#endif
485498
else
486499
{
500+
SetThemeAttributes(true, false);
487501
qApp->setStyle(s_state.unthemed_style_name);
488502
qApp->setPalette(s_state.unthemed_palette);
489503
qApp->setStyleSheet(QString());
490-
qApp->styleHints()->unsetColorScheme();
491504
}
492505
}
493506

494507
bool QtHost::IsDarkApplicationTheme()
495508
{
496-
return (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark);
509+
return s_state.is_variable_color_theme ? (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark) :
510+
s_state.is_dark_theme;
497511
}
498512

499513
void QtHost::SetIconThemeFromStyle()

0 commit comments

Comments
 (0)