Files
pipelines/app/lib/features/changes/widgets/change_style.dart
T

143 lines
4.3 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 text inherits the host's typography without a diff-specific font.
TextStyle nativeDiffText(TextStyle base) => base;
/// Keep Material-derived metadata roles readable inside a Cupertino host.
ThemeData hostTextTheme(BuildContext context) => ThemeData(
brightness: CupertinoTheme.brightnessOf(context),
platform: Theme.of(context).platform,
);
/// Native semantic roles, never a bespoke palette or added row tint. The exact
/// recorded +/- markers also distinguish changes without relying on colour.
({Color text, Color? background}) diffLineColors(
BuildContext context,
ChangeLineKind kind,
) => switch (kind) {
ChangeLineKind.add => (
text: addedColor(context),
background: addedColor(context).withValues(alpha: 0.12),
),
ChangeLineKind.remove => (
text: removedColor(context),
background: removedColor(context).withValues(alpha: 0.12),
),
ChangeLineKind.context => (text: labelColor(context), background: null),
};
/// Added lines and counts: the platform's green, readable in both themes.
Color addedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.systemGreen.resolveFrom(context)
: (Theme.of(context).brightness == Brightness.dark
? const Color(0xFF81C995)
: const Color(0xFF1E8E3E));
/// Removed lines and counts: the platform's red.
Color removedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error;
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,
onPressed: enabled ? onPressed : null,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(CupertinoIcons.doc_on_doc),
const SizedBox(width: 8),
Text(label),
],
),
);
}
return TextButton.icon(
onPressed: enabled ? onPressed : null,
icon: const Icon(Icons.copy_all_outlined),
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,
onPressed: onClose,
child: const Icon(CupertinoIcons.xmark),
),
);
}
return IconButton(
tooltip: 'Close diff',
onPressed: onClose,
icon: const Icon(Icons.close),
);
}