diff --git a/app/lib/shell/windows/window_entry.dart b/app/lib/shell/windows/window_entry.dart index 584c861f..83fe7afe 100644 --- a/app/lib/shell/windows/window_entry.dart +++ b/app/lib/shell/windows/window_entry.dart @@ -75,22 +75,54 @@ String windowFrameKey(String kind, String? serverKey, String contentId) => '$kind\n${serverKey ?? ''}\n$contentId'; /// [rect] moved and shrunk to fit inside [bounds], at least [minSize] where -/// the bounds allow it. +/// the bounds allow it, on whole logical pixels. +/// +/// Whole pixels keep a moved window exactly its size from frame to frame: a +/// `Rect` stores left, top, right and bottom, so shifting a frame by a +/// fractional delta makes its width differ in its last bits, and every widget +/// that reads the window's size (its `MediaQuery`) rebuilds and lays out +/// again on every pointer event. Rect fitWindowFrame(Rect rect, Size bounds, Size minSize) { - final width = rect.width - .clamp(minSize.width, double.infinity) - .clamp(0.0, bounds.width); - final height = rect.height - .clamp(minSize.height, double.infinity) - .clamp(0.0, bounds.height); + final width = _wholePixel( + rect.width.clamp(minSize.width, double.infinity).clamp(0.0, bounds.width), + minSize.width, + bounds.width, + ); + final height = _wholePixel( + rect.height + .clamp(minSize.height, double.infinity) + .clamp(0.0, bounds.height), + minSize.height, + bounds.height, + ); return Rect.fromLTWH( - rect.left.clamp(0.0, bounds.width - width), - rect.top.clamp(0.0, bounds.height - height), + _wholePixel( + rect.left.clamp(0.0, bounds.width - width), + 0, + bounds.width - width, + ), + _wholePixel( + rect.top.clamp(0.0, bounds.height - height), + 0, + bounds.height - height, + ), width, height, ); } +/// [value] rounded to a whole logical pixel and kept within +/// `[minimum, limit]`. +/// +/// A fractional [minimum] rounds up, so the whole-pixel size never drops below +/// the minimum the caller asked for; when the limit is too small to hold that, +/// the limit wins. +double _wholePixel(double value, double minimum, double limit) { + final max = limit.floorToDouble(); + final min = minimum.ceilToDouble().clamp(0.0, max); + return value.roundToDouble().clamp(min, max); +} + /// Where [entry] is drawn inside [bounds]: fitted, or filling the area less /// a margin when maximised. Rect drawnWindowFrame(WindowEntry entry, Size bounds, Size minSize) => diff --git a/app/lib/shell/windows/window_layer.dart b/app/lib/shell/windows/window_layer.dart index f17c23da..b5d93d66 100644 --- a/app/lib/shell/windows/window_layer.dart +++ b/app/lib/shell/windows/window_layer.dart @@ -106,12 +106,26 @@ class _WindowLayerState extends State /// Where the pointer is in the host while the title is dragged. Offset? _pointer; + /// The frame the current move or resize started from and the pointer delta + /// summed since. Every event sets the frame from `start + total`: fitting + /// one fractional step after another would round part of the pointer's + /// movement away each time and drift, and shifting the fitted frame would + /// change its size in its last bits. + Rect? _dragStart; + Offset _dragTotal = Offset.zero; + void _move(Offset delta, Offset globalPosition) { final box = context.findRenderObject(); _pointer = box is RenderBox ? box.globalToLocal(globalPosition) : null; + final start = _dragStart ??= _current; + _dragTotal += delta; widget.manager.setFrame( _id, - fitWindowFrame(_current.shift(delta), widget.bounds, widget.kind.minSize), + fitWindowFrame( + start.shift(_dragTotal), + widget.bounds, + widget.kind.minSize, + ), save: false, ); } @@ -121,6 +135,7 @@ class _WindowLayerState extends State void _moveEnded() { final pointer = _pointer; _pointer = null; + _endDrag(); final bounds = widget.bounds; final snap = pointer == null ? null : windowSnapFor(pointer, bounds); if (snap == WindowSnap.maximize) { @@ -145,15 +160,16 @@ class _WindowLayerState extends State } void _resize(Offset delta) { - final rect = _current; + final start = _dragStart ??= _current; + _dragTotal += delta; widget.manager.setFrame( _id, fitWindowFrame( Rect.fromLTWH( - rect.left, - rect.top, - rect.width + delta.dx, - rect.height + delta.dy, + start.left, + start.top, + start.width + _dragTotal.dx, + start.height + _dragTotal.dy, ), widget.bounds, widget.kind.minSize, @@ -162,16 +178,34 @@ class _WindowLayerState extends State ); } + void _resizeEnded() { + _endDrag(); + widget.manager.commit(_id); + } + + /// A drag ends here even if it is cancelled or another gesture begins. + void _endDrag() { + _dragStart = null; + _dragTotal = Offset.zero; + } + /// A press brings the window to the top and gives it the keyboard at /// once, before the press does anything else (focus a field, open a /// popup that takes the keyboard itself). void _pressed() { + // A new gesture measures from where it starts, even if the last drag was + // cancelled before it ended. + _endDrag(); if (!widget.focus.has(_id)) widget.focus.focusNow(_id); widget.manager.raise(_id); } ThemeData _pageThemeFor(ThemeData theme) { - if (identical(theme, _themeSource)) return _pageTheme!; + // Equal to the last theme means the same page theme, even when the app's + // animated theme hands over a new but equal `ThemeData` object: a new + // page theme would notify every content widget that reads the theme on + // every frame of a move. + if (theme == _themeSource) return _pageTheme!; _themeSource = theme; return _pageTheme = WindowNavigator.themeData(theme); } @@ -263,7 +297,7 @@ class _WindowLayerState extends State bottom: widget.bounds.height - frame.bottom, child: WindowResizeCorner( onResize: _resize, - onResizeEnd: () => manager.commit(_id), + onResizeEnd: _resizeEnded, ), ), ], diff --git a/app/notes/perf-round5.md b/app/notes/perf-round5.md index 91022c70..c00b1391 100644 --- a/app/notes/perf-round5.md +++ b/app/notes/perf-round5.md @@ -100,3 +100,115 @@ this scenario, not content. (`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. + diff --git a/app/test/shell/windows/window_rebuild_test.dart b/app/test/shell/windows/window_rebuild_test.dart index dcbf9252..11058b00 100644 --- a/app/test/shell/windows/window_rebuild_test.dart +++ b/app/test/shell/windows/window_rebuild_test.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/windows/window_entry.dart'; +import 'package:pipelines_app/shell/windows/window_frame.dart'; import 'package:pipelines_app/shell/windows/window_kind.dart'; import 'package:pipelines_app/shell/windows/window_layer.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; @@ -36,6 +37,23 @@ void main() { ProbeContentState content(WidgetTester tester, String id) => tester.state(find.byKey(ValueKey('content-$id'))); + Size windowSize(WidgetTester tester, String title) => tester.getSize( + find.ancestor(of: find.text(title), matching: find.byType(WindowFrame)), + ); + + /// The elements inside the probe windows that depend on the window's size: + /// its buttons and the scroll view around them. + Set contentElements() => { + for (final id in const ['a', 'b']) + for (final type in const [TextButton, ListView, Scrollable]) + ...find + .descendant( + of: find.byKey(ValueKey('content-$id')), + matching: find.byType(type), + ) + .evaluate(), + }; + Future openTwo(WidgetTester tester) async { manager.open('probe', 'a', frame: const Rect.fromLTWH(100, 80, 360, 420)); manager.open('probe', 'b', frame: const Rect.fromLTWH(600, 200, 360, 420)); @@ -77,6 +95,99 @@ void main() { expect(content(tester, 'b').builds, beforeB); }); + testWidgets('a fractional title drag keeps the window size exactly and does ' + 'not rebuild content', (tester) async { + await openTwo(tester); + final start = windowRect(tester, 'Probe a').topLeft; + final startSize = windowSize(tester, 'Probe a'); + final watched = contentElements(); + expect(watched, isNotEmpty); + final rebuilt = {}; + final gesture = await tester.startGesture( + tester.getCenter( + inWindow('Probe a', find.byKey(const ValueKey('window-title'))), + ), + ); + // The press raises and focuses the window before the drag starts; only + // the drag's own rebuilds are watched. + await tester.pump(); + debugOnRebuildDirtyWidget = (element, builtOnce) { + if (watched.contains(element)) rebuilt.add(element); + }; + try { + final a = content(tester, 'a'); + final b = content(tester, 'b'); + final beforeA = a.builds; + final beforeB = b.builds; + for (var i = 0; i < 8; i++) { + await gesture.moveBy(const Offset(10.3, 4.7)); + await tester.pump(); + expect( + rebuilt.map((e) => e.widget.runtimeType).toSet(), + isEmpty, + reason: 'window content rebuilt at move $i', + ); + expect( + windowSize(tester, 'Probe a'), + startSize, + reason: 'window size changed at move $i', + ); + } + await gesture.up(); + await tester.pumpAndSettle(); + expect(rebuilt, isEmpty); + expect(a.builds, beforeA); + expect(b.builds, beforeB); + // Eight fractional moves, rounded to whole pixels, without drift. + final moved = windowRect(tester, 'Probe a').topLeft - start; + expect(moved.dx, closeTo(8 * 10.3, 1)); + expect(moved.dy, closeTo(8 * 4.7, 1)); + } finally { + debugOnRebuildDirtyWidget = null; + } + }); + + testWidgets('resizing by fractional deltas gives whole-pixel sizes', ( + tester, + ) async { + await openTwo(tester); + final a = content(tester, 'a'); + final b = content(tester, 'b'); + final beforeA = a.builds; + final beforeB = b.builds; + final startSize = windowSize(tester, 'Probe b'); + final startBody = b.lastSize!; + final titleHeight = startSize.height - startBody.height; + final gesture = await tester.startGesture( + tester.getCenter( + inWindow('Probe b', find.byKey(const ValueKey('window-resize'))), + ), + ); + for (var i = 0; i < 5; i++) { + await gesture.moveBy(const Offset(-10.3, -6.7)); + await tester.pump(); + final size = windowSize(tester, 'Probe b'); + expect(size.width, size.width.roundToDouble(), reason: 'move $i width'); + expect( + size.height, + size.height.roundToDouble(), + reason: 'move $i height', + ); + expect(a.builds, beforeA, reason: 'other window rebuilt at move $i'); + } + await gesture.up(); + await tester.pumpAndSettle(); + expect(content(tester, 'a').builds, beforeA); + final state = content(tester, 'b'); + expect(state.builds, greaterThan(beforeB)); + final size = windowSize(tester, 'Probe b'); + expect(size.width, closeTo(startSize.width - 5 * 10.3, 1)); + expect(size.height, closeTo(startSize.height - 5 * 6.7, 1)); + // The content saw the new, whole-pixel size. + expect(state.lastSize!.width, size.width); + expect(state.lastSize!.height, size.height - titleHeight); + }); + testWidgets('resizing a window keeps another window and sees the new size', ( tester, ) async { @@ -108,9 +219,7 @@ void main() { testWidgets('content still rebuilds when its window is retargeted', ( tester, ) async { - manager = WindowManager( - kinds: WindowKinds()..register(_StableProbeKind()), - ); + manager = WindowManager(kinds: WindowKinds()..register(_StableProbeKind())); final entry = manager.open('stable', 'a'); await pumpProbe(tester, manager); final state = tester.state( diff --git a/app/test/shell/windows/window_store_test.dart b/app/test/shell/windows/window_store_test.dart index 7250c6e7..a0175dca 100644 --- a/app/test/shell/windows/window_store_test.dart +++ b/app/test/shell/windows/window_store_test.dart @@ -162,6 +162,24 @@ void main() { ), const Rect.fromLTWH(0, 0, 300, 200), ); + // A fractional minimum is never rounded below itself: the whole-pixel + // frame rounds up, and when the bounds cannot hold it the bounds win. + expect( + fitWindowFrame( + const Rect.fromLTWH(0, 0, 320.1, 240.4), + const Size(1000, 800), + const Size(320.1, 240.4), + ), + const Rect.fromLTWH(0, 0, 321, 241), + ); + expect( + fitWindowFrame( + const Rect.fromLTWH(0, 0, 320.1, 240.4), + const Size(319.6, 239.2), + const Size(320.1, 240.4), + ), + const Rect.fromLTWH(0, 0, 319, 239), + ); const entry = WindowEntry( id: 'w', kind: 'chat',