Skip to content

Commit

Permalink
Allow the user to choose the UI theme
Browse files Browse the repository at this point in the history
Mainly useful for those who want dark mode on Windows.
  • Loading branch information
nadiaholmquist committed Feb 7, 2024
1 parent d48e5f2 commit 10c70eb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/frontend/qt_sdl/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ bool MouseHide;
int MouseHideSeconds;

bool PauseLostFocus;
std::string UITheme;

int64_t RTCOffset;

Expand Down Expand Up @@ -344,6 +345,7 @@ ConfigEntry ConfigFile[] =
{"MouseHide", 1, &MouseHide, false, false},
{"MouseHideSeconds", 0, &MouseHideSeconds, 5, false},
{"PauseLostFocus", 1, &PauseLostFocus, false, false},
{"UITheme", 2, &UITheme, (std::string)"", false},

{"RTCOffset", 3, &RTCOffset, (int64_t)0, true},

Expand Down
1 change: 1 addition & 0 deletions src/frontend/qt_sdl/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ extern bool EnableCheats;
extern bool MouseHide;
extern int MouseHideSeconds;
extern bool PauseLostFocus;
extern std::string UITheme;

extern int64_t RTCOffset;

Expand Down
23 changes: 22 additions & 1 deletion src/frontend/qt_sdl/InterfaceSettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
with melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include <QStyleFactory>
#include "InterfaceSettingsDialog.h"
#include "ui_InterfaceSettingsDialog.h"

#include "types.h"
#include "Platform.h"
#include "Config.h"
#include "main.h"

InterfaceSettingsDialog* InterfaceSettingsDialog::currentDlg = nullptr;

InterfaceSettingsDialog::InterfaceSettingsDialog(QWidget* parent) : QDialog(parent), ui(new Ui::InterfaceSettingsDialog)
{
ui->setupUi(this);
Expand All @@ -35,6 +36,18 @@ InterfaceSettingsDialog::InterfaceSettingsDialog(QWidget* parent) : QDialog(pare
ui->spinMouseHideSeconds->setValue(Config::MouseHideSeconds);
ui->cbPauseLostFocus->setChecked(Config::PauseLostFocus != 0);
ui->spinMaxFPS->setValue(Config::MaxFPS);

const QList<QString> themeKeys = QStyleFactory::keys();
const QString currentTheme = qApp->style()->objectName();

ui->cbxUITheme->addItem("System default", "");

for (int i = 0; i < themeKeys.length(); i++)
{
ui->cbxUITheme->addItem(themeKeys[i], themeKeys[i]);
if (!Config::UITheme.empty() && themeKeys[i].compare(currentTheme, Qt::CaseInsensitive) == 0)
ui->cbxUITheme->setCurrentIndex(i + 1);
}
}

InterfaceSettingsDialog::~InterfaceSettingsDialog()
Expand Down Expand Up @@ -63,8 +76,16 @@ void InterfaceSettingsDialog::done(int r)
Config::PauseLostFocus = ui->cbPauseLostFocus->isChecked() ? 1:0;
Config::MaxFPS = ui->spinMaxFPS->value();

QString themeName = ui->cbxUITheme->currentData().toString();
Config::UITheme = themeName.toStdString();

Config::Save();

if (!Config::UITheme.empty())
qApp->setStyle(themeName);
else
qApp->setStyle(*systemThemeName);

emit updateMouseTimer();
}

Expand Down
23 changes: 20 additions & 3 deletions src/frontend/qt_sdl/InterfaceSettingsDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>337</width>
<height>233</height>
<height>275</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -20,12 +20,29 @@
<string>Interface settings - melonDS</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,0">
<item alignment="Qt::AlignTop">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Main window</string>
<string>User interface</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Theme</string>
</property>
<property name="buddy">
<cstring>cbxUITheme</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbxUITheme"/>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="cbMouseHide">
<property name="text">
Expand Down
12 changes: 12 additions & 0 deletions src/frontend/qt_sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ QStringList NdsRomExtensions { ".nds", ".srl", ".dsi", ".ids" };
QString GbaRomMimeType = "application/x-gba-rom";
QStringList GbaRomExtensions { ".gba", ".agb" };

QString* systemThemeName;

// This list of supported archive formats is based on libarchive(3) version 3.6.2 (2022-12-09).
QStringList ArchiveMimeTypes
Expand Down Expand Up @@ -292,6 +293,11 @@ int main(int argc, char** argv)

qputenv("QT_SCALE_FACTOR", "1");

#if QT_VERSION_MAJOR == 6 && defined(__WIN32__)
// Allow using the system dark theme palette on Windows
qputenv("QT_QPA_PLATFORM", "windows:darkmode=2");
#endif

printf("melonDS " MELONDS_VERSION "\n");
printf(MELONDS_URL "\n");

Expand Down Expand Up @@ -360,6 +366,12 @@ int main(int argc, char** argv)
camManager[0]->setXFlip(Config::Camera[0].XFlip);
camManager[1]->setXFlip(Config::Camera[1].XFlip);

systemThemeName = new QString(QApplication::style()->name());

if (!Config::UITheme.empty())
{
QApplication::setStyle(QString::fromStdString(Config::UITheme));
}

Input::JoystickID = Config::JoystickID;
Input::OpenJoystick();
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/qt_sdl/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ class MelonApplication : public QApplication
bool event(QEvent* event) override;
};

extern QString* systemThemeName;

#endif // MAIN_H

0 comments on commit 10c70eb

Please sign in to comment.