diff --git a/misc/desktoputils.cpp b/misc/desktoputils.cpp index e487875..dea7aeb 100644 --- a/misc/desktoputils.cpp +++ b/misc/desktoputils.cpp @@ -2,6 +2,10 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) +#include +#include +#endif #ifdef Q_OS_WIN32 #include #endif @@ -41,11 +45,28 @@ bool openLocalFileOrDir(const QString &path) /*! * \brief Returns whether \a palette is dark. - * \remarks Just call with no argument to check for the default palette to see whether "dark mode" is enabled. */ bool isPaletteDark(const QPalette &palette) { return palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness(); } +/*! + * \brief Returns whether dark mode is enabled. + * \remarks Whether dark mode is enabled can only be determined on Qt 6.5 or newer and only on certain + * platforms. If it cannot be determined, std::nullopt is returned. + */ +std::optional isDarkModeEnabled() +{ +#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) + if (const auto *const styleHints = QGuiApplication::styleHints()) { + const auto colorScheme = styleHints->colorScheme(); + if (colorScheme != Qt::ColorScheme::Unknown) { + return colorScheme == Qt::ColorScheme::Dark; + } + } +#endif + return std::nullopt; +} + } // namespace QtUtilities diff --git a/misc/desktoputils.h b/misc/desktoputils.h index 0b54187..a67abc6 100644 --- a/misc/desktoputils.h +++ b/misc/desktoputils.h @@ -5,12 +5,15 @@ #include +#include + QT_FORWARD_DECLARE_CLASS(QString) namespace QtUtilities { QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path); QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette = QPalette()); +QT_UTILITIES_EXPORT std::optional isDarkModeEnabled(); } // namespace QtUtilities diff --git a/resources/resources.cpp b/resources/resources.cpp index 2312e10..fc61220 100644 --- a/resources/resources.cpp +++ b/resources/resources.cpp @@ -313,10 +313,14 @@ bool hasCoreApp() */ void setupCommonQtApplicationAttributes() { - // enable dark window frame on Windows if the configured color palette is dark (supported as of Qt 6.4) - // see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819 -#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) - if (QLibraryInfo::version() >= QVersionNumber(6, 4, 0) && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) { + // enable dark window frame on Windows if the configured color palette is dark + // - supported as of Qt 6.4; no longer required as of Qt 6.5 + // - see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819 + // and https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5 +#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 5, 0)) + if (const auto qtVersion = QLibraryInfo::version(); + qtVersion >= QVersionNumber(6, 4, 0) && qtVersion < QVersionNumber(6, 5, 0) + && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) { qputenv("QT_QPA_PLATFORM", "windows:darkmode=1"); } #endif