Files
pipelines/app/lib/ui/status/outcome_palette.dart
T
alan 3ca1bf4a00 Remove Cupertino UI branches
Keep the Material-only status, text, and scale paths while preserving public signatures and the Cupertino colors still required by the status API.
2026-11-11 01:00:35 +00:00

147 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
/// Colours for recorded outcome names within one run.
///
/// Well-known outcome words get a colour family that matches their meaning —
/// success-like words are green, failures red, rejections orange, retries and
/// advisories amber, skips and cancellations grey — and every other word gets
/// a distinct hue outside those families, by first appearance. The same name
/// always gets the same colour, and two different names never share one: a
/// second word of the same family takes that family's next shade.
class OutcomePalette {
OutcomePalette(Iterable<String> outcomes) {
final familyUse = <_Family, int>{};
var other = 0;
for (final outcome in outcomes) {
final key = _key(outcome);
if (key.isEmpty || _swatches.containsKey(key)) continue;
final family = _familyOf(key);
if (family != null) {
final used = familyUse[family] ?? 0;
final shades = _familyShades[family]!;
if (used < shades.length) {
familyUse[family] = used + 1;
_swatches[key] = _Swatch.material(shades[used]);
continue;
}
}
_swatches[key] = other < _otherHues.length
? _Swatch.material(_otherHues[other])
: _Swatch.wheel(other);
other += 1;
}
}
final Map<String, _Swatch> _swatches = {};
static String _key(String outcome) => outcome.trim().toLowerCase();
/// Whether [outcome] was seen (and so has a colour of its own).
bool contains(String outcome) => _swatches.containsKey(_key(outcome));
/// [outcome]'s colour for the theme's brightness; an unseen or blank name
/// uses the theme's secondary text colour.
Color colorFor(BuildContext context, String outcome) {
final swatch = _swatches[_key(outcome)];
if (swatch == null) {
return Theme.of(context).colorScheme.onSurfaceVariant;
}
return swatch.resolve(Theme.of(context).brightness);
}
/// [outcome]'s colour on a page of [brightness], or null when unseen.
Color? colorForBrightness(String outcome, Brightness brightness) =>
_swatches[_key(outcome)]?.resolve(brightness);
}
enum _Family { success, failure, rejection, caution, neutral }
/// The meaning a well-known outcome word carries, if any.
_Family? _familyOf(String key) {
final word = key.replaceAll(RegExp(r'[\s-]+'), '_');
const success = {
'success',
'succeeded',
'ok',
'pass',
'passed',
'approved',
'accepted',
'done',
'completed',
'complete',
'merged',
'landed',
};
const failure = {'error', 'failure', 'failed', 'fail', 'crashed', 'broken'};
const rejection = {
'rejected',
'reject',
'blocked',
'denied',
'declined',
'changes_requested',
};
const caution = {
'retry',
'blocked_retryable',
'advisory',
'warning',
'needs_work',
'partial',
};
const neutral = {
'skipped',
'skip',
'canceled',
'cancelled',
'stopped',
'none',
};
if (success.contains(word)) return _Family.success;
if (failure.contains(word)) return _Family.failure;
if (rejection.contains(word)) return _Family.rejection;
if (caution.contains(word)) return _Family.caution;
if (neutral.contains(word)) return _Family.neutral;
return null;
}
/// Each family's shades, in the order successive distinct words take them.
const Map<_Family, List<MaterialColor>> _familyShades = {
_Family.success: [Colors.green, Colors.lightGreen, Colors.teal],
_Family.failure: [Colors.red, Colors.pink],
_Family.rejection: [Colors.deepOrange, Colors.orange],
_Family.caution: [Colors.amber, Colors.yellow],
_Family.neutral: [Colors.blueGrey, Colors.grey],
};
/// Hues for words with no family, chosen away from the family colours.
const List<MaterialColor> _otherHues = [
Colors.blue,
Colors.purple,
Colors.indigo,
Colors.cyan,
Colors.brown,
Colors.deepPurple,
Colors.lightBlue,
];
class _Swatch {
const _Swatch.material(MaterialColor this.color) : wheelIndex = null;
const _Swatch.wheel(int this.wheelIndex) : color = null;
final MaterialColor? color;
final int? wheelIndex;
Color resolve(Brightness brightness) {
final dark = brightness == Brightness.dark;
final material = color;
if (material != null) return dark ? material.shade300 : material.shade800;
// Past the named hues, continue round the wheel by the golden angle,
// starting from a blue-violet so the next words stay clear of the
// families' greens, reds, oranges and ambers.
final degrees = (230 + wheelIndex! * 137.508) % 360;
return HSLColor.fromAHSL(1, degrees, 0.55, dark ? 0.72 : 0.35).toColor();
}
}