Add helper to react to darkmode changes
This commit is contained in:
parent
d20130d770
commit
ded20b34a6
|
@ -69,4 +69,27 @@ std::optional<bool> isDarkModeEnabled()
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Invokes the specified callback when the color scheme changed.
|
||||||
|
*
|
||||||
|
* The first argument of the callback will be whether the color scheme is dark now (Qt::ColorScheme::Dark).
|
||||||
|
* The callback is invoked immediately by this function unless \a invokeImmediately is set to false.
|
||||||
|
*
|
||||||
|
* \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 the \a darkModeChangedCallback is never invoked.
|
||||||
|
*/
|
||||||
|
QMetaObject::Connection onDarkModeChanged(std::function<void(bool)> &&darkModeChangedCallback, QObject *context, bool invokeImmediately)
|
||||||
|
{
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
||||||
|
if (const auto *const styleHints = QGuiApplication::styleHints()) {
|
||||||
|
if (invokeImmediately) {
|
||||||
|
darkModeChangedCallback(styleHints->colorScheme() == Qt::ColorScheme::Dark);
|
||||||
|
}
|
||||||
|
return QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, context,
|
||||||
|
[handler = std::move(darkModeChangedCallback)](Qt::ColorScheme colorScheme) { return handler(colorScheme == Qt::ColorScheme::Dark); });
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return QMetaObject::Connection();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QtUtilities
|
} // namespace QtUtilities
|
||||||
|
|
|
@ -3,10 +3,13 @@
|
||||||
|
|
||||||
#include "../global.h"
|
#include "../global.h"
|
||||||
|
|
||||||
|
#include <QMetaObject>
|
||||||
#include <QPalette>
|
#include <QPalette>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QObject)
|
||||||
QT_FORWARD_DECLARE_CLASS(QString)
|
QT_FORWARD_DECLARE_CLASS(QString)
|
||||||
|
|
||||||
namespace QtUtilities {
|
namespace QtUtilities {
|
||||||
|
@ -14,6 +17,8 @@ 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();
|
QT_UTILITIES_EXPORT std::optional<bool> isDarkModeEnabled();
|
||||||
|
QT_UTILITIES_EXPORT QMetaObject::Connection onDarkModeChanged(
|
||||||
|
std::function<void(bool)> &&darkModeChangedCallback, QObject *context = nullptr, bool invokeImmediately = true);
|
||||||
|
|
||||||
} // namespace QtUtilities
|
} // namespace QtUtilities
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue