162 lines
5.0 KiB
Dart
162 lines
5.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/unified_patch.dart';
|
|
import 'diff_form_control.dart';
|
|
|
|
export 'diff_form_control.dart';
|
|
|
|
/// Patch columns use the bundled fixed-width face so gutters and markers align.
|
|
TextStyle nativeDiffText(TextStyle base) =>
|
|
base.copyWith(fontFamily: 'RobotoMono');
|
|
|
|
/// Keep Material-derived metadata roles readable inside a Cupertino host.
|
|
ThemeData hostTextTheme(BuildContext context) => ThemeData(
|
|
brightness: CupertinoTheme.brightnessOf(context),
|
|
platform: Theme.of(context).platform,
|
|
);
|
|
|
|
/// GitHub's diff palette (Primer): each changed line on a green or red tint,
|
|
/// its +/- marker and the +N/-N counts in the strong green or red. The code
|
|
/// itself keeps the label colour, as on GitHub, and the exact recorded
|
|
/// markers also distinguish changes without relying on colour.
|
|
({Color text, Color marker, Color? background}) diffLineColors(
|
|
BuildContext context,
|
|
ChangeLineKind kind,
|
|
) {
|
|
final dark = _isDark(context);
|
|
return switch (kind) {
|
|
ChangeLineKind.add => (
|
|
text: labelColor(context),
|
|
marker: addedColor(context),
|
|
background: dark ? const Color(0x262EA043) : const Color(0xFFDAFBE1),
|
|
),
|
|
ChangeLineKind.remove => (
|
|
text: labelColor(context),
|
|
marker: removedColor(context),
|
|
background: dark ? const Color(0x1AF85149) : const Color(0xFFFFEBE9),
|
|
),
|
|
ChangeLineKind.context => (
|
|
text: labelColor(context),
|
|
marker: labelColor(context),
|
|
background: null,
|
|
),
|
|
};
|
|
}
|
|
|
|
bool _isDark(BuildContext context) =>
|
|
(usesCupertino(context)
|
|
? CupertinoTheme.brightnessOf(context)
|
|
: Theme.of(context).brightness) ==
|
|
Brightness.dark;
|
|
|
|
/// Added lines and counts: GitHub's addition green.
|
|
Color addedColor(BuildContext context) =>
|
|
_isDark(context) ? const Color(0xFF3FB950) : const Color(0xFF1A7F37);
|
|
|
|
/// Removed lines and counts: GitHub's deletion red.
|
|
Color removedColor(BuildContext context) =>
|
|
_isDark(context) ? const Color(0xFFF85149) : const Color(0xFFD1242F);
|
|
|
|
final _statToken = RegExp(r'(?<![\w-])([+-]\d+)(?![\w])');
|
|
|
|
/// [text] with every `+N` in green and `-N` in red; the rest unchanged.
|
|
class ChangeStatText extends StatelessWidget {
|
|
const ChangeStatText(this.text, {super.key, this.style});
|
|
|
|
final String text;
|
|
final TextStyle? style;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final spans = <TextSpan>[];
|
|
var at = 0;
|
|
for (final match in _statToken.allMatches(text)) {
|
|
if (match.start > at) {
|
|
spans.add(TextSpan(text: text.substring(at, match.start)));
|
|
}
|
|
final token = match.group(1)!;
|
|
spans.add(
|
|
TextSpan(
|
|
text: token,
|
|
style: TextStyle(
|
|
color: token.startsWith('+')
|
|
? addedColor(context)
|
|
: removedColor(context),
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
);
|
|
at = match.end;
|
|
}
|
|
if (at < text.length) spans.add(TextSpan(text: text.substring(at)));
|
|
return Text.rich(TextSpan(children: spans), style: style);
|
|
}
|
|
}
|
|
|
|
Color labelColor(BuildContext context) => usesCupertino(context)
|
|
? CupertinoColors.label.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.onSurface;
|
|
|
|
Color mutedColor(BuildContext context) => usesCupertino(context)
|
|
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.onSurfaceVariant;
|
|
|
|
Color hunkHeaderColor(BuildContext context) => usesCupertino(context)
|
|
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.surfaceContainerHighest;
|
|
|
|
Widget copyPatchControl(
|
|
BuildContext context, {
|
|
required bool enabled,
|
|
required bool copied,
|
|
required VoidCallback onPressed,
|
|
}) {
|
|
final label = copied ? 'Copied' : 'Copy patch';
|
|
if (usesCupertino(context)) {
|
|
return CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
minimumSize: const Size(44, 44),
|
|
onPressed: enabled ? onPressed : null,
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(CupertinoIcons.doc_on_doc, size: 30),
|
|
const SizedBox(width: 8),
|
|
Text(label),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return TextButton.icon(
|
|
style: TextButton.styleFrom(minimumSize: const Size(0, 48)),
|
|
onPressed: enabled ? onPressed : null,
|
|
icon: const Icon(Icons.copy_all_outlined, size: 28),
|
|
label: Text(label),
|
|
);
|
|
}
|
|
|
|
Widget closeDiffControl(BuildContext context, VoidCallback onClose) {
|
|
if (usesCupertino(context)) {
|
|
return Semantics(
|
|
label: 'Close diff',
|
|
button: true,
|
|
excludeSemantics: true,
|
|
onTap: onClose,
|
|
child: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
minimumSize: const Size(44, 44),
|
|
onPressed: onClose,
|
|
child: const Icon(CupertinoIcons.xmark, size: 30),
|
|
),
|
|
);
|
|
}
|
|
return IconButton(
|
|
constraints: const BoxConstraints(minWidth: 48, minHeight: 48),
|
|
padding: EdgeInsets.zero,
|
|
tooltip: 'Close diff',
|
|
onPressed: onClose,
|
|
icon: const Icon(Icons.close, size: 28),
|
|
);
|
|
}
|