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 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; } /// Patch text inherits the host's typography, including its font and scaler. /// A diff is not an exception to the native-defaults requirement. TextStyle nativeDiffText(TextStyle base) => base; /// Native semantic colours, never a bespoke diff palette. The recorded +/- /// markers carry meaning independently of colour. No background tint is added. ({Color text, Color? background}) diffLineColors( BuildContext context, ChangeLineKind kind, ) { return switch (kind) { ChangeLineKind.add => ( text: usesCupertino(context) ? CupertinoColors.systemGreen.resolveFrom(context) : Theme.of(context).colorScheme.primary, background: null, ), ChangeLineKind.remove => ( text: usesCupertino(context) ? CupertinoColors.destructiveRed.resolveFrom(context) : Theme.of(context).colorScheme.error, background: null, ), 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 onChanged, }) { if (usesCupertino(context)) { return CupertinoSlidingSegmentedControl( groupValue: form, onValueChanged: (value) { if (value != null) onChanged(value); }, children: const { DiffForm.rendered: _SegmentLabel('Rendered'), DiffForm.raw: _SegmentLabel('Raw patch'), }, ); } return SegmentedButton( 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), ); }