Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "qmsetup"]
path = qmsetup
url = ../../stdware/qmsetup.git
branch = main
url = ../qmsetup.git
Comment thread
JulienMaille marked this conversation as resolved.
Outdated
branch = main
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if(QWINDOWKIT_BUILD_WIDGETS)
add_subdirectory(mainwindow)
endif()

add_subdirectory(accent_color)
Comment thread
JulienMaille marked this conversation as resolved.
Outdated

if(QWINDOWKIT_BUILD_QUICK)
add_subdirectory(qml)
endif()
8 changes: 8 additions & 0 deletions examples/accent_color/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
project(QWKExample_AccentColor)

qwk_add_example(${PROJECT_NAME}
FEATURES cxx_std_17
SOURCES main.cpp
QT_LINKS Core Gui
LINKS QWKCore
)
18 changes: 18 additions & 0 deletions examples/accent_color/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <QGuiApplication>
#include <QWKCore/styleagent.h>
#include <QDebug>
#include <QTimer>

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

QWK::StyleAgent agent;
QObject::connect(&agent, &QWK::StyleAgent::systemAccentColorChanged, [](){
qDebug() << "Accent color changed!" << QWK::StyleAgent().systemAccentColor();
});

qDebug() << "Initial Accent Color:" << agent.systemAccentColor();

return app.exec();
}
33 changes: 33 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,15 @@ namespace QWK {
Q_EMIT q->systemThemeChanged();
}

void StyleAgentPrivate::notifyAccentColorChanged(const QColor &color) {
Comment thread
JulienMaille marked this conversation as resolved.
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 +71,21 @@ namespace QWK {
return d->systemTheme;
}

/*!
Returns the system accent color.
*/
QColor StyleAgent::systemAccentColor() const {
Q_D(const StyleAgent);
if (d->systemAccentColor.isValid()) {
return d->systemAccentColor;
Comment thread
JulienMaille marked this conversation as resolved.
Outdated
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
return QGuiApplication::palette().color(QPalette::Accent);
#else
return QGuiApplication::palette().color(QPalette::Highlight);
#endif
Comment thread
JulienMaille marked this conversation as resolved.
Outdated
}

/*!
\internal
*/
Expand All @@ -74,4 +101,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
Comment thread
JulienMaille marked this conversation as resolved.
}

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