From cbf26d125d446a48ddffbe147b68556906a002b2 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Sat, 22 Apr 2023 23:48:25 -0700 Subject: [PATCH] Add support for dynamic color scheme update --- UEFITool/uefitool.cpp | 30 +++++++++++++++++++++++++++--- UEFITool/uefitool.h | 4 ++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/UEFITool/uefitool.cpp b/UEFITool/uefitool.cpp index 59a041e..1028f40 100644 --- a/UEFITool/uefitool.cpp +++ b/UEFITool/uefitool.cpp @@ -15,6 +15,10 @@ #include "uefitool.h" #include "ui_uefitool.h" +#if QT_VERSION_MAJOR >= 6 +#include +#endif + UEFITool::UEFITool(QWidget *parent) : QMainWindow(parent), ui(new Ui::UEFITool), @@ -167,13 +171,13 @@ void UEFITool::init() ui->finderMessagesListWidget->installEventFilter(this); ui->builderMessagesListWidget->installEventFilter(this); - // Detect UI dark mode + // Detect and set UI light or dark mode #if QT_VERSION_MAJOR >= 6 +#if QT_VERSION_MINOR < 5 #if defined Q_OS_WIN QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat); if (settings.value("AppsUseLightTheme", 1).toInt() == 0) { model->setMarkingDarkMode(true); - // TODO: remove this once default style gains dark theme support QApplication::setStyle(QStyleFactory::create("Fusion")); QApplication::setPalette(QApplication::style()->standardPalette()); } @@ -183,10 +187,30 @@ void UEFITool::init() if (color.lightness() < 127) { // TreeView has dark background model->setMarkingDarkMode(true); } +#endif // defined Q_OS_WIN +#else // QT_VERSION_MINOR >= 5 + // Qt 6.5.0 added proper support for dark UI mode, including detection and notification on mode change + // It also supposed to work in all OSes, but still requires changing the default style on Windows from Vista to Fusion + auto styleHints = QGuiApplication::styleHints(); + model->setMarkingDarkMode(styleHints->colorScheme() == Qt::ColorScheme::Dark); + connect(styleHints, SIGNAL(colorSchemeChanged(Qt::ColorScheme)), this, SLOT(updateUiForNewColorScheme(Qt::ColorScheme))); + +#if defined Q_OS_WIN + QApplication::setStyle(QStyleFactory::create("Fusion")); + QApplication::setPalette(QApplication::style()->standardPalette()); #endif -#endif +#endif // QT_VERSION_MINOR +#endif // QT_VERSION_MAJOR } +#if QT_VERSION_MAJOR >= 6 && QT_VERSION_MINOR >= 5 +void UEFITool::updateUiForNewColorScheme(Qt::ColorScheme scheme) +{ + model->setMarkingDarkMode(scheme == Qt::ColorScheme::Dark); + QApplication::setPalette(QApplication::style()->standardPalette()); +} +#endif + void UEFITool::populateUi(const QItemSelection &selected) { if (selected.isEmpty()) { diff --git a/UEFITool/uefitool.h b/UEFITool/uefitool.h index 5533a3e..08ef908 100644 --- a/UEFITool/uefitool.h +++ b/UEFITool/uefitool.h @@ -127,6 +127,10 @@ private slots: void currentTabChanged(int index); +#if QT_VERSION_MAJOR >= 6 && QT_VERSION_MINOR >= 5 + void updateUiForNewColorScheme(Qt::ColorScheme scheme); +#endif + private: Ui::UEFITool* ui; TreeModel* model;