From 8209cdfb1d85a40cc770f854d773c3a10f5ab576 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周三, 20 12月 2023 17:32:57 +0800
Subject: [PATCH] Add hot-switch to agent

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

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 145b19c..3b2d3e8 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -845,6 +845,7 @@
         g_wndProcHash->insert(hWnd, ctx);
     }
 
+    template <bool Destroyed = true>
     static inline void removeManagedWindow(HWND hWnd) {
         // Remove window handle mapping
         if (!g_wndProcHash->remove(hWnd))
@@ -853,6 +854,11 @@
         // Remove event filter if the all windows has been destroyed
         if (g_wndProcHash->empty()) {
             WindowsNativeEventFilter::uninstall();
+        }
+
+        // Restore window proc
+        if constexpr (!Destroyed) {
+            ::SetWindowLongPtrW(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_qtWindowProc));
         }
     }
 
@@ -964,9 +970,8 @@
                 return;
             }
 
-            default: {
+            default:
                 break;
-            }
         }
         AbstractWindowContext::virtual_hook(id, data);
     }
@@ -979,8 +984,12 @@
         return getWindowFrameBorderThickness(reinterpret_cast<HWND>(windowId));
     }
 
-    void Win32WindowContext::winIdChanged(QWindow *oldWindow) {
-        removeManagedWindow(reinterpret_cast<HWND>(windowId));
+    void Win32WindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) {
+        if (isDestroyed) {
+            removeManagedWindow(reinterpret_cast<HWND>(windowId));
+        } else {
+            removeManagedWindow<false>(reinterpret_cast<HWND>(windowId));
+        }
         if (!m_windowHandle) {
             return;
         }
@@ -1000,7 +1009,7 @@
 #endif
 
         // Inform Qt we want and have set custom margins
-        updateInternalWindowFrameMargins(hWnd, m_windowHandle);
+        updateInternalWindowFrameMargins(hWnd, m_windowHandle); // TODO: Restore?
 
         // Add managed window
         addManagedWindow(hWnd, this);
@@ -1296,9 +1305,9 @@
                         // widget to be implicitly grabbed. After the menu is closed, Windows
                         // immediately sends WM_NCHITTEST, because the mouse is in the title bar
                         // draggable area, the result is HTCAPTION, so when the mouse is released,
-                        // Windows sends WM_NCLBUTTONUP, which is a non-client area message, and it
+                        // Windows sends WM_NCLBUTTONUP, which is a non-client message, and it
                         // will be ignored by Qt. As a consequence, QWidgetWindow can't receive a
-                        // mouse release messages in the client area, so the grab remains, and other
+                        // mouse release message in the client area, so the grab remains, and other
                         // widgets cannot receive mouse events.
 
                         // Since we didn't watch the menu window, we cannot capture any mouse
@@ -1609,7 +1618,7 @@
                         *result = (isTitleBar ? HTCAPTION : HTCLIENT);
                         return true;
                     }
-                    // At this point, we know that the cursor is inside the client area
+                    // At this point, we know that the cursor is inside the client area,
                     // so it has to be either the little border at the top of our custom
                     // title bar or the drag bar. Apparently, it must be the drag bar or
                     // the little border at the top which the user can use to move or

--
Gitblit v1.9.1