Prevent floating window content rebuilds
Keep window content scope independent from frame changes, and retain the page theme and root page across layer rebuilds. Add repaint boundaries and regression coverage so dragging and resizing avoid unrelated content work while content changes still propagate.
This commit is contained in:
@@ -55,6 +55,16 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
final GlobalKey _navigator = GlobalKey();
|
||||
late final LocalKey _rootPage = ValueKey('window-root-${widget.entry.id}');
|
||||
|
||||
/// The page-transition theme handed to the window's navigator, rebuilt only
|
||||
/// when the app's theme itself changes.
|
||||
ThemeData? _themeSource;
|
||||
ThemeData? _pageTheme;
|
||||
|
||||
/// The root page kept across builds: a new page object would rebuild the
|
||||
/// window's content on every move and resize.
|
||||
WindowKind? _pageKind;
|
||||
Page<void>? _page;
|
||||
|
||||
String get _id => widget.entry.id;
|
||||
|
||||
@override
|
||||
@@ -160,6 +170,22 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
widget.manager.raise(_id);
|
||||
}
|
||||
|
||||
ThemeData _pageThemeFor(ThemeData theme) {
|
||||
if (identical(theme, _themeSource)) return _pageTheme!;
|
||||
_themeSource = theme;
|
||||
return _pageTheme = WindowNavigator.themeData(theme);
|
||||
}
|
||||
|
||||
Page<void> _rootPageFor(WindowKind kind) {
|
||||
final page = _page;
|
||||
if (page != null && identical(kind, _pageKind)) return page;
|
||||
_pageKind = kind;
|
||||
return _page = MaterialPage<void>(
|
||||
key: _rootPage,
|
||||
child: _WindowContent(kind: kind),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final manager = widget.manager;
|
||||
@@ -174,6 +200,7 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
frame.bottom,
|
||||
);
|
||||
final title = kind.title(context, entry);
|
||||
final pageTheme = _pageThemeFor(Theme.of(context));
|
||||
final hiding = !widget.visible;
|
||||
final content = Stack(
|
||||
fit: StackFit.expand,
|
||||
@@ -185,17 +212,21 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
explicitChildNodes: true,
|
||||
label: 'Window: $title',
|
||||
identifier: 'window-${entry.kind}',
|
||||
child: WindowFrame(
|
||||
title: title,
|
||||
icon: kind.icon,
|
||||
actions: kind.actions(context, entry),
|
||||
maximized: entry.maximized,
|
||||
onMove: _move,
|
||||
onMoveEnd: _moveEnded,
|
||||
onMinimize: () => manager.setMinimized(_id, true),
|
||||
onToggleMaximized: () =>
|
||||
manager.setMaximized(_id, !entry.maximized),
|
||||
onClose: () => manager.close(_id),
|
||||
// Its own layer: moving the window moves the title bar without
|
||||
// repainting it.
|
||||
child: RepaintBoundary(
|
||||
child: WindowFrame(
|
||||
title: title,
|
||||
icon: kind.icon,
|
||||
actions: kind.actions(context, entry),
|
||||
maximized: entry.maximized,
|
||||
onMove: _move,
|
||||
onMoveEnd: _moveEnded,
|
||||
onMinimize: () => manager.setMinimized(_id, true),
|
||||
onToggleMaximized: () =>
|
||||
manager.setMaximized(_id, !entry.maximized),
|
||||
onClose: () => manager.close(_id),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -210,19 +241,16 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
child: WindowScope(
|
||||
manager: manager,
|
||||
entry: entry,
|
||||
body: body,
|
||||
child: ScaffoldMessenger(
|
||||
child: WindowNavigator.theme(
|
||||
context,
|
||||
child: WindowNavigator(
|
||||
key: _navigator,
|
||||
pages: [
|
||||
MaterialPage<void>(
|
||||
key: _rootPage,
|
||||
child: _WindowContent(kind: kind),
|
||||
),
|
||||
],
|
||||
onDidRemovePage: (_) {},
|
||||
child: WindowBodyScope(
|
||||
body: body,
|
||||
child: ScaffoldMessenger(
|
||||
child: Theme(
|
||||
data: pageTheme,
|
||||
child: WindowNavigator(
|
||||
key: _navigator,
|
||||
pages: [_rootPageFor(kind)],
|
||||
onDidRemovePage: (_) {},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -18,23 +18,21 @@ import 'window_scope.dart';
|
||||
class WindowNavigator extends Navigator {
|
||||
const WindowNavigator({super.key, super.pages, super.onDidRemovePage});
|
||||
|
||||
/// [child] with page transitions that keep every `MaterialPageRoute` of a
|
||||
/// window navigator below it inside the window's content area.
|
||||
static Widget theme(BuildContext context, {required Widget child}) {
|
||||
final theme = Theme.of(context);
|
||||
/// [theme] with page transitions that keep every `MaterialPageRoute` of a
|
||||
/// window navigator inside the window's content area. The caller keeps the
|
||||
/// result, so rebuilding a window layer does not hand its contents a new
|
||||
/// theme.
|
||||
static ThemeData themeData(ThemeData theme) {
|
||||
final builders = theme.pageTransitionsTheme.builders;
|
||||
return Theme(
|
||||
data: theme.copyWith(
|
||||
pageTransitionsTheme: PageTransitionsTheme(
|
||||
builders: {
|
||||
for (final platform in TargetPlatform.values)
|
||||
platform: _WindowPageTransitions(
|
||||
builders[platform] ?? const ZoomPageTransitionsBuilder(),
|
||||
),
|
||||
},
|
||||
),
|
||||
return theme.copyWith(
|
||||
pageTransitionsTheme: PageTransitionsTheme(
|
||||
builders: {
|
||||
for (final platform in TargetPlatform.values)
|
||||
platform: _WindowPageTransitions(
|
||||
builders[platform] ?? const ZoomPageTransitionsBuilder(),
|
||||
),
|
||||
},
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -117,9 +115,8 @@ class WindowPageBox extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scope = WindowScope.maybeOf(context);
|
||||
if (scope == null) return child;
|
||||
final body = scope.body;
|
||||
final body = WindowBodyScope.maybeOf(context);
|
||||
if (body == null) return child;
|
||||
const radius = Radius.circular(windowCornerRadius);
|
||||
return Stack(
|
||||
children: [
|
||||
@@ -140,7 +137,9 @@ class WindowPageBox extends StatelessWidget {
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
primary: false,
|
||||
body: child,
|
||||
// Its own layer: moving or resizing the window repositions the
|
||||
// page without repainting it.
|
||||
body: RepaintBoundary(child: child),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'window_entry.dart';
|
||||
import 'window_manager.dart';
|
||||
|
||||
/// The floating window some content is in: its entry, the manager, and where
|
||||
/// the content area is drawn in the window host. Absent outside a window, so
|
||||
/// shared content can tell the tiled app from a window.
|
||||
/// The floating window some content is in: its entry and the manager. Absent
|
||||
/// outside a window, so shared content can tell the tiled app from a window.
|
||||
///
|
||||
/// Content depends on what it can read of the entry, not on where the window
|
||||
/// is drawn: moving, resizing, minimising or maximising a window keeps its
|
||||
/// content as it is.
|
||||
class WindowScope extends InheritedWidget {
|
||||
const WindowScope({
|
||||
super.key,
|
||||
required this.manager,
|
||||
required this.entry,
|
||||
required this.body,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
final WindowManager manager;
|
||||
final WindowEntry entry;
|
||||
|
||||
/// The content area (the window less its title bar), in the host's
|
||||
/// coordinates.
|
||||
final Rect body;
|
||||
|
||||
static WindowScope? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<WindowScope>();
|
||||
|
||||
@@ -30,6 +29,26 @@ class WindowScope extends InheritedWidget {
|
||||
@override
|
||||
bool updateShouldNotify(WindowScope oldWidget) =>
|
||||
oldWidget.manager != manager ||
|
||||
oldWidget.entry != entry ||
|
||||
oldWidget.body != body;
|
||||
oldWidget.entry.id != entry.id ||
|
||||
oldWidget.entry.kind != entry.kind ||
|
||||
oldWidget.entry.contentId != entry.contentId ||
|
||||
oldWidget.entry.serverKey != entry.serverKey ||
|
||||
!mapEquals(oldWidget.entry.data, entry.data);
|
||||
}
|
||||
|
||||
/// Where a window's content area is drawn in the window host. Kept apart from
|
||||
/// [WindowScope] so a move or a resize repositions the page without rebuilding
|
||||
/// what the page shows.
|
||||
class WindowBodyScope extends InheritedWidget {
|
||||
const WindowBodyScope({super.key, required this.body, required super.child});
|
||||
|
||||
/// The content area (the window less its title bar), in the host's
|
||||
/// coordinates.
|
||||
final Rect body;
|
||||
|
||||
static Rect? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<WindowBodyScope>()?.body;
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(WindowBodyScope oldWidget) => oldWidget.body != body;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# 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.
|
||||
@@ -0,0 +1,130 @@
|
||||
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_kind.dart';
|
||||
import 'package:pipelines_app/shell/windows/window_layer.dart';
|
||||
import 'package:pipelines_app/shell/windows/window_manager.dart';
|
||||
|
||||
import 'window_test_harness.dart';
|
||||
|
||||
/// A probe kind whose content keeps its element when the window is retargeted,
|
||||
/// so a rebuild is visible in the same state's counter.
|
||||
class _StableProbeKind extends WindowKind {
|
||||
@override
|
||||
String get key => 'stable';
|
||||
@override
|
||||
bool get serverScoped => false;
|
||||
@override
|
||||
IconData get icon => Icons.science_outlined;
|
||||
@override
|
||||
String title(BuildContext context, WindowEntry entry) =>
|
||||
'Probe ${entry.contentId}';
|
||||
@override
|
||||
Widget build(BuildContext context, WindowEntry entry) =>
|
||||
ProbeContent(key: const ValueKey('stable-content'), id: entry.contentId);
|
||||
}
|
||||
|
||||
void main() {
|
||||
late WindowManager manager;
|
||||
setUp(() => manager = probeManager());
|
||||
|
||||
Finder window(String title) => find.byWidgetPredicate(
|
||||
(w) => w is WindowLayer && 'Probe ${w.entry.contentId}' == title,
|
||||
);
|
||||
Finder inWindow(String title, Finder what) =>
|
||||
find.descendant(of: window(title), matching: what);
|
||||
ProbeContentState content(WidgetTester tester, String id) =>
|
||||
tester.state<ProbeContentState>(find.byKey(ValueKey('content-$id')));
|
||||
|
||||
Future<void> 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));
|
||||
await pumpProbe(tester, manager);
|
||||
}
|
||||
|
||||
testWidgets('dragging a window moves it without rebuilding any content', (
|
||||
tester,
|
||||
) async {
|
||||
await openTwo(tester);
|
||||
final a = content(tester, 'a');
|
||||
final b = content(tester, 'b');
|
||||
final beforeA = a.builds;
|
||||
final beforeB = b.builds;
|
||||
final start = windowRect(tester, 'Probe a').topLeft;
|
||||
final fieldStart = tester
|
||||
.getRect(find.byKey(const ValueKey('a-field')))
|
||||
.topLeft;
|
||||
final gesture = await tester.startGesture(
|
||||
tester.getCenter(
|
||||
inWindow('Probe a', find.byKey(const ValueKey('window-title'))),
|
||||
),
|
||||
);
|
||||
for (var i = 0; i < 8; i++) {
|
||||
await gesture.moveBy(const Offset(12, 6));
|
||||
await tester.pump();
|
||||
expect(a.builds, beforeA, reason: 'dragged window rebuilt at move $i');
|
||||
expect(b.builds, beforeB, reason: 'other window rebuilt at move $i');
|
||||
}
|
||||
await gesture.up();
|
||||
await tester.pumpAndSettle();
|
||||
// It really moved, and its content followed it.
|
||||
expect(windowRect(tester, 'Probe a').topLeft, start + const Offset(96, 48));
|
||||
expect(
|
||||
tester.getRect(find.byKey(const ValueKey('a-field'))).topLeft,
|
||||
fieldStart + const Offset(96, 48),
|
||||
);
|
||||
expect(content(tester, 'a').builds, beforeA);
|
||||
expect(content(tester, 'b').builds, beforeB);
|
||||
});
|
||||
|
||||
testWidgets('resizing a window keeps another window and sees the new size', (
|
||||
tester,
|
||||
) async {
|
||||
await openTwo(tester);
|
||||
final a = content(tester, 'a');
|
||||
final b = content(tester, 'b');
|
||||
final beforeA = a.builds;
|
||||
final beforeB = b.builds;
|
||||
final beforeSize = b.lastSize!;
|
||||
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(-16, -10));
|
||||
await tester.pump();
|
||||
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));
|
||||
expect(state.lastSize!.width, beforeSize.width - 80);
|
||||
expect(state.lastSize!.height, beforeSize.height - 50);
|
||||
});
|
||||
|
||||
testWidgets('content still rebuilds when its window is retargeted', (
|
||||
tester,
|
||||
) async {
|
||||
manager = WindowManager(
|
||||
kinds: WindowKinds()..register(_StableProbeKind()),
|
||||
);
|
||||
final entry = manager.open('stable', 'a');
|
||||
await pumpProbe(tester, manager);
|
||||
final state = tester.state<ProbeContentState>(
|
||||
find.byKey(const ValueKey('stable-content')),
|
||||
);
|
||||
final before = state.builds;
|
||||
manager.setContent(entry.id, 'c');
|
||||
await tester.pumpAndSettle();
|
||||
final after = tester.state<ProbeContentState>(
|
||||
find.byKey(const ValueKey('stable-content')),
|
||||
);
|
||||
expect(after, same(state));
|
||||
expect(after.builds, greaterThan(before));
|
||||
expect(after.widget.id, 'c');
|
||||
expect(find.text('Probe c'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -52,6 +52,10 @@ class ProbeContentState extends State<ProbeContent> {
|
||||
int builds = 0;
|
||||
String? picked;
|
||||
|
||||
/// The size the content was last laid out at (the window's content area),
|
||||
/// so tests can see a resize reach it.
|
||||
Size? lastSize;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
text.dispose();
|
||||
@@ -67,6 +71,7 @@ class ProbeContentState extends State<ProbeContent> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
builds++;
|
||||
lastSize = MediaQuery.sizeOf(context);
|
||||
final id = widget.id;
|
||||
return ListView(
|
||||
children: [
|
||||
|
||||
Reference in New Issue
Block a user