import 'dart:math' as math; import 'dart:ui' show PathMetric; import 'package:flutter/material.dart'; /// Arc-length geometry for the composer's running highlight. /// /// 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 highlight is the only thing drawn: a /// short segment of the outline, with no glow, trail or tint of the rest. /// The fraction of the outline one highlight covers. It is a short segment /// that travels, not a trail round the whole edge. const double runningHighlightSpan = .12; /// Colour fractions along the highlight's own length, tail (0) to head (1). const List runningHighlightStops = [0, .3, .6, 1]; /// Half a device pixel of overlap so neighbouring pieces 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; } /// One stroke of the highlight: a piece of the outline and its colour. class RunningBorderStroke { const RunningBorderStroke({required this.path, required this.color}); final Path path; final Color color; } /// The arc length of the highlight's tail along an outline of [length]. double runningHighlightOrigin(double progress, double length) { final lap = progress - progress.floorToDouble(); return lap * length; } /// The highlight colour at [fraction] along its own length (0 is the tail, /// 1 the head). Outside [0, 1] it is transparent. Color runningHighlightColorAt(double fraction, Color primary, Color tertiary) { if (fraction < 0 || fraction > 1) return primary.withValues(alpha: 0); final colors = [ primary.withValues(alpha: 0), primary, tertiary, tertiary.withValues(alpha: 0), ]; for (var i = 1; i < runningHighlightStops.length; i++) { final stop = runningHighlightStops[i]; if (fraction <= stop) { final from = runningHighlightStops[i - 1]; final t = (fraction - from) / (stop - from); return Color.lerp(colors[i - 1], colors[i], t)!; } } return colors.last; } /// Splits the highlight into pieces about [step] long along an outline of /// [length], starting at the lap's origin and wrapping past the outline's /// start. Each piece takes the colour of its middle along the highlight. List runningHighlightSegments({ required double progress, required double length, required Color primary, required Color tertiary, double step = 3, }) { if (length <= 0) return const []; final span = length * runningHighlightSpan; final count = math.max(1, (span / step).ceil()); final size = span / count; final origin = runningHighlightOrigin(progress, length); final segments = []; for (var i = 0; i < count; i++) { final color = runningHighlightColorAt( (i + .5) * size / span, primary, tertiary, ); if (color.a == 0) continue; _addWrapped( segments, from: origin + i * size - _seamOverlap, to: origin + (i + 1) * size + _seamOverlap, length: length, color: color, ); } return segments; } /// Adds the arc from [from] to [to] (which may lie outside the outline) as /// one or, across the outline's start, two pieces. void _addWrapped( List out, { required double from, required double to, required double length, required Color color, }) { final start = from - (from / length).floorToDouble() * length; final end = start + math.min(to - from, length); if (end <= length) { out.add(RunningArcSegment(start: start, end: end, color: color)); return; } out ..add(RunningArcSegment(start: start, end: length, color: color)) ..add(RunningArcSegment(start: 0, end: end - length, color: color)); } /// The strokes that paint the highlight over [outline] at [progress]: one /// per visible piece, each a plain stroke with no blur or glow. Empty for an /// empty outline. List runningHighlightStrokes({ required Path outline, required double progress, required Color primary, required Color tertiary, }) { final metrics = outline.computeMetrics().toList(); final length = metrics.fold(0, (sum, m) => sum + m.length); final segments = runningHighlightSegments( progress: progress, length: length, primary: primary, tertiary: tertiary, ); return [ for (final segment in segments) RunningBorderStroke( path: runningArcPath(metrics, segment.start, segment.end), color: segment.color, ), ]; } /// 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 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; }