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:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user