3939#include < QtWidgets/QScrollBar>
4040#include < QtWidgets/QMessageBox>
4141#include < QtWidgets/QStyle>
42+ #include < QtGui/QStyleHints>
4243#include < QtWidgets/QStyleFactory>
4344#include < QtNetwork/QNetworkReply>
4445#include < QtCore/QSignalBlocker>
4546#include < QtCore/QScopedValueRollback>
4647#include < fstream>
4748#include < iostream>
4849#include < cmath>
49-
50- namespace {
51- QPalette createFusionDarkPalette (const QPalette &base) {
52- QPalette palette = base;
53- palette.setColor (QPalette::Window, QColor (53 , 53 , 53 ));
54- palette.setColor (QPalette::WindowText, Qt::white);
55- palette.setColor (QPalette::Base, QColor (25 , 25 , 25 ));
56- palette.setColor (QPalette::AlternateBase, QColor (53 , 53 , 53 ));
57- palette.setColor (QPalette::ToolTipBase, Qt::white);
58- palette.setColor (QPalette::ToolTipText, Qt::white);
59- palette.setColor (QPalette::Text, Qt::white);
60- palette.setColor (QPalette::Button, QColor (53 , 53 , 53 ));
61- palette.setColor (QPalette::ButtonText, Qt::white);
62- palette.setColor (QPalette::BrightText, Qt::red);
63- palette.setColor (QPalette::Link, QColor (42 , 130 , 218 ));
64- palette.setColor (QPalette::Highlight, QColor (76 , 163 , 224 ));
65- palette.setColor (QPalette::HighlightedText, Qt::black);
66-
67- static constexpr QColor disabledColor (127 , 127 , 127 );
68- palette.setColor (QPalette::Disabled, QPalette::Text, disabledColor);
69- palette.setColor (QPalette::Disabled, QPalette::ButtonText, disabledColor);
70- palette.setColor (QPalette::PlaceholderText, disabledColor);
71- return palette;
72- }
73- }
74-
7550#ifdef Q_OS_MACOS
7651# include " os/mac/kdmactouchbar.h"
7752#endif
@@ -87,6 +62,8 @@ Q_DECLARE_METATYPE(emu_data_t)
8762# include < unistd.h>
8863#endif
8964
65+ using namespace Qt ::StringLiterals;
66+
9067MainWindow::MainWindow (CEmuOpts &cliOpts, QWidget *p) : QMainWindow(p), ui(new Ui::MainWindow), opts(cliOpts) {
9168 keypadBridge = new QtKeypadBridge (this ); // This must be before setupUi for some reason >.>
9269
@@ -101,23 +78,6 @@ MainWindow::MainWindow(CEmuOpts &cliOpts, QWidget *p) : QMainWindow(p), ui(new U
10178
10279 ui->setupUi (this );
10380
104- m_styleForMode[0 ] = QApplication::style ()->objectName ();
105- if (m_styleForMode[0 ].isEmpty ()) {
106- m_styleForMode[0 ] = QApplication::style ()->name ();
107- }
108- if (m_styleForMode[0 ].isEmpty ()) {
109- m_styleForMode[0 ] = QStringLiteral (" fusion" );
110- }
111- m_styleForMode[1 ] = QStringLiteral (" fusion" );
112-
113- if (const QStyle *fusionStyle = QStyleFactory::create (QStringLiteral (" Fusion" ))) {
114- m_fusionLightPalette = fusionStyle->standardPalette ();
115- delete fusionStyle;
116- } else {
117- m_fusionLightPalette = QApplication::style ()->standardPalette ();
118- }
119- m_fusionDarkPalette = createFusionDarkPalette (m_fusionLightPalette);
120-
12181 {
12282 QSignalBlocker blocker (ui->comboTheme );
12383 ui->comboTheme ->setCurrentIndex (static_cast <int >(m_themePreference));
@@ -1195,24 +1155,46 @@ static void repolishAfterThemeChanged() {
11951155}
11961156
11971157void MainWindow::applyThemeFromPreference () {
1158+ Qt::ColorScheme scheme = Qt::ColorScheme::Unknown;
1159+ bool explicitScheme = false ;
11981160 switch (m_themePreference) {
11991161 case ThemePreference::System:
1200- QApplication::setStyle (m_styleForMode[0 ]);
1201- QApplication::setPalette (QApplication::style ()->standardPalette ());
1162+ scheme = Qt::ColorScheme::Unknown;
12021163 break ;
12031164 case ThemePreference::Light:
1204- QApplication::setStyle (m_styleForMode[ 1 ]) ;
1205- QApplication::setPalette (m_fusionLightPalette) ;
1165+ scheme = Qt::ColorScheme::Light ;
1166+ explicitScheme = true ;
12061167 break ;
12071168 case ThemePreference::Dark:
1208- QApplication::setStyle (m_styleForMode[ 1 ]) ;
1209- QApplication::setPalette (m_fusionDarkPalette) ;
1169+ scheme = Qt::ColorScheme::Dark ;
1170+ explicitScheme = true ;
12101171 break ;
12111172 }
1173+ qApp->styleHints ()->setColorScheme (scheme);
12121174
1213- const bool dark = m_themePreference == ThemePreference::Dark || (m_themePreference == ThemePreference::System && isSystemInDarkMode ());
1214- darkModeSwitch (dark);
1175+ #if defined(Q_OS_WIN)
1176+ if (explicitScheme) {
1177+ if (QStyle *fusion = QStyleFactory::create (" Fusion" _L1)) {
1178+ QApplication::setStyle (fusion);
1179+ }
1180+ } else {
1181+ const auto available = QStyleFactory::keys ();
1182+ if (available.contains (" WindowsVista" _L1, Qt::CaseInsensitive)) {
1183+ if (QStyle *vista = QStyleFactory::create (" WindowsVista" _L1)) {
1184+ QApplication::setStyle (vista);
1185+ }
1186+ }
1187+ }
1188+ #else
1189+ if (explicitScheme) {
1190+ if (QStyle *fusion = QStyleFactory::create (" Fusion" _L1)) {
1191+ QApplication::setStyle (fusion);
1192+ }
1193+ }
1194+ #endif
12151195
1196+ const bool dark = (qApp->styleHints ()->colorScheme () == Qt::ColorScheme::Dark);
1197+ darkModeSwitch (dark);
12161198 repolishAfterThemeChanged ();
12171199}
12181200
@@ -1240,8 +1222,12 @@ void MainWindow::changeEvent(QEvent* event) {
12401222 translateSwitch (QLocale::system ());
12411223 }
12421224 QMainWindow::changeEvent (event);
1243- if (eventType == QEvent::ThemeChange) {
1244- applyThemeFromPreference ();
1225+ if (eventType == QEvent::ThemeChange ||
1226+ eventType == QEvent::ApplicationPaletteChange ||
1227+ eventType == QEvent::PaletteChange) {
1228+ const bool dark = (qApp->styleHints ()->colorScheme () == Qt::ColorScheme::Dark);
1229+ darkModeSwitch (dark);
1230+ repolishAfterThemeChanged ();
12451231 }
12461232}
12471233
0 commit comments