7b045e5aab
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
5.8 KiB
Dart
194 lines
5.8 KiB
Dart
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<ChatSendUsageRing> createState() => _ChatSendUsageRingState();
|
|
}
|
|
|
|
class _ChatSendUsageRingState extends State<ChatSendUsageRing> {
|
|
/// Keeps the reset times and a passed reset current while a ring shows.
|
|
Timer? _tick;
|
|
|
|
/// What the last build showed: the tooltip and the ring's arc and tone.
|
|
/// The source (the thread controller) notifies on every streamed delta;
|
|
/// only a change here rebuilds the button.
|
|
Object? _shown;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
widget.source?.addListener(_onSource);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(ChatSendUsageRing oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.source != widget.source) {
|
|
oldWidget.source?.removeListener(_onSource);
|
|
widget.source?.addListener(_onSource);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.source?.removeListener(_onSource);
|
|
_tick?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onSource() {
|
|
if (!mounted) return;
|
|
final now = widget.now();
|
|
final usage = widget.usage();
|
|
if (_shownOf(usage, chatUsageRing(usage, now), now) == _shown) return;
|
|
setState(() {});
|
|
}
|
|
|
|
Object _shownOf(ChatAccountUsage? usage, ChatUsageRing? ring, DateTime now) =>
|
|
(
|
|
chatSendTooltip(widget.label, usage, ring, now),
|
|
ring?.fraction,
|
|
ring?.tone,
|
|
);
|
|
|
|
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 now = widget.now();
|
|
final usage = widget.usage();
|
|
final ring = chatUsageRing(usage, now);
|
|
_ticking(ring != null);
|
|
_shown = _shownOf(usage, ring, now);
|
|
// 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;
|
|
}
|