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:
parent
89bbd75950
commit
fd151995ce
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
||||||
|
#include <QGuiApplication>
|
||||||
|
#include <QStyleHints>
|
||||||
|
#endif
|
||||||
#ifdef Q_OS_WIN32
|
#ifdef Q_OS_WIN32
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#endif
|
#endif
|
||||||
|
@ -41,11 +45,28 @@ bool openLocalFileOrDir(const QString &path)
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Returns whether \a palette is dark.
|
* \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)
|
bool isPaletteDark(const QPalette &palette)
|
||||||
{
|
{
|
||||||
return palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness();
|
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
|
} // namespace QtUtilities
|
||||||
|
|
|
@ -5,12 +5,15 @@
|
||||||
|
|
||||||
#include <QPalette>
|
#include <QPalette>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
QT_FORWARD_DECLARE_CLASS(QString)
|
QT_FORWARD_DECLARE_CLASS(QString)
|
||||||
|
|
||||||
namespace QtUtilities {
|
namespace QtUtilities {
|
||||||
|
|
||||||
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path);
|
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path);
|
||||||
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette = QPalette());
|
QT_UTILITIES_EXPORT bool isPaletteDark(const QPalette &palette = QPalette());
|
||||||
|
QT_UTILITIES_EXPORT std::optional<bool> isDarkModeEnabled();
|
||||||
|
|
||||||
} // namespace QtUtilities
|
} // namespace QtUtilities
|
||||||
|
|
||||||
|
|
|
@ -313,10 +313,14 @@ bool hasCoreApp()
|
||||||
*/
|
*/
|
||||||
void setupCommonQtApplicationAttributes()
|
void setupCommonQtApplicationAttributes()
|
||||||
{
|
{
|
||||||
// enable dark window frame on Windows if the configured color palette is dark (supported as of Qt 6.4)
|
// enable dark window frame on Windows if the configured color palette is dark
|
||||||
// see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819
|
// - supported as of Qt 6.4; no longer required as of Qt 6.5
|
||||||
#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
// - see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819
|
||||||
if (QLibraryInfo::version() >= QVersionNumber(6, 4, 0) && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) {
|
// 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");
|
qputenv("QT_QPA_PLATFORM", "windows:darkmode=1");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue