Skip to content

Commit e64ad99

Browse files
committed
Windows: report window moves and resizes from WM_WINDOWPOSCHANGED
The process-wide EVENT_OBJECT_LOCATIONCHANGE WinEvent hook added for the moved/resized/minimized/maximized/restored events is delivered through the UI thread's message queue, so its callbacks ran whenever that thread read a message, including inside Flutter's resize while it pumps messages waiting for a frame of the new size. With a listener attached every resize step called into Dart from there and the window lagged behind the pointer. Subclass each reported window instead and emit after its own window procedure has handled WM_WINDOWPOSCHANGED. Showing and destroying still come from a (rare) WinEvent hook. Fixes libnativeapi/nativeapi#17
1 parent e723f31 commit e64ad99

1 file changed

Lines changed: 48 additions & 20 deletions

File tree

‎src/platform/windows/window_manager_windows.cpp‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <windows.h>
2+
#include <commctrl.h>
23
#include <iostream>
34
#include <string>
45
#include <unordered_map>
@@ -104,16 +105,22 @@ static void CALLBACK ForegroundEventProc(HWINEVENTHOOK hook,
104105
// Geometry and show-state tracking for WindowMinimizedEvent, WindowMaximizedEvent,
105106
// WindowRestoredEvent, WindowMovedEvent and WindowResizedEvent. The windows are
106107
// usually owned by someone else (Flutter's runner, the host application), so
107-
// there is no window procedure to read WM_SIZE / WM_MOVE from. A WinEvent hook
108-
// scoped to this process reports every change of a window's rectangle instead,
109-
// minimizing and maximizing included, and the snapshot kept per window tells
110-
// which of the five events a change amounts to.
108+
// the library subclasses each one it reports on and reads WM_WINDOWPOSCHANGED,
109+
// which every change of a window's rectangle goes through, minimizing and
110+
// maximizing included. The snapshot kept per window tells which of the five
111+
// events a change amounts to.
112+
//
113+
// Not a WinEvent hook on EVENT_OBJECT_LOCATIONCHANGE: out-of-context WinEvents
114+
// are queued to this thread and delivered whenever it next reads a message —
115+
// in the middle of Flutter's resize, which pumps messages while it waits for a
116+
// frame of the new size, among other places — and the listeners called from
117+
// there (Dart ones synchronously) made every resize step late. The subclass
118+
// reports after the window's own procedure, and so Flutter, is done with it.
111119
struct WindowSnapshot {
112120
bool minimized;
113121
bool maximized;
114122
RECT rect;
115123
};
116-
static HWINEVENTHOOK g_location_hook = nullptr;
117124
static HWINEVENTHOOK g_lifetime_hook = nullptr;
118125
static std::unordered_map<HWND, WindowSnapshot> g_window_snapshots;
119126
// Windows seen on screen, which is what WindowCreatedEvent and WindowClosedEvent
@@ -144,6 +151,31 @@ static bool IsReportableWindow(HWND hwnd) {
144151
return !(GetWindowLongPtr(hwnd, GWL_EXSTYLE) & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE));
145152
}
146153

154+
static LRESULT CALLBACK WindowGeometryProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp,
155+
UINT_PTR subclass_id, DWORD_PTR) {
156+
if (message == WM_NCDESTROY) {
157+
RemoveWindowSubclass(hwnd, WindowGeometryProc, subclass_id);
158+
} else if (message == WM_WINDOWPOSCHANGED) {
159+
const LRESULT result = DefSubclassProc(hwnd, message, wp, lp);
160+
if (g_window_changed_fn && g_window_changed_context) {
161+
g_window_changed_fn(g_window_changed_context, EVENT_OBJECT_LOCATIONCHANGE, hwnd);
162+
}
163+
return result;
164+
}
165+
return DefSubclassProc(hwnd, message, wp, lp);
166+
}
167+
168+
// Starts reporting geometry changes of `hwnd`. Only windows of the calling
169+
// thread can be subclassed; the others are tracked for creation and closing
170+
// only.
171+
static void WatchWindowGeometry(HWND hwnd) {
172+
SetWindowSubclass(hwnd, WindowGeometryProc, reinterpret_cast<UINT_PTR>(&WindowGeometryProc), 0);
173+
}
174+
175+
static void UnwatchWindowGeometry(HWND hwnd) {
176+
RemoveWindowSubclass(hwnd, WindowGeometryProc, reinterpret_cast<UINT_PTR>(&WindowGeometryProc));
177+
}
178+
147179
static void CALLBACK WindowChangedEventProc(HWINEVENTHOOK hook,
148180
DWORD event,
149181
HWND hwnd,
@@ -155,8 +187,8 @@ static void CALLBACK WindowChangedEventProc(HWINEVENTHOOK hook,
155187
(void)event_thread;
156188
(void)event_time;
157189

158-
// OBJID_WINDOW leaves out the caret and the cursor, which report location
159-
// changes through the same event.
190+
// OBJID_WINDOW leaves out the caret and the cursor, which report showing
191+
// through the same event.
160192
if (id_object != OBJID_WINDOW || id_child != CHILDID_SELF || !hwnd) {
161193
return;
162194
}
@@ -450,18 +482,13 @@ class WindowManager::Impl {
450482
static_cast<Impl*>(impl)->OnWindowChanged(event, hwnd);
451483
};
452484

453-
// Unlike the foreground hook these only concern our own windows, so they
454-
// are scoped to this process and cost nothing while other applications work.
455-
const DWORD process_id = GetCurrentProcessId();
456-
if (!g_location_hook) {
457-
g_location_hook =
458-
SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, nullptr,
459-
WindowChangedEventProc, process_id, 0, WINEVENT_OUTOFCONTEXT);
460-
}
485+
// Unlike the foreground hook this only concerns our own windows, so it is
486+
// scoped to this process and costs nothing while other applications work.
487+
// Showing and destroying are rare, unlike location changes (see above).
461488
if (!g_lifetime_hook) {
462489
// EVENT_OBJECT_DESTROY and EVENT_OBJECT_SHOW are adjacent
463490
g_lifetime_hook = SetWinEventHook(EVENT_OBJECT_DESTROY, EVENT_OBJECT_SHOW, nullptr,
464-
WindowChangedEventProc, process_id, 0,
491+
WindowChangedEventProc, GetCurrentProcessId(), 0,
465492
WINEVENT_OUTOFCONTEXT);
466493
}
467494

@@ -470,6 +497,7 @@ class WindowManager::Impl {
470497
[](HWND hwnd, LPARAM) -> BOOL {
471498
if (IsOwnProcessWindow(hwnd) && IsReportableWindow(hwnd)) {
472499
g_window_snapshots[hwnd] = TakeSnapshot(hwnd);
500+
WatchWindowGeometry(hwnd);
473501
// Already on screen, so not created under our eyes: no
474502
// WindowCreatedEvent for it, only the WindowClosedEvent.
475503
if (IsWindowVisible(hwnd)) {
@@ -489,10 +517,6 @@ class WindowManager::Impl {
489517
UnhookWinEvent(g_foreground_hook);
490518
g_foreground_hook = nullptr;
491519
}
492-
if (g_location_hook) {
493-
UnhookWinEvent(g_location_hook);
494-
g_location_hook = nullptr;
495-
}
496520
if (g_lifetime_hook) {
497521
UnhookWinEvent(g_lifetime_hook);
498522
g_lifetime_hook = nullptr;
@@ -502,6 +526,9 @@ class WindowManager::Impl {
502526
g_foreground_changed_context = nullptr;
503527
g_window_changed_fn = nullptr;
504528
g_window_changed_context = nullptr;
529+
for (const auto& [hwnd, snapshot] : g_window_snapshots) {
530+
UnwatchWindowGeometry(hwnd);
531+
}
505532
g_window_snapshots.clear();
506533
g_shown_windows.clear();
507534
}
@@ -545,6 +572,7 @@ class WindowManager::Impl {
545572
if (it == g_window_snapshots.end()) {
546573
// First sight (a window being shown): nothing to compare with yet
547574
g_window_snapshots[hwnd] = current;
575+
WatchWindowGeometry(hwnd);
548576
return;
549577
}
550578
if (event != EVENT_OBJECT_LOCATIONCHANGE) {

0 commit comments

Comments
 (0)