2626#include < QtCore/QProcess>
2727#include < QtCore/QRegularExpression>
2828#include < QtGui/QFont>
29+ #include < QtGui/QPalette>
30+ #include < QtGui/QColor>
2931#include < QtGui/QWindow>
3032#include < QtGui/QDesktopServices>
3133#include < QtGui/QClipboard>
3638#include < QtWidgets/QComboBox>
3739#include < QtWidgets/QScrollBar>
3840#include < QtWidgets/QMessageBox>
41+ #include < QtWidgets/QStyle>
42+ #include < QtGui/QStyleHints>
43+ #include < QtWidgets/QStyleFactory>
3944#include < QtNetwork/QNetworkReply>
45+ #include < QtCore/QSignalBlocker>
46+ #include < QtCore/QScopedValueRollback>
4047#include < fstream>
4148#include < iostream>
4249#include < cmath>
43-
4450#ifdef Q_OS_MACOS
4551# include " os/mac/kdmactouchbar.h"
4652#endif
@@ -56,6 +62,8 @@ Q_DECLARE_METATYPE(emu_data_t)
5662# include < unistd.h>
5763#endif
5864
65+ using namespace Qt ::StringLiterals;
66+
5967MainWindow::MainWindow (CEmuOpts &cliOpts, QWidget *p) : QMainWindow(p), ui(new Ui::MainWindow), opts(cliOpts) {
6068 keypadBridge = new QtKeypadBridge (this ); // This must be before setupUi for some reason >.>
6169
@@ -70,8 +78,13 @@ MainWindow::MainWindow(CEmuOpts &cliOpts, QWidget *p) : QMainWindow(p), ui(new U
7078
7179 ui->setupUi (this );
7280
73- m_styleForMode[0 ] = m_styleForMode[1 ] = QApplication::style ()->name ();
74- darkModeSwitch (isSystemInDarkMode ());
81+ {
82+ QSignalBlocker blocker (ui->comboTheme );
83+ ui->comboTheme ->setCurrentIndex (static_cast <int >(m_themePreference));
84+ }
85+ connect (ui->comboTheme , &QComboBox::currentIndexChanged, this , &MainWindow::setThemePreference);
86+
87+ applyThemeFromPreference ();
7588
7689 setStyleSheet (QStringLiteral (" QMainWindow::separator{ width: 0px; height: 0px; }" ));
7790
@@ -597,6 +610,7 @@ MainWindow::MainWindow(CEmuOpts &cliOpts, QWidget *p) : QMainWindow(p), ui(new U
597610 }
598611
599612 setAutoUpdates (m_config->value (SETTING_AUTOUPDATE, CEMU_RELEASE).toBool ());
613+ setThemePreference (m_config->value (SETTING_UI_THEME, static_cast <int >(ThemePreference::System)).toInt ());
600614 checkVersion ();
601615
602616#ifdef Q_OS_WIN
@@ -1129,14 +1143,52 @@ void MainWindow::translateExtras(int init) {
11291143 }
11301144}
11311145
1132- void MainWindow::darkModeSwitch (bool darkMode) {
1133- QApplication::setStyle (m_styleForMode[darkMode]);
1134- if (darkMode != isRunningInDarkMode ()) {
1135- m_styleForMode[darkMode] = QStringLiteral (" fusion" );
1136- QApplication::setStyle (m_styleForMode[darkMode]);
1137- darkMode = isRunningInDarkMode ();
1146+ void MainWindow::applyThemeFromPreference () {
1147+ Qt::ColorScheme scheme = Qt::ColorScheme::Unknown;
1148+ bool explicitScheme = false ;
1149+ switch (m_themePreference) {
1150+ case ThemePreference::System:
1151+ scheme = Qt::ColorScheme::Unknown;
1152+ break ;
1153+ case ThemePreference::Light:
1154+ scheme = Qt::ColorScheme::Light;
1155+ explicitScheme = true ;
1156+ break ;
1157+ case ThemePreference::Dark:
1158+ scheme = Qt::ColorScheme::Dark;
1159+ explicitScheme = true ;
1160+ break ;
1161+ }
1162+ qApp->styleHints ()->setColorScheme (scheme);
1163+
1164+ #if defined(Q_OS_WIN)
1165+ if (explicitScheme) {
1166+ if (QStyle *fusion = QStyleFactory::create (" Fusion" _L1)) {
1167+ QApplication::setStyle (fusion);
1168+ }
1169+ } else {
1170+ const auto available = QStyleFactory::keys ();
1171+ if (available.contains (" WindowsVista" _L1, Qt::CaseInsensitive)) {
1172+ if (QStyle *vista = QStyleFactory::create (" WindowsVista" _L1)) {
1173+ QApplication::setStyle (vista);
1174+ }
1175+ }
1176+ }
1177+ #elif defined(Q_OS_MACOS)
1178+ Q_UNUSED (explicitScheme);
1179+ #else
1180+ if (explicitScheme) {
1181+ if (QStyle *fusion = QStyleFactory::create (" Fusion" _L1)) {
1182+ QApplication::setStyle (fusion);
1183+ }
11381184 }
1185+ #endif
1186+
1187+ const bool dark = (qApp->styleHints ()->colorScheme () == Qt::ColorScheme::Dark);
1188+ darkModeSwitch (dark);
1189+ }
11391190
1191+ void MainWindow::darkModeSwitch (bool darkMode) {
11401192 m_isInDarkMode = darkMode;
11411193 if (darkMode) {
11421194 m_cBack.setColor (QPalette::Base, QColor (Qt::blue).lighter (180 ));
@@ -1160,12 +1212,29 @@ void MainWindow::changeEvent(QEvent* event) {
11601212 translateSwitch (QLocale::system ());
11611213 }
11621214 QMainWindow::changeEvent (event);
1163- if (eventType == QEvent::ThemeChange) {
1164- bool darkMode = isSystemInDarkMode ();
1165- if (darkMode != m_isInDarkMode) {
1166- darkModeSwitch (darkMode);
1167- }
1215+ if (eventType == QEvent::ThemeChange ||
1216+ eventType == QEvent::ApplicationPaletteChange ||
1217+ eventType == QEvent::PaletteChange) {
1218+ const bool dark = (qApp->styleHints ()->colorScheme () == Qt::ColorScheme::Dark);
1219+ darkModeSwitch (dark);
1220+ }
1221+ }
1222+
1223+ void MainWindow::setThemePreference (int index) {
1224+ const auto pref = static_cast <ThemePreference>(index);
1225+
1226+ if (ui && ui->comboTheme && ui->comboTheme ->currentIndex () != index) {
1227+ QSignalBlocker blocker (ui->comboTheme );
1228+ ui->comboTheme ->setCurrentIndex (index);
11681229 }
1230+
1231+ m_themePreference = pref;
1232+
1233+ if (m_config && opts.useSettings ) {
1234+ m_config->setValue (SETTING_UI_THEME, index);
1235+ }
1236+
1237+ applyThemeFromPreference ();
11691238}
11701239
11711240void MainWindow::showEvent (QShowEvent *e) {
@@ -1485,6 +1554,7 @@ void MainWindow::resetGui() {
14851554 m_config->remove (SETTING_WINDOW_STATUSBAR);
14861555 m_config->remove (SETTING_WINDOW_SEPARATOR);
14871556 m_config->remove (SETTING_UI_EDIT_MODE);
1557+ m_config->remove (SETTING_UI_THEME);
14881558 m_needReload = true ;
14891559 close ();
14901560}
0 commit comments