diff --git a/lib/widgets/material_player_layout.dart b/lib/widgets/material_player_layout.dart index ce7f4be04..bd6c774b7 100644 --- a/lib/widgets/material_player_layout.dart +++ b/lib/widgets/material_player_layout.dart @@ -114,17 +114,21 @@ class MaterialPlayerLayout extends StatelessWidget { child: SizedBox( width: artSize, height: artSize, - child: _CoverSwitcher( - child: PlayerArtwork( - key: ValueKey( - mediaItem.artUri?.toString() ?? mediaItem.id, - ), - artUri: mediaItem.artUri?.toString(), - colorScheme: colorScheme, - cacheWidth: - (artSize * - MediaQuery.devicePixelRatioOf(context)) - .round(), + // A track change keeps the old cover until the new + // one has a frame. It then fades in on the effects + // spring and grows from 0.94 on the slow spatial + // spring, which does not overshoot because the app + // started this motion. + child: PlayerArtwork( + artUri: mediaItem.artUri?.toString(), + colorScheme: colorScheme, + cacheWidth: + (artSize * MediaQuery.devicePixelRatioOf(context)) + .round(), + crossfade: playerTrackChangeMotion(context), + crossfadeScale: AppMotion.spatialSlow( + context, + playerTrackChangeMotion(context), ), ), ), @@ -393,39 +397,3 @@ class MaterialPlayerLayout extends StatelessWidget { ]; } } - -/// The cover's track change: the new cover fades in on the effects spring -/// and grows from 0.94 on the slow spatial spring, which does not overshoot -/// because the app started this motion. The previous cover stays opaque -/// underneath until the new one covers it. -class _CoverSwitcher extends StatelessWidget { - const _CoverSwitcher({required this.child}); - - final Widget child; - - @override - Widget build(BuildContext context) { - final fade = playerTrackChangeMotion(context); - final scale = AppMotion.spatialSlow(context, fade); - final total = fade.duration > scale.duration - ? fade.duration - : scale.duration; - double share(Duration part) => - total == Duration.zero ? 1 : part.inMicroseconds / total.inMicroseconds; - final fadeCurve = Interval(0, share(fade.duration), curve: fade.curve); - final scaleCurve = Interval(0, share(scale.duration), curve: scale.curve); - return AnimatedSwitcher( - duration: total, - transitionBuilder: (child, animation) => FadeTransition( - opacity: animation.drive(CurveTween(curve: fadeCurve)), - child: ScaleTransition( - scale: animation.drive( - Tween(begin: 0.94, end: 1.0).chain(CurveTween(curve: scaleCurve)), - ), - child: child, - ), - ), - child: child, - ); - } -} diff --git a/lib/widgets/player_artwork.dart b/lib/widgets/player_artwork.dart index 5b7d7da11..3bea6836e 100644 --- a/lib/widgets/player_artwork.dart +++ b/lib/widgets/player_artwork.dart @@ -13,6 +13,8 @@ class PlayerArtwork extends StatelessWidget { /// Shared decode for the mini player and the full player's blurred cover. static const miniCacheWidth = 132; + static const _grownFrom = 0.94; + /// An HTTP(S) URL, file URI, absolute local artwork path, or `network://` /// cover reference. final String? artUri; @@ -25,6 +27,10 @@ class PlayerArtwork extends StatelessWidget { /// Null, a zero duration or reduced motion swap the cover at once. final AppMotionSpec? crossfade; + /// With [crossfade], the new cover also grows from 0.94 on this motion + /// while it fades in. + final AppMotionSpec? crossfadeScale; + const PlayerArtwork({ super.key, required this.artUri, @@ -32,6 +38,7 @@ class PlayerArtwork extends StatelessWidget { this.cacheWidth, this.iconSize = 40, this.crossfade, + this.crossfadeScale, }); /// Share one full-size decode while artwork moves between player panels. @@ -55,7 +62,12 @@ class PlayerArtwork extends StatelessWidget { if (motion != null && motion.duration > Duration.zero && !MediaQuery.disableAnimationsOf(context)) { - return _ArtworkCrossfade(artUri: artUri, motion: motion, builder: _layer); + return _ArtworkCrossfade( + artUri: artUri, + motion: motion, + scale: crossfadeScale, + builder: _layer, + ); } final placeholder = _placeholder(); @@ -130,11 +142,13 @@ class _ArtworkCrossfade extends StatefulWidget { const _ArtworkCrossfade({ required this.artUri, required this.motion, + required this.scale, required this.builder, }); final String? artUri; final AppMotionSpec motion; + final AppMotionSpec? scale; final Widget Function(BuildContext, String?, VoidCallback onShown) builder; @override @@ -146,11 +160,23 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade> late _ArtworkLayer _current = _ArtworkLayer(widget.artUri, _opacity(1)); _ArtworkLayer? _previous; - AnimationController _opacity(double value) => AnimationController( - vsync: this, - value: value, - duration: widget.motion.duration, - ); + /// One linear timeline per cover: the fade and the growth each run their + /// own curve over their own share of it. + Duration get _total { + final scale = widget.scale?.duration ?? Duration.zero; + return scale > widget.motion.duration ? scale : widget.motion.duration; + } + + Animation _part(_ArtworkLayer layer, AppMotionSpec motion) { + final total = _total.inMicroseconds; + final share = total == 0 ? 1.0 : motion.duration.inMicroseconds / total; + return layer.opacity.drive( + CurveTween(curve: Interval(0, share, curve: motion.curve)), + ); + } + + AnimationController _opacity(double value) => + AnimationController(vsync: this, value: value, duration: _total); @override void didUpdateWidget(_ArtworkCrossfade oldWidget) { @@ -193,7 +219,7 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade> // Runs during build: start the fade once this frame is done. SchedulerBinding.instance.addPostFrameCallback((_) { if (!mounted || !identical(layer, _current)) return; - final fade = layer.opacity.animateTo(1, curve: widget.motion.curve); + final fade = layer.opacity.animateTo(1); fade.whenCompleteOrCancel(() { if (!mounted || !identical(layer, _current) || _previous == null) { return; @@ -214,13 +240,22 @@ class _ArtworkCrossfadeState extends State<_ArtworkCrossfade> super.dispose(); } - Widget _build(BuildContext context, _ArtworkLayer layer) => KeyedSubtree( - key: layer.key, - child: FadeTransition( - opacity: layer.opacity, - child: widget.builder(context, layer.uri, () => _show(layer)), - ), - ); + Widget _build(BuildContext context, _ArtworkLayer layer) { + Widget child = widget.builder(context, layer.uri, () => _show(layer)); + if (widget.scale case final scale?) { + child = ScaleTransition( + scale: _part( + layer, + scale, + ).drive(Tween(begin: PlayerArtwork._grownFrom, end: 1.0)), + child: child, + ); + } + return KeyedSubtree( + key: layer.key, + child: FadeTransition(opacity: _part(layer, widget.motion), child: child), + ); + } @override Widget build(BuildContext context) { diff --git a/test/now_playing_lyrics_transition_test.dart b/test/now_playing_lyrics_transition_test.dart index d4b94d1ca..d749a7fc5 100644 --- a/test/now_playing_lyrics_transition_test.dart +++ b/test/now_playing_lyrics_transition_test.dart @@ -2940,7 +2940,11 @@ void main() { matching: find.byType(PlayerArtwork), ); expect(covers, findsOneWidget); - expect(tester.widget(covers).key, const ValueKey('second')); + // Reduced motion swaps the cover in place, with no fading layers. + expect( + find.descendant(of: covers, matching: find.byType(FadeTransition)), + findsNothing, + ); expect(tester.takeException(), isNull); }); diff --git a/test/player_artwork_test.dart b/test/player_artwork_test.dart index efe636593..ccec887e0 100644 --- a/test/player_artwork_test.dart +++ b/test/player_artwork_test.dart @@ -178,6 +178,89 @@ void main() { }); } + testWidgets('a growing crossfade waits for the frame, then fades and grows', ( + tester, + ) async { + final cache = PaintingBinding.instance.imageCache; + addTearDown(() { + cache.clear(); + cache.clearLiveImages(); + }); + final image = await tester.runAsync(() async { + final recorder = ui.PictureRecorder(); + Canvas(recorder).drawColor(Colors.green, BlendMode.src); + final picture = recorder.endRecording(); + final result = await picture.toImage(1, 1); + picture.dispose(); + return result; + }); + final pending = Completer(); + cache.putIfAbsent( + FileImage(File('/grow-first.png')), + () => OneFrameImageStreamCompleter( + Future.value(ImageInfo(image: image!.clone())), + ), + ); + cache.putIfAbsent( + FileImage(File('/grow-second.png')), + () => OneFrameImageStreamCompleter(pending.future), + ); + Widget app(String path) => MaterialApp( + home: PlayerArtwork( + artUri: path, + colorScheme: const ColorScheme.dark(), + crossfade: const AppMotionSpec( + Duration(milliseconds: 100), + Curves.linear, + ), + crossfadeScale: const AppMotionSpec( + Duration(milliseconds: 400), + Curves.linear, + ), + ), + ); + ({double opacity, double scale}) top() { + final layer = find.byType(KeyedSubtree).last; + return ( + opacity: tester + .widget( + find.descendant(of: layer, matching: find.byType(FadeTransition)), + ) + .opacity + .value, + scale: tester + .widget( + find + .descendant(of: layer, matching: find.byType(ScaleTransition)) + .first, + ) + .scale + .value, + ); + } + + await tester.pumpWidget(app('/grow-first.png')); + await tester.pump(); + expect(top(), (opacity: 1.0, scale: 1.0)); + await tester.pumpWidget(app('/grow-second.png')); + await tester.pump(const Duration(milliseconds: 300)); + // No frame yet: the new cover waits, hidden and small. + expect(top(), (opacity: 0.0, scale: 0.94)); + + pending.complete(ImageInfo(image: image!)); + for (var frame = 0; frame < 10; frame++) { + await tester.pump(const Duration(milliseconds: 16)); + } + // The fade is done well before the growth. + expect(top().opacity, 1); + expect(top().scale, inExclusiveRange(0.94, 1)); + await tester.pump(const Duration(milliseconds: 400)); + expect(top(), (opacity: 1.0, scale: 1.0)); + expect(find.byType(RawImage), findsOneWidget); + expect(tester.takeException(), isNull); + await tester.pumpWidget(const SizedBox()); + }); + testWidgets('covers with no file show the placeholder without throwing', ( tester, ) async {