Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/core/style/styleagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "styleagent_p.h"

#include <QtCore/QVariant>
#include <QtGui/QColor>
#include <QtGui/QPalette>
#include <QtGui/QGuiApplication>

namespace QWK {

Expand Down Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -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.
*/

}
3 changes: 3 additions & 0 deletions src/core/style/styleagent.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <QtCore/QObject>
#include <QtGui/QWindow>
#include <QtGui/QColor>

#include <QWKCore/qwkglobal.h>

Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/core/style/styleagent_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
#include "styleagent_p.h"

#include <QtCore/QVariant>
#include <QtGui/QColor>
#include <QtGui/QPalette>
#include <QtGui/QGuiApplication>

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() {
Expand Down
23 changes: 21 additions & 2 deletions src/core/style/styleagent_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Cocoa/Cocoa.h>

#include <QtCore/QVariant>
#include <QtGui/QColor>

namespace QWK {

Expand All @@ -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();

}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/style/styleagent_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

}
Expand Down
3 changes: 3 additions & 0 deletions src/core/style/styleagent_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -84,6 +86,7 @@ namespace QWK {

void StyleAgentPrivate::setupSystemThemeHook() {
systemTheme = getSystemTheme();
systemAccentColor = getAccentColor();

g_styleAgentSet->insert(this);
SystemSettingEventFilter::install();
Expand Down