diff --git a/lib/screens/now_playing_screen.dart b/lib/screens/now_playing_screen.dart index 196c1f4e4..a601640cd 100644 --- a/lib/screens/now_playing_screen.dart +++ b/lib/screens/now_playing_screen.dart @@ -58,6 +58,7 @@ import 'package:mornye/widgets/mornye_player_actions_sheet.dart'; import 'package:mornye/widgets/mornye_context_menu.dart'; import 'package:mornye/widgets/mornye_player_artwork.dart'; import 'package:mornye/providers/player_motion_artwork_provider.dart'; +import 'package:mornye/providers/runtime_profile_provider.dart'; import 'package:mornye/services/motion_artwork_store.dart'; export 'package:mornye/routes/now_playing_route.dart'; @@ -923,6 +924,7 @@ class _NowPlayingScreenState extends ConsumerState { squareArtwork: squareArtwork, artworkAspectRatio: artworkRatio, fadeStart: fadeStart, + coverBlur: ref.watch(mornyeBlurEnabledProvider), artwork: showsFullArtwork ? PlayerTransitionArtwork( artworkKey: _motionArtworkKey, diff --git a/lib/widgets/material_player_backdrop.dart b/lib/widgets/material_player_backdrop.dart index a199486e4..22363e5f1 100644 --- a/lib/widgets/material_player_backdrop.dart +++ b/lib/widgets/material_player_backdrop.dart @@ -51,7 +51,7 @@ class MaterialPlayerBackdrop extends StatelessWidget { @override Widget build(BuildContext context) { final size = MediaQuery.sizeOf(context); - final blurredCover = _BlurredCover( + final blurredCover = PlayerCoverBlur( artUri: mediaItem.artUri?.toString(), colorScheme: colorScheme, ); @@ -270,17 +270,32 @@ class _RenderFadeBand extends RenderProxyBox { /// tint over it stay live layers, so the artwork scheme moving to the next /// track's colours never draws the blur again. A new cover's blur fades in /// over the previous one with the track change. -class _BlurredCover extends StatefulWidget { - const _BlurredCover({required this.artUri, required this.colorScheme}); +/// A cover blurred beyond detail, filling its box. The blur is drawn once +/// per cover and size, then shown as a plain image, and a new cover fades +/// in over the previous one. +class PlayerCoverBlur extends StatefulWidget { + const PlayerCoverBlur({ + super.key, + required this.artUri, + required this.colorScheme, + this.reveal, + }); final String? artUri; final ColorScheme colorScheme; + /// Without it the blur sits between two layers of the surface colour and + /// is blurred live while the cover decodes. With it, only the blur is + /// drawn, transparent down to `top` and opaque from `bottom` (logical + /// pixels from the top of the box), so it can lie over a sharp cover that + /// ends there, and nothing shows until the cover has decoded. + final ({double top, double bottom})? reveal; + @override - State<_BlurredCover> createState() => _BlurredCoverState(); + State createState() => _PlayerCoverBlurState(); } -class _BlurredCoverState extends State<_BlurredCover> +class _PlayerCoverBlurState extends State with SingleTickerProviderStateMixin { static const _sigma = 48.0; static const _artWidth = PlayerArtwork.miniCacheWidth; @@ -311,7 +326,7 @@ class _BlurredCoverState extends State<_BlurredCover> } @override - void didUpdateWidget(_BlurredCover oldWidget) { + void didUpdateWidget(PlayerCoverBlur oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.artUri != widget.artUri) _resolve(); } @@ -372,19 +387,24 @@ class _BlurredCoverState extends State<_BlurredCover> /// leaves without visible detail. The live filter blurs in logical pixels /// under the device pixel ratio; this canvas has no transform, so sigma /// takes the same dpr / 4 scale and still spans 48 dp once drawn back. - static ui.Image _render(ui.Image art, Size size, double dpr) { + static ui.Image _render( + ui.Image art, + Size size, + double dpr, + ({double top, double bottom})? reveal, + ) { final scale = dpr / 4; final width = math.max(1, (size.width * scale).ceil()); final height = math.max(1, (size.height * scale).ceil()); final rect = Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble()); final sigma = _sigma * scale; final recorder = ui.PictureRecorder(); - final canvas = Canvas(recorder) - ..saveLayer( - rect, - Paint() - ..imageFilter = ui.ImageFilter.blur(sigmaX: sigma, sigmaY: sigma), - ); + final canvas = Canvas(recorder); + if (reveal != null) canvas.saveLayer(rect, Paint()); + canvas.saveLayer( + rect, + Paint()..imageFilter = ui.ImageFilter.blur(sigmaX: sigma, sigmaY: sigma), + ); paintImage( canvas: canvas, rect: rect, @@ -393,6 +413,21 @@ class _BlurredCoverState extends State<_BlurredCover> filterQuality: FilterQuality.low, ); canvas.restore(); + if (reveal != null) { + // Baked into the image, so showing it costs no mask per frame. + canvas + ..drawRect( + rect, + Paint() + ..blendMode = BlendMode.dstIn + ..shader = ui.Gradient.linear( + Offset(0, reveal.top * scale), + Offset(0, math.max(reveal.bottom, reveal.top + 1) * scale), + const [Color(0x00ffffff), Color(0xffffffff)], + ), + ) + ..restore(); + } final picture = recorder.endRecording(); try { return picture.toImageSync(width, height); @@ -412,8 +447,10 @@ class _BlurredCoverState extends State<_BlurredCover> _motion = playerTrackChangeMotion(context); final surface = widget.colorScheme.surface; final art = _art?.image; + final reveal = widget.reveal; final Widget blurred; if (art == null) { + if (reveal != null) return const SizedBox.shrink(); // Blur live while the cover decodes and for the placeholder. blurred = ImageFiltered( imageFilter: ui.ImageFilter.blur(sigmaX: _sigma, sigmaY: _sigma), @@ -427,10 +464,10 @@ class _BlurredCoverState extends State<_BlurredCover> final dpr = MediaQuery.devicePixelRatioOf(context); blurred = LayoutBuilder( builder: (context, constraints) { - final inputs = (art, constraints.biggest, dpr); + final inputs = (art, constraints.biggest, dpr, reveal); if (inputs != _blurInputs) { _blur?.dispose(); - _blur = _render(art, constraints.biggest, dpr); + _blur = _render(art, constraints.biggest, dpr, reveal); _blurInputs = inputs; } final outgoing = _outgoing; @@ -448,6 +485,7 @@ class _BlurredCoverState extends State<_BlurredCover> }, ); } + if (reveal != null) return blurred; return Stack( fit: StackFit.expand, children: [ diff --git a/lib/widgets/mornye_player_background.dart b/lib/widgets/mornye_player_background.dart index ec59d02fe..e0954287d 100644 --- a/lib/widgets/mornye_player_background.dart +++ b/lib/widgets/mornye_player_background.dart @@ -2,6 +2,7 @@ import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:mornye/theme/cover_palette.dart'; +import 'package:mornye/widgets/material_player_backdrop.dart'; /// The player's background for a cover whose average is [sourceColor]. With /// [fullBleed] it runs behind the artwork and the controls, otherwise behind @@ -67,6 +68,7 @@ class MornyePlayerBackground extends StatelessWidget { this.squareArtwork = true, this.artworkAspectRatio, this.fadeStart = 0.58, + this.coverBlur = true, }); final Uri? artUri; @@ -77,6 +79,10 @@ class MornyePlayerBackground extends StatelessWidget { /// Fraction of the artwork's height that stays sharp before fading out. final double fadeStart; + /// Whether the full-bleed cover fades into a blur of itself. Without it, + /// as where blur effects are off, it fades into the flat gradient. + final bool coverBlur; + @override Widget build(BuildContext context) { final theme = Theme.of(context); @@ -124,6 +130,27 @@ class MornyePlayerBackground extends StatelessWidget { artworkHeight, ), ), + // The cover carries on under the controls as a blur, so + // its colours stay around the buttons instead of ending + // in one flat tone. The gradient shows until it is ready + // and for a song without a cover. + AnimatedSwitcher( + duration: motion, + switchInCurve: Curves.easeInOutCubic, + switchOutCurve: Curves.easeInOutCubic, + layoutBuilder: (current, previous) => Stack( + fit: StackFit.expand, + children: [...previous, ?current], + ), + child: artwork == null || !coverBlur + ? const SizedBox.shrink() + : _CoverBlur( + key: const ValueKey('full-player-blur'), + artUri: artUri, + top: artworkHeight * fadeStart, + bottom: artworkHeight, + ), + ), ], ), ), @@ -219,6 +246,58 @@ class MornyeControlsDim extends StatelessWidget { } } +/// The blurred cover from the artwork's fade down, under a shade that keeps +/// a vivid cover from competing with the controls. +class _CoverBlur extends StatelessWidget { + const _CoverBlur({ + super.key, + required this.artUri, + required this.top, + required this.bottom, + }); + + final Uri? artUri; + final double top; + final double bottom; + + @override + Widget build(BuildContext context) { + final height = MediaQuery.sizeOf(context).height; + return IgnorePointer( + child: Stack( + fit: StackFit.expand, + children: [ + PlayerCoverBlur( + artUri: artUri?.toString(), + colorScheme: Theme.of(context).colorScheme, + // Opaque by the middle of the artwork's own fade, before the + // flat gradient under it shows as a dull band between the two. + reveal: (top: top, bottom: (top + bottom) / 2), + ), + DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + stops: [ + (top / height).clamp(0.0, 1.0), + (bottom / height).clamp(0.0, 1.0), + 1, + ], + colors: const [ + Color(0x00000000), + Color(0x2e000000), + Color(0x52000000), + ], + ), + ), + ), + ], + ), + ); + } +} + class _GradientTween extends Tween { _GradientTween({super.end}); diff --git a/test/mornye_player_background_test.dart b/test/mornye_player_background_test.dart index 6784242a8..b8d6ce86b 100644 --- a/test/mornye_player_background_test.dart +++ b/test/mornye_player_background_test.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mornye/theme/cover_palette.dart'; +import 'package:mornye/widgets/material_player_backdrop.dart'; import 'package:mornye/widgets/mornye_player_background.dart'; const _width = 200.0; @@ -213,6 +214,8 @@ void main() { artwork: artwork ? _artwork : null, artworkAspectRatio: ratio, fadeStart: overlayFadeStart ?? fadeStart, + // The reference composite ends in the flat gradient. + coverBlur: false, ), ), ], @@ -240,6 +243,84 @@ void main() { return largest; } + testWidgets('the cover carries on under the controls as a blur', ( + tester, + ) async { + final directory = Directory.systemTemp.createTempSync('player-blur-'); + addTearDown(() => directory.deleteSync(recursive: true)); + // Far from the tone the flat gradient derives from it. + final cover = (await tester.runAsync( + () => _cover(directory, const Color(0xffe8e010)), + ))!; + + Future show({required bool artwork, bool coverBlur = true}) => + tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Align( + alignment: Alignment.topLeft, + child: side( + overlay, + MornyePlayerBackground( + artUri: cover, + artwork: artwork ? _artwork : null, + coverBlur: coverBlur, + ), + ), + ), + ), + ); + final blur = find.descendant( + of: find.byType(PlayerCoverBlur), + matching: find.byType(RawImage), + ); + Future settle({bool untilBlurred = false}) async { + // The cover decodes off the test clock. + for (var i = 0; i < 200; i++) { + if (i >= 5 && (!untilBlurred || blur.evaluate().isNotEmpty)) break; + await tester.runAsync( + () => Future.delayed(const Duration(milliseconds: 5)), + ); + await tester.pump(); + } + // Past the fades of the switch and of a new cover. + await tester.pump(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); + } + + int channel(Uint8List data, int x, int y, int offset) => + data[(y * _width.toInt() + x) * 4 + offset]; + + await show(artwork: true, coverBlur: false); + await settle(); + expect(find.byType(PlayerCoverBlur), findsNothing); + final flat = await pixels(tester, overlay); + + await show(artwork: true); + await settle(untilBlurred: true); + expect(tester.widget(blur).image, isNotNull); + final blurred = await pixels(tester, overlay); + // The sharp part of the cover is untouched. + for (final offset in [0, 1, 2]) { + expect(channel(blurred, 100, 40, offset), channel(flat, 100, 40, offset)); + } + // Under the controls the cover's own yellow shows, where the flat + // gradient was a muted gray-green: far more red than blue. + final red = channel(blurred, 100, 360, 0); + final blue = channel(blurred, 100, 360, 2); + expect(red - blue, greaterThan(60)); + expect( + channel(flat, 100, 360, 0) - channel(flat, 100, 360, 2), + lessThan(red - blue), + ); + + // Lyrics and the queue keep their plain gradient. + await show(artwork: false); + await settle(); + expect(find.byType(PlayerCoverBlur), findsNothing); + expect(tester.takeException(), isNull); + }); + setUp(() { final view = TestWidgetsFlutterBinding.instance.platformDispatcher.views.single;