Merge commit '7f31e16bc1f9fe8cd6d0ac0fbe0d509756880aff' into pipelines/run-20261112-1235-gear-icon-for-background-tasks-1cb4
This commit is contained in:
@@ -9,22 +9,22 @@ const _primary = Color(0xFF3366FF);
|
||||
const _tertiary = Color(0xFFFF6633);
|
||||
|
||||
/// The pill outline as the painter builds it, measured by arc length.
|
||||
List<PathMetric> _outline(Size size, {double radius = 24}) {
|
||||
final path = RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(radius),
|
||||
).getOuterPath(Offset.zero & size);
|
||||
return path.computeMetrics().toList();
|
||||
}
|
||||
Path _outlinePath(Size size, {double radius = 24}) => RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(radius),
|
||||
).getOuterPath(Offset.zero & size);
|
||||
|
||||
List<PathMetric> _outline(Size size, {double radius = 24}) =>
|
||||
_outlinePath(size, radius: radius).computeMetrics().toList();
|
||||
|
||||
double _length(List<PathMetric> metrics) =>
|
||||
metrics.fold<double>(0, (sum, m) => sum + m.length);
|
||||
|
||||
/// The arc the visible band covers: from its first piece to its last. The
|
||||
/// pieces overlap by a seam each, so their own lengths would overcount.
|
||||
/// The arc the visible highlight covers: from its first piece to its last.
|
||||
/// The pieces overlap by a seam each, so their own lengths would overcount.
|
||||
double _visibleSpan(List<RunningArcSegment> segments) =>
|
||||
segments.last.end - segments.first.start;
|
||||
|
||||
/// Colours that differ only by float rounding at a band edge still match.
|
||||
/// Colours that differ only by float rounding at a stop still match.
|
||||
void _expectSameColor(Color actual, Color expected, {required String reason}) {
|
||||
expect(actual.a, closeTo(expected.a, 1e-6), reason: reason);
|
||||
expect(actual.r, closeTo(expected.r, 1e-6), reason: reason);
|
||||
@@ -32,6 +32,14 @@ void _expectSameColor(Color actual, Color expected, {required String reason}) {
|
||||
expect(actual.b, closeTo(expected.b, 1e-6), reason: reason);
|
||||
}
|
||||
|
||||
/// The pieces cover [span] of the outline. Each overlaps its neighbour by a
|
||||
/// seam (a half pixel on each side), so their summed lengths may exceed it by
|
||||
/// about a pixel per piece; path metrics add about a pixel of approximation.
|
||||
void _expectCovered(double covered, int pieces, double span) {
|
||||
expect(covered, greaterThanOrEqualTo(span - 1));
|
||||
expect(covered, lessThanOrEqualTo(span + pieces + 1));
|
||||
}
|
||||
|
||||
void main() {
|
||||
const size = Size(320, 48);
|
||||
final metrics = _outline(size);
|
||||
@@ -86,43 +94,34 @@ void main() {
|
||||
}
|
||||
});
|
||||
|
||||
test('colours wrap: a fraction and its +1 are the same colour', () {
|
||||
for (var f = 0.0; f < 1; f += .02) {
|
||||
test(
|
||||
'the colour fades in, turns tertiary and fades out along its length',
|
||||
() {
|
||||
expect(runningHighlightColorAt(0, _primary, _tertiary).a, 0);
|
||||
expect(runningHighlightColorAt(1, _primary, _tertiary).a, 0);
|
||||
_expectSameColor(
|
||||
runningHighlightColorAt(f + 1, _primary, _tertiary),
|
||||
runningHighlightColorAt(f, _primary, _tertiary),
|
||||
reason: 'f=$f',
|
||||
runningHighlightColorAt(.3, _primary, _tertiary),
|
||||
_primary,
|
||||
reason: 'primary stop',
|
||||
);
|
||||
}
|
||||
expect(runningHighlightColorAt(0, _primary, _tertiary).a, 0);
|
||||
_expectSameColor(
|
||||
runningHighlightColorAt(.6, _primary, _tertiary),
|
||||
_tertiary,
|
||||
reason: 'tertiary stop',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('colour is transparent outside the highlight', () {
|
||||
expect(runningHighlightColorAt(-.1, _primary, _tertiary).a, 0);
|
||||
expect(runningHighlightColorAt(1.1, _primary, _tertiary).a, 0);
|
||||
});
|
||||
|
||||
test('the highlight travels by arc: a shift of one step shifts colours', () {
|
||||
// The colour at a given arc point, one step later, equals the colour one
|
||||
// step earlier at a point shifted back by the arc that step covers.
|
||||
const dt = .1;
|
||||
final arc = dt * length;
|
||||
for (var x = 0.0; x < length; x += length / 40) {
|
||||
final before = runningHighlightColorAt(
|
||||
(x - runningHighlightOrigin(0, length)) / length,
|
||||
_primary,
|
||||
_tertiary,
|
||||
);
|
||||
final after = runningHighlightColorAt(
|
||||
(x + arc - runningHighlightOrigin(dt, length)) / length,
|
||||
_primary,
|
||||
_tertiary,
|
||||
);
|
||||
_expectSameColor(after, before, reason: 'x=$x');
|
||||
}
|
||||
});
|
||||
|
||||
test('the visible band covers the same arc on a long and a square shape', () {
|
||||
// The band is 38% of a lap. Measured along the outline it has the same
|
||||
// length whatever the aspect, which an angle-based sweep would not give.
|
||||
final long = _outline(const Size(320, 48));
|
||||
final square = _outline(const Size(120, 120), radius: 12);
|
||||
for (final shape in [long, square]) {
|
||||
test('the highlight is a short segment, never a trail round the edge', () {
|
||||
for (final shape in [
|
||||
_outline(const Size(320, 48)),
|
||||
_outline(const Size(120, 120), radius: 12),
|
||||
]) {
|
||||
final len = _length(shape);
|
||||
final segments = runningHighlightSegments(
|
||||
progress: .3,
|
||||
@@ -130,11 +129,12 @@ void main() {
|
||||
primary: _primary,
|
||||
tertiary: _tertiary,
|
||||
);
|
||||
expect(_visibleSpan(segments), closeTo(.38 * len, 2 * 3 + 1));
|
||||
expect(_visibleSpan(segments), closeTo(runningHighlightSpan * len, 7));
|
||||
expect(runningHighlightSpan, lessThanOrEqualTo(.15));
|
||||
}
|
||||
});
|
||||
|
||||
test('the visible segments tile the outline without gaps', () {
|
||||
test('the visible segments tile the highlight without gaps', () {
|
||||
final segments = runningHighlightSegments(
|
||||
progress: .6,
|
||||
length: length,
|
||||
@@ -150,6 +150,58 @@ void main() {
|
||||
expect(segments.last.end, lessThanOrEqualTo(length));
|
||||
});
|
||||
|
||||
test('a highlight across the outline start is split into two pieces', () {
|
||||
// At progress .99 the segment begins near the end and wraps to the start.
|
||||
final segments = runningHighlightSegments(
|
||||
progress: .99,
|
||||
length: length,
|
||||
primary: _primary,
|
||||
tertiary: _tertiary,
|
||||
);
|
||||
expect(segments.any((s) => s.start == 0), isTrue);
|
||||
expect(segments.any((s) => s.end == length), isTrue);
|
||||
_expectCovered(
|
||||
segments.fold<double>(0, (s, p) => s + p.end - p.start),
|
||||
segments.length,
|
||||
runningHighlightSpan * length,
|
||||
);
|
||||
});
|
||||
|
||||
test('only the travelling segment is painted, not the whole outline', () {
|
||||
for (var k = 0; k < 8; k++) {
|
||||
final strokes = runningHighlightStrokes(
|
||||
outline: _outlinePath(size),
|
||||
progress: k / 8,
|
||||
primary: _primary,
|
||||
tertiary: _tertiary,
|
||||
);
|
||||
final painted = strokes.fold<double>(
|
||||
0,
|
||||
(sum, s) => sum + _length(s.path.computeMetrics().toList()),
|
||||
);
|
||||
expect(strokes, isNotEmpty, reason: 'lap $k');
|
||||
_expectCovered(painted, strokes.length, runningHighlightSpan * length);
|
||||
expect(painted, lessThan(length * .2), reason: 'lap $k');
|
||||
}
|
||||
});
|
||||
|
||||
test('strokes are plain colour strokes inside the outline bounds', () {
|
||||
final strokes = runningHighlightStrokes(
|
||||
outline: _outlinePath(size),
|
||||
progress: .4,
|
||||
primary: _primary,
|
||||
tertiary: _tertiary,
|
||||
);
|
||||
for (final stroke in strokes) {
|
||||
expect(stroke.color.a, inInclusiveRange(0, 1));
|
||||
final bounds = stroke.path.getBounds();
|
||||
expect(bounds.left, greaterThanOrEqualTo(-1));
|
||||
expect(bounds.top, greaterThanOrEqualTo(-1));
|
||||
expect(bounds.right, lessThanOrEqualTo(size.width + 1));
|
||||
expect(bounds.bottom, lessThanOrEqualTo(size.height + 1));
|
||||
}
|
||||
});
|
||||
|
||||
test('runningArcPath draws the requested arc of the outline', () {
|
||||
final piece = runningArcPath(metrics, 10, 30);
|
||||
final pieceMetrics = piece.computeMetrics().toList();
|
||||
|
||||
Reference in New Issue
Block a user