From dd742ec8e49313f3e8aa1ddf10c3bcaac85a5e12 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 21 Nov 2026 20:06:45 +0000 Subject: [PATCH] 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 --- app/lib/application/pipelines_app.dart | 9 +- .../changes/widgets/file_diff_page.dart | 42 +++++- .../changes/windows/diff_windows.dart | 132 ++++++++++++++++++ .../shown/chat_shown_diff_card.dart | 10 ++ .../changes/windows/diff_windows_test.dart | 83 +++++++++++ 5 files changed, 271 insertions(+), 5 deletions(-) create mode 100644 app/lib/features/changes/windows/diff_windows.dart create mode 100644 app/test/features/changes/windows/diff_windows_test.dart diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 23adcefd..1de6fa02 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -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 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 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 _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 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 diff --git a/app/lib/features/changes/widgets/file_diff_page.dart b/app/lib/features/changes/widgets/file_diff_page.dart index c8810e0b..d7622c91 100644 --- a/app/lib/features/changes/widgets/file_diff_page.dart +++ b/app/lib/features/changes/widgets/file_diff_page.dart @@ -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 showFileDiffViewer( }) { final list = files == null || !files.contains(file) ? [file] : files; final page = FileDiffPage(files: list, initialIndex: list.indexOf(file)); - return Navigator.of( - context, - ).push(MaterialPageRoute(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( + MaterialPageRoute( + 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 files; final int initialIndex; + /// Shown inside a floating window: no Back, and no Open in a window. + final bool inWindow; + @override State createState() => _FileDiffPageState(); } @@ -157,6 +173,13 @@ class _FileDiffPageState extends State { } } + /// 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 { 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 (/)', diff --git a/app/lib/features/changes/windows/diff_windows.dart b/app/lib/features/changes/windows/diff_windows.dart new file mode 100644 index 00000000..25773be2 --- /dev/null +++ b/app/lib/features/changes/windows/diff_windows.dart @@ -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 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 _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 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? 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()?.windows; + + /// The diff windows when the app is wide enough to show floating windows. + static DiffWindows? maybeOf(BuildContext context) { + final scope = context + .dependOnInheritedWidgetOfExactType(); + if (scope == null) return null; + return MediaQuery.sizeOf(context).width >= kListDetailMinWidth + ? scope.windows + : null; + } + + @override + bool updateShouldNotify(DiffWindowsScope oldWidget) => + windows != oldWidget.windows; +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart index 50185b02..f60c80f2 100644 --- a/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart @@ -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 { 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( diff --git a/app/test/features/changes/windows/diff_windows_test.dart b/app/test/features/changes/windows/diff_windows_test.dart new file mode 100644 index 00000000..1f0e386d --- /dev/null +++ b/app/test/features/changes/windows/diff_windows_test.dart @@ -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 _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); + }); +}