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

167 lines
5.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../domain/unified_patch.dart';
/// Which form of a recorded patch the viewer is showing.
enum DiffForm { rendered, raw }
/// The bundled code face (`assets/fonts/RobotoMono.ttf`, SIL OFL 1.1). Flutter
/// web cannot resolve a system monospace family, so the app carries one and
/// every target renders patch text the same way. The fallbacks are only for
/// hosts that cannot load the asset.
const codeFontFamily = 'RobotoMono';
/// The app's established platform switch: Material for Android/desktop/web,
/// Cupertino for iOS and macOS. Feature widgets stay portable across all three
/// targets instead of hard-coding one control set.
bool usesCupertino(BuildContext context) {
final platform = Theme.of(context).platform;
return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS;
}
/// The app's code face for patch text. The size, weight and text scaler come
/// from the caller's theme style, so code follows the operator's text size.
TextStyle monospaceText(TextStyle base) => base.copyWith(
fontFamily: codeFontFamily,
fontFamilyFallback: const ['Menlo', 'Consolas', 'Courier New', 'monospace'],
);
/// The distinguishable success/removal colours shared by the stat line and the
/// diff rows. A theme-aware green/red pair, never two shades of the same hue:
/// the light Material containers alone made additions and removals read alike.
({Color text, Color? background}) diffLineColors(
BuildContext context,
ChangeLineKind kind,
) {
final dark = usesCupertino(context)
? CupertinoTheme.of(context).brightness == Brightness.dark
: Theme.of(context).brightness == Brightness.dark;
return switch (kind) {
ChangeLineKind.add =>
dark
? (text: const Color(0xFF7FE0A8), background: const Color(0x297FE0A8))
: (
text: const Color(0xFF116B33),
background: const Color(0xFFE3F3E7),
),
ChangeLineKind.remove =>
dark
? (text: const Color(0xFFFF9E9E), background: const Color(0x29FF9E9E))
: (
text: const Color(0xFFB3261E),
background: const Color(0xFFFBE7E5),
),
ChangeLineKind.context => (text: labelColor(context), background: null),
};
}
/// The plain body/label colour for non-diff text inside the viewer.
Color labelColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.label.resolveFrom(context)
: Theme.of(context).colorScheme.onSurface;
/// The muted colour for gutters, notes and secondary metadata.
Color mutedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
/// The hunk header's fill.
Color hunkHeaderColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
: Theme.of(context).colorScheme.surfaceContainerHighest;
/// The viewer's shared Rendered/Raw control: a native segmented control on each
/// platform, with the same two words the browser uses.
Widget diffFormControl(
BuildContext context, {
required DiffForm form,
required ValueChanged<DiffForm> onChanged,
}) {
if (usesCupertino(context)) {
return CupertinoSlidingSegmentedControl<DiffForm>(
groupValue: form,
onValueChanged: (value) {
if (value != null) onChanged(value);
},
children: const {
DiffForm.rendered: _SegmentLabel('Rendered'),
DiffForm.raw: _SegmentLabel('Raw patch'),
},
);
}
return SegmentedButton<DiffForm>(
segments: const [
ButtonSegment(value: DiffForm.rendered, label: Text('Rendered')),
ButtonSegment(value: DiffForm.raw, label: Text('Raw patch')),
],
selected: {form},
showSelectedIcon: false,
onSelectionChanged: (selection) {
if (selection.isNotEmpty) onChanged(selection.first);
},
);
}
class _SegmentLabel extends StatelessWidget {
const _SegmentLabel(this.label);
final String label;
@override
Widget build(BuildContext context) =>
Padding(padding: const EdgeInsets.all(6), child: Text(label));
}
/// Copy the server's raw patch exactly as recorded. Disabled when the entry has
/// no readable patch, so the action never copies a fabricated empty diff.
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),
);
}
/// The standard close action, labelled exactly on both platforms.
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),
);
}