Files
pipelines/app/lib/features/chat/conversation/composer_running_border.dart
T
alan 9d37db5939 Measure the running composer border once per size
The highlight's outline was rebuilt and measured (computeMetrics) on
every animation frame of a running turn, the largest app cost while a
reply streams (about 600 ms of CPU over 15 s at 4K). The painter now
keeps the measured outline for its size and only extracts the moving
pieces per frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:19:22 +00:00

153 lines
4.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'composer_running_arc.dart';
/// The composer's "agent is working" edge: one short highlight in [primary]
/// and [tertiary] that travels around the pill's rounded border at constant
/// speed. Nothing else changes: the rest of the border, its fill and its
/// thickness stay the normal static outline drawn beneath it.
///
/// Mount it only while a turn runs; disposing it stops the animation, so the
/// border ends cleanly with the turn.
///
/// With reduced motion (the platform's disable-animations setting) it paints
/// nothing, leaving the normal static border, and never ticks.
class ComposerRunningBorder extends StatefulWidget {
const ComposerRunningBorder({
super.key,
required this.shape,
required this.primary,
required this.tertiary,
this.focused = false,
});
/// The field has focus, so its focus ring already outlines the pill in the
/// same colour; the highlight is drawn thicker to stay visible over it.
final bool focused;
/// The pill's shape, so the highlight follows its edge.
final OutlinedBorder shape;
final Color primary;
final Color tertiary;
/// One lap of the highlight around the border (about 2.5–3 s).
static const Duration period = Duration(milliseconds: 2750);
/// The stroke width of the highlight, drawn just inside the pill's edge.
static const double strokeWidth = 2;
/// The stroke width over a focused field, wider than its focus ring.
static const double focusedStrokeWidth = 4;
@override
State<ComposerRunningBorder> createState() => _ComposerRunningBorderState();
}
class _ComposerRunningBorderState extends State<ComposerRunningBorder>
with SingleTickerProviderStateMixin {
late final AnimationController _lap = AnimationController(
vsync: this,
duration: ComposerRunningBorder.period,
);
bool _reduced = false;
@override
void initState() {
super.initState();
_lap.repeat();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
final reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
if (reduced == _reduced) return;
_reduced = reduced;
if (reduced) {
_lap.stop();
} else {
_lap.repeat();
}
}
@override
void dispose() {
_lap.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => RepaintBoundary(
key: const Key('chat-composer-running-border'),
child: CustomPaint(
painter: _RunningBorderPainter(
shape: widget.shape,
primary: widget.primary,
tertiary: widget.tertiary,
width: widget.focused
? ComposerRunningBorder.focusedStrokeWidth
: ComposerRunningBorder.strokeWidth,
lap: _reduced ? null : _lap,
),
),
);
}
class _RunningBorderPainter extends CustomPainter {
_RunningBorderPainter({
required this.shape,
required this.primary,
required this.tertiary,
required this.width,
required this.lap,
}) : super(repaint: lap);
/// The highlight's stroke width.
final double width;
final OutlinedBorder shape;
final Color primary;
final Color tertiary;
/// The lap's progress, or null for the static reduced-motion border.
final Animation<double>? lap;
Size? _outlineSize;
RunningOutline? _outline;
@override
void paint(Canvas canvas, Size size) {
final progress = lap?.value;
// Reduced motion: no highlight; the normal static border shows alone.
if (progress == null || size.isEmpty) return;
// The outline is measured once per size (the painter lives across the
// lap's frames; a new shape, width or colour makes a new painter).
if (_outline == null || _outlineSize != size) {
_outlineSize = size;
_outline = RunningOutline(
shape.getOuterPath((Offset.zero & size).deflate(width / 2)),
);
}
final line = Paint()
..style = PaintingStyle.stroke
..strokeWidth = width;
// The highlight is laid along the outline by arc length, so its speed is
// even around corners and straight edges alike. Only its pieces are drawn.
for (final stroke in _outline!.strokes(
progress: progress,
primary: primary,
tertiary: tertiary,
)) {
canvas.drawPath(stroke.path, line..color = stroke.color);
}
}
@override
bool shouldRepaint(_RunningBorderPainter oldDelegate) =>
oldDelegate.shape != shape ||
oldDelegate.primary != primary ||
oldDelegate.tertiary != tertiary ||
oldDelegate.width != width ||
oldDelegate.lap != lap;
}