Skip to content

Commit dcb10a5

Browse files
committed
Dashed strokes: no endless loop, and the same dashes on every paint
A pattern without a positive entry looped forever, and the pattern's position carried over between paints, shifting the dashes on repaint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GfenNMqKhKopni53RqAKgd
1 parent 9e650ec commit dcb10a5

2 files changed

Lines changed: 74 additions & 9 deletions

File tree

‎lib/src/transformers/utils/stroke_painter.dart‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class StrokePainter extends CustomPainter {
2727
final StrokeCapEnum strokeCap;
2828

2929
/// The dash pattern of the stroke. Empty if solid.
30+
/// The most dashes painted along one path, so a pattern with tiny
31+
/// entries can't stall a frame.
32+
static const int maxDashes = 20000;
33+
3034
final List<double> dashPattern;
3135

3236
/// The side of the stroke. Whether it is all, top, bottom, left, or right.
@@ -41,10 +45,6 @@ class StrokePainter extends CustomPainter {
4145
/// The border radius of the box.
4246
final BorderRadius borderRadius;
4347

44-
/// A helper list to iterate the dashes cleanly for dashed or dotted borders.
45-
/// This is constructed from the dash pattern.
46-
final LoopingList<double> dashArray;
47-
4848
/// A convenience getter for the inside alignment of the stroke. Helps
4949
/// simplify some calculations.
5050
double get isInside => strokeAlign == StrokeAlignC.inside ? -1 : 1;
@@ -68,7 +68,7 @@ class StrokePainter extends CustomPainter {
6868
this.strokeAlign = StrokeAlignC.inside,
6969
this.strokeCap = StrokeCapEnum.square,
7070
this.borderRadius = BorderRadius.zero,
71-
}) : dashArray = LoopingList(dashPattern);
71+
});
7272

7373
@override
7474
void paint(Canvas canvas, Size size) {
@@ -127,14 +127,22 @@ class StrokePainter extends CustomPainter {
127127
// We loop over each "segment" of the path. Not up to us to know what
128128
// those segments are, we just work with them. Flutter likes being
129129
// mysterious.
130+
// A pattern that doesn't advance (no positive entry) would loop
131+
// forever; the cap keeps a tiny pattern on a long path from stalling a
132+
// frame.
133+
final double period = dashPattern.fold(0.0, (a, b) => a + max(0.0, b));
130134
for (final PathMetric metric in path.computeMetrics()) {
135+
if (period <= 0) break;
131136
double totalDistance = 0;
132137
bool draw = true;
138+
int dashes = 0;
139+
// Each path starts the pattern from its first entry.
140+
final LoopingList<double> dashArray = LoopingList(dashPattern);
133141

134142
// We need to draw a dashed line, so we iterate with a while loop
135143
// our desired dash distance.
136-
while (totalDistance < metric.length) {
137-
final double dashDistance = dashArray.nextValue();
144+
while (totalDistance < metric.length && dashes++ < maxDashes) {
145+
final double dashDistance = max(0.0, dashArray.nextValue());
138146

139147
if (draw) {
140148
// Now that we're about to draw a singular dash, we need to do it
@@ -247,8 +255,9 @@ class StrokePainter extends CustomPainter {
247255
..strokeCap = StrokeCap.butt;
248256
double distance = 0;
249257
bool draw = true;
250-
while (distance < length) {
251-
final double dash = dashes.nextValue();
258+
int count = 0;
259+
while (distance < length && count++ < maxDashes) {
260+
final double dash = max(0.0, dashes.nextValue());
252261
if (draw && dash > 0) {
253262
canvas.drawLine(
254263
start + direction * distance,

‎test/stroke_painter_dash_test.dart‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:codelessly_sdk/codelessly_sdk.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
/// Records the lines painted.
6+
class _Lines implements Canvas {
7+
final List<(Offset, Offset, double)> lines = [];
8+
9+
@override
10+
void drawLine(Offset p1, Offset p2, Paint paint) =>
11+
lines.add((p1, p2, paint.strokeWidth));
12+
13+
@override
14+
dynamic noSuchMethod(Invocation invocation) => null;
15+
}
16+
17+
List<(Offset, Offset, double)> paintRing(StrokePainter painter) {
18+
final _Lines canvas = _Lines();
19+
painter.paint(canvas, const Size(100, 100));
20+
return canvas.lines;
21+
}
22+
23+
void main() {
24+
test('a dash pattern that never advances paints nothing instead of '
25+
'looping forever', () {
26+
for (final List<double> pattern in [
27+
[0.0],
28+
[0.0, 0.0],
29+
[-2.0, 1.0],
30+
]) {
31+
final StrokePainter painter = StrokePainter(
32+
dashPattern: pattern,
33+
boxShape: BoxShape.circle,
34+
strokeWidth: 4,
35+
);
36+
expect(
37+
paintRing(painter).length,
38+
lessThanOrEqualTo(StrokePainter.maxDashes * 2),
39+
);
40+
}
41+
});
42+
43+
test('every paint starts the dash pattern from its first entry', () {
44+
// An odd number of entries per lap: a pattern carried over between
45+
// paints would shift the dashes on every repaint.
46+
final StrokePainter painter = StrokePainter(
47+
dashPattern: [3, 7, 5],
48+
boxShape: BoxShape.circle,
49+
strokeWidth: 4,
50+
);
51+
final first = paintRing(painter);
52+
final second = paintRing(painter);
53+
expect(first, isNotEmpty);
54+
expect(second, first);
55+
});
56+
}

0 commit comments

Comments
 (0)