677d202ae7
The focus ring outlines the pill in the same colour, hiding the 2 px highlight; draw it 4 px wide over a focused field.
144 lines
4.2 KiB
Dart
144 lines
4.2 KiB
Dart
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;
|
||
|
||
@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;
|
||
final outline = 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 runningHighlightStrokes(
|
||
outline: outline,
|
||
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;
|
||
}
|