From 96552dfcb531053f4a331c2c80ecf5d8cb2aaf36 Mon Sep 17 00:00:00 2001
From: 某莔 <seiuryuu@gmail.com>
Date: 周四, 28 12月 2023 22:55:40 +0800
Subject: [PATCH] fix: NSVisualEffectView

---
 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