Files
pipelines/app/lib/features/changes/widgets/file_diff_viewer.dart
T
alan d407a72a06 Diff viewer: vim keys, scrolling, find in diff and an all-files view
Left/right and h/l step between files (or headers in the all-files
view), up/down and j/k scroll, / opens find-in-diff (highlighted
matches across every file, n/N or Enter/Shift+Enter to move, Escape to
close), and a top-bar toggle switches between one file at a time and
every file stacked with collapsible headers, like a pull request's
Files changed tab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:13:08 +00:00

257 lines
9.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../domain/change_kind.dart';
import '../domain/change_wording.dart';
import '../domain/file_change.dart';
import '../domain/unified_patch.dart';
import 'change_style.dart';
import 'diff_highlight.dart';
import 'patch_view.dart';
import 'raw_patch_view.dart';
export 'file_diff_page.dart';
/// The reusable viewer for one recorded file change: its kind, path, stat,
/// tags, the rendered or raw unified patch, Copy patch (the server's exact
/// bytes) and Close. Binary/truncated/mode-only/no-patch entries show an honest
/// reason instead of a fabricated diff.
class FileDiffViewer extends StatefulWidget {
const FileDiffViewer({
super.key,
required this.file,
this.onClose,
this.floatBehindTopBar = false,
this.scrollController,
});
final FileChange file;
/// Scrolls the diff body; the page passes its own so the keyboard can
/// scroll it.
final ScrollController? scrollController;
/// Called by the Close control. Defaults to popping the enclosing route.
final VoidCallback? onClose;
/// The full-page presentation clears its floating navigation bar while the
/// diff content still occupies the page edge behind it.
final bool floatBehindTopBar;
@override
State<FileDiffViewer> createState() => _FileDiffViewerState();
}
class _FileDiffViewerState extends State<FileDiffViewer> {
DiffForm _form = DiffForm.rendered;
bool _copied = false;
Timer? _copyTimer;
@override
void dispose() {
_copyTimer?.cancel();
super.dispose();
}
void _selectForm(DiffForm value) => setState(() => _form = value);
Future<void> _copy() async {
final patch = widget.file.patch;
if (patch == null) return;
await Clipboard.setData(ClipboardData(text: patch));
if (!mounted) return;
setState(() => _copied = true);
_copyTimer?.cancel();
_copyTimer = Timer(const Duration(seconds: 2), () {
if (mounted) setState(() => _copied = false);
});
}
@override
Widget build(BuildContext context) {
final file = widget.file;
final base = DefaultTextStyle.of(context).style;
final mono = nativeDiffText(base);
final stat = changeStat(file.added, file.removed);
final tags = changeEntryTags(file);
return SafeArea(
top: false,
minimum: const EdgeInsets.symmetric(horizontal: 8),
child: ConstrainedBox(
key: const ValueKey('file-diff-viewer'),
constraints: BoxConstraints(
maxWidth: 900,
maxHeight: MediaQuery.sizeOf(context).height * 0.85,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: widget.floatBehindTopBar
? withTopBarClearance(
context,
const EdgeInsets.fromLTRB(16, 12, 16, 8),
)
: const EdgeInsets.fromLTRB(16, 12, 16, 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Semantics(
container: true,
header: true,
excludeSemantics: true,
label: [
changeKindTitle(file.kind),
if (stat.isNotEmpty) stat,
if (tags.isNotEmpty) tags.join(' · '),
'for ${file.path}',
if (file.steps.isNotEmpty)
'changed by ${file.steps.join(' → ')}',
if (file.modeChanged && !file.binary)
'mode ${file.beforeMode ?? ''}→'
'${file.afterMode ?? ''}',
].join(', '),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
spacing: 12,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text(
changeKindTitle(file.kind),
style: Theme.of(context).textTheme.titleMedium,
),
if (stat.isNotEmpty)
ChangeStatText(stat, style: mono),
if (tags.isNotEmpty)
Text(
tags.join(' · '),
style: Theme.of(context).textTheme.bodySmall,
),
],
),
const SizedBox(height: 4),
Text(file.path, style: mono),
if (file.steps.isNotEmpty)
Text(
'changed by ${file.steps.join(' → ')}',
style: Theme.of(context).textTheme.bodySmall,
),
if (file.modeChanged && !file.binary)
Text(
'mode ${file.beforeMode ?? ''}→'
'${file.afterMode ?? ''}',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Wrap(
spacing: 8,
runSpacing: 8,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (file.hasPatch)
LayoutBuilder(
builder: (context, constraints) {
// Two native segmented-control segments cannot show both
// fixed words at 2x text on a narrow host; their menu
// shows the same labels whole, one per row.
final control =
diffFormSegmentsFit(context, constraints.maxWidth)
? diffFormControl(
context,
form: _form,
onChanged: _selectForm,
)
: diffFormMenu(
context,
form: _form,
onChanged: _selectForm,
);
return control;
},
),
_scrollsWhenTooWide(
copyPatchControl(
context,
enabled: file.hasPatch,
copied: _copied,
onPressed: _copy,
),
),
if (widget.onClose != null)
closeDiffControl(context, widget.onClose!),
],
),
),
Divider(height: 16, color: Theme.of(context).dividerColor),
Flexible(child: _body(context, mono)),
],
),
),
);
}
Widget _body(BuildContext context, TextStyle mono) {
final file = widget.file;
// A recorded reason is more specific than the generic binary message.
final reason = noPatchReasonMessage(file.noPatchReason);
if (file.binary && reason == null) {
return _message(context, binaryDiffMessage);
}
if (!file.hasPatch) {
return _message(context, patchUnavailableReason(file));
}
// A search highlights rendered lines, so it shows the rendered form.
final searching = DiffHighlight.maybeOf(context)?.active ?? false;
if (_form == DiffForm.raw && !searching) {
return Semantics(
container: true,
label: 'Raw patch for ${file.path}',
value: file.patch,
child: RawPatchView(
patch: file.patch!,
style: mono,
controller: widget.scrollController,
),
);
}
return SingleChildScrollView(
controller: widget.scrollController,
padding: const EdgeInsets.only(bottom: 16),
child: PatchView(file: file),
);
}
Widget _message(BuildContext context, String text) {
return SingleChildScrollView(
controller: widget.scrollController,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
child: Text(text),
);
}
}
/// Gives a native control its intrinsic width and lets it scroll sideways when
/// the dialog is narrower than the control, instead of letting the control clip
/// or break one of its fixed labels.
///
/// The scroll view shrink-wraps when the control already fits, so the action
/// keeps its usual layout until scaled text or a narrow phone demands more room.
Widget _scrollsWhenTooWide(Widget control) =>
SingleChildScrollView(scrollDirection: Axis.horizontal, child: control);