From e33059ee1ef7e1be07490ad0245676ded42589f4 Mon Sep 17 00:00:00 2001
From: Zhao Yuhang <2546789017@qq.com>
Date: 周五, 22 12月 2023 23:12:16 +0800
Subject: [PATCH] win: allow dynamic switch dark mode

---
 src/core/contexts/win32windowcontext.cpp |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 24dfcd9..8481066 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -729,16 +729,6 @@
         }
 #endif
 
-#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
-        for (const auto attr : {
-                 _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
-                 _DWMWA_USE_IMMERSIVE_DARK_MODE,
-             }) {
-            const BOOL enable = TRUE;
-            DynamicApis::instance().pDwmSetWindowAttribute(hWnd, attr, &enable, sizeof(enable));
-        }
-#endif
-
         // Add managed window
         addManagedWindow(m_windowHandle, hWnd, this);
 
@@ -943,6 +933,23 @@
                 apis.pDwmExtendFrameIntoClientArea(hwnd, &defaultMargins);
             }
             return true;
+        } else if (key == QStringLiteral("dark-mode")) {
+            if (!isWin101809OrGreater()) {
+                return false;
+            }
+            BOOL enable = attribute.toBool();
+            for (const auto attr : {
+                    _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
+                    _DWMWA_USE_IMMERSIVE_DARK_MODE,
+            }) {
+                apis.pDwmSetWindowAttribute(hwnd, attr, &enable, sizeof(enable));
+            }
+            WINDOWCOMPOSITIONATTRIBDATA wcad{};
+            wcad.Attrib = WCA_USEDARKMODECOLORS;
+            wcad.pvData = &enable;
+            wcad.cbData = sizeof(enable);
+            apis.pSetWindowCompositionAttribute(hwnd, &wcad);
+            return true;
         }
         return false;
     }

--
Gitblit v1.9.1