Files
pipelines/app/lib/features/changes/widgets/patch_view.dart
T

140 lines
4.5 KiB
Dart

import 'package:flutter/material.dart';
import '../domain/change_wording.dart';
import '../domain/file_change.dart';
import '../domain/unified_patch.dart';
import 'change_style.dart';
/// The rendered reading of one recorded unified patch: each hunk's `@@` header,
/// the shared diff line language (`+`/`-`/space) and the old/new gutter numbers.
/// This is the only custom drawing in the feature; everything around it is a
/// standard platform control.
class PatchView extends StatelessWidget {
const PatchView({super.key, required this.file});
final FileChange file;
@override
Widget build(BuildContext context) {
final parsed = file.parsedPatch;
final base = nativeDiffText(DefaultTextStyle.of(context).style);
return Semantics(
container: true,
label: 'Rendered diff for ${file.path}',
value: changeStat(parsed.added, parsed.removed).isEmpty
? noLineChangesMessage
: changeStat(parsed.added, parsed.removed),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (file.truncated)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: Text(
truncatedPatchNote,
style: base.copyWith(color: mutedColor(context)),
),
),
// The web rendered view shows hunks, not the recorded `---`/`+++`
// file headers (they stay in the Raw view), so the dialog does not
// show a long header clipped at its edge.
for (final hunk in parsed.hunks) _HunkView(hunk: hunk),
],
),
);
}
}
class _HunkView extends StatelessWidget {
const _HunkView({required this.hunk});
final PatchHunk hunk;
@override
Widget build(BuildContext context) {
final base = nativeDiffText(DefaultTextStyle.of(context).style);
final width = gutterWidth(hunk);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: hunkHeaderColor(context),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Text(hunk.header, style: base),
),
LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: ConstrainedBox(
// At least the dialog's width, so a short hunk still paints
// full-width bands; wider content scrolls sideways.
constraints: BoxConstraints(minWidth: constraints.maxWidth),
// The widest recorded line sets the hunk's content width, so
// every line tint is a full-width band instead of a bar as long
// as its own text.
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (var index = 0; index < hunk.lines.length; index++)
_LineRow(
key: ValueKey('diff-line-$index'),
line: hunk.lines[index],
width: width,
base: base,
),
],
),
),
),
);
},
),
],
);
}
}
class _LineRow extends StatelessWidget {
const _LineRow({
super.key,
required this.line,
required this.width,
required this.base,
});
final PatchLine line;
final int width;
final TextStyle base;
@override
Widget build(BuildContext context) {
final gutter = line.noNewlineMarker
? ' ' * (width * 2 + 1)
: '${gutterText(line.oldLine, width)} ${gutterText(line.newLine, width)}';
final colors = diffLineColors(context, line.kind);
final content = '${diffLineSymbol(line.kind)} ${line.text}';
return Container(
color: colors.background,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: gutter,
style: base.copyWith(color: mutedColor(context)),
),
TextSpan(
text: content,
style: base.copyWith(color: colors.text),
),
],
),
softWrap: false,
overflow: TextOverflow.visible,
),
);
}
}