From 0339e9e5e19fd4fe65113e1a819826109b7e4727 Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: ćšć, 28 12æ 2023 23:13:33 +0800 Subject: [PATCH] minor tweaks --- src/core/style/styleagent_mac.mm | 81 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 81 insertions(+), 0 deletions(-) diff --git a/src/core/style/styleagent_mac.mm b/src/core/style/styleagent_mac.mm index 3eb3577..6c89931 100644 --- a/src/core/style/styleagent_mac.mm +++ b/src/core/style/styleagent_mac.mm @@ -1,13 +1,94 @@ #include "styleagent_p.h" +#include <Cocoa/Cocoa.h> + #include <QtCore/QVariant> namespace QWK { + static StyleAgent::SystemTheme getSystemTheme() { + NSString *osxMode = + [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; + bool isDark = [osxMode isEqualToString:@"Dark"]; + return isDark ? StyleAgent::Dark : StyleAgent::Light; + } + + static void notifyAllStyleAgents(); + +} + +// +// Objective C++ Begin +// + +@interface QWK_SystemThemeObserver : NSObject { +} +@end + +@implementation QWK_SystemThemeObserver + +- (id)init { + self = [super init]; + if (self) { + [[NSDistributedNotificationCenter defaultCenter] + addObserver:self + selector:@selector(interfaceModeChanged:) + name:@"AppleInterfaceThemeChangedNotification" + object:nil]; + } + return self; +} + +- (void)dealloc { + [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (void)interfaceModeChanged:(NSNotification *)notification { + QWK::notifyAllStyleAgents(); +} + +@end + +// +// Objective C++ End +// + + +namespace QWK { + + using StyleAgentSet = QSet<StyleAgentPrivate *>; + Q_GLOBAL_STATIC(StyleAgentSet, g_styleAgentSet) + + static QWK_SystemThemeObserver *g_systemThemeObserver = nil; + + void notifyAllStyleAgents() { + auto theme = getSystemTheme(); + for (auto &&ap : std::as_const(*g_styleAgentSet())) { + ap->notifyThemeChanged(theme); + } + } + void StyleAgentPrivate::setupSystemThemeHook() { + systemTheme = getSystemTheme(); + + // Alloc + if (g_styleAgentSet->isEmpty()) { + g_systemThemeObserver = [[QWK_SystemThemeObserver alloc] init]; + } + + g_styleAgentSet->insert(this); } void StyleAgentPrivate::removeSystemThemeHook() { + if (!g_styleAgentSet->remove(this)) + return; + + if (g_styleAgentSet->isEmpty()) { + // Delete + [g_systemThemeObserver release]; + g_systemThemeObserver = nil; + } } } \ No newline at end of file -- Gitblit v1.9.1