From 2d2fc799bc698ebf7e7c8bcc394366d0d7bf071b Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周二, 20 2月 2024 18:18:39 +0800
Subject: [PATCH] Partially finish the bug after window close and reshow

---
 src/core/style/styleagent_win.cpp |   37 +++++++++++++++++++++++++++----------
 1 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/core/style/styleagent_win.cpp b/src/core/style/styleagent_win.cpp
index 9d6072e..8adf936 100644
--- a/src/core/style/styleagent_win.cpp
+++ b/src/core/style/styleagent_win.cpp
@@ -1,3 +1,7 @@
+// 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 <QtCore/QSet>
@@ -11,6 +15,23 @@
 
     using StyleAgentSet = QSet<StyleAgentPrivate *>;
     Q_GLOBAL_STATIC(StyleAgentSet, g_styleAgentSet)
+
+    static StyleAgent::SystemTheme getSystemTheme() {
+        if (isHighContrastModeEnabled()) {
+            return StyleAgent::HighContrast;
+        } else if (isDarkThemeActive()) {
+            return StyleAgent::Dark;
+        } else {
+            return StyleAgent::Light;
+        }
+    }
+
+    static void notifyAllStyleAgents() {
+        auto theme = getSystemTheme();
+        for (auto &&ap : std::as_const(*g_styleAgentSet())) {
+            ap->notifyThemeChanged(theme);
+        }
+    }
 
     class SystemSettingEventFilter : public AppNativeEventFilter {
     public:
@@ -26,15 +47,13 @@
                 case WM_THEMECHANGED:
                 case WM_SYSCOLORCHANGE:
                 case WM_DWMCOLORIZATIONCOLORCHANGED: {
-                    // TODO: walk through `g_styleAgentSet`
+                    notifyAllStyleAgents();
                     break;
                 }
 
                 case WM_SETTINGCHANGE: {
-                    if (!msg->wParam && msg->lParam &&
-                        std::wcscmp(reinterpret_cast<LPCWSTR>(msg->lParam), L"ImmersiveColorSet") ==
-                            0) {
-                        // TODO: walk through `g_styleAgentSet`
+                    if (isImmersiveColorSetChange(msg->wParam, msg->lParam)) {
+                        notifyAllStyleAgents();
                     }
                     break;
                 }
@@ -45,7 +64,7 @@
             return false;
         }
 
-        static SystemSettingEventFilter *instance;
+        static inline SystemSettingEventFilter *instance = nullptr;
 
         static inline void install() {
             if (instance) {
@@ -63,13 +82,11 @@
         }
     };
 
-    SystemSettingEventFilter *SystemSettingEventFilter::instance = nullptr;
-
     void StyleAgentPrivate::setupSystemThemeHook() {
+        systemTheme = getSystemTheme();
+
         g_styleAgentSet->insert(this);
         SystemSettingEventFilter::install();
-
-        // Initialize `systemTheme` variable
     }
 
     void StyleAgentPrivate::removeSystemThemeHook() {

--
Gitblit v1.9.1