feat(app): a file diff opens as its own page with previous and next file chevrons
This commit is contained in:
@@ -230,42 +230,137 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
|
||||
Widget _scrollsWhenTooWide(Widget control) =>
|
||||
SingleChildScrollView(scrollDirection: Axis.horizontal, child: control);
|
||||
|
||||
/// 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<void> showFileDiffViewer(BuildContext context, FileChange file) {
|
||||
final cupertino = usesCupertino(context);
|
||||
Widget buildDialog(BuildContext dialogContext) {
|
||||
/// 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,
|
||||
onClose: () => Navigator.of(dialogContext).pop(),
|
||||
);
|
||||
final pager = count < 2
|
||||
? null
|
||||
: FileDiffPager(
|
||||
index: _index,
|
||||
count: count,
|
||||
onPrevious: _index > 0 ? () => _step(-1) : null,
|
||||
onNext: _index < count - 1 ? () => _step(1) : null,
|
||||
);
|
||||
final body = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Expanded(child: viewer),
|
||||
?pager,
|
||||
],
|
||||
);
|
||||
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 Theme(
|
||||
// Dialogs open on the app Navigator, above the Changes page's own
|
||||
// brightness-aware theme, so the popup needs its own.
|
||||
data: hostTextTheme(dialogContext),
|
||||
child: 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 CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(middle: Text(name)),
|
||||
child: SafeArea(
|
||||
child: Theme(data: hostTextTheme(context), child: body),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Dialog(child: SafeArea(child: viewer));
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(name)),
|
||||
body: SafeArea(top: false, child: body),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Previous and next as chevrons in one row at the bottom right, with the
|
||||
/// position between them read as `2 of 5`.
|
||||
class FileDiffPager extends StatelessWidget {
|
||||
const FileDiffPager({
|
||||
super.key,
|
||||
required this.index,
|
||||
required this.count,
|
||||
this.onPrevious,
|
||||
this.onNext,
|
||||
});
|
||||
|
||||
final int index;
|
||||
final int count;
|
||||
final VoidCallback? onPrevious;
|
||||
final VoidCallback? onNext;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = usesCupertino(context);
|
||||
Widget button(
|
||||
IconData material,
|
||||
IconData ios,
|
||||
String tip,
|
||||
VoidCallback? on,
|
||||
) => cupertino
|
||||
? Semantics(
|
||||
button: true,
|
||||
label: tip,
|
||||
child: CupertinoButton(
|
||||
padding: const EdgeInsets.all(8),
|
||||
onPressed: on,
|
||||
child: Icon(ios),
|
||||
),
|
||||
)
|
||||
: IconButton(onPressed: on, tooltip: tip, icon: Icon(material));
|
||||
return Padding(
|
||||
key: const ValueKey('file-diff-pager'),
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 8, 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Text('${index + 1} of $count'),
|
||||
const SizedBox(width: 4),
|
||||
button(
|
||||
Icons.chevron_left,
|
||||
CupertinoIcons.chevron_left,
|
||||
'Previous file',
|
||||
onPrevious,
|
||||
),
|
||||
button(
|
||||
Icons.chevron_right,
|
||||
CupertinoIcons.chevron_right,
|
||||
'Next file',
|
||||
onNext,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (cupertino) {
|
||||
return showCupertinoDialog<void>(context: context, builder: buildDialog);
|
||||
}
|
||||
return showDialog<void>(context: context, builder: buildDialog);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user