Repository navigation
Description
Summary
On Windows, the WindowManager constructor (StartEventListening → InstallOcclusionHooks) installs out-of-context WinEvent hooks for all processes (idProcess = 0, idThread = 0). One of them covers EVENT_OBJECT_LOCATIONCHANGE. That event fires on every cursor move (OBJID_CURSOR) and every window move anywhere on the desktop. Out-of-context events are delivered on the thread that installed the hook, which in a Flutter app is the platform/UI thread, and they arrive whenever that thread reads messages.
When the user drags the window border to resize, the same thread is running the modal size loop and Flutter's synchronous resize, which waits for a frame at the new size. The window edge falls further and further behind the mouse during the drag and keeps moving after the mouse stops.
The hooks are installed as soon as WindowManager is created (for example by WindowManager.instance.getCurrent()). They are installed even when nothing listens for WindowOcclusionChangedEvent.
- Affected: window_manager 0.6.0 with nativeapi / nativeapi_flutter / cnativeapi 0.5.10. The same code is still in cnativeapi 0.6.1 and on
mainof nativeapi-core (src/platform/windows/window_manager_windows.cpp,InstallOcclusionHooks). - OS / toolchain: Windows Server 2022 (build 20348), Flutter 3.47.9 (Dart 3.13.7), release builds. We first saw it on regular Windows desktops: the border can't keep up with the mouse. The numbers below come from a VM without a GPU (Microsoft Basic Display Adapter, software rendering). Absolute frame times there are high, so compare the variants with each other, not with real hardware.
Reproduction
Minimal app (flutter create --platforms=windows, release build), window_manager: 0.6.0, lib/main.dart:
import 'package:flutter/widgets.dart';
import 'package:window_manager/window_manager.dart';
const bool kUseWm = bool.fromEnvironment('USE_WM');
void main() {
WidgetsFlutterBinding.ensureInitialized();
if (kUseWm) {
final Window? w = WindowManager.instance.getCurrent();
if (w != null) {
w.titleBarStyle = TitleBarStyle.hidden;
w.isWindowControlButtonsVisible = true;
w.minimumSize = const Size(360, 640).toNative();
w.addListener((WindowEvent e) {});
}
}
runApp(const ProbeApp());
}
class ProbeApp extends StatelessWidget {
const ProbeApp({super.key});
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: ColoredBox(
color: const Color(0xFFF5F5F0),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints c) => Wrap(
children: <Widget>[
for (int i = 0; i < 150; i++)
SizedBox(
width: c.maxWidth / 12,
height: 22,
child: Text('cell $i ${c.maxWidth.round()}',
style: const TextStyle(fontSize: 11, color: Color(0xFF333333))),
),
],
),
),
),
);
}
}Variants:
- A:
--dart-define=USE_WM=false. The package is linked, butWindowManageris never created. - B:
--dart-define=USE_WM=true. - C: same as B, with
cnativeapioverridden by a local copy of 0.5.10 in whichInstallOcclusionHooksskips theEVENT_OBJECT_LOCATIONCHANGErange. That is the only change:
for (int i = 0; i < 5; ++i)
+ if (ranges[i][0] != EVENT_OBJECT_LOCATIONCHANGE)
g_occlusion_hooks[i] = SetWinEventHook(ranges[i][0], ranges[i][1], nullptr, OcclusionEventProc,
0, 0, WINEVENT_OUTOFCONTEXT);Measurement: an external process drags the right edge with SendInput: button down, then absolute moves every 8 ms, 400 px over 1 s, with "Show window contents while dragging" turned on. It polls GetWindowRect about every 1 ms. Lag is the time from the cursor reaching an x position to the window edge reaching it. Each variant was dragged 10 times, in two interleaved rounds:
| Variant | Lag p90 per drag (ms) |
|---|---|
A (no WindowManager) |
10.9 – 15.0 (all 10 drags) |
B (WindowManager created) |
12.3 – 88.2; 7 of 10 drags above 18; two drags had a median of 43 – 50 |
| C (B without the LOCATIONCHANGE hook) | 11.4 – 14.2 (all 10 drags) |
The probe's frames are light. In a larger production app with heavier frames, the effect is much stronger:
- With window_manager 0.6.0, the edge trails the cursor by a median of 85 – 217 ms and keeps moving for 230 – 420 ms after the mouse stops.
- An older release of the same app, built with window_manager 0.5.2 (and Flutter 3.44), trails by about 9 ms on the same machine.
Why we think it is this hook
- The comment above the geometry tracking in the same file already describes this problem with a
EVENT_OBJECT_LOCATIONCHANGEWinEvent hook: deliveries in the middle of Flutter's resize "made every resize step late". Geometry tracking moved to a window subclass, but the occlusion hooks bring back a system-wideEVENT_OBJECT_LOCATIONCHANGEhook on the same thread. - Also, every location change of any top-level window, including ours on every resize step, arms the 100 ms occlusion timer.
RefreshOcclusiontherefore runs again and again during a drag. We have not measured how much this part contributes. - Removing only that hook range (variant C) brings B back to A.
Possible fixes
- Install the occlusion hooks only while something needs occlusion state: a listener for
WindowOcclusionChangedEvent, or a caller of the occlusion getter. Don't install them unconditionally in theWindowManagerconstructor. - Avoid a system-wide
EVENT_OBJECT_LOCATIONCHANGEhook. It can't be filtered by object at the source, so every cursor move is delivered. One option is to recompute occlusion on show / hide / reorder / foreground / minimize / cloak events plus our own windows'WM_WINDOWPOSCHANGED. Another is to suspend it betweenWM_ENTERSIZEMOVEandWM_EXITSIZEMOVE. - An opt-out, such as a flag that disables occlusion tracking, would also work for apps that don't use it.
We're happy to test a patch.
中文摘要: Windows 上,WindowManager 一创建就无条件挂上一组全系统(所有进程)的 out-of-context WinEvent 钩子,其中有 EVENT_OBJECT_LOCATIONCHANGE。光标每动一下、任何窗口一挪,这个事件都会投递到 Flutter 的 UI 线程。拖动改窗口大小时,边框越来越跟不上鼠标。最小复现里只去掉这一个钩子就恢复正常,数据见上表。建议有人订阅遮挡事件时才挂钩子,或者不用全系统的 LOCATIONCHANGE。
Activity
Merge pull request #43 from libnativeapi/fix/issue-40