import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.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 'patch_view.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}); final FileChange file; /// Called by the Close control. Defaults to popping the enclosing route. final VoidCallback? onClose; @override State createState() => _FileDiffViewerState(); } class _FileDiffViewerState extends State { DiffForm _form = DiffForm.rendered; bool _copied = false; Timer? _copyTimer; @override void dispose() { _copyTimer?.cancel(); super.dispose(); } void _close() { final close = widget.onClose; if (close != null) { close(); } else { Navigator.of(context).maybePop(); } } Future _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 cupertino = usesCupertino(context); final base = DefaultTextStyle.of(context).style; final mono = nativeDiffText(base); final stat = changeStat(file.added, file.removed); final tags = changeEntryTags(file); return 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: const EdgeInsets.fromLTRB(16, 12, 8, 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) Text(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, ), ], ), ), ), closeDiffControl(context, _close), ], ), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Wrap( spacing: 8, runSpacing: 8, crossAxisAlignment: WrapCrossAlignment.center, children: [ if (file.hasPatch) diffFormControl( context, form: _form, onChanged: (value) => setState(() => _form = value), ), copyPatchControl( context, enabled: file.hasPatch, copied: _copied, onPressed: _copy, ), ], ), ), Divider( height: 16, color: cupertino ? CupertinoColors.separator.resolveFrom(context) : Theme.of(context).dividerColor, ), Flexible(child: _body(context, cupertino, mono)), ], ), ); } Widget _body(BuildContext context, bool cupertino, TextStyle mono) { final file = widget.file; if (file.binary) return _message(context, binaryDiffMessage); if (!file.hasPatch) { return _message(context, patchUnavailableReason(file)); } if (_form == DiffForm.raw) { return Semantics( container: true, label: 'Raw patch for ${file.path}', value: file.patch, child: SingleChildScrollView( padding: const EdgeInsets.fromLTRB(16, 0, 16, 16), child: SingleChildScrollView( // Raw patch bytes are one line each: scroll them sideways like the // web view instead of wrapping a continuation under a new line. scrollDirection: Axis.horizontal, child: ExcludeSemantics( child: SelectionArea( child: Text( file.patch!, style: mono, softWrap: false, overflow: TextOverflow.visible, ), ), ), ), ), ); } return SingleChildScrollView( padding: const EdgeInsets.only(bottom: 16), child: PatchView(file: file), ); } Widget _message(BuildContext context, String text) { return SingleChildScrollView( padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), child: Text(text), ); } } /// Open [file] in the platform's standard modal: a Material dialog on /// Android/desktop/web, a Cupertino dialog with a popup surface on iOS/macOS. Future showFileDiffViewer(BuildContext context, FileChange file) { final cupertino = usesCupertino(context); Widget buildDialog(BuildContext dialogContext) { final viewer = FileDiffViewer( file: file, onClose: () => Navigator.of(dialogContext).pop(), ); if (cupertino) { // A popup surface spans the window unless it is inset; on a phone the // screen edge would clip its native rounded corners. final width = MediaQuery.sizeOf(dialogContext).width; return Padding( padding: EdgeInsets.symmetric( horizontal: width < 600 ? 16 : 40, vertical: 24, ), child: Center( child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 900), child: CupertinoPopupSurface(child: SafeArea(child: viewer)), ), ), ); } return Dialog(child: SafeArea(child: viewer)); } if (cupertino) { return showCupertinoDialog(context: context, builder: buildDialog); } return showDialog(context: context, builder: buildDialog); }