import '../../../ui/adaptive/segmented_bar.dart'; import '../../../ui/input/arrow_key_pager.dart'; import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../../ui/navigation/chevron_pager.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; import '../../../ui/adaptive/glass_bar.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'; import 'raw_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, this.floatBehindTopBar = false, }); final FileChange file; /// 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 createState() => _FileDiffViewerState(); } class _FileDiffViewerState extends State { DiffForm _form = DiffForm.rendered; bool _copied = false; Timer? _copyTimer; @override void dispose() { _copyTimer?.cancel(); super.dispose(); } void _selectForm(DiffForm value) => setState(() => _form = value); 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 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)); } if (_form == DiffForm.raw) { return Semantics( container: true, label: 'Raw patch for ${file.path}', value: file.patch, child: RawPatchView(patch: file.patch!, style: mono), ); } 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), ); } } /// 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); /// Open [file] on its own page, as the browser's file view. [files] is the /// list it was picked from (the visit's shown files); the page steps through /// them with previous/next chevrons at its bottom right. Future showFileDiffViewer( BuildContext context, FileChange file, { List? files, }) { final list = files == null || !files.contains(file) ? [file] : files; final page = FileDiffPage(files: list, initialIndex: list.indexOf(file)); return Navigator.of( context, ).push(MaterialPageRoute(builder: (_) => page)); } /// One recorded file's diff as a page, with the previous and next file of the /// list it came from. class FileDiffPage extends StatefulWidget { const FileDiffPage({super.key, required this.files, this.initialIndex = 0}); final List files; final int initialIndex; @override State createState() => _FileDiffPageState(); } class _FileDiffPageState extends State { late int _index = widget.initialIndex.clamp(0, widget.files.length - 1); void _step(int delta) => setState(() => _index += delta); @override Widget build(BuildContext context) { final file = widget.files[_index]; final name = file.path.split('/').last; final count = widget.files.length; final viewer = FileDiffViewer( key: ValueKey('file-diff-${file.path}'), file: file, floatBehindTopBar: true, ); final pager = count < 2 ? null : ChevronPager( key: const ValueKey('file-diff-pager'), index: _index, count: count, previousLabel: 'Previous file', nextLabel: 'Next file', onPrevious: _index > 0 ? () => _step(-1) : null, onNext: _index < count - 1 ? () => _step(1) : null, ); // Left/right arrows step between changed files, like the chevrons; // Escape closes the page, like its back button. final body = ArrowKeyPager( onPrevious: _index > 0 ? () => _step(-1) : null, onNext: _index < count - 1 ? () => _step(1) : null, onEscape: () => Navigator.of(context).maybePop(), child: FloatingBarOverlay( bar: pager == null ? const SizedBox.shrink() : Align( alignment: AlignmentDirectional.centerEnd, heightFactor: 1, child: SafeArea( top: false, minimum: const EdgeInsets.all(8), child: GlassControlBar(child: pager), ), ), child: viewer, ), ); return Scaffold( extendBodyBehindAppBar: true, appBar: SegmentedBar(title: name), body: body, ); } }