From 0f8eb4b175f620ebd444ec7f9efb90c9fbd41219 Mon Sep 17 00:00:00 2001 From: Daniel Boyd Date: Wed, 11 Nov 2026 10:49:38 -0600 Subject: [PATCH] win32: fire timers and redisplay while a window is moved or sized Moving or sizing a window runs Windows' own modal loop inside DefWindowProc (WM_SYSCOMMAND SC_MOVE/SC_SIZE) until the drag ends. For that whole time the run loop doesn't run in any mode: NSTimers don't fire and nothing is redisplayed, so an indeterminate NSProgressIndicator or any timer-driven animation freezes while the user drags or resizes a window. A Win32 timer, set on WM_ENTERSIZEMOVE and killed on WM_EXITSIZEMOVE, now fires the run loop's due timers in its current mode (-limitDateForMode:, which polls no input sources, so Windows' loop keeps its own messages) and redisplays the windows that need it. It is guarded against reentry, and an exception from a timer is logged rather than unwinding through DefWindowProc. A timer may itself start a nested event loop (a modal panel, say). That loop would take the drag's input from under Windows' loop, so a later Escape or button-up never reached the drag. When a nested loop first asks for an event (-getEventMatchingMask:...) while the timer is pumping, the drag is cancelled with WM_CANCELMODE, as if Escape were pressed; Windows' loop ends once the timer returns. Testing: Tests/win32/sizemove.m (new; the directory's tests skip on every other backend). It posts its window WM_SYSCOMMAND SC_MOVE, a keyboard move that needs no mouse, from a second thread, which watches GetGUIThreadInfo's GUI_INMOVESIZE flag and ends the loop with Escape. It skips when Windows doesn't enter the loop (no interactive desktop). - "timers during a move loop": the window stays in the loop for a second, a 20 ms default-mode timer fires at least 25 times in it, and Escape still ends it. This guards against timers stopping during a drag, and against the timer taking the loop's own input. - "a modal panel started by a timer during a move loop": a timer opens a modal panel only while the window is in the loop. The panel must run and return, the drag must be over once it has, and no loop may be left running. This guards against a nested loop taking the drag's input. On master (2c7d5f1) 4 of the 7 checks fail: no ticks in the loop, so no panel during the drag; with this change all 7 pass. Both runs were on Windows 11 build 26300, MSYS2 CLANG64, clang 21.1.8, against libs-base master 220f7bdce and libs-gui master 662f89071. The test is written to fail rather than hang without the change. Measured with a separate test program over 3 s in each loop, at 96 DPI, on libs-gui 0.32.0 with the same change applied to the 0.32.0.5 backend we ship: a timer-driven progress indicator drew once without the change and 37 times with it, the same as outside the loop. In 20 runs where a timer opened a modal panel in the middle of a drag: no hangs, no lost Escape, and every drag ended before the Escape was sent. Not covered: an indicator using threaded animation still freezes. Its -setNeedsDisplay: reaches the main thread as a queued -performSelectorOnMainThread:, which only -acceptInputForMode: runs, and that would also poll Windows' input. A mouse-driven drag, which Windows ends on button-up, isn't tested, as the test can't press a button; the keyboard move runs the same loop. AI assistance: this change, its test and this description were written with Claude (Anthropic's Claude Code). Co-Authored-By: Claude Opus 5.5 --- ChangeLog | 18 +++ Headers/win32/WIN32Server.h | 2 + Source/win32/WIN32Server.m | 6 + Source/win32/w32_movesize.m | 96 +++++++++++ Tests/win32/GNUmakefile.preamble | 18 +++ Tests/win32/TestInfo | 0 Tests/win32/sizemove.m | 265 +++++++++++++++++++++++++++++++ 7 files changed, 405 insertions(+) create mode 100644 Tests/win32/GNUmakefile.preamble create mode 100644 Tests/win32/TestInfo create mode 100644 Tests/win32/sizemove.m diff --git a/ChangeLog b/ChangeLog index 2b1b7791..8470ac19 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2026-11-11 Daniel Boyd + + * Source/win32/w32_movesize.m: + * Source/win32/WIN32Server.m: + * Headers/win32/WIN32Server.h: + Fire the run loop's timers and redisplay windows while a window is + moved or sized. Windows runs its own modal loop inside DefWindowProc + for the whole drag, so timers stopped and a progress indicator froze. + A Win32 timer set on WM_ENTERSIZEMOVE and killed on WM_EXITSIZEMOVE + now fires the due timers in the run loop's current mode and displays + windows that need it. A timer that starts a nested event loop (a + modal panel) cancels the drag first, so the drag can't lose its input. + * Tests/win32/GNUmakefile.preamble: + * Tests/win32/TestInfo: + * Tests/win32/sizemove.m: + New win32 backend tests: timers during a move loop, and a modal panel + started by a timer during one. + 2026-07-22 Richard Frith-Macdonald * Source/x11/XGServerEvent.m: diff --git a/Headers/win32/WIN32Server.h b/Headers/win32/WIN32Server.h index 7c77da95..7faaa62a 100644 --- a/Headers/win32/WIN32Server.h +++ b/Headers/win32/WIN32Server.h @@ -147,6 +147,8 @@ typedef struct w32serverFlags - (void) decodeWM_WINDOWPOSCHANGEDParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd; - (LRESULT) decodeWM_GETMINMAXINFOParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd; - (LRESULT) decodeWM_ENTERSIZEMOVEParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd; +- (BOOL) decodeWM_TIMERParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd; +- (void) cancelSizeMoveForNestedEventLoop; - (LRESULT) decodeWM_EXITSIZEMOVEParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd; - (LRESULT) decodeWM_MOVINGParams: (HWND)hwnd : (WPARAM)wParam : (LPARAM)lParam; - (LRESULT) decodeWM_SIZINGParams: (HWND)hwnd : (WPARAM)wParam : (LPARAM)lParam; diff --git a/Source/win32/WIN32Server.m b/Source/win32/WIN32Server.m index 081046b5..866ebe3d 100644 --- a/Source/win32/WIN32Server.m +++ b/Source/win32/WIN32Server.m @@ -263,6 +263,8 @@ - (NSEvent*) getEventMatchingMask: (unsigned)mask dequeue: (BOOL)flag { // [self callback: nil]; + /* A nested event loop started from the move/size loop's timer. */ + [self cancelSizeMoveForNestedEventLoop]; return [super getEventMatchingMask: mask beforeDate: limit inMode: mode @@ -1231,6 +1233,10 @@ - (LRESULT) windowEventProc: (HWND)hwnd : (UINT)uMsg case WM_ENTERSIZEMOVE: return [self decodeWM_ENTERSIZEMOVEParams: wParam : lParam : hwnd]; break; + case WM_TIMER: + if ([self decodeWM_TIMERParams: wParam : lParam : hwnd]) + return 0; + break; case WM_EXITSIZEMOVE: return [self decodeWM_EXITSIZEMOVEParams: wParam : lParam : hwnd]; break; diff --git a/Source/win32/w32_movesize.m b/Source/win32/w32_movesize.m index 70ab9bc5..34c010c8 100644 --- a/Source/win32/w32_movesize.m +++ b/Source/win32/w32_movesize.m @@ -26,6 +26,10 @@ #include #include +#include +#include +#include +#include #include "win32/WIN32Server.h" #include "win32/WIN32Geometry.h" @@ -298,16 +302,108 @@ - (LRESULT) decodeWM_GETMINMAXINFOParams: (WPARAM)wParam : (LPARAM)lParam : (HWN return 0; } +/* While a window is moved or sized, Windows runs its own modal loop inside + DefWindowProc (WM_SYSCOMMAND SC_MOVE / SC_SIZE) until the drag ends, so + the GNUstep run loop doesn't run in any mode: timers stop and nothing is + redisplayed (a progress indicator freezes, threaded or not). A Win32 + timer for the length of the loop fires the run loop's due timers in its + current mode and redisplays the windows that need it. Only timers + fire: -limitDateForMode: doesn't poll input sources, so the modal loop + keeps its own messages. Not covered: work queued for the main thread + (-performSelectorOnMainThread:, so a threaded progress indicator's + redisplay), which only -acceptInputForMode: runs. + + A timer may start a nested event loop of its own (a modal panel, say). + That loop would take the drag's input from under Windows' loop, so a + later Escape or button-up never reaches the drag. Before the nested + loop asks for its first event, the drag is cancelled (WM_CANCELMODE, as + if Escape were pressed) and Windows' loop ends once the timer returns. */ +#define GS_SIZEMOVE_TIMER_ID 0x4753 +#define GS_SIZEMOVE_TIMER_MS 15 + +/* The window being moved or sized while the timer pumps the run loop. */ +static HWND pumpingWindow = NULL; + - (LRESULT) decodeWM_ENTERSIZEMOVEParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd { + SetTimer(hwnd, GS_SIZEMOVE_TIMER_ID, GS_SIZEMOVE_TIMER_MS, NULL); return DefWindowProc(hwnd, WM_ENTERSIZEMOVE, wParam, lParam); } - (LRESULT) decodeWM_EXITSIZEMOVEParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd { + KillTimer(hwnd, GS_SIZEMOVE_TIMER_ID); return DefWindowProc(hwnd, WM_EXITSIZEMOVE, wParam, lParam); } +- (void) cancelSizeMoveForNestedEventLoop +{ + HWND hwnd = pumpingWindow; + + if (hwnd != NULL) + { + /* Once is enough: the drag is over, Windows' loop just hasn't + returned yet. */ + pumpingWindow = NULL; + KillTimer(hwnd, GS_SIZEMOVE_TIMER_ID); + SendMessage(hwnd, WM_CANCELMODE, 0, 0); + } +} + +/* YES if it was the move/size loop's timer. */ +- (BOOL) decodeWM_TIMERParams: (WPARAM)wParam : (LPARAM)lParam : (HWND)hwnd +{ + static BOOL pumping = NO; + NSRunLoop *loop; + NSEnumerator *windows; + NSWindow *window; + + if (wParam != GS_SIZEMOVE_TIMER_ID) + { + return NO; + } + if (pumping) + { + return YES; + } + pumping = YES; + pumpingWindow = hwnd; + NS_DURING + { + CREATE_AUTORELEASE_POOL(pool); + + NSString *mode; + + /* The mode the run loop was in when it dispatched the message that + started the drag: the default mode, or a modal panel's during a + modal session, so timers of other modes stay quiet. */ + loop = [NSRunLoop currentRunLoop]; + mode = [loop currentMode]; + if (mode == nil) + { + mode = NSDefaultRunLoopMode; + } + [loop limitDateForMode: mode]; + windows = [[NSApp windows] objectEnumerator]; + while ((window = [windows nextObject]) != nil) + { + if ([window isVisible] && [window viewsNeedDisplay]) + { + [window displayIfNeeded]; + } + } + [pool drain]; + } + NS_HANDLER + { + NSLog(@"exception in the move/size loop's timer: %@", localException); + } + NS_ENDHANDLER + pumpingWindow = NULL; + pumping = NO; + return YES; +} + - (LRESULT) decodeWM_SIZINGParams:(HWND)hwnd : (WPARAM)wParam : (LPARAM)lParam { [EVENT_WINDOW(hwnd) displayIfNeeded]; diff --git a/Tests/win32/GNUmakefile.preamble b/Tests/win32/GNUmakefile.preamble new file mode 100644 index 00000000..41423c91 --- /dev/null +++ b/Tests/win32/GNUmakefile.preamble @@ -0,0 +1,18 @@ +# Extra build settings for the win32 backend tests. +# +# config.make names the backend being built (BUILD_SERVER); the tests carry a +# matching source-level guard (via config.h) so they still build -- and +# skip -- on every other backend. + +# config.h (used by the source-level guard) sits at the top of the (build) tree. +ADDITIONAL_INCLUDE_DIRS += -I$(GNUSTEP_BUILD_DIR)/../.. -I../.. + +-include $(GNUSTEP_BUILD_DIR)/../../config.make +-include ../../config.make + +# The tests open windows through the gui library. +ADDITIONAL_TOOL_LIBS += -lgnustep-gui + +ifeq ($(BUILD_SERVER),win32) +ADDITIONAL_TOOL_LIBS += -luser32 +endif diff --git a/Tests/win32/TestInfo b/Tests/win32/TestInfo new file mode 100644 index 00000000..e69de29b diff --git a/Tests/win32/sizemove.m b/Tests/win32/sizemove.m new file mode 100644 index 00000000..abd0d46f --- /dev/null +++ b/Tests/win32/sizemove.m @@ -0,0 +1,265 @@ +/* Timers while a window is moved or sized (Source/win32/w32_movesize.m). + * + * Moving or sizing a window runs Windows' own modal loop inside + * DefWindowProc until the drag ends, so the run loop doesn't run: timers + * stopped and nothing was redisplayed for the whole drag. The backend now + * runs a Win32 timer for the length of the loop that fires the run loop's + * due timers. A timer that starts a nested event loop (a modal panel) + * cancels the drag first, so the nested loop doesn't take the drag's input + * from under Windows' loop. + * + * The test posts the window WM_SYSCOMMAND SC_MOVE (a keyboard move, so no + * mouse is needed) from a second thread, which watches the GUI thread's + * GUI_INMOVESIZE flag, counts a default-mode timer's ticks for a second + * inside the loop, and ends the loop with Escape. It skips when Windows + * doesn't enter the loop (no interactive desktop). + * + * win32 backend only: it skips on every other backend. + */ +#import +#import "Testing.h" +#include "config.h" + +#if defined(BUILD_SERVER) && defined(SERVER_win32) \ + && BUILD_SERVER == SERVER_win32 + +#import +#define WIN32_LEAN_AND_MEAN 1 +#include + +static HWND window = NULL; +static DWORD guiThread = 0; +static volatile LONG ticks = 0; +static volatile LONG done = 0; +static volatile LONG modalWanted = 0; +static volatile LONG modalRuns = 0; +static volatile LONG modalReturned = 0; + +/* What the driving thread saw. */ +static BOOL entered = NO; +static LONG ticksInLoop = 0; +static BOOL stillInAfterSecond = NO; +static BOOL dragOverAfterModal = NO; +static BOOL endedOnEscape = NO; + +static BOOL +InMoveSize(void) +{ + GUITHREADINFO info; + + memset(&info, 0, sizeof(info)); + info.cbSize = sizeof(info); + if (GetGUIThreadInfo(guiThread, &info) == 0) + { + return NO; + } + return (info.flags & GUI_INMOVESIZE) ? YES : NO; +} + +static BOOL +WaitForMoveSize(BOOL wanted, int milliseconds) +{ + int i; + + for (i = 0; i < milliseconds / 10; i++) + { + if (InMoveSize() == wanted) + { + return YES; + } + Sleep(10); + } + return (InMoveSize() == wanted); +} + +@interface Driver : NSObject +- (void) tick: (NSTimer *)timer; +- (void) stopPanel: (NSTimer *)timer; +- (void) drive: (id)modal; +@end + +@implementation Driver + +- (void) stopPanel: (NSTimer *)timer +{ + /* -stopModal takes effect when the modal loop next gets an event, so + give it one. */ + [NSApp stopModal]; + [NSApp postEvent: [NSEvent otherEventWithType: NSApplicationDefined + location: NSZeroPoint + modifierFlags: 0 + timestamp: 0 + windowNumber: 0 + context: nil + subtype: 0 + data1: 0 + data2: 0] + atStart: NO]; +} + +- (void) tick: (NSTimer *)timer +{ + InterlockedIncrement(&ticks); + /* Only from inside the move loop: without timers there, the panel + would open after the drag instead and prove nothing. */ + if (modalWanted && modalRuns == 0 && InMoveSize()) + { + NSPanel *panel; + NSTimer *stop; + + InterlockedIncrement(&modalRuns); + panel = [[NSPanel alloc] initWithContentRect: NSMakeRect(300, 300, 200, 80) + styleMask: NSTitledWindowMask + backing: NSBackingStoreBuffered + defer: NO]; + stop = [NSTimer timerWithTimeInterval: 0.3 + target: self + selector: @selector(stopPanel:) + userInfo: nil + repeats: NO]; + [[NSRunLoop currentRunLoop] addTimer: stop + forMode: NSModalPanelRunLoopMode]; + [NSApp runModalForWindow: panel]; + [panel orderOut: nil]; + RELEASE(panel); + InterlockedIncrement(&modalReturned); + } +} + +- (void) drive: (id)modal +{ + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + LONG before; + int i; + + Sleep(300); + PostMessage(window, WM_SYSCOMMAND, SC_MOVE, 0); + entered = WaitForMoveSize(YES, 2000); + if (entered) + { + if (modal != nil) + { + modalWanted = 1; + for (i = 0; i < 300 && modalReturned == 0; i++) + { + Sleep(10); + } + modalWanted = 0; + Sleep(200); + dragOverAfterModal = (InMoveSize() == NO); + } + else + { + before = ticks; + Sleep(1000); + ticksInLoop = ticks - before; + stillInAfterSecond = InMoveSize(); + } + PostMessage(window, WM_KEYDOWN, VK_ESCAPE, 0); + PostMessage(window, WM_KEYUP, VK_ESCAPE, 0); + endedOnEscape = WaitForMoveSize(NO, 2000); + if (endedOnEscape == NO) + { + /* Leave no drag behind for the next set. */ + PostMessage(window, WM_CANCELMODE, 0, 0); + WaitForMoveSize(NO, 2000); + } + } + InterlockedExchange(&done, 1); + PostMessage(window, WM_NULL, 0, 0); + [pool drain]; +} + +@end + +/* Runs one drag in the default mode, with a timer ticking every 20 ms. */ +static void +RunDrag(Driver *driver, BOOL modal) +{ + NSDate *limit = [NSDate dateWithTimeIntervalSinceNow: 15.0]; + NSTimer *timer; + + ticks = 0; + done = 0; + modalWanted = 0; + modalRuns = 0; + modalReturned = 0; + entered = NO; + timer = [NSTimer timerWithTimeInterval: 0.02 + target: driver + selector: @selector(tick:) + userInfo: nil + repeats: YES]; + [[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode]; + [NSThread detachNewThreadSelector: @selector(drive:) + toTarget: driver + withObject: (modal ? (id)@"modal" : nil)]; + while (done == 0 && [limit timeIntervalSinceNow] > 0) + { + [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode + beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]]; + } + [timer invalidate]; +} + +int +main(void) +{ + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + Driver *driver; + NSWindow *win; + + [NSApplication sharedApplication]; + guiThread = GetCurrentThreadId(); + driver = [Driver new]; + win = [[NSWindow alloc] initWithContentRect: NSMakeRect(200, 200, 300, 200) + styleMask: (NSTitledWindowMask | NSResizableWindowMask) + backing: NSBackingStoreBuffered + defer: NO]; + [win setTitle: @"sizemove"]; + [win orderFront: nil]; + window = (HWND)(intptr_t)[win windowNumber]; + + START_SET("timers during a move loop") + RunDrag(driver, NO); + if (entered == NO) + { + SKIP("Windows didn't enter the move loop (no interactive desktop?)") + } + PASS(stillInAfterSecond, "the window stays in the move loop for the second") + PASS(ticksInLoop >= 25, + "a 20 ms default-mode timer fires during the move loop (%ld ticks in 1 s)", + (long)ticksInLoop) + PASS(endedOnEscape, "Escape still ends the move loop") + END_SET("timers during a move loop") + + START_SET("a modal panel started by a timer during a move loop") + RunDrag(driver, YES); + if (entered == NO) + { + SKIP("Windows didn't enter the move loop (no interactive desktop?)") + } + PASS(modalRuns == 1, "the timer runs the modal panel during the drag") + PASS(modalReturned == 1, "the modal panel returns") + PASS(dragOverAfterModal, "the drag has ended once the modal panel returns") + PASS(endedOnEscape, "no move loop is left running") + END_SET("a modal panel started by a timer during a move loop") + + [win close]; + RELEASE(driver); + [pool drain]; + return 0; +} + +#else + +int +main(void) +{ + START_SET("timers during a move loop") + SKIP("win32 backend tests: the win32 backend isn't being built") + END_SET("timers during a move loop") + return 0; +} + +#endif