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
@@ -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;
}