3951c65a7a
A thin ring around Send shows the harness account's 5-hour window used share (the most used live window when there is no 5-hour one): barely visible normally, tertiary from 80%, error from 95%. The button's tooltip (hover, long-press on touch) adds every window with its reset time. No ring when the quota is unknown or unsupported. Painted behind the unchanged button, so its size, target, states and animations stay as they were. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.4 KiB
Dart
96 lines
3.4 KiB
Dart
/// What the composer's send-button usage ring shows: the harness account's
|
||
/// 5-hour window (the most constrained short window harnesses report), or the
|
||
/// most used window when there is no 5-hour one, and how loudly to draw it.
|
||
/// Only reported windows count; one whose reset has passed is stale and left
|
||
/// out, and nothing is estimated.
|
||
library;
|
||
|
||
import 'chat_account_usage.dart';
|
||
import 'chat_usage_warning.dart';
|
||
|
||
/// How the ring is drawn: barely visible, a warning from the first warning
|
||
/// level (80%), urgent from [chatQuotaUrgentLevel] (95%).
|
||
enum ChatUsageRingTone { quiet, warning, urgent }
|
||
|
||
class ChatUsageRing {
|
||
const ChatUsageRing({required this.shown, required this.windows});
|
||
|
||
/// The window the ring's arc measures.
|
||
final ChatQuotaWindow shown;
|
||
|
||
/// Every live window, in the harness's order, for the tooltip.
|
||
final List<ChatQuotaWindow> windows;
|
||
|
||
/// The arc's share of the circle, 0–1 (past a cap stays full).
|
||
double get fraction => (shown.usedPercent / 100).clamp(0.0, 1.0);
|
||
|
||
ChatUsageRingTone get tone {
|
||
final level = chatQuotaLevel(shown.usedPercent);
|
||
if (level >= chatQuotaUrgentLevel) return ChatUsageRingTone.urgent;
|
||
if (level > 0) return ChatUsageRingTone.warning;
|
||
return ChatUsageRingTone.quiet;
|
||
}
|
||
}
|
||
|
||
bool _fiveHour(ChatQuotaWindow window) =>
|
||
window.id == 'five_hour' || window.label.toLowerCase() == '5-hour';
|
||
|
||
/// The ring for [usage] at [now], or null when the harness reports no quota
|
||
/// (unknown, unsupported, or every window already reset).
|
||
ChatUsageRing? chatUsageRing(ChatAccountUsage? usage, DateTime now) {
|
||
if (usage == null || usage.unsupported) return null;
|
||
final at = now.millisecondsSinceEpoch;
|
||
final live = [
|
||
for (final window in usage.windows)
|
||
if (window.resetsAt <= 0 || window.resetsAt > at) window,
|
||
];
|
||
if (live.isEmpty) return null;
|
||
var shown = live.firstWhere(_fiveHour, orElse: () => live.first);
|
||
if (!_fiveHour(shown)) {
|
||
for (final window in live) {
|
||
if (window.usedPercent > shown.usedPercent) shown = window;
|
||
}
|
||
}
|
||
return ChatUsageRing(shown: shown, windows: List.unmodifiable(live));
|
||
}
|
||
|
||
const List<String> _weekdays = [
|
||
'Mon',
|
||
'Tue',
|
||
'Wed',
|
||
'Thu',
|
||
'Fri',
|
||
'Sat',
|
||
'Sun',
|
||
];
|
||
|
||
String _two(int value) => value.toString().padLeft(2, '0');
|
||
|
||
/// A reset time in local 24-hour time: "17:20" today, "Thu 09:00" within the
|
||
/// coming week, "28 Nov 09:00" further out.
|
||
String chatUsageRingResetTime(DateTime at, DateTime now) {
|
||
final local = at.toLocal();
|
||
final today = now.toLocal();
|
||
final time = '${_two(local.hour)}:${_two(local.minute)}';
|
||
final day = DateTime(local.year, local.month, local.day);
|
||
final days = day.difference(DateTime(today.year, today.month, today.day));
|
||
if (days.inHours.abs() < 12) return time;
|
||
if (!days.isNegative && days.inDays < 7) {
|
||
return '${_weekdays[local.weekday - 1]} $time';
|
||
}
|
||
const months = [
|
||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', //
|
||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
|
||
];
|
||
return '${local.day} ${months[local.month - 1]} $time';
|
||
}
|
||
|
||
/// "5-hour 42% · resets 17:20 · Weekly 61% · resets Thu 09:00".
|
||
String chatUsageRingSummary(ChatUsageRing ring, DateTime now) => [
|
||
for (final window in ring.windows) ...[
|
||
'${window.label} ${window.usedPercent.round()}%',
|
||
if (window.resetsAt > 0)
|
||
'resets ${chatUsageRingResetTime(DateTime.fromMillisecondsSinceEpoch(window.resetsAt), now)}',
|
||
],
|
||
].join(' · ');
|