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); final stat = changeStat(parsed.added, parsed.removed); return Semantics( container: true, label: 'Rendered diff for ${file.path}', value: stat.isEmpty ? noLineChangesMessage : stat, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), child: ExcludeSemantics( child: _StatLine( added: parsed.added, removed: parsed.removed, base: base, ), ), ), 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), ], ), ); } } /// `+N` in the add colour and `-M` in the remove colour, or the honest note when /// the patch records no changed line. class _StatLine extends StatelessWidget { const _StatLine({ required this.added, required this.removed, required this.base, }); final int added; final int removed; final TextStyle base; @override Widget build(BuildContext context) { if (added == 0 && removed == 0) { return Text( noLineChangesMessage, style: base.copyWith(color: mutedColor(context)), ); } return Text.rich( TextSpan( children: [ if (added > 0) TextSpan( text: '+$added', style: base.copyWith( color: diffLineColors(context, ChangeLineKind.add).text, ), ), if (added > 0 && removed > 0) TextSpan(text: ' ', style: base), if (removed > 0) TextSpan( text: '-$removed', style: base.copyWith( color: diffLineColors(context, ChangeLineKind.remove).text, ), ), ], ), ); } } 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, ), ); } }