From 2d223770ee33ddf7d93d83bc2cb538bfd66d38d9 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周三, 08 5月 2024 11:30:55 +0800
Subject: [PATCH] update

---
 src/core/contexts/win32windowcontext.cpp |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 661e0e8..93a186c 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -1376,7 +1376,7 @@
                     if (lastHitTestResultRaw == HTSYSMENU) {
                         switch (message) {
                             case WM_NCLBUTTONDOWN:
-                                if (iconButtonClickTime == 0) {
+                                if (iconButtonClickLevel == 0) {
                                     // A message of WM_SYSCOMMAND with SC_MOUSEMENU will be sent by
                                     // Windows, and the current control flow will be blocked by the
                                     // menu while Windows will create and execute a new event loop
@@ -1384,10 +1384,13 @@
                                     iconButtonClickTime = ::GetTickCount64();
                                     *result =
                                         ::DefWindowProcW(hWnd, WM_NCLBUTTONDOWN, wParam, lParam);
-                                    iconButtonClickTime = 0;
+                                    if (iconButtonClickLevel == 2) {
+                                        ::PostMessageW(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
+                                    }
+                                    // No need to reset `iconButtonClickLevel`, if it has value,
+                                    // there must be another incoming WM_NCLBUTTONDOWN
                                 } else {
-                                    *result = FALSE;
-                                    emulateClientAreaMessage(hWnd, message, wParam, lParam);
+                                    iconButtonClickLevel = 0;
                                 }
                                 break;
                             case WM_NCLBUTTONDBLCLK:
@@ -2144,6 +2147,8 @@
             static HHOOK mouseHook = nullptr;
             static std::optional<POINT> mouseClickPos;
             bool mouseHookedLocal = false;
+
+            // The menu is triggered by a click on icon button
             if (iconButtonClickTime > 0) {
                 POINT menuPos{0, static_cast<LONG>(getTitleBarHeight(hWnd))};
                 if (const auto tb = titleBar()) {
@@ -2173,6 +2178,7 @@
                     mouseHookedLocal = true;
                 }
             }
+
             bool res = showSystemMenu_sys(hWnd, nativeGlobalPos, broughtByKeyboard,
                                           m_delegate->isHostSizeFixed(m_host));
 
@@ -2182,8 +2188,7 @@
 
                 // Emulate the Windows icon button's behavior
                 static uint32_t doubleClickTime = ::GetDoubleClickTime();
-                if (!res && mouseClickPos.has_value() &&
-                    ::GetTickCount64() - iconButtonClickTime <= doubleClickTime) {
+                if (!res && mouseClickPos.has_value()) {
                     POINT nativeLocalPos = mouseClickPos.value();
                     ::ScreenToClient(hWnd, &nativeLocalPos);
                     QPoint qtScenePos = QHighDpi::fromNativeLocalPosition(
@@ -2191,7 +2196,10 @@
                     WindowAgentBase::SystemButton sysButtonType = WindowAgentBase::Unknown;
                     if (isInSystemButtons(qtScenePos, &sysButtonType) &&
                         sysButtonType == WindowAgentBase::WindowIcon) {
-                        ::PostMessageW(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
+                        iconButtonClickLevel = 1;
+                        if (::GetTickCount64() - iconButtonClickTime <= doubleClickTime) {
+                            iconButtonClickLevel = 2;
+                        }
                     }
                 }
 

--
Gitblit v1.9.1