import 'dart:async'; import 'dart:math' as math; import 'package:flutter/material.dart'; import '../domain/chat_account_usage.dart'; import '../domain/chat_usage_ring.dart'; /// A thin ring around the composer's send button showing how much of the /// harness account's quota is used ([chatUsageRing]): barely visible /// normally, the tertiary colour from 80% and the error colour from 95%. The /// button's tooltip (hover, or long-press on touch) adds the account and /// every window: "Send" then "sam@example.com · 5-hour 42% · resets 17:20 · /// Weekly 61% · resets Thu 09:00" ([chatSendTooltip]). /// /// The ring is painted beside the button, never in its layout or hit test, /// so the button keeps its size, target, states and animations. Without a /// reported quota there is no ring and the tooltip is the label alone. class ChatSendUsageRing extends StatefulWidget { const ChatSendUsageRing({ super.key, required this.source, required this.usage, required this.label, required this.button, this.now = DateTime.now, }); /// Notifies when [usage] may have changed (the chat's thread controller). final Listenable? source; final ChatAccountUsage? Function() usage; /// The button's own tooltip, e.g. "Send". final String label; /// Builds the button with the tooltip to show. final Widget Function(String tooltip) button; final DateTime Function() now; @override State createState() => _ChatSendUsageRingState(); } class _ChatSendUsageRingState extends State { /// Keeps the reset times and a passed reset current while a ring shows. Timer? _tick; @override void dispose() { _tick?.cancel(); super.dispose(); } void _ticking(bool on) { if (on == (_tick != null)) return; _tick?.cancel(); _tick = on ? Timer.periodic(const Duration(minutes: 1), (_) { if (mounted) setState(() {}); }) : null; } @override Widget build(BuildContext context) { final source = widget.source; if (source == null) return _ring(context); return ListenableBuilder( listenable: source, builder: (context, _) => _ring(context), ); } Widget _ring(BuildContext context) { final now = widget.now(); final usage = widget.usage(); final ring = chatUsageRing(usage, now); _ticking(ring != null); // The button stays the same child with or without a ring, so a report // arriving never remounts it (its hover, focus and ink stay). return CustomPaint( painter: ring == null ? null : _painter(context, ring), child: widget.button(chatSendTooltip(widget.label, usage, ring, now)), ); } ChatUsageRingPainter _painter(BuildContext context, ChatUsageRing ring) { final scheme = Theme.of(context).colorScheme; return ChatUsageRingPainter( fraction: ring.fraction, color: switch (ring.tone) { ChatUsageRingTone.quiet => scheme.onSurfaceVariant.withValues( alpha: .45, ), ChatUsageRingTone.warning => scheme.tertiary, ChatUsageRingTone.urgent => scheme.error, }, track: scheme.outlineVariant.withValues(alpha: .35), circle: chatSendButtonDiameter(context), ); } } /// The filled send button's visible circle: a Material 3 icon button's 40 /// logical pixels adjusted by its visual density, inside a target that may /// be padded beyond it. double chatSendButtonDiameter(BuildContext context) { final density = IconButtonTheme.of(context).style?.visualDensity ?? Theme.of(context).visualDensity; return 40 + density.baseSizeAdjustment.dx; } /// The ring: a faint full track and the used arc clockwise from the top, a /// hairline gap outside the button's circle of diameter [circle], centred /// in the painted box. class ChatUsageRingPainter extends CustomPainter { const ChatUsageRingPainter({ required this.fraction, required this.color, required this.track, required this.circle, }); final double fraction; final Color color; final Color track; final double circle; static const double stroke = 2; static const double gap = 1; /// The ring's centre-line radius. double get radius => circle / 2 + gap + stroke / 2; @override void paint(Canvas canvas, Size size) { final rect = Rect.fromCircle( center: size.center(Offset.zero), radius: radius, ); final paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = stroke ..isAntiAlias = true; canvas.drawCircle(rect.center, radius, paint..color = track); if (fraction <= 0) return; canvas.drawArc( rect, -math.pi / 2, 2 * math.pi * fraction, false, paint ..color = color ..strokeCap = fraction >= 1 ? StrokeCap.butt : StrokeCap.round, ); } @override bool shouldRepaint(ChatUsageRingPainter old) => old.fraction != fraction || old.color != color || old.track != track || old.circle != circle; }