Files
pipelines/app/lib/features/changes/widgets/stacked_diff_view.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

150 lines
4.4 KiB
Dart

import 'package:flutter/material.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../domain/change_wording.dart';
import '../domain/file_change.dart';
import '../domain/unified_patch.dart';
import 'capture_section.dart' show changeKindIcon;
import 'change_style.dart';
import 'diff_highlight.dart';
import 'patch_view.dart';
/// Every file of a diff one below the other, like a pull request's Files
/// changed tab: each file has a header that collapses or expands its patch.
/// [headerKeys] (one per file) let the page jump between files.
class StackedDiffView extends StatelessWidget {
const StackedDiffView({
super.key,
required this.files,
required this.collapsed,
required this.onToggle,
required this.controller,
required this.headerKeys,
});
final List<FileChange> files;
final Set<int> collapsed;
final ValueChanged<int> onToggle;
final ScrollController controller;
final List<GlobalKey> headerKeys;
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
key: const ValueKey('stacked-diff'),
controller: controller,
padding: withTopBarClearance(
context,
const EdgeInsets.fromLTRB(8, 8, 8, 96),
),
child: Align(
alignment: Alignment.topCenter,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1100),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (var index = 0; index < files.length; index++)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: _FileSection(
key: ValueKey('stacked-diff-file-${files[index].path}'),
headerKey: headerKeys[index],
index: index,
file: files[index],
expanded: !collapsed.contains(index),
onToggle: () => onToggle(index),
),
),
],
),
),
),
);
}
}
class _FileSection extends StatelessWidget {
const _FileSection({
super.key,
required this.headerKey,
required this.index,
required this.file,
required this.expanded,
required this.onToggle,
});
final GlobalKey headerKey;
final int index;
final FileChange file;
final bool expanded;
final VoidCallback onToggle;
@override
Widget build(BuildContext context) {
final mono = nativeDiffText(DefaultTextStyle.of(context).style);
final stat = changeStat(file.added, file.removed);
final header = Semantics(
key: headerKey,
button: true,
expanded: expanded,
label: '${expanded ? 'Collapse' : 'Expand'} ${file.path}',
excludeSemantics: true,
onTap: onToggle,
child: InkWell(
onTap: onToggle,
child: Padding(
padding: const EdgeInsets.fromLTRB(8, 8, 12, 8),
child: Row(
children: [
Icon(expanded ? Icons.expand_more : Icons.chevron_right),
const SizedBox(width: 4),
Icon(changeKindIcon(file.kind), size: 20),
const SizedBox(width: 8),
Expanded(
child: Text(
file.path,
style: mono,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
if (stat.isNotEmpty) ...[
const SizedBox(width: 12),
ChangeStatText(stat, style: mono),
],
],
),
),
),
);
return Card.outlined(
margin: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
header,
if (expanded) ...[
const Divider(height: 1),
DiffFileIndex(index: index, child: _body(context)),
],
],
),
);
}
Widget _body(BuildContext context) {
final reason = noPatchReasonMessage(file.noPatchReason);
final message = file.binary && reason == null
? binaryDiffMessage
: !file.hasPatch
? patchUnavailableReason(file)
: null;
if (message != null) {
return Padding(padding: const EdgeInsets.all(12), child: Text(message));
}
return PatchView(file: file);
}
}