Adapt to dark mode support in Qt 6.5

* Avoid setting platform setting in Qt 6.5 as it is no longer needed for
  dark Window frames
* Reference recent blog post
* Add real `isDarkModeEnabled()` function using new Qt 6.5 API (as existing
  `isPaletteDark()` function is only based on the current palette)
This commit is contained in:
Martchus 2023-03-13 20:34:08 +01:00
parent 89bbd75950
commit fd151995ce
3 changed files with 33 additions and 5 deletions

View File

@ -2,6 +2,10 @@
#include <QDesktopServices>
#include <QUrl>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
#include <QGuiApplication>
#include <QStyleHints>
#endif
#ifdef Q_OS_WIN32
#include <QFileInfo>
#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<bool> 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

View File

@ -5,12 +5,15 @@
#include <QPalette>
#include <optional>
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<bool> isDarkModeEnabled();
} // namespace QtUtilities

View File

@ -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