diff --git a/lib/screens/now_playing_screen.dart b/lib/screens/now_playing_screen.dart index 94a131e0f..196c1f4e4 100644 --- a/lib/screens/now_playing_screen.dart +++ b/lib/screens/now_playing_screen.dart @@ -356,9 +356,12 @@ class _NowPlayingScreenState extends ConsumerState { child: mornye ? _buildPlayer(context, mediaItem, fullScreenCover: fullScreenCover) // Material takes the track's artwork scheme. The providers it needs - // are read above, so the scoped builder reads none. + // are read above, so the scoped builder reads none. A skip moves + // only the page background, so the whole player is not rebuilt + // on every frame of the colour change. : PlayerArtworkTheme( artUri: mediaItem?.artUri?.toString(), + fadesSurfaceOnly: true, child: Builder( builder: (context) => _buildPlayer( context, @@ -549,18 +552,16 @@ class _NowPlayingScreenState extends ConsumerState { !widget.embedded && MediaQuery.orientationOf(context) == Orientation.landscape && ref.watch(settingsProvider.select((s) => s.landscapeLyricsArtwork)); + // The Material page background is painted below the scaffold, where it + // moves to the next track's colour on its own. final player = Scaffold( extendBodyBehindAppBar: materialLandscapeArtwork, - backgroundColor: mornye || materialBackdrop - ? Colors.transparent - : colorScheme.surface, + backgroundColor: Colors.transparent, appBar: _FadingAppBar( opacity: contentOpacity, hidden: !mornye && _lyricsImmersive, child: AppBar( - backgroundColor: mornye || materialBackdrop - ? Colors.transparent - : colorScheme.surface, + backgroundColor: Colors.transparent, foregroundColor: overCover ? Colors.white : colorScheme.onSurface, surfaceTintColor: Colors.transparent, systemOverlayStyle: mornye @@ -870,7 +871,11 @@ class _NowPlayingScreenState extends ConsumerState { ), ); if (!mornye) { - if (!materialBackdrop) return _lyricsPointerListener(player); + if (!materialBackdrop) { + return _lyricsPointerListener( + PlayerArtworkSurface(surface: colorScheme.surface, child: player), + ); + } return Stack( fit: StackFit.expand, children: [ diff --git a/lib/theme/player_artwork_theme.dart b/lib/theme/player_artwork_theme.dart index 97e6ec511..affb5e3f2 100644 --- a/lib/theme/player_artwork_theme.dart +++ b/lib/theme/player_artwork_theme.dart @@ -25,11 +25,16 @@ class PlayerArtworkPalette { /// scheme moves at once on the effects spring, or swaps under reduced /// motion. Tracks without readable artwork use the app scheme. With AMOLED /// on, the page background stays pure black. +/// +/// A large page sets [fadesSurfaceOnly]: moving the whole scheme rebuilds +/// everything under it on every frame, so its scheme swaps in one step and +/// only the page background, painted by [PlayerArtworkSurface], moves. class PlayerArtworkTheme extends StatefulWidget { const PlayerArtworkTheme({ super.key, required this.artUri, required this.child, + this.fadesSurfaceOnly = false, }); /// The track's artwork, in any form `PlayerArtwork` shows: an HTTP(S) @@ -38,6 +43,16 @@ class PlayerArtworkTheme extends StatefulWidget { final Widget child; + /// Swaps the scheme in one step and moves only the page background to + /// the next track's surface colour. The page paints that background with + /// [PlayerArtworkSurface] instead of the scheme's surface. + final bool fadesSurfaceOnly; + + /// The moving page background of the nearest [PlayerArtworkTheme] with + /// [fadesSurfaceOnly]. Null elsewhere, where the whole scheme moves. + static Animation? surfaceOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType<_ArtworkSurface>()?.surface; + static PlayerArtworkPalette _palette = const PlayerArtworkPalette(); /// Replaces [CoverPalette] in tests, whose fake clock cannot finish a real @@ -57,6 +72,9 @@ class _PlayerArtworkThemeState extends State value: 1, ); + /// The page background for [PlayerArtworkTheme.fadesSurfaceOnly]. + late final _surface = _SurfaceAnimation(_controller); + /// The current cover's scheme. Null follows the app scheme. ColorScheme? _artwork; @@ -138,6 +156,10 @@ class _PlayerArtworkThemeState extends State _controller.value = 1; return; } + // From the colour on screen, which may be partway through a move. + _surface + ..from = _surface.value + ..curve = motion.curve; _curve = motion.curve; _controller ..duration = motion.duration @@ -161,9 +183,10 @@ class _PlayerArtworkThemeState extends State : artwork; final from = _from; final progress = _controller.value; - final scheme = from == null || progress >= 1 + final scheme = widget.fadesSurfaceOnly || from == null || progress >= 1 ? target : ColorScheme.lerp(from, target, _curve.transform(progress)); + _surface.to = target.surface; _shown = scheme; if (!identical(base, _base) || scheme != _scheme) { _base = base; @@ -208,6 +231,14 @@ class _PlayerArtworkThemeState extends State @override Widget build(BuildContext context) { final base = Theme.of(context); + if (widget.fadesSurfaceOnly) { + final theme = _themeFor(base); + return _ArtworkSurface( + surface: _surface, + target: _surface.to, + child: Theme(data: theme, child: widget.child), + ); + } return AnimatedBuilder( animation: _controller, builder: (context, child) => Theme(data: _themeFor(base), child: child!), @@ -215,3 +246,79 @@ class _PlayerArtworkThemeState extends State ); } } + +/// The page background moving from [from] to [to] with the theme's +/// transition, at [to] while none runs. +class _SurfaceAnimation extends Animation + with AnimationWithParentMixin { + _SurfaceAnimation(this.parent); + + @override + final Animation parent; + + Color from = Colors.transparent; + Color to = Colors.transparent; + Curve curve = Curves.linear; + + @override + Color get value { + final progress = parent.value; + if (progress >= 1) return to; + return Color.lerp(from, to, curve.transform(progress))!; + } +} + +/// Hands [PlayerArtworkSurface] the moving background. The animation keeps +/// its identity, so only a new resting colour, such as a brightness change +/// without a transition, rebuilds the surfaces. +class _ArtworkSurface extends InheritedWidget { + const _ArtworkSurface({ + required this.surface, + required this.target, + required super.child, + }); + + final Animation surface; + final Color target; + + @override + bool updateShouldNotify(_ArtworkSurface oldWidget) => + target != oldWidget.target || surface != oldWidget.surface; +} + +/// Paints a Material player's page background: [surface], or the moving +/// background of a [PlayerArtworkTheme] with +/// [PlayerArtworkTheme.fadesSurfaceOnly]. Only this box repaints while that +/// background moves. +class PlayerArtworkSurface extends StatelessWidget { + const PlayerArtworkSurface({ + super.key, + required this.surface, + this.opacity = 1, + this.child, + }); + + /// The scheme's surface, used outside a surface-moving theme. + final Color surface; + + /// Opacity of the background over what lies below it. + final double opacity; + + final Widget? child; + + @override + Widget build(BuildContext context) { + final moving = PlayerArtworkTheme.surfaceOf(context); + Color tint(Color color) => + opacity == 1 ? color : color.withValues(alpha: color.a * opacity); + if (moving == null) { + return ColoredBox(color: tint(surface), child: child); + } + return AnimatedBuilder( + animation: moving, + builder: (context, child) => + ColoredBox(color: tint(moving.value), child: child), + child: child, + ); + } +} diff --git a/lib/widgets/material_player_backdrop.dart b/lib/widgets/material_player_backdrop.dart index 258059257..a199486e4 100644 --- a/lib/widgets/material_player_backdrop.dart +++ b/lib/widgets/material_player_backdrop.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:mornye/routes/now_playing_route.dart'; import 'package:mornye/theme/app_motion.dart'; +import 'package:mornye/theme/player_artwork_theme.dart'; import 'package:mornye/widgets/cached_cover_image.dart'; import 'package:mornye/widgets/mornye_player_artwork.dart'; import 'package:mornye/widgets/player_artwork.dart'; @@ -450,9 +451,9 @@ class _BlurredCoverState extends State<_BlurredCover> return Stack( fit: StackFit.expand, children: [ - ColoredBox(color: surface), + PlayerArtworkSurface(surface: surface), blurred, - ColoredBox(color: surface.withValues(alpha: 0.72)), + PlayerArtworkSurface(surface: surface, opacity: 0.72), ], ); } diff --git a/test/player_artwork_theme_test.dart b/test/player_artwork_theme_test.dart index c3a2b28bc..03d959341 100644 --- a/test/player_artwork_theme_test.dart +++ b/test/player_artwork_theme_test.dart @@ -96,20 +96,24 @@ void main() { ColorScheme shown(WidgetTester tester) => Theme.of(tester.element(find.byKey(probe))).colorScheme; - Widget host(String? artUri, ThemeData theme, {bool reduceMotion = false}) => - MaterialApp( - theme: theme, - builder: (context, child) => MediaQuery( - data: MediaQuery.of( - context, - ).copyWith(disableAnimations: reduceMotion), - child: child!, - ), - home: PlayerArtworkTheme( - artUri: artUri, - child: const SizedBox(key: probe), - ), - ); + Widget host( + String? artUri, + ThemeData theme, { + bool reduceMotion = false, + bool fadesSurfaceOnly = false, + Widget child = const SizedBox(key: probe), + }) => MaterialApp( + theme: theme, + builder: (context, child) => MediaQuery( + data: MediaQuery.of(context).copyWith(disableAnimations: reduceMotion), + child: child!, + ), + home: PlayerArtworkTheme( + artUri: artUri, + fadesSurfaceOnly: fadesSurfaceOnly, + child: child, + ), + ); testWidgets('shows a cached cover scheme from the first frame', ( tester, @@ -183,6 +187,124 @@ void main() { expect(shown(tester), green); }); + group('moving only the page background', () { + var builds = 0; + // Builds whenever the scheme it reads changes. + final page = PlayerArtworkSurface( + surface: Colors.pink, + child: Builder( + builder: (context) { + builds++; + Theme.of(context); + return const SizedBox(key: probe); + }, + ), + ); + Color background(WidgetTester tester) => tester + .widget( + find.descendant( + of: find.byType(PlayerArtworkSurface), + matching: find.byType(ColoredBox), + ), + ) + .color; + + setUp(() => builds = 0); + + testWidgets('swaps the scheme in one step while the background moves', ( + tester, + ) async { + final theme = AppTheme.light(); + palette.cached[_coverA] = red; + await tester.pumpWidget( + host(_coverA, theme, fadesSurfaceOnly: true, child: page), + ); + expect(shown(tester), red); + expect(background(tester), red.surface); + + await tester.pumpWidget( + host(_coverB, theme, fadesSurfaceOnly: true, child: page), + ); + palette.complete(_coverB, green); + await tester.pump(); + final before = builds; + await tester.pump(); + await tester.pump(const Duration(milliseconds: 40)); + expect(shown(tester), green); + final midway = background(tester); + expect(midway, isNot(red.surface)); + expect(midway, isNot(green.surface)); + for (var i = 0; i < 10; i++) { + await tester.pump(const Duration(milliseconds: 16)); + } + await tester.pumpAndSettle(); + // The page under the theme built only for the swap itself. + expect(builds - before, lessThanOrEqualTo(1)); + expect(background(tester), green.surface); + }); + + testWidgets('a skip partway through starts from the colour on screen', ( + tester, + ) async { + final theme = AppTheme.light(); + palette.cached[_coverA] = red; + palette.cached[_coverB] = green; + await tester.pumpWidget( + host(_coverA, theme, fadesSurfaceOnly: true, child: page), + ); + await tester.pumpWidget( + host(_coverB, theme, fadesSurfaceOnly: true, child: page), + ); + await tester.pump(const Duration(milliseconds: 40)); + final midway = background(tester); + expect(midway, isNot(green.surface)); + + await tester.pumpWidget( + host(_coverA, theme, fadesSurfaceOnly: true, child: page), + ); + expect(shown(tester), red); + expect(background(tester), midway); + await tester.pumpAndSettle(); + expect(background(tester), red.surface); + }); + + testWidgets('reduced motion swaps the background with the scheme', ( + tester, + ) async { + final theme = AppTheme.light(); + palette.cached[_coverA] = red; + palette.cached[_coverB] = green; + await tester.pumpWidget( + host( + _coverA, + theme, + reduceMotion: true, + fadesSurfaceOnly: true, + child: page, + ), + ); + await tester.pumpWidget( + host( + _coverB, + theme, + reduceMotion: true, + fadesSurfaceOnly: true, + child: page, + ), + ); + expect(shown(tester), green); + expect(background(tester), green.surface); + }); + + testWidgets('outside it the background is the given surface', ( + tester, + ) async { + palette.cached[_coverA] = red; + await tester.pumpWidget(host(_coverA, AppTheme.light(), child: page)); + expect(background(tester), Colors.pink); + }); + }); + testWidgets('AMOLED keeps the page background pure black', (tester) async { final amoled = _seeded( Colors.purple, @@ -314,22 +436,22 @@ void main() { ); palette.cached[_coverA] = cover; await tester.pumpWidget(app(const NowPlayingScreen(), mornye: false)); - // The card, whose scaffold shows the scheme's surface. + // The card, whose page shows the scheme's surface. ProviderScope.containerOf( tester.element(find.byType(NowPlayingScreen)), ).read(settingsProvider.notifier).setPlayerCoverStyle('default'); await tester.pump(); await tester.pump(const Duration(seconds: 1)); - final scaffold = tester.widget( + final background = tester.widget( find .descendant( - of: find.byType(NowPlayingScreen), - matching: find.byType(Scaffold), + of: find.byType(PlayerArtworkSurface), + matching: find.byType(ColoredBox), ) .first, ); - expect(scaffold.backgroundColor, cover.surface); + expect(background.color, cover.surface); expect(Theme.of(tester.element(find.text('Song'))).colorScheme, cover); });