From bc49e2f6274b3e625d7f1702a0762a6de387a695 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 24 Jul 2022 23:17:22 +1000 Subject: [PATCH] Qt: Don't reset theme when changing language --- src/duckstation-qt/mainwindow.cpp | 25 ++++++++++++++++++------- src/duckstation-qt/mainwindow.h | 11 ++++++----- src/duckstation-qt/qthost.cpp | 3 +++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/duckstation-qt/mainwindow.cpp b/src/duckstation-qt/mainwindow.cpp index 34a74c5d8..23ab64583 100644 --- a/src/duckstation-qt/mainwindow.cpp +++ b/src/duckstation-qt/mainwindow.cpp @@ -55,6 +55,8 @@ static constexpr char DISC_IMAGE_FILTER[] = QT_TRANSLATE_NOOP( static const char* DEFAULT_THEME_NAME = "darkfusion"; MainWindow* g_main_window = nullptr; +static QString s_unthemed_style_name; +static bool s_unthemed_style_name_set; #if defined(_WIN32) || defined(__APPLE__) static const bool s_use_central_widget = false; @@ -80,7 +82,7 @@ bool QtHost::IsSystemValid() return s_system_valid; } -MainWindow::MainWindow() : QMainWindow(nullptr), m_unthemed_style_name(QApplication::style()->objectName()) +MainWindow::MainWindow() : QMainWindow(nullptr) { Assert(!g_main_window); g_main_window = this; @@ -100,11 +102,20 @@ MainWindow::~MainWindow() g_main_window = nullptr; } -void MainWindow::initialize() +void MainWindow::updateApplicationTheme() { + if (!s_unthemed_style_name_set) + { + s_unthemed_style_name_set = true; + s_unthemed_style_name = QApplication::style()->objectName(); + } + setStyleFromSettings(); setIconThemeFromSettings(); +} +void MainWindow::initialize() +{ m_ui.setupUi(this); setupAdditionalUi(); connectSignals(); @@ -1945,9 +1956,9 @@ void MainWindow::addThemeToMenu(const QString& name, const QString& key) void MainWindow::setTheme(const QString& theme) { Host::SetBaseStringSettingValue("UI", "Theme", theme.toUtf8().constData()); - setStyleFromSettings(); - setIconThemeFromSettings(); - updateMenuSelectedTheme(); + updateApplicationTheme(); + + // Sadly we need to recreate here, because otherwise the icon theme doesn't update. recreate(); } @@ -1957,7 +1968,7 @@ void MainWindow::setStyleFromSettings() if (theme == "qdarkstyle") { - qApp->setStyle(m_unthemed_style_name); + qApp->setStyle(s_unthemed_style_name); qApp->setPalette(QApplication::style()->standardPalette()); QFile f(QStringLiteral(":qdarkstyle/style.qss")); @@ -2047,7 +2058,7 @@ void MainWindow::setStyleFromSettings() { qApp->setPalette(QApplication::style()->standardPalette()); qApp->setStyleSheet(QString()); - qApp->setStyle(m_unthemed_style_name); + qApp->setStyle(s_unthemed_style_name); } } diff --git a/src/duckstation-qt/mainwindow.h b/src/duckstation-qt/mainwindow.h index 45dd9f452..1260dc2dd 100644 --- a/src/duckstation-qt/mainwindow.h +++ b/src/duckstation-qt/mainwindow.h @@ -65,6 +65,9 @@ public: explicit MainWindow(); ~MainWindow(); + /// Sets application theme according to settings. + static void updateApplicationTheme(); + /// Initializes the window. Call once at startup. void initialize(); @@ -166,9 +169,8 @@ protected: void dropEvent(QDropEvent* event) override; private: - void setTheme(const QString& theme); - void setStyleFromSettings(); - void setIconThemeFromSettings(); + static void setStyleFromSettings(); + static void setIconThemeFromSettings(); void setupAdditionalUi(); void connectSignals(); void addThemeToMenu(const QString& name, const QString& key); @@ -212,6 +214,7 @@ private: void updateMenuSelectedTheme(); std::string getDeviceDiscPath(const QString& title); void setGameListEntryCoverImage(const GameList::Entry* entry); + void setTheme(const QString& theme); void recreate(); /// Fills menu with save state info and handlers. @@ -233,8 +236,6 @@ private: Ui::MainWindow m_ui; - QString m_unthemed_style_name; - GameListWidget* m_game_list_widget = nullptr; DisplayWidget* m_display_widget = nullptr; diff --git a/src/duckstation-qt/qthost.cpp b/src/duckstation-qt/qthost.cpp index 7511ae26e..2f8ff9477 100644 --- a/src/duckstation-qt/qthost.cpp +++ b/src/duckstation-qt/qthost.cpp @@ -2241,6 +2241,9 @@ int main(int argc, char* argv[]) if (!QtHost::ParseCommandLineParametersAndInitializeConfig(app, autoboot)) return EXIT_FAILURE; + // Set theme before creating any windows. + MainWindow::updateApplicationTheme(); + // Start up the CPU thread. MainWindow* main_window = new MainWindow(); QtHost::HookSignals();