317 lines
11 KiB
Dart
317 lines
11 KiB
Dart
import '../../../ui/input/arrow_key_pager.dart';
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../../ui/navigation/chevron_pager.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});
|
|
|
|
final FileChange file;
|
|
|
|
/// Called by the Close control. Defaults to popping the enclosing route.
|
|
final VoidCallback? onClose;
|
|
|
|
@override
|
|
State<FileDiffViewer> createState() => _FileDiffViewerState();
|
|
}
|
|
|
|
class _FileDiffViewerState extends State<FileDiffViewer> {
|
|
DiffForm _form = DiffForm.rendered;
|
|
bool _copied = false;
|
|
Timer? _copyTimer;
|
|
|
|
@override
|
|
void dispose() {
|
|
_copyTimer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _selectForm(DiffForm value) => setState(() => _form = value);
|
|
|
|
void _close() {
|
|
final close = widget.onClose;
|
|
if (close != null) {
|
|
close();
|
|
} else {
|
|
Navigator.of(context).maybePop();
|
|
}
|
|
}
|
|
|
|
Future<void> _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)
|
|
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,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
closeDiffControl(context, _close),
|
|
],
|
|
),
|
|
),
|
|
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,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Divider(
|
|
height: 16,
|
|
color: cupertino
|
|
? CupertinoColors.separator.resolveFrom(context)
|
|
: Theme.of(context).dividerColor,
|
|
),
|
|
Flexible(child: _body(context, mono)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _body(BuildContext context, 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: 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<void> showFileDiffViewer(
|
|
BuildContext context,
|
|
FileChange file, {
|
|
List<FileChange>? files,
|
|
}) {
|
|
final list = files == null || !files.contains(file) ? [file] : files;
|
|
final page = FileDiffPage(files: list, initialIndex: list.indexOf(file));
|
|
return Navigator.of(context).push<void>(
|
|
usesCupertino(context)
|
|
? CupertinoPageRoute<void>(builder: (_) => page)
|
|
: MaterialPageRoute<void>(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<FileChange> files;
|
|
final int initialIndex;
|
|
|
|
@override
|
|
State<FileDiffPage> createState() => _FileDiffPageState();
|
|
}
|
|
|
|
class _FileDiffPageState extends State<FileDiffPage> {
|
|
late int _index = widget.initialIndex.clamp(0, widget.files.length - 1);
|
|
|
|
void _step(int delta) => setState(() => _index += delta);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final cupertino = usesCupertino(context);
|
|
final file = widget.files[_index];
|
|
final name = file.path.split('/').last;
|
|
final count = widget.files.length;
|
|
final viewer = FileDiffViewer(
|
|
key: ValueKey<String>('file-diff-${file.path}'),
|
|
file: file,
|
|
);
|
|
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.
|
|
final body = ArrowKeyPager(
|
|
onPrevious: _index > 0 ? () => _step(-1) : null,
|
|
onNext: _index < count - 1 ? () => _step(1) : null,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Expanded(child: viewer),
|
|
?pager,
|
|
],
|
|
),
|
|
);
|
|
if (cupertino) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(middle: Text(name)),
|
|
child: SafeArea(
|
|
child: Theme(data: hostTextTheme(context), child: body),
|
|
),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(name)),
|
|
body: SafeArea(top: false, child: body),
|
|
);
|
|
}
|
|
}
|