Files
alan 8361322fe2 Keep floating window moves pixel-stable
Round fitted window frames to whole logical pixels and accumulate drag deltas from the gesture start. This prevents floating-point size noise from rebuilding window content while preserving fractional pointer movement and resizing behavior.
2026-11-11 03:28:34 +00:00

9.9 KiB

Performance round 5: moving a window does not rebuild its content

Round 5's candidate: every pointer event of a window drag rebuilt every window's whole content. WindowScope carried the body Rect and compared the whole WindowEntry, the navigator's ThemeData was rebuilt with copyWith on every build, and the root MaterialPage was a new object on every build.

Change

lib/shell/windows/:

  • window_scope.dart: WindowScope keeps only manager and entry, and notifies only when what content can read changes (id, kind, contentId, serverKey, data). A new WindowBodyScope (an InheritedWidget with the body Rect) carries the position and size that used to be in WindowScope.
  • window_navigator.dart: WindowNavigator.theme(context, child:) became WindowNavigator.themeData(theme), so the layer can keep the page-transition ThemeData. WindowPageBox reads the body from WindowBodyScope, and its page child is a RepaintBoundary.
  • window_layer.dart: _WindowLayerState keeps the page-transition theme in a field and rebuilds it only when Theme.of(context) is a different object; it keeps the root MaterialPage in a field (same key) and rebuilds it only when the kind changes. WindowFrame is wrapped in a RepaintBoundary.

No behaviour change: frames, snapping, resize limits, popups, pushed pages, minimise/maximise, focus and the shell's APIs are as before.

tool/capture/window_probe.dart: ProbeContentState.lastSize records the MediaQuery size the content built at, so tests can see a resize reach it (no visual change).

Deterministic build counts

test/shell/windows/window_rebuild_test.dart:

  • dragging a window's title over 8 pointer moves: the dragged window's content and the other window's content keep their builds count (0 rebuilds);
  • resizing a window over 5 pointer moves: the other window keeps its count, the resized window's content rebuilds and sees the new MediaQuery size (360x380 -> 280x330 body);
  • manager.setContent retargets a content whose element is keyed stably: the same state rebuilds with the new contentId.

Against the old implementation (lib changes stashed, test run unchanged): 2 failed — dragged window rebuilt at move 0 (builds 2 -> 3) and other window rebuilt at move 0 (2 -> 3). The new code passes all 3.

Browser bench

Built twice: the old code (lib/shell/windows changes stashed) into build/probe-before, and this branch into build/probe-app:

/Users/alan/development/flutter/bin/flutter build web \
  --target tool/search_selection_probe_entry.dart --output build/probe-before
/Users/alan/development/flutter/bin/flutter build web \
  --target tool/search_selection_probe_entry.dart --output build/probe-app

Run, from app/, against each build in turn:

node tool/perf/interaction_bench.mjs --build build/probe-before --out /tmp/bench \
  --gpu --only 'desktop: drag a probe window' --repeat 3
node tool/perf/interaction_bench.mjs --build build/probe-app --out /tmp/bench \
  --gpu --only 'desktop: drag a probe window' --repeat 3

Three before/after rounds (the machine was shared with other pipeline test runs; load average 9-14 during these, so frame counts vary. buildMs/frame = buildSum / frames):

round code frames buildSum ms/frame buildMax longest over16 rasterSum
1 before 50 952.2 19.0 63.5 73.9 31 107.4
1 after 49 824.8 16.8 59.1 74.0 27 107.9
2 before 49 615.6 12.6 25.5 28.4 15 62.1
2 after 51 583.0 11.4 37.4 39.9 13 87.2
3 before 51 527.9 10.4 16.2 20.3 9 68.3
3 after 47 606.8 12.9 24.1 26.9 18 83.5

The browser numbers are inconclusive on this loaded machine: only round 1 shows less build work, and the spread between rounds (10.4-19.0 ms/frame on the same code) is larger than the change. The reliable result is the build count: the drag went from one content build per pointer event per window (120 builds over a 40-move drag with three windows) to none. Window chrome and framework layout still run on every pointer event; that is the remaining build cost in this scenario, not content.

Checks

  • tool/memwatch/full.sh 'test/shell/windows test/features/chat/chat_floating_pane_test.dart test/features/terminal': 119 passed, 0 failed.
  • tool/analyze.sh: clean.

Affected UI states (for ui-review)

/runs?windows=2 at 1280x800 light: the windows after a title drag (drag=250,132>300,150), after a corner resize (drag=483,523>120,80), and a page pushed inside a window (click page) then dragged: the content must follow the window, stay inside it, and show no stale or clipped frame.

Whole-pixel frames

Follow-up: after the change above, a drag still built and laid out every window's content on every pointer event (14-17 ms of build per frame in the browser bench). A Rect stores left, top, right and bottom, so shifting a fractional frame changes its width in the last bits (360.00000000000006), and WindowPageBox puts that size in a MediaQuery: every MediaQuery dependent rebuilt and laid out again on every move. The layer also regenerated its page theme on every frame, because Material's animated theme hands over a new but equal ThemeData object and _pageThemeFor compared with identical, so every theme dependent rebuilt too.

Change

lib/shell/windows/:

  • window_entry.dart: fitWindowFrame rounds left, top, width and height to whole logical pixels after clamping, keeping the frame inside the bounds and at the minimum size. A fractional minimum rounds up, so a whole-pixel frame is never smaller than the minimum the kind asked for (the bounds still win when they cannot hold it). Whole numbers add and subtract exactly, so a moved window keeps exactly its size.
  • window_layer.dart: _WindowLayerState remembers the frame a move or resize started from and the pointer delta summed since, and sets the frame from start + total on every event, so rounding one fractional step after another cannot lose part of the pointer's movement or drift. The drag state is cleared in _moveEnded, in the corner's onPanEnd (_resizeEnded, which still calls manager.commit) and on pointer down.
  • window_layer.dart: _pageThemeFor reuses the cached page theme when the app theme is == to the last one, not only identical, so an equal theme from an animation does not notify every content widget.

Snapping, manager.commit, windowSnapRect (still fractional where the bounds are) and a maximised frame are unchanged.

Tests

test/shell/windows/window_rebuild_test.dart:

  • dragging a title by eight Offset(10.3, 4.7) moves with debugOnRebuildDirtyWidget (reset in a finally): no TextButton, ListView or Scrollable element inside either window's content rebuilds, the WindowFrame size stays exactly 360x420 after every move, and the window ends within 1 px of start + 8 x (10.3, 4.7) (100,80 -> 182,118);
  • resizing by five Offset(-10.3, -6.7) moves: every frame is a whole-pixel size, the other window does not rebuild, and the resized window's content rebuilds with the new MediaQuery size (within 1 px of 360x420 - 5 x (10.3, 6.7)).

test/shell/windows/window_store_test.dart also pins the fit: a fractional minimum (320.1x240.4) yields a whole-pixel 321x241 frame, and bounds smaller than that minimum (319.6x239.2) yield 319x239 instead of exceeding the area.

Against the old lib (lib/shell/windows changes stashed, tests unchanged): 2 failed - the drag rebuilt ListView, Scrollable and TextButton at move 1, and the first resize move gave a fractional 349.70000000000005 width. The new code passes all 5.

Browser bench

Built after (build/probe-app) and before (build/probe-before, lib change stashed):

/Users/alan/development/flutter/bin/flutter build web \
  --target tool/search_selection_probe_entry.dart --output build/probe-app
/Users/alan/development/flutter/bin/flutter build web \
  --target tool/search_selection_probe_entry.dart --output build/probe-before

Then, from app/:

node tool/perf/interaction_bench.mjs --build build/probe-before --out /tmp/bench \
  --gpu --repeat 3 --only 'desktop: drag a probe window'
node tool/perf/interaction_bench.mjs --build build/probe-app --out /tmp/bench \
  --gpu --repeat 3 --only 'desktop: drag a probe window'

The machine was shared with other pipeline test runs (load average 11.8-75.7 during the rounds; ms/frame = buildSum / frames):

round load code frames buildSum ms/frame buildMax longest over16
1 22.1 before 54 995.1 18.4 77.8 80.4 29
1 22.1 after 42 419.2 10.0 25.8 30.3 11
2 11.8 before 51 489.8 9.6 20.2 22.4 3
2 11.8 after 42 549.8 13.1 44.4 59.7 14
3 75.7 before 48 4590.1 95.6 320.0 437.1 44
3 75.7 after 42 1741.3 41.5 136.3 148.9 36

The browser numbers stay indicative on this loaded machine: round 2 has the before build ahead, while rounds 1 and 3 (including the heavily loaded one) show the after build at about half the build time per frame. The deterministic result is the widget test: a fractional drag no longer rebuilds any content, and the window size is bit-identical from frame to frame.

Checks

  • tool/memwatch/full.sh 'test/shell/windows test/features/chat/chat_floating_pane_test.dart test/features/terminal': 121 passed, 0 failed.
  • tool/analyze.sh: clean.

Affected UI states (for ui-review)

/runs?windows=2 at 1280x800 light: drag the top window by its title (drag=250,132>301,153), then resize it by its corner: the title bar, body and edges must stay aligned with no gap or overlap of a pixel, the content must not reflow during the drag, and the resize corner must stay on the window's lower right corner.