Files
pipelines/app/lib/features/chat/conversation/chat_send_usage_ring.dart
T
alan 09bafca21c Merge branch 'main' into worktree-agent-abb9404cc062f4b30
# Conflicts:
#	app/lib/application/app.dart
#	app/lib/application/detached/detached_app.dart
#	app/lib/application/pipelines_root.dart
#	app/lib/features/chat/ui/sessions/chat_color_palette.dart
#	app/lib/ui/theme/app_design.dart
#	app/lib/ui/theme/app_design_tokens.dart
2026-12-04 00:41:42 +00:00

268 lines
8.6 KiB
Dart

import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../../../ui/theme/app_design_tokens.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;
final tokens = AppDesignTokens.of(context);
// A design with a contrast floor draws the ring solid: the quiet arc
// in the secondary ink over a track in the rule colour.
final solid = tokens.minContrast != null;
return ChatUsageRingPainter(
fraction: ring.fraction,
color: switch (ring.tone) {
ChatUsageRingTone.quiet =>
solid
? scheme.onSurfaceVariant
: scheme.onSurfaceVariant.withValues(alpha: .45),
ChatUsageRingTone.warning => scheme.tertiary,
ChatUsageRingTone.urgent => scheme.error,
},
track: solid
? scheme.outlineVariant
: scheme.outlineVariant.withValues(alpha: .35),
circle: chatSendButtonDiameter(context),
// A design with square-ish corners frames its square send button.
cornerRadius: tokens.radius,
stroke: tokens.ringStroke,
);
}
}
/// The send button's visible size in every design: the composer pins it
/// ([chatSendButtonStyle]), so the ring's gap is the same everywhere.
const double chatSendButtonSize = 40;
/// The send button's fixed size, whatever the design's density.
ButtonStyle chatSendButtonStyle({Color? background, Color? foreground}) =>
IconButton.styleFrom(
backgroundColor: background,
foregroundColor: foreground,
fixedSize: const Size.square(chatSendButtonSize),
minimumSize: const Size.square(chatSendButtonSize),
maximumSize: const Size.square(chatSendButtonSize),
padding: EdgeInsets.zero,
);
/// The filled send button's visible size.
double chatSendButtonDiameter(BuildContext context) => chatSendButtonSize;
/// 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,
this.cornerRadius,
this.stroke = 2,
});
final double fraction;
final Color color;
final Color track;
final double circle;
/// The button's corner radius under a design that squares it; null for the
/// round button. The ring then follows a rounded square instead.
final double? cornerRadius;
/// The ring's width ([AppDesignTokens.ringStroke]).
final double stroke;
/// A clear gap between the button and the ring, so the ring reads as its
/// own feature.
static const double gap = 3;
/// 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;
final corner = cornerRadius;
if (corner != null) {
_paintSquare(canvas, size, corner, paint);
return;
}
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,
);
}
/// The rounded-square ring: the track all round and the used part
/// clockwise from the top centre.
void _paintSquare(Canvas canvas, Size size, double corner, Paint paint) {
final half = radius;
final rect = Rect.fromCenter(
center: size.center(Offset.zero),
width: half * 2,
height: half * 2,
);
// A square button (no corner radius) keeps a square ring; a rounded one
// a ring with its corners offset by the gap.
final r = corner <= 0
? Radius.zero
: Radius.circular(corner + gap + stroke / 2);
final c = rect.center;
// Built clockwise from the top centre, so the used length starts there.
final path = Path()
..moveTo(c.dx, rect.top)
..lineTo(rect.right - r.x, rect.top)
..arcToPoint(Offset(rect.right, rect.top + r.y), radius: r)
..lineTo(rect.right, rect.bottom - r.y)
..arcToPoint(Offset(rect.right - r.x, rect.bottom), radius: r)
..lineTo(rect.left + r.x, rect.bottom)
..arcToPoint(Offset(rect.left, rect.bottom - r.y), radius: r)
..lineTo(rect.left, rect.top + r.y)
..arcToPoint(Offset(rect.left + r.x, rect.top), radius: r)
..close();
canvas.drawPath(path, paint..color = track);
if (fraction <= 0) return;
final metric = path.computeMetrics().first;
canvas.drawPath(
metric.extractPath(0, metric.length * fraction.clamp(0, 1)),
paint
..color = color
..strokeCap = fraction >= 1 ? StrokeCap.butt : StrokeCap.square,
);
}
@override
bool shouldRepaint(ChatUsageRingPainter old) =>
old.cornerRadius != cornerRadius ||
old.stroke != stroke ||
old.fraction != fraction ||
old.color != color ||
old.track != track ||
old.circle != circle;
}