From 7610c3a509b8a04d25e72a6282d9c8ea8ecbd27f Mon Sep 17 00:00:00 2001
From: iveswang <1660583890@qq.com>
Date: 摹曛, 13 6月 2024 14:09:06 +0800
Subject: [PATCH] fix: QObjectPrivate::threadData type change when qt >=5.15

---
 src/core/style/styleagent_mac.mm |   85 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/src/core/style/styleagent_mac.mm b/src/core/style/styleagent_mac.mm
index 3eb3577..e4764ad 100644
--- a/src/core/style/styleagent_mac.mm
+++ b/src/core/style/styleagent_mac.mm
@@ -1,13 +1,98 @@
+// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
+// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
+// SPDX-License-Identifier: Apache-2.0
+
 #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