Animate composer border at constant arc speed

Lay the running highlight out by rounded-border arc length so it advances at a steady linear speed without a loop jump, while preserving the existing colours and reduced-motion border. Add focused geometry tests and implementation notes.
This commit is contained in:
2026-11-12 10:54:42 +00:00
parent 82418d31a7
commit d9ec43e607
4 changed files with 343 additions and 21 deletions
@@ -0,0 +1,113 @@
import 'dart:math' as math;
import 'dart:ui' show PathMetric;
import 'package:flutter/material.dart';
/// Arc-length geometry for the composer's running border.
///
/// The highlight is laid out by distance travelled along the outline, not by
/// angle around the centre, so equal time steps move it equal lengths around
/// a rounded rectangle: no racing along the long edges, no crawling at ends.
///
/// One lap is `progress` in [0, 1]; progress 1 is the same place as 0, so the
/// loop restarts without a jump.
/// The colour bands, as fractions of a lap measured forward from the origin.
/// They match the original sweep's stops and colours.
const List<double> runningHighlightStops = [0, .12, .24, .38, 1];
/// Half a device pixel of overlap so neighbouring segments leave no seam.
const double _seamOverlap = .5;
/// One piece of the outline and the highlight colour drawn over it.
class RunningArcSegment {
const RunningArcSegment({
required this.start,
required this.end,
required this.color,
});
/// Arc length from the outline's start where the piece begins.
final double start;
/// Arc length where the piece ends (exclusive, may overlap by a seam).
final double end;
final Color color;
}
/// The arc length of the highlight's origin along an outline of [length].
double runningHighlightOrigin(double progress, double length) {
final lap = progress - progress.floorToDouble();
return lap * length;
}
/// The highlight colour [fraction] of a lap ahead of its origin. Fractions
/// wrap, so `f` and `f + 1` are the same colour.
Color runningHighlightColorAt(double fraction, Color primary, Color tertiary) {
final colors = [
primary.withValues(alpha: 0),
primary,
tertiary,
tertiary.withValues(alpha: 0),
primary.withValues(alpha: 0),
];
final f = fraction - fraction.floorToDouble();
for (var i = 1; i < runningHighlightStops.length; i++) {
final stop = runningHighlightStops[i];
if (f <= stop) {
final from = runningHighlightStops[i - 1];
final t = (f - from) / (stop - from);
return Color.lerp(colors[i - 1], colors[i], t)!;
}
}
return colors.last;
}
/// Splits an outline of [length] into pieces about [step] long and colours
/// each by where it sits in the highlight at [progress]. Pieces with no
/// visible colour are left out.
List<RunningArcSegment> runningHighlightSegments({
required double progress,
required double length,
required Color primary,
required Color tertiary,
double step = 3,
}) {
if (length <= 0) return const [];
final count = math.max(1, (length / step).ceil());
final size = length / count;
final origin = runningHighlightOrigin(progress, length);
final segments = <RunningArcSegment>[];
for (var i = 0; i < count; i++) {
final mid = (i + .5) * size;
final color = runningHighlightColorAt(
(mid - origin) / length,
primary,
tertiary,
);
if (color.a == 0) continue;
segments.add(
RunningArcSegment(
start: math.max(0, i * size - _seamOverlap),
end: math.min(length, (i + 1) * size + _seamOverlap),
color: color,
),
);
}
return segments;
}
/// The part of an outline between arc lengths [start] and [end]. The outline
/// may be made of several contours, so the range is taken across all of them.
Path runningArcPath(List<PathMetric> metrics, double start, double end) {
final out = Path();
var offset = 0.0;
for (final metric in metrics) {
final from = math.max(start, offset) - offset;
final to = math.min(end, offset + metric.length) - offset;
if (to > from) out.addPath(metric.extractPath(from, to), Offset.zero);
offset += metric.length;
}
return out;
}
@@ -1,7 +1,7 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'composer_running_arc.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
@@ -22,8 +22,8 @@ class ComposerRunningBorder extends StatefulWidget {
final Color primary;
final Color tertiary;
/// One lap of the highlight around the border.
static const Duration period = Duration(seconds: 4);
/// One lap of the highlight around the border (about 2.5–3 s).
static const Duration period = Duration(milliseconds: 2750);
/// The stroke drawn just inside the pill's edge.
static const double strokeWidth = 2;
@@ -117,24 +117,29 @@ class _RunningBorderPainter extends CustomPainter {
);
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);
return;
}
// The highlight is laid along the outline by arc length, one piece at a
// time, so its speed is even around corners and straight edges alike.
final metrics = path.computeMetrics().toList();
final length = metrics.fold<double>(0, (sum, m) => sum + m.length);
final segments = runningHighlightSegments(
progress: progress,
length: length,
primary: primary,
tertiary: tertiary,
);
for (final segment in segments) {
final piece = runningArcPath(metrics, segment.start, segment.end);
glow.color = segment.color;
line.color = segment.color;
canvas
..drawPath(piece, glow)
..drawPath(piece, line);
}
canvas
..drawPath(path, glow)
..drawPath(path, line);
}
@override