Open a diff as a floating window

Diffs join the app's window system: Open in a window on a diff page or a
chat diff card shows the same files, on the same file, in a draggable,
resizable window with the full diff viewer (keys, search, all-files
view). Offered only where floating windows show; a diff window's files
live in memory, so it is not restored after a reload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 20:06:45 +00:00
parent 71719b5707
commit dd742ec8e4
5 changed files with 271 additions and 5 deletions
+8 -1
View File
@@ -48,6 +48,7 @@ import '../shell/windows/window_manager.dart';
import '../shell/windows/window_store.dart';
import 'app_terminals.dart';
import '../features/chat/chat_runtime.dart';
import '../features/changes/windows/diff_windows.dart';
import '../features/chat/windows/chat_windows.dart';
import 'open_in_chat.dart';
import 'send_to_chat.dart';
@@ -311,6 +312,7 @@ class _PipelinesAppState extends State<PipelinesApp>
NotificationServiceHandoff? _deliveryHandoff;
late final AppTerminals _terminals;
late final WindowManager _windows;
late final DiffWindows _diffWindows;
late final ChatRuntime _chatRuntime;
late final ChatWindows _chatWindows;
@@ -368,6 +370,7 @@ class _PipelinesAppState extends State<PipelinesApp>
layout: widget.terminalLayout ?? TerminalLayout.instance,
windows: _windows,
);
_diffWindows = DiffWindows(_windows)..start();
// Every kind is registered: saved windows can be restored. Then the
// terminal may open its window, and the floating chats an older build
// saved are brought over once.
@@ -604,6 +607,7 @@ class _PipelinesAppState extends State<PipelinesApp>
_session.removeListener(_chatSyncChanged);
widget.chatPool?.removeListener(_chatSyncChanged);
_chatWindows.dispose();
_diffWindows.dispose();
_chatRuntime.dispose();
if (widget.windows == null) _windows.dispose();
_session.dispose();
@@ -931,7 +935,10 @@ class _PipelinesAppState extends State<PipelinesApp>
ValueListenableBuilder<_RunIndicators>(
valueListenable: _attentionCount,
builder: (context, count, _) => _terminals.surround(
_shellFor(route, count),
DiffWindowsScope(
windows: _diffWindows,
child: _shellFor(route, count),
),
// Every narrow Chat bar carries Terminal itself (the list's top bar
// and an open chat's bar, see terminalChatBarAction), so the floating
// bubble is off on every route. It otherwise covered the list's
@@ -11,6 +11,7 @@ import 'diff_search_bar.dart';
import 'diff_search_controller.dart';
import 'file_diff_viewer.dart';
import 'stacked_diff_view.dart';
import '../windows/diff_windows.dart';
/// Whether diff pages show every file stacked (GitHub's Files changed) rather
/// than one file at a time. Remembered for the rest of the session.
@@ -29,20 +30,35 @@ Future<void> showFileDiffViewer(
}) {
final list = files == null || !files.contains(file) ? [file] : files;
final page = FileDiffPage(files: list, initialIndex: list.indexOf(file));
return Navigator.of(
context,
).push<void>(MaterialPageRoute<void>(builder: (_) => page));
// The page may sit above the shell: carry the app's diff windows into it
// so it can offer Open in a window.
final windows = DiffWindowsScope.windowsOf(context);
return Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (_) => windows == null
? page
: DiffWindowsScope(windows: windows, child: page),
),
);
}
/// A diff's files as a page: one file at a time with previous/next, or every
/// file stacked with collapsible headers. The keyboard steps between files
/// (arrows, `h`/`l`), scrolls (arrows, `j`/`k`) and searches (`/`, `n`/`N`).
class FileDiffPage extends StatefulWidget {
const FileDiffPage({super.key, required this.files, this.initialIndex = 0});
const FileDiffPage({
super.key,
required this.files,
this.initialIndex = 0,
this.inWindow = false,
});
final List<FileChange> files;
final int initialIndex;
/// Shown inside a floating window: no Back, and no Open in a window.
final bool inWindow;
@override
State<FileDiffPage> createState() => _FileDiffPageState();
}
@@ -157,6 +173,13 @@ class _FileDiffPageState extends State<FileDiffPage> {
}
}
/// Leaves the page and shows the same files, on the same file, in a
/// floating window.
void _openInWindow(DiffWindows windows) {
Navigator.of(context).maybePop();
windows.open(widget.files, initialIndex: _index);
}
void _toggleFile(int index) => setState(() {
if (!_collapsed.remove(index)) _collapsed.add(index);
});
@@ -249,7 +272,18 @@ class _FileDiffPageState extends State<FileDiffPage> {
extendBodyBehindAppBar: true,
appBar: SegmentedBar(
title: title,
automaticallyImplyLeading: !widget.inWindow,
actions: [
if (!widget.inWindow)
?switch (DiffWindowsScope.maybeOf(context)) {
final windows? => IconButton(
key: const ValueKey('diff-open-window'),
tooltip: 'Open in a window',
onPressed: () => _openInWindow(windows),
icon: const Icon(Icons.open_in_new),
),
null => null,
},
IconButton(
key: const ValueKey('diff-search-open'),
tooltip: 'Find in diff (/)',
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
import '../../../shell/list_detail_policy.dart';
import '../../../shell/windows/window_entry.dart';
import '../../../shell/windows/window_kind.dart';
import '../../../shell/windows/window_manager.dart';
import '../domain/file_change.dart';
import '../widgets/file_diff_page.dart';
const diffWindowKind = 'diff';
/// What one diff window shows: the files and the one it opened on.
class DiffWindowContent {
const DiffWindowContent(this.files, this.initialIndex);
final List<FileChange> files;
final int initialIndex;
}
/// Diffs as floating windows of the app's window system, beside the floating
/// chats and the terminal. A diff's files live only in memory, so its windows
/// are not restored after a reload.
class DiffWindows {
DiffWindows(this.manager);
final WindowManager manager;
late final DiffWindowKind kind = DiffWindowKind(this);
final Map<String, DiffWindowContent> _contents = {};
var _next = 0;
bool _started = false;
/// Registers the kind. Call before the manager restores its windows.
void start() {
if (_started) return;
_started = true;
manager.kinds.register(kind);
}
void dispose() {
if (!_started) return;
_started = false;
manager.kinds.unregister(diffWindowKind);
}
DiffWindowContent? contentOf(String id) => _contents[id];
/// Opens [files] in a new window on [initialIndex].
WindowEntry open(List<FileChange> files, {int initialIndex = 0}) {
final id = 'diff-${++_next}-${DateTime.now().microsecondsSinceEpoch}';
_contents[id] = DiffWindowContent(List.unmodifiable(files), initialIndex);
return manager.open(diffWindowKind, id);
}
void _closed(String id) => _contents.remove(id);
}
class DiffWindowKind extends WindowKind {
DiffWindowKind(this.windows);
final DiffWindows windows;
@override
String get key => diffWindowKind;
@override
bool get serverScoped => false;
@override
IconData get icon => Icons.difference_outlined;
@override
Size get minSize => const Size(420, 320);
@override
Rect get defaultFrame => const Rect.fromLTWH(160, 96, 780, 640);
@override
double get minHostWidth => kListDetailMinWidth;
@override
String title(BuildContext context, WindowEntry entry) {
final files = windows.contentOf(entry.contentId)?.files;
if (files == null || files.isEmpty) return 'Diff';
if (files.length == 1) return files.single.path.split('/').last;
return '${files.length} files changed';
}
@override
Widget build(BuildContext context, WindowEntry entry) {
final content = windows.contentOf(entry.contentId);
if (content == null) {
return const Center(child: Text('This diff is no longer open.'));
}
return FileDiffPage(
key: ValueKey('diff-window-${entry.contentId}'),
files: content.files,
initialIndex: content.initialIndex,
inWindow: true,
);
}
@override
void closed(WindowEntry entry) => windows._closed(entry.contentId);
/// A saved diff window has nothing to show after a reload: drop it.
@override
Map<String, Object?>? decodeData(Object? raw) => null;
}
/// Hands the app's diff windows to the screens that open diffs.
class DiffWindowsScope extends InheritedWidget {
const DiffWindowsScope({
super.key,
required this.windows,
required super.child,
});
final DiffWindows windows;
/// The diff windows of the app, whatever its width.
static DiffWindows? windowsOf(BuildContext context) =>
context.getInheritedWidgetOfExactType<DiffWindowsScope>()?.windows;
/// The diff windows when the app is wide enough to show floating windows.
static DiffWindows? maybeOf(BuildContext context) {
final scope = context
.dependOnInheritedWidgetOfExactType<DiffWindowsScope>();
if (scope == null) return null;
return MediaQuery.sizeOf(context).width >= kListDetailMinWidth
? scope.windows
: null;
}
@override
bool updateShouldNotify(DiffWindowsScope oldWidget) =>
windows != oldWidget.windows;
}
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import '../../../../../core/api_gateway.dart';
import '../../../../changes/changes.dart';
import '../../../../changes/windows/diff_windows.dart';
import '../../../domain/chat_shown_diff.dart';
import 'chat_shown_surface.dart';
@@ -51,6 +52,15 @@ class _ChatShownDiffCardState extends State<ChatShownDiffCard> {
title: title,
subtitle: diff.comparison,
leading: Icon(Icons.difference_outlined, color: secondary),
trailing: switch (DiffWindowsScope.maybeOf(context)) {
final windows? when files.isNotEmpty => IconButton(
key: const Key('chat-shown-diff-window'),
tooltip: 'Open in a window',
onPressed: () => windows.open(files),
icon: const Icon(Icons.open_in_new),
),
_ => null,
},
),
),
Padding(
@@ -0,0 +1,83 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/changes/changes.dart';
import 'package:pipelines_app/features/changes/windows/diff_windows.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
FileChange _file(String path) => FileChange(
path: path,
kind: ChangeKind.modified,
patch: '--- a/$path\n+++ b/$path\n@@ -1 +1 @@\n-a\n+b\n',
);
Future<DiffWindows> _pump(WidgetTester tester, Size size) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final windows = DiffWindows(WindowManager(kinds: WindowKinds()))..start();
addTearDown(windows.dispose);
final files = [_file('lib/one.dart'), _file('lib/two.dart')];
await tester.pumpWidget(
MaterialApp(
home: DiffWindowsScope(
windows: windows,
child: Builder(
builder: (context) => Scaffold(
body: TextButton(
onPressed: () =>
showFileDiffViewer(context, files[1], files: files),
child: const Text('open'),
),
),
),
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
return windows;
}
void main() {
testWidgets('Open in a window leaves the page for a diff window on the '
'same file', (tester) async {
final windows = await _pump(tester, const Size(1200, 800));
await tester.tap(find.byKey(const ValueKey('diff-open-window')));
await tester.pumpAndSettle();
expect(find.byType(FileDiffPage), findsNothing);
final entry = windows.manager.windows.single;
expect(entry.kind, diffWindowKind);
final content = windows.contentOf(entry.contentId)!;
expect(content.files, hasLength(2));
expect(content.initialIndex, 1);
final context = tester.element(find.text('open'));
expect(windows.kind.title(context, entry), '2 files changed');
// Closing the window forgets its files; a saved one is never restored.
windows.manager.close(entry.id);
expect(windows.contentOf(entry.contentId), isNull);
expect(windows.kind.decodeData(const {}), isNull);
});
testWidgets('a phone-width app offers no window', (tester) async {
await _pump(tester, const Size(400, 800));
expect(find.byType(FileDiffPage), findsOneWidget);
expect(find.byKey(const ValueKey('diff-open-window')), findsNothing);
});
testWidgets('a diff shown in a window has no Back and no Open in a window', (
tester,
) async {
await tester.pumpWidget(
MaterialApp(
home: FileDiffPage(files: [_file('lib/one.dart')], inWindow: true),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('diff-open-window')), findsNothing);
expect(find.byType(BackButton), findsNothing);
});
}