Files
pipelines/app/lib/features/chat/conversation/composer_running_border.dart
T
alan f3a469c356 Animate chat composer while turns run
Replace the running Stop button with an animated composer border and task spinner. Add Escape, slash-command, and touch-menu stopping paths with focused widget coverage, including reduced-motion behavior.
2026-11-12 02:24:46 +00:00

147 lines
4.0 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
/// The composer's "agent is working" edge: a soft highlight that travels once
/// around the pill's rounded border in [primary] and [tertiary], with a faint
/// glow 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 draws
/// a static, evenly highlighted border instead and never ticks.
class ComposerRunningBorder extends StatefulWidget {
const ComposerRunningBorder({
super.key,
required this.shape,
required this.primary,
required this.tertiary,
});
/// 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.
static const Duration period = Duration(seconds: 4);
/// The stroke drawn just inside the pill's edge.
static const double strokeWidth = 2;
/// The static border's opacity under reduced motion.
static const double staticOpacity = 0.7;
@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,
lap: _reduced ? null : _lap,
),
),
);
}
class _RunningBorderPainter extends CustomPainter {
_RunningBorderPainter({
required this.shape,
required this.primary,
required this.tertiary,
required this.lap,
}) : super(repaint: lap);
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;
@override
void paint(Canvas canvas, Size size) {
if (size.isEmpty) return;
const width = ComposerRunningBorder.strokeWidth;
final rect = Offset.zero & size;
final path = shape.getOuterPath(rect.deflate(width / 2));
final glow = Paint()
..style = PaintingStyle.stroke
..strokeWidth = width * 3
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 4);
final line = Paint()
..style = PaintingStyle.stroke
..strokeWidth = width;
final progress = lap?.value;
if (progress == null) {
final solid = primary.withValues(
alpha: ComposerRunningBorder.staticOpacity,
);
glow.color = solid.withValues(alpha: solid.a * .4);
line.color = solid;
} else {
final shader = SweepGradient(
colors: [
primary.withValues(alpha: 0),
primary,
tertiary,
tertiary.withValues(alpha: 0),
primary.withValues(alpha: 0),
],
stops: const [0, .12, .24, .38, 1],
transform: GradientRotation(progress * 2 * math.pi),
).createShader(rect);
glow.shader = shader;
line.shader = shader;
}
canvas
..drawPath(path, glow)
..drawPath(path, line);
}
@override
bool shouldRepaint(_RunningBorderPainter oldDelegate) =>
oldDelegate.shape != shape ||
oldDelegate.primary != primary ||
oldDelegate.tertiary != tertiary ||
oldDelegate.lap != lap;
}