Skip to content

Commit eeaa282

Browse files
committed
Qt: Set QStyleHints colorScheme property properly
1 parent a159256 commit eeaa282

File tree

7 files changed

+42
-24
lines changed

7 files changed

+42
-24
lines changed

pcsx2-qt/Debugger/DisassemblyView.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "QtHost.h"
1717
#include <QtCore/QPointer>
1818
#include <QtGui/QMouseEvent>
19+
#include <QtGui/QStyleHints>
1920
#include <QtWidgets/QMenu>
2021
#include <QtGui/QClipboard>
2122
#include <QtWidgets/QInputDialog>
@@ -939,7 +940,7 @@ inline QString DisassemblyView::DisassemblyStringFromAddress(u32 address, QFont
939940
QColor DisassemblyView::GetAddressFunctionColor(u32 address)
940941
{
941942
std::array<QColor, 6> colors;
942-
if (QtUtils::IsLightTheme(palette()))
943+
if (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Light)
943944
{
944945
colors = {
945946
QColor::fromRgba(0xFFFA3434),

pcsx2-qt/Debugger/Docking/DropIndicators.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "DropIndicators.h"
55

6+
#include "QtHost.h"
67
#include "QtUtils.h"
78
#include "Debugger/Docking/DockViews.h"
89

@@ -15,13 +16,14 @@
1516
#include <kddockwidgets/qtwidgets/ViewFactory.h>
1617

1718
#include <QtGui/QPainter>
19+
#include <QtGui/QStyleHints>
1820

1921
static std::pair<QColor, QColor> pickNiceColours(const QPalette& palette, bool hovered)
2022
{
2123
QColor fill = palette.highlight().color();
2224
QColor outline = palette.highlight().color();
2325

24-
if (QtUtils::IsLightTheme(palette))
26+
if (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Light)
2527
{
2628
fill = fill.darker(200);
2729
outline = outline.darker(200);
@@ -197,7 +199,7 @@ static const constexpr int INDICATOR_MARGIN = 10;
197199
static bool isWayland()
198200
{
199201
return KDDockWidgets::Core::Platform::instance()->displayType() ==
200-
KDDockWidgets::Core::Platform::DisplayType::Wayland;
202+
KDDockWidgets::Core::Platform::DisplayType::Wayland;
201203
}
202204

203205
static QWidget* parentForIndicatorWindow(KDDockWidgets::Core::ClassicDropIndicatorOverlay* classic_indicators)

pcsx2-qt/LogWindow.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QtCore/QLatin1StringView>
1010
#include <QtCore/QUtf8StringView>
1111
#include <QtGui/QIcon>
12+
#include <QtGui/QStyleHints>
1213
#include <QtWidgets/QMenuBar>
1314
#include <QtWidgets/QScrollBar>
1415

@@ -336,10 +337,10 @@ void LogWindow::appendMessage(quint32 level, quint32 color, const QString& messa
336337
temp_cursor.insertText(qtimestamp);
337338
}
338339

339-
const bool dark = static_cast<u32>(QtHost::IsDarkApplicationTheme());
340+
const u32 palette = static_cast<u32>(qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark);
340341

341342
// message has \n already
342-
format.setForeground(QBrush(qcolors[static_cast<u32>(dark)][color]));
343+
format.setForeground(QBrush(qcolors[palette][color]));
343344
temp_cursor.setCharFormat(format);
344345
temp_cursor.insertText(message);
345346
}

pcsx2-qt/QtHost.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ namespace QtHost
237237
/// Sets application theme according to settings.
238238
void UpdateApplicationTheme();
239239

240-
/// Returns true if the application theme is using dark colours.
241-
bool IsDarkApplicationTheme();
242-
243240
/// Sets the icon theme, based on the current style (light/dark).
244241
void SetIconThemeFromStyle();
245242

pcsx2-qt/QtUtils.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ namespace QtUtils
9090

9191
const int min_column_width = header->minimumSectionSize();
9292
const int scrollbar_width = ((view->verticalScrollBar() && view->verticalScrollBar()->isVisible()) ||
93-
view->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn) ? view->verticalScrollBar()->width() : 0;
93+
view->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn) ?
94+
view->verticalScrollBar()->width() :
95+
0;
9496
int num_flex_items = 0;
9597
int total_width = 0;
9698
int column_index = 0;
@@ -346,11 +348,6 @@ namespace QtUtils
346348
return csv;
347349
}
348350

349-
bool IsLightTheme(const QPalette& palette)
350-
{
351-
return palette.text().color().lightnessF() < 0.5;
352-
}
353-
354351
bool IsCompositorManagerRunning()
355352
{
356353
if (qEnvironmentVariableIsSet("PCSX2_NO_COMPOSITING"))

pcsx2-qt/QtUtils.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ namespace QtUtils
9696
/// Converts an abstract item model to a CSV string.
9797
QString AbstractItemModelToCSV(QAbstractItemModel* model, int role = Qt::DisplayRole, bool useQuotes = false);
9898

99-
// Heuristic to check if the current theme is a light or dark theme.
100-
bool IsLightTheme(const QPalette& palette);
101-
99+
/// Checks if we can use transparency effects e.g. for dock drop indicators.
102100
bool IsCompositorManagerRunning();
103101

104102
/// Sets the scalable icon to a given label (svg icons, or icons with multiple size pixmaps)

pcsx2-qt/Themes.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
#include <QtCore/QFile>
1212
#include <QtGui/QPalette>
1313
#include <QtGui/QPixmapCache>
14+
#include <QtGui/QStyleHints>
1415
#include <QtWidgets/QApplication>
1516
#include <QtWidgets/QStyle>
1617
#include <QtWidgets/QStyleFactory>
1718

1819
namespace QtHost
1920
{
2021
static void SetStyleFromSettings();
22+
static void SetColorSchemeBasedOnPalette();
2123
} // namespace QtHost
2224

2325
static QString s_unthemed_style_name;
@@ -52,15 +54,9 @@ void QtHost::UpdateApplicationTheme()
5254
QPixmapCache::clear();
5355
}
5456

55-
bool QtHost::IsDarkApplicationTheme()
56-
{
57-
QPalette palette = qApp->palette();
58-
return (palette.windowText().color().value() > palette.window().color().value());
59-
}
60-
6157
void QtHost::SetIconThemeFromStyle()
6258
{
63-
const bool dark = IsDarkApplicationTheme();
59+
const bool dark = qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark;
6460
QIcon::setThemeName(dark ? QStringLiteral("white") : QStringLiteral("black"));
6561
}
6662

@@ -73,13 +69,15 @@ void QtHost::SetStyleFromSettings()
7369
qApp->setStyle(QStyleFactory::create("Fusion"));
7470
qApp->setPalette(s_unthemed_palette);
7571
qApp->setStyleSheet(QString());
72+
SetColorSchemeBasedOnPalette();
7673
}
7774
#ifdef _WIN32
7875
else if (theme == "windowsvista")
7976
{
8077
qApp->setStyle(QStyleFactory::create("windowsvista"));
8178
qApp->setPalette(s_unthemed_palette);
8279
qApp->setStyleSheet(QString());
80+
SetColorSchemeBasedOnPalette();
8381
}
8482
#endif
8583
else if (theme == "darkfusion")
@@ -116,6 +114,7 @@ void QtHost::SetStyleFromSettings()
116114

117115
qApp->setPalette(darkPalette);
118116
qApp->setStyleSheet(QString());
117+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
119118
}
120119
else if (theme == "darkfusionblue")
121120
{
@@ -151,6 +150,7 @@ void QtHost::SetStyleFromSettings()
151150

152151
qApp->setPalette(darkBluePalette);
153152
qApp->setStyleSheet(QString());
153+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
154154
}
155155
else if (theme == "GreyMatter")
156156
{
@@ -187,6 +187,7 @@ void QtHost::SetStyleFromSettings()
187187

188188
qApp->setPalette(greyMatterPalette);
189189
qApp->setStyleSheet(QString());
190+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
190191
}
191192
else if (theme == "UntouchedLagoon")
192193
{
@@ -222,6 +223,7 @@ void QtHost::SetStyleFromSettings()
222223

223224
qApp->setPalette(untouchedLagoonPalette);
224225
qApp->setStyleSheet(QString());
226+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
225227
}
226228
else if (theme == "BabyPastel")
227229
{
@@ -259,6 +261,7 @@ void QtHost::SetStyleFromSettings()
259261

260262
qApp->setPalette(babyPastelPalette);
261263
qApp->setStyleSheet(QString());
264+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
262265
}
263266
else if (theme == "PizzaBrown")
264267
{
@@ -296,6 +299,7 @@ void QtHost::SetStyleFromSettings()
296299

297300
qApp->setPalette(pizzaPalette);
298301
qApp->setStyleSheet(QString());
302+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
299303
}
300304
else if (theme == "PCSX2Blue")
301305
{
@@ -331,6 +335,7 @@ void QtHost::SetStyleFromSettings()
331335

332336
qApp->setPalette(pcsx2BluePalette);
333337
qApp->setStyleSheet(QString());
338+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
334339
}
335340
else if (theme == "ScarletDevilRed")
336341
{
@@ -364,6 +369,7 @@ void QtHost::SetStyleFromSettings()
364369

365370
qApp->setPalette(scarletDevilPalette);
366371
qApp->setStyleSheet(QString());
372+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
367373
}
368374
else if (theme == "VioletAngelPurple")
369375
{
@@ -397,6 +403,7 @@ void QtHost::SetStyleFromSettings()
397403

398404
qApp->setPalette(violetAngelPalette);
399405
qApp->setStyleSheet(QString());
406+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
400407
}
401408
else if (theme == "CobaltSky")
402409
{
@@ -434,6 +441,7 @@ void QtHost::SetStyleFromSettings()
434441

435442
qApp->setPalette(cobaltSkyPalette);
436443
qApp->setStyleSheet(QString());
444+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
437445
}
438446
else if (theme == "AMOLED")
439447
{
@@ -470,6 +478,7 @@ void QtHost::SetStyleFromSettings()
470478

471479
qApp->setPalette(AMOLEDPalette);
472480
qApp->setStyleSheet(QString());
481+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
473482
}
474483
else if (theme == "Ruby")
475484
{
@@ -503,6 +512,7 @@ void QtHost::SetStyleFromSettings()
503512

504513
qApp->setPalette(rubyPalette);
505514
qApp->setStyleSheet(QString());
515+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
506516
}
507517
else if (theme == "Sapphire")
508518
{
@@ -536,6 +546,7 @@ void QtHost::SetStyleFromSettings()
536546

537547
qApp->setPalette(sapphirePalette);
538548
qApp->setStyleSheet(QString());
549+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
539550
}
540551
else if (theme == "Emerald")
541552
{
@@ -569,6 +580,7 @@ void QtHost::SetStyleFromSettings()
569580

570581
qApp->setPalette(emeraldPalette);
571582
qApp->setStyleSheet(QString());
583+
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
572584
}
573585
else if (theme == "Custom")
574586
{
@@ -587,11 +599,21 @@ void QtHost::SetStyleFromSettings()
587599
{
588600
qApp->setStyle(QStyleFactory::create("Fusion"));
589601
}
602+
603+
SetColorSchemeBasedOnPalette();
590604
}
591605
else
592606
{
593607
qApp->setStyle(s_unthemed_style_name);
594608
qApp->setPalette(s_unthemed_palette);
595609
qApp->setStyleSheet(QString());
610+
SetColorSchemeBasedOnPalette();
596611
}
597612
}
613+
614+
static void QtHost::SetColorSchemeBasedOnPalette()
615+
{
616+
QPalette palette = qApp->palette();
617+
bool dark = palette.windowText().color().value() > palette.window().color().value();
618+
qApp->styleHints()->setColorScheme(dark ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light);
619+
}

0 commit comments

Comments
 (0)