diff --git a/src/core/style/styleagent.cpp b/src/core/style/styleagent.cpp index 4b88447..d1e15f0 100644 --- a/src/core/style/styleagent.cpp +++ b/src/core/style/styleagent.cpp @@ -6,6 +6,9 @@ #include "styleagent_p.h" #include +#include +#include +#include namespace QWK { @@ -38,6 +41,16 @@ namespace QWK { Q_EMIT q->systemThemeChanged(); } + void StyleAgentPrivate::notifyAccentColorChanged(const QColor &color) { + Q_ASSERT(color.isValid()); + if (color == systemAccentColor) + return; + systemAccentColor = color; + + Q_Q(StyleAgent); + Q_EMIT q->systemAccentColorChanged(); + } + /*! Constructor. Since it is not related to a concrete window instance, it is better to be used as a singleton. @@ -59,6 +72,14 @@ namespace QWK { return d->systemTheme; } + /*! + Returns the system accent color. + */ + QColor StyleAgent::systemAccentColor() const { + Q_D(const StyleAgent); + return d->systemAccentColor; + } + /*! \internal */ @@ -74,4 +95,10 @@ namespace QWK { This signal is emitted when the system theme changes. */ + /*! + \fn void StyleAgent::systemAccentColorChanged() + + This signal is emitted when the system accent color changes. + */ + } diff --git a/src/core/style/styleagent.h b/src/core/style/styleagent.h index c573e8d..e0cbfd2 100644 --- a/src/core/style/styleagent.h +++ b/src/core/style/styleagent.h @@ -9,6 +9,7 @@ #include #include +#include #include @@ -33,9 +34,11 @@ namespace QWK { public: SystemTheme systemTheme() const; + QColor systemAccentColor() const; Q_SIGNALS: void systemThemeChanged(); + void systemAccentColorChanged(); protected: StyleAgent(StyleAgentPrivate &d, QObject *parent = nullptr); diff --git a/src/core/style/styleagent_linux.cpp b/src/core/style/styleagent_linux.cpp index 2825f09..df80b0e 100644 --- a/src/core/style/styleagent_linux.cpp +++ b/src/core/style/styleagent_linux.cpp @@ -5,10 +5,18 @@ #include "styleagent_p.h" #include +#include +#include +#include namespace QWK { void StyleAgentPrivate::setupSystemThemeHook() { +#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) + systemAccentColor = QGuiApplication::palette().color(QPalette::Accent); +#else + systemAccentColor = QGuiApplication::palette().color(QPalette::Highlight); +#endif } void StyleAgentPrivate::removeSystemThemeHook() { diff --git a/src/core/style/styleagent_mac.mm b/src/core/style/styleagent_mac.mm index e4764ad..0e7b9d8 100644 --- a/src/core/style/styleagent_mac.mm +++ b/src/core/style/styleagent_mac.mm @@ -7,6 +7,7 @@ #include #include +#include namespace QWK { @@ -17,6 +18,17 @@ return isDark ? StyleAgent::Dark : StyleAgent::Light; } + static QColor getAccentColor() { + if (@available(macOS 10.14, *)) { + NSColor *color = [NSColor controlAccentColor]; + NSColor *rgbColor = [color colorUsingColorSpace:[NSColorSpace sRGBColorSpace]]; + if (rgbColor) { + return QColor::fromRgbF(rgbColor.redComponent, rgbColor.greenComponent, rgbColor.blueComponent, rgbColor.alphaComponent); + } + } + return {}; + } + static void notifyAllStyleAgents(); } @@ -34,11 +46,15 @@ @implementation QWK_SystemThemeObserver - (id)init { self = [super init]; if (self) { - [[NSDistributedNotificationCenter defaultCenter] - addObserver:self + NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter]; + [center addObserver:self selector:@selector(interfaceModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; + [center addObserver:self + selector:@selector(interfaceModeChanged:) + name:@"AppleColorPreferencesChangedNotification" + object:nil]; } return self; } @@ -68,13 +84,16 @@ - (void)interfaceModeChanged:(NSNotification *)notification { void notifyAllStyleAgents() { auto theme = getSystemTheme(); + auto color = getAccentColor(); for (auto &&ap : std::as_const(*g_styleAgentSet())) { ap->notifyThemeChanged(theme); + ap->notifyAccentColorChanged(color); } } void StyleAgentPrivate::setupSystemThemeHook() { systemTheme = getSystemTheme(); + systemAccentColor = getAccentColor(); // Alloc if (g_styleAgentSet->isEmpty()) { diff --git a/src/core/style/styleagent_p.h b/src/core/style/styleagent_p.h index 0ee6500..9c0b93d 100644 --- a/src/core/style/styleagent_p.h +++ b/src/core/style/styleagent_p.h @@ -31,11 +31,13 @@ namespace QWK { StyleAgent *q_ptr; StyleAgent::SystemTheme systemTheme = StyleAgent::Unknown; + QColor systemAccentColor; void setupSystemThemeHook(); void removeSystemThemeHook(); void notifyThemeChanged(StyleAgent::SystemTheme theme); + void notifyAccentColorChanged(const QColor &color); }; } diff --git a/src/core/style/styleagent_win.cpp b/src/core/style/styleagent_win.cpp index 8adf936..2d8733a 100644 --- a/src/core/style/styleagent_win.cpp +++ b/src/core/style/styleagent_win.cpp @@ -28,8 +28,10 @@ namespace QWK { static void notifyAllStyleAgents() { auto theme = getSystemTheme(); + auto color = getAccentColor(); for (auto &&ap : std::as_const(*g_styleAgentSet())) { ap->notifyThemeChanged(theme); + ap->notifyAccentColorChanged(color); } } @@ -84,6 +86,7 @@ namespace QWK { void StyleAgentPrivate::setupSystemThemeHook() { systemTheme = getSystemTheme(); + systemAccentColor = getAccentColor(); g_styleAgentSet->insert(this); SystemSettingEventFilter::install();