From c24d1a17a72be472d559d19cfb399ffbaf40601b Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 27 Nov 2024 16:55:49 +0000 Subject: [PATCH] Windowing: Avoid changing window bounds in WM_WINDOWPOSCHANGING This change intends to address a bug observed only on Windows 10 with a display scale factor of 125%. When the native titlebar is enabled, and the window's border-resizer is used to resize the window slowly the with mouse, the client area of the window may move to the wrong location, or be drawn with some areas obscured/clipped. This is especially observable when resizing the WidgetsDemo to its smallest size, and then dragging the right border a single pixel to the right. On my computer, this consistently causes the client area to display at the wrong location. I haven't been able to find any obvious bug in JUCE that might cause this behaviour. In particular, it seems that the window begins displaying incorrectly *before* the window ever actually resizes. During the resize, the system sends events (WM_SIZING and WM_WINDOWPOSCHANGING) to the window, and according to the documentation, the window may modify the message parameters in order to constrain the new window size. When running on a scaled display, JUCE attempts to map the logical client area size to a sensible size in physical pixels, and uses the sizing messages to enforce this size requirement. In the case of the broken window rendering, the system requests a new window size, which JUCE rejects. The window's display state doesn't change, so the swap chain does not resize, and the swap chain does not present. Put another way, the broken rendering happens *independently* of JUCE modifying the swap chain in any way. Therefore, I believe that the bug is introduced elsewhere, potentially by Windows itself. I also checked to see whether the issue could be caused by mishandling of the NCCALCSIZE message, which is normally used to configure the relative positions of the client and nonclient areas. However, in the buggy case, NCCALCSIZE is not sent until *after* the first 'broken' frame is painted - and even then, the implementation immediately falls back to DefWindowProc. Given that the issue appears to be a bug in Windows, the proposed change is a workaround, rather than a true fix. It appears as though the problem goes away when WM_WINDOWPOSCHANGING does not modify the requested bounds. Therefore, for windows with native titlebars, we rely on the constraints to be applied in WM_SIZING only, when sizing the window in a sizemove gesture. --- .../native/juce_Windowing_windows.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp index 8cb548ee43..8af733c511 100644 --- a/modules/juce_gui_basics/native/juce_Windowing_windows.cpp +++ b/modules/juce_gui_basics/native/juce_Windowing_windows.cpp @@ -2231,7 +2231,7 @@ private: uint32 lastPaintTime = 0; ULONGLONG lastMagnifySize = 0; bool isDragging = false, isMouseOver = false, - hasCreatedCaret = false, constrainerIsResizing = false; + hasCreatedCaret = false, constrainerIsResizing = false, sizing = false; IconConverters::IconPtr currentWindowIcon; FileDropTarget* dropTarget = nullptr; UWPUIViewSettings uwpViewSettings; @@ -4010,13 +4010,25 @@ private: break; //============================================================================== + case WM_ENTERSIZEMOVE: + sizing = true; + break; + + case WM_EXITSIZEMOVE: + sizing = false; + break; + case WM_SIZING: + sizing = true; return handleSizeConstraining (*(RECT*) lParam, wParam); case WM_MOVING: return handleSizeConstraining (*(RECT*) lParam, 0); case WM_WINDOWPOSCHANGING: + if (hasTitleBar() && sizing) + break; + return handlePositionChanging (*(WINDOWPOS*) lParam); case 0x2e0: /* WM_DPICHANGED */ return handleDPIChanging ((int) HIWORD (wParam), *(RECT*) lParam);