Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: the redesigned album page plays animated covers

My-Name-Is-Jeff1bb2e7132ea1
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 48b7e13f..a8c11cb3 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -190,7 +190,9 @@ Redesigned:
Laid out on the Mac against harness/playlist/
Album/ the album page laid out the same way, on the page the Creative Work Platform builds rather than the
playlist's, so it shares nothing with Playlist/ but the Kit: the cover full bleed dissolving into the
- field, the title, the artist and the kind and date centred under it, and the same row of glass
+ field (Apple Music's animated cover over it where the album has one, fading in once its first frame
+ is decoded and moving only in front of the app with Reduce Motion and Low Power Mode off and Auto-Play
+ Video Previews on), the title, the artist and the kind and date centred under it, and the same row of glass
controls -- play and shuffle float over the album page outside its header, so they are concealed
there and the row carries the Kit's stand-ins, which draw their glyph and fire them. Under the tracks
everything the server sends is dropped -- more by the artist, videos, concerts, merch, you might also
diff --git a/harness/album/README.md b/harness/album/README.md
index 7d63b9ca..8c898cc7 100644
--- a/harness/album/README.md
+++ b/harness/album/README.md
@@ -38,6 +38,11 @@ At 3.5 s it puts Spotify's own frames back on the title block, its stack and the
both groups for a layout pass, which is what the redesign's `SGRObserveLayout` watches have to survive.
The log says what the redesign answered with.
+`motion` on the launch line, with `SIMCTL_CHILD_HARNESS_CANVAS_FILE` set to any mp4, hands the hero that clip at
+2 s the way Apple Music's catalog would. The log says the clip was added at opacity 0 and not yet fading, that by
+2.5 s its first frame was ready and it had faded in and was playing, and that taken out of the window at 4.5 s it
+stopped and put back it played again.
+
`SGLog` goes to the unified log rather than stdout, so the redesign's own lines are read with
xcrun simctl spawn booted log stream --predicate 'eventMessage CONTAINS "spotifyglass"' --style compact
diff --git a/harness/album/main.m b/harness/album/main.m
index 72d8e4b5..005c29d1 100644
--- a/harness/album/main.m
+++ b/harness/album/main.m
@@ -2,6 +2,7 @@
// trees/clean/album/03.txt, so Redesigned/Album can be laid out and looked at on the Mac.
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
+#import <AVFoundation/AVFoundation.h>
#import "../download-mock.h"
#pragma mark - Spotify's classes, by name
@@ -443,6 +444,47 @@ static NSString *trailingLabel(UIView *root) {
at(7, ^{ [trailingButton(rootView) sendActionsForControlEvents:UIControlEventTouchUpInside]; });
}
+ // `motion`: Apple Music's animated cover, HARNESS_CANVAS_FILE (any mp4), handed to the hero at 2 s as the
+ // catalog would hand it. The clip stays transparent until its first frame is decoded and then fades in;
+ // it plays in a window and stops out of one.
+ if ([NSProcessInfo.processInfo.arguments containsObject:@"motion"]) {
+ UIView *rootView = root.view;
+ __block UIView *hero = nil;
+ at(2, ^{
+ NSMutableArray<UIView *> *queue = [NSMutableArray arrayWithObject:rootView];
+ while (queue.count && !hero) {
+ UIView *v = queue.firstObject;
+ [queue removeObjectAtIndex:0];
+ if ([NSStringFromClass(v.class) isEqualToString:@"SGRAlbumHero"]) hero = v;
+ [queue addObjectsFromArray:v.subviews];
+ }
+ const char *path = getenv("HARNESS_CANVAS_FILE");
+ NSLog(@"[harness] motion: hero %@, clip %s", hero ? @"found" : @"missing", path ?: "not given");
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
+ if (hero && path) [hero performSelector:NSSelectorFromString(@"showMotion:") withObject:[NSURL fileURLWithPath:@(path)]];
+#pragma clang diagnostic pop
+ AVPlayerLayer *clip = [hero valueForKey:@"_motion"];
+ NSLog(@"[harness] motion: added, ready %d, opacity %.0f, fading %@", clip.readyForDisplay, clip.opacity,
+ [clip animationForKey:@"fade"] ? @"yes" : @"no");
+ });
+ for (NSNumber *when in @[@2.5, @4]) {
+ at(when.doubleValue, ^{
+ AVPlayerLayer *clip = [hero valueForKey:@"_motion"];
+ NSLog(@"[harness] motion at %@s: ready %d, opacity %.0f, rate %.0f", when, clip.readyForDisplay, clip.opacity, clip.player.rate);
+ });
+ }
+ at(4.5, ^{
+ UIView *parent = hero.superview;
+ NSUInteger index = [parent.subviews indexOfObject:hero];
+ [hero removeFromSuperview];
+ AVPlayerLayer *clip = [hero valueForKey:@"_motion"];
+ NSLog(@"[harness] motion out of the window: rate %.0f", clip.player.rate);
+ [parent insertSubview:hero atIndex:index];
+ NSLog(@"[harness] motion back in it: rate %.0f", clip.player.rate);
+ });
+ }
+
// In `late`, add arrives at 2.5 s, after every pass of the header's and the metadata's re-reads: an arranged
// subview of the row, which lays out the row and nothing above it.
if (late) {
diff --git a/tweak/Sources/Redesigned/Album/AlbumHeader.x b/tweak/Sources/Redesigned/Album/AlbumHeader.x
index ecc94da6..1ae90794 100644
--- a/tweak/Sources/Redesigned/Album/AlbumHeader.x
+++ b/tweak/Sources/Redesigned/Album/AlbumHeader.x
@@ -24,8 +24,10 @@
// Play and shuffle are not in the header. The album page floats them over the page, pinned to the top
// trailing corner outside the scroll (01.txt:1431, :1436): they are concealed where they are, and the row
// draws them and fires them all the same.
+#import <AVFoundation/AVFoundation.h>
#import "Core/SGCore.h"
#import "Redesigned/Kit/SGRKit.h"
+#import "Shared/AnimatedArtwork/AnimatedArtwork.h"
#import "Album.h"
// How much of the cover's height the dissolve into the field covers, and the scrim over the top of it that
@@ -119,13 +121,21 @@ static void watch(UIView *view, const void *key, void (^laidOut)(UIView *view))
// The cover in Spotify's artwork view, and every cover it puts there afterwards: the hero keeps itself
// right, rather than being handed a picture on each of the header's passes and staying empty between them.
- (void)followCover:(UIImageView *)source;
+// Apple Music's animated cover over the picture, where the album has one. The same album again is a no-op.
+- (void)playMotionForArtist:(NSString *)artist album:(NSString *)album;
@end
@implementation SGRAlbumHero {
CAGradientLayer *_scrim, *_dissolve;
__weak UIImageView *_cover;
+ NSString *_motionAlbum;
+ AVPlayerLayer *_motion;
+ AVQueuePlayer *_player;
+ AVPlayerLooper *_looper;
}
+static void *kReadyContext = &kReadyContext;
+
- (instancetype)initWithFrame:(CGRect)frame {
if (!(self = [super initWithFrame:frame])) return nil;
self.userInteractionEnabled = NO;
@@ -148,13 +158,19 @@ static void watch(UIView *view, const void *key, void (^laidOut)(UIView *view))
[self.layer addSublayer:_dissolve];
self.fieldColor = SGRNeutralField();
// The colour is read off the main thread, so it can land after the last layout pass of the page.
- [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(sgr_fieldColorDidChange)
- name:SGRFieldColorDidChangeNotification object:nil];
+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
+ [center addObserver:self selector:@selector(sgr_fieldColorDidChange) name:SGRFieldColorDidChangeNotification object:nil];
+ for (NSNotificationName name in @[UIApplicationDidBecomeActiveNotification, UIApplicationWillResignActiveNotification,
+ NSProcessInfoPowerStateDidChangeNotification, UIAccessibilityReduceMotionStatusDidChangeNotification,
+ UIAccessibilityVideoAutoplayStatusDidChangeNotification]) {
+ [center addObserver:self selector:@selector(sgr_updateMotionSoon) name:name object:nil];
+ }
return self;
}
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
+ [self sgr_dropMotion];
}
- (void)sgr_fieldColorDidChange {
@@ -180,12 +196,99 @@ static void watch(UIView *view, const void *key, void (^laidOut)(UIView *view))
CGRect bounds = self.bounds;
[CATransaction begin];
[CATransaction setDisableActions:YES];
+ _motion.frame = bounds;
_scrim.frame = CGRectMake(0, 0, bounds.size.width, MIN(kTopScrim, bounds.size.height));
CGFloat fade = round(bounds.size.height * kDissolve);
_dissolve.frame = CGRectMake(0, bounds.size.height - fade, bounds.size.width, fade);
[CATransaction commit];
}
+- (void)playMotionForArtist:(NSString *)artist album:(NSString *)album {
+ NSString *key = artist.length && album.length ? [NSString stringWithFormat:@"%@\n%@", artist, album] : nil;
+ if (!key || [key isEqualToString:_motionAlbum]) return;
+ _motionAlbum = key;
+ CGFloat pixels = MAX(self.bounds.size.width, UIScreen.mainScreen.bounds.size.width) * UIScreen.mainScreen.scale;
+ __weak SGRAlbumHero *weakSelf = self;
+ SGMotionAlbumCover(artist, album, SGMotionSquare, pixels, ^(NSURL *file) {
+ SGRAlbumHero *hero = weakSelf;
+ if (hero && file && [hero->_motionAlbum isEqualToString:key]) [hero showMotion:file];
+ });
+}
+
+// Muted and looping, over the picture and under the two gradients. The picture stays under it until the
+// first frame is decoded, and only then does the clip fade in: a fade started at once was spent on a layer
+// with nothing in it, and the clip cut in after it.
+- (void)showMotion:(NSURL *)file {
+ AVPlayerItem *item = [AVPlayerItem playerItemWithURL:file];
+ _player = [AVQueuePlayer new];
+ _player.muted = YES;
+ _player.preventsDisplaySleepDuringVideoPlayback = NO;
+ _looper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:item];
+ [self sgr_dropMotion];
+ _motion = [AVPlayerLayer playerLayerWithPlayer:_player];
+ _motion.videoGravity = AVLayerVideoGravityResizeAspectFill;
+ _motion.zPosition = 0.5;
+ _motion.frame = self.bounds;
+ _motion.opacity = 0;
+ [self.layer addSublayer:_motion];
+ [_motion addObserver:self forKeyPath:@"readyForDisplay" options:NSKeyValueObservingOptionInitial context:kReadyContext];
+ [self sgr_updateMotion];
+ SGLog(@"redesign album: animated cover %@", file.lastPathComponent);
+}
+
+- (void)sgr_dropMotion {
+ [_motion removeObserver:self forKeyPath:@"readyForDisplay" context:kReadyContext];
+ [_motion removeFromSuperlayer];
+ _motion = nil;
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ if (context != kReadyContext) {
+ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
+ return;
+ }
+ __weak SGRAlbumHero *weakSelf = self;
+ dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf sgr_fadeInMotion:object]; });
+}
+
+// Once, when the clip has a frame to show: eased out, over the Kit's crossfade, on screen; at once off it.
+- (void)sgr_fadeInMotion:(AVPlayerLayer *)layer {
+ if (layer != _motion || !layer.readyForDisplay || layer.opacity == 1) return;
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+ layer.opacity = 1;
+ if (self.window) {
+ CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
+ fade.fromValue = @0;
+ fade.toValue = @1;
+ fade.duration = SGRCrossfade;
+ fade.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
+ [layer addAnimation:fade forKey:@"fade"];
+ }
+ [CATransaction commit];
+}
+
+// The power state is reported off the main thread.
+- (void)sgr_updateMotionSoon {
+ dispatch_async(dispatch_get_main_queue(), ^{ [self sgr_updateMotion]; });
+}
+
+// The clip moves under the conditions the player's does (Player/PlayerMotion.x), and only with Auto-Play
+// Video Previews on; otherwise it holds its frame. Off screen it stops: an album page left on the
+// navigation stack would otherwise keep decoding.
+- (void)sgr_updateMotion {
+ BOOL front = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;
+ BOOL may = self.window && front && !SGRReduceMotion() && !NSProcessInfo.processInfo.lowPowerModeEnabled
+ && UIAccessibilityIsVideoAutoplayEnabled();
+ if (may) [_player play];
+ else [_player pause];
+}
+
+- (void)didMoveToWindow {
+ [super didMoveToWindow];
+ [self sgr_updateMotion];
+}
+
- (void)followCover:(UIImageView *)source {
if (!source) return;
[self takeCover:source late:NO];
@@ -359,7 +462,7 @@ static SGRHeaderInfo *applyInfo(UIView *header, UIView *page) {
if (!logged && header.window && title) {
logged = YES;
SGLog(@"redesign album: own block \"%@\" by %@, \"%@\"; shuffle %@, play %@, %@", firstText(title),
- firstText(parent) ?: @"nobody", metadataText(metadata) ?: @"no metadata", shuffle ? @"found" : @"missing",
+ firstText(parent) ?: trimmed(parent.accessibilityLabel) ?: @"nobody", metadataText(metadata) ?: @"no metadata", shuffle ? @"found" : @"missing",
play ? @"found" : @"missing", add ? @"add" : (download ? @"download" : @"nothing on the right"));
}
return info;
@@ -436,6 +539,10 @@ static void applyHeader(UIView *header, UIView *page) {
CGFloat bottom = rest - SGRHeaderInfoBottom - [info contentHeightForWidth:header.bounds.size.width] + SGRHeaderInfoTitleRise;
UIView *cover = SGRFindByIdentifier(header, @"CreativeWorkPlatform.Components.UI.ArtWorkElement.WithCoverArt", &kCoverKey);
if (cover) applyHero(header, cover, bottom);
+ SGRAlbumHero *hero = objc_getAssociatedObject(header, &kHeroKey);
+ UIView *title = SGRFindByIdentifier(header, @"CreativeWorkPlatform.Components.UI.TitleRow", &kTitleKey);
+ UIView *parent = SGRFindByIdentifier(header, @"CreativeWorkPlatform.Components.UI.ParentRow", &kParentKey);
+ [hero playMotionForArtist:firstText(parent) ?: trimmed(parent.accessibilityLabel) album:firstText(title)];
}
static void applyPage(UIView *page) {