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>
This commit is contained in:
@@ -137,17 +137,35 @@ List<RunningBorderStroke> runningHighlightStrokes({
|
|||||||
required double progress,
|
required double progress,
|
||||||
required Color primary,
|
required Color primary,
|
||||||
required Color tertiary,
|
required Color tertiary,
|
||||||
}) {
|
}) => RunningOutline(
|
||||||
final metrics = outline.computeMetrics().toList();
|
outline,
|
||||||
final length = metrics.fold<double>(0, (sum, m) => sum + m.length);
|
).strokes(progress: progress, primary: primary, tertiary: tertiary);
|
||||||
final segments = runningHighlightSegments(
|
|
||||||
|
/// An outline measured once: its contours and total length. Measuring a
|
||||||
|
/// path is the expensive part of drawing the highlight, and the outline only
|
||||||
|
/// changes with the pill's size and shape, so a painter keeps one of these
|
||||||
|
/// across animation frames and only extracts the moving pieces per frame.
|
||||||
|
class RunningOutline {
|
||||||
|
RunningOutline(Path outline)
|
||||||
|
: metrics = outline.computeMetrics().toList(growable: false) {
|
||||||
|
length = metrics.fold<double>(0, (sum, m) => sum + m.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<PathMetric> metrics;
|
||||||
|
late final double length;
|
||||||
|
|
||||||
|
/// The strokes of the highlight at [progress] along this outline.
|
||||||
|
List<RunningBorderStroke> strokes({
|
||||||
|
required double progress,
|
||||||
|
required Color primary,
|
||||||
|
required Color tertiary,
|
||||||
|
}) => [
|
||||||
|
for (final segment in runningHighlightSegments(
|
||||||
progress: progress,
|
progress: progress,
|
||||||
length: length,
|
length: length,
|
||||||
primary: primary,
|
primary: primary,
|
||||||
tertiary: tertiary,
|
tertiary: tertiary,
|
||||||
);
|
))
|
||||||
return [
|
|
||||||
for (final segment in segments)
|
|
||||||
RunningBorderStroke(
|
RunningBorderStroke(
|
||||||
path: runningArcPath(metrics, segment.start, segment.end),
|
path: runningArcPath(metrics, segment.start, segment.end),
|
||||||
color: segment.color,
|
color: segment.color,
|
||||||
|
|||||||
@@ -112,19 +112,28 @@ class _RunningBorderPainter extends CustomPainter {
|
|||||||
/// The lap's progress, or null for the static reduced-motion border.
|
/// The lap's progress, or null for the static reduced-motion border.
|
||||||
final Animation<double>? lap;
|
final Animation<double>? lap;
|
||||||
|
|
||||||
|
Size? _outlineSize;
|
||||||
|
RunningOutline? _outline;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void paint(Canvas canvas, Size size) {
|
void paint(Canvas canvas, Size size) {
|
||||||
final progress = lap?.value;
|
final progress = lap?.value;
|
||||||
// Reduced motion: no highlight; the normal static border shows alone.
|
// Reduced motion: no highlight; the normal static border shows alone.
|
||||||
if (progress == null || size.isEmpty) return;
|
if (progress == null || size.isEmpty) return;
|
||||||
final outline = shape.getOuterPath((Offset.zero & size).deflate(width / 2));
|
// 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()
|
final line = Paint()
|
||||||
..style = PaintingStyle.stroke
|
..style = PaintingStyle.stroke
|
||||||
..strokeWidth = width;
|
..strokeWidth = width;
|
||||||
// The highlight is laid along the outline by arc length, so its speed is
|
// 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.
|
// even around corners and straight edges alike. Only its pieces are drawn.
|
||||||
for (final stroke in runningHighlightStrokes(
|
for (final stroke in _outline!.strokes(
|
||||||
outline: outline,
|
|
||||||
progress: progress,
|
progress: progress,
|
||||||
primary: primary,
|
primary: primary,
|
||||||
tertiary: tertiary,
|
tertiary: tertiary,
|
||||||
|
|||||||
@@ -208,4 +208,33 @@ void main() {
|
|||||||
// Curve metrics are approximations; a fraction of a pixel is exact enough.
|
// Curve metrics are approximations; a fraction of a pixel is exact enough.
|
||||||
expect(_length(pieceMetrics), closeTo(20, .5));
|
expect(_length(pieceMetrics), closeTo(20, .5));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a measured outline draws the same strokes as measuring each time', () {
|
||||||
|
final outline = Path()
|
||||||
|
..addRRect(
|
||||||
|
RRect.fromRectAndRadius(
|
||||||
|
const Rect.fromLTWH(0, 0, 300, 56),
|
||||||
|
const Radius.circular(28),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final measured = RunningOutline(outline);
|
||||||
|
for (final progress in [0.0, 0.3, 0.97]) {
|
||||||
|
final once = runningHighlightStrokes(
|
||||||
|
outline: outline,
|
||||||
|
progress: progress,
|
||||||
|
primary: const Color(0xFF0000FF),
|
||||||
|
tertiary: const Color(0xFFFF0000),
|
||||||
|
);
|
||||||
|
final kept = measured.strokes(
|
||||||
|
progress: progress,
|
||||||
|
primary: const Color(0xFF0000FF),
|
||||||
|
tertiary: const Color(0xFFFF0000),
|
||||||
|
);
|
||||||
|
expect(kept.length, once.length);
|
||||||
|
for (var i = 0; i < kept.length; i++) {
|
||||||
|
expect(kept[i].color, once[i].color);
|
||||||
|
expect(kept[i].path.getBounds(), once[i].path.getBounds());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user