diff --git a/src/mumble/MainWindow.cpp b/src/mumble/MainWindow.cpp index 4f511bb10ab..0cf75b1e294 100644 --- a/src/mumble/MainWindow.cpp +++ b/src/mumble/MainWindow.cpp @@ -756,6 +756,9 @@ void MainWindow::changeEvent(QEvent *e) { QTimer::singleShot(0, this, SLOT(hide())); } #endif + if (e->type() == QEvent::ThemeChange) { + Themes::apply(); + } } void MainWindow::keyPressEvent(QKeyEvent *e) { diff --git a/src/mumble/Themes.cpp b/src/mumble/Themes.cpp index 4b1a121c44d..5a4d08a27ef 100644 --- a/src/mumble/Themes.cpp +++ b/src/mumble/Themes.cpp @@ -8,6 +8,14 @@ #include "MumbleApplication.h" #include "Global.h" +#include +#include +#include +#include +#include + +QString Themes::currentThemePath; + boost::optional< ThemeInfo::StyleInfo > Themes::getConfiguredStyle(const Settings &settings) { if (settings.themeName.isEmpty() && settings.themeStyleName.isEmpty()) { return boost::none; @@ -55,25 +63,45 @@ void Themes::applyFallback() { } bool Themes::applyConfigured() { + + boost::optional< ThemeInfo::StyleInfo > style = Themes::getConfiguredStyle(Global::get().s); if (!style) { return false; } - const QFileInfo qssFile(style->getPlatformQss()); + QString themePath = style->getPlatformQss().absoluteFilePath(); + if (style->themeName == "Auto" || style->name == "Auto") { + + bool isDarkTheme = detectSystemTheme(); + if (isDarkTheme) { + themePath = ":/themes/Default/Dark.qss"; + } else { + themePath = ":/themes/Default/Lite.qss"; + } + + } + + // Early exit if the theme path is the same as the current one + if (themePath == currentThemePath) { + qWarning() << "Themes::applyConfigured(): Skipping redundant theme application for path:" << themePath; + return true; + } + + currentThemePath = themePath; // Update the current theme path qWarning() << "Theme:" << style->themeName; qWarning() << "Style:" << style->name; - qWarning() << "--> qss:" << qssFile.absoluteFilePath(); + qWarning() << "--> qss:" << themePath; - QFile file(qssFile.absoluteFilePath()); + QFile file(themePath); if (!file.open(QFile::ReadOnly)) { qWarning() << "Failed to open theme stylesheet:" << file.errorString(); return false; } QStringList skinPaths; - skinPaths << qssFile.path(); + skinPaths << QFileInfo(themePath).path(); skinPaths << QLatin1String(":/themes/Default"); // Some skins might want to fall-back on our built-in resources QString themeQss = QString::fromUtf8(file.readAll()); @@ -105,6 +133,29 @@ bool Themes::apply() { return result; } +bool Themes::detectSystemTheme() { +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark; +#else +# ifdef Q_OS_WIN + QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", + QSettings::NativeFormat); + return settings.value("AppsUseLightTheme", 1).toInt() == 0; // 0 means dark mode +# else + // Fallback for other OSes + QByteArray platform = qgetenv("QT_QPA_PLATFORM"); + if (platform.contains("darkmode=2")) { + return true; + } else if (platform.contains("darkmode=1")) { + QPalette defaultPalette; + return defaultPalette.color(QPalette::WindowText).lightness() + > defaultPalette.color(QPalette::Window).lightness(); + } + return false; +# endif +#endif +} + ThemeMap Themes::getThemes() { return ThemeInfo::scanDirectories(getSearchDirectories()); } diff --git a/src/mumble/Themes.h b/src/mumble/Themes.h index 000fd09b57c..93eae62739a 100644 --- a/src/mumble/Themes.h +++ b/src/mumble/Themes.h @@ -31,6 +31,9 @@ class Themes { /// @note Can only apply a theme before MainWindow etc. is opened static bool apply(); + /// Detects current OS theme + static bool detectSystemTheme(); + /// Return a theme name to theme map static ThemeMap getThemes(); @@ -63,6 +66,9 @@ class Themes { /// If a the file is is available, the function returns true. /// If no file is available, it returns false. static bool readStylesheet(const QString &stylesheetFn, QString &stylesheetContent); + + /// Member variable to store the current theme path + static QString currentThemePath; }; #endif // MUMBLE_MUMBLE_THEMES_H_ diff --git a/themes/Default/theme.ini b/themes/Default/theme.ini index 89b91ff52af..89660bdedbc 100644 --- a/themes/Default/theme.ini +++ b/themes/Default/theme.ini @@ -1,6 +1,6 @@ [theme] name=Mumble -styles=dark,lite +styles=dark,lite,auto [dark] name=Dark qss=Dark.qss @@ -9,3 +9,7 @@ qss_MAC=OSX Dark.qss name=Lite qss=Lite.qss qss_MAC=OSX Lite.qss +[auto] +name=Auto +qss=Lite.qss +qss_MAC=OSX Lite.qss