Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: the player's background follows the playing track, and moves in its colours (#58, #59)
Vojtěch Škopeka70fe1a01d30
diff --git a/harness/album/build.sh b/harness/album/build.shindex 30caacff..f85c5deb 100755--- a/harness/album/build.sh+++ b/harness/album/build.sh@@ -17,7 +17,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$OUT"/gen/*.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \- "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m \+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m "$SRC"/Redesigned/Kit/SGRFlow.m \"$SRC"/Redesigned/Kit/SGRGlass.m "$SRC"/Redesigned/Kit/SGRGlyph.m "$SRC"/Redesigned/Kit/SGRRestyle.m \"$SRC"/Redesigned/Kit/SGRActionRow.m "$SRC"/Redesigned/Kit/SGRDownload.m "$SRC"/Redesigned/Kit/SGRHeaderInfo.m \"$SRC"/Redesigned/Kit/SGRedesign.m \diff --git a/harness/album/stubs.m b/harness/album/stubs.mindex 8c64e941..ef2a5b60 100644--- a/harness/album/stubs.m+++ b/harness/album/stubs.m@@ -7,3 +7,7 @@ __weak UIView *sgr_lyricsCardRoot = nil;__weak UIView *sgr_lyricsPageRoot = nil;__weak UIView *sgr_playlistRoot = nil;__weak UIView *sgr_albumRoot = nil;++// Redesigned/Kit/SGRBridges.x: SGRField.m asks whether the player is opening or closing for its moving field.+BOOL SGRPlayerIsTransitioning(void) { return NO; }+void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)(id owner)) {}diff --git a/harness/artist/build.sh b/harness/artist/build.shindex 596cafa3..84c3a3cc 100755--- a/harness/artist/build.sh+++ b/harness/artist/build.sh@@ -17,7 +17,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$OUT"/gen/*.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \- "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m \+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m "$SRC"/Redesigned/Kit/SGRFlow.m \"$SRC"/Redesigned/Kit/SGRGlass.m "$SRC"/Redesigned/Kit/SGRGlyph.m "$SRC"/Redesigned/Kit/SGRRestyle.m \"$SRC"/Redesigned/Kit/SGRActionRow.m "$SRC"/Redesigned/Kit/SGRDownload.m "$SRC"/Redesigned/Kit/SGRHeaderInfo.m \"$SRC"/Redesigned/Kit/SGRedesign.m \diff --git a/harness/artist/stubs.m b/harness/artist/stubs.mindex 9e2549ce..7879b96d 100644--- a/harness/artist/stubs.m+++ b/harness/artist/stubs.m@@ -8,3 +8,7 @@ __weak UIView *sgr_lyricsPageRoot = nil;__weak UIView *sgr_playlistRoot = nil;__weak UIView *sgr_albumRoot = nil;__weak UIView *sgr_artistRoot = nil;++// Redesigned/Kit/SGRBridges.x: SGRField.m asks whether the player is opening or closing for its moving field.+BOOL SGRPlayerIsTransitioning(void) { return NO; }+void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)(id owner)) {}diff --git a/harness/library/build.sh b/harness/library/build.shindex a567b4bc..9e4c3c31 100755--- a/harness/library/build.sh+++ b/harness/library/build.sh@@ -18,7 +18,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$OUT"/gen/*.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \- "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m \+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m "$SRC"/Redesigned/Kit/SGRFlow.m \"$SRC"/Redesigned/Kit/SGRGlass.m "$SRC"/Redesigned/Kit/SGRGlyph.m "$SRC"/Redesigned/Kit/SGRRestyle.m \"$SRC"/Redesigned/Kit/SGRActionRow.m "$SRC"/Redesigned/Kit/SGRDownload.m "$SRC"/Redesigned/Kit/SGRHeaderInfo.m \"$SRC"/Redesigned/Kit/SGRedesign.m \diff --git a/harness/library/stubs.m b/harness/library/stubs.mindex 9e2549ce..7879b96d 100644--- a/harness/library/stubs.m+++ b/harness/library/stubs.m@@ -8,3 +8,7 @@ __weak UIView *sgr_lyricsPageRoot = nil;__weak UIView *sgr_playlistRoot = nil;__weak UIView *sgr_albumRoot = nil;__weak UIView *sgr_artistRoot = nil;++// Redesigned/Kit/SGRBridges.x: SGRField.m asks whether the player is opening or closing for its moving field.+BOOL SGRPlayerIsTransitioning(void) { return NO; }+void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)(id owner)) {}diff --git a/harness/player/README.md b/harness/player/README.mdindex 5564080d..01a92987 100644--- a/harness/player/README.md+++ b/harness/player/README.md@@ -1,28 +1,47 @@# Player harnessSpotify's full screen player mocked under its own class names and accessibility identifiers-(from `trees/clean/player/01.txt`), so the redesign's lyrics state can be laid out, animated and+(from `trees/clean/player/01.txt`), so the redesign's player can be laid out, animated andlooked at on the Mac without the phone.THEOS=$HOME/theos ./build.sh- xcrun simctl install booted build/PlayerHarness.app- xcrun simctl launch booted com.vojta.playerharness- xcrun simctl io booted screenshot shot.png+ xcrun simctl install <udid> build/PlayerHarness.app+ SIMCTL_CHILD_HARNESS_SCENARIO=artwork xcrun simctl launch --console-pty <udid> com.vojta.playerharness+ xcrun simctl io <udid> screenshot shot.png++Launch it on an iOS 26 simulator by UDID. The iOS 27 runtime kills an app that has a scene manifest but+no scene delegate. `SRC=<another checkout>/tweak/Sources OUT=<dir> ./build.sh` builds it against other+sources, for example an older commit, to see a bug before its fix.`build.sh` runs `logos.pl -c generator=internal` over `PlayerLyrics.x`, `PlayerArtwork.x`,-`PlayerFooter.x` and `PlayerScroll.x` and links them with the real `Core/`, `Redesigned/Kit/` and-`SGRKaraokeView`; `stubs.m` stands in for the hooks the harness does not compile (the Kit's bridges-and repaint, the rest of the player, the lyrics store, the haptics) and holds a song of ten timed-lines that plays on from launch.+`PlayerFooter.x`, `PlayerScroll.x`, `PlayerField.x` and the Kit's `SGRBridges.x`, and links them with+the real `Core/`, `Redesigned/Kit/` and `SGRKaraokeView`. `stubs.m` stands in for the hooks the harness+does not compile (the Kit's accent and repaint, the rest of the player, the lyrics store, the haptics)+and plays a mock player: `SGRHarnessSetTrack` reports a track, with the image ids Spotify's metadata+carries, to every state observer. A song of ten timed lines plays on from launch. `main.m` also answers+for i.scdn.co through an `NSURLProtocol` handed to every session, so each picture the Kit fetches can+come late, out of order, or not at all.++`HARNESS_SCENARIO` picks what the harness does:++- `lyrics` (default) opens the lyrics at 2 s, closes them at 6 and opens them again at 10.+- `look` plays one track, then a track from another album at 8 s, whose picture reaches the screens+ 0.4 s later. It shows the field, the moving background and its crossfade, and the footer row. At 3 s+ it logs whether touches on the lowered footer row (issue #54) still reach it.+- `artwork` is issue #58. Tracks change while the covers on screen lag behind (3.5 s late, past the+ Kit's last look), two skips come in a row with the older picture answering last, and one track plays+ offline. Each step checks by colour that the Kit and the field show that track's picture, and the log+ ends with `artwork checks: n of 4 right -- PASS` or `FAIL`. Before the fix it read 1 of 4.++`HARNESS_VOLUME=0` leaves out the volume row that the phone has and the tree does not.-The mock opens the lyrics two seconds after launch, closes them at six and opens them again at ten,-so a screenshot catches each state and a screen recording catches the move:+A screen recording is the way to see a move:- xcrun simctl io booted recordVideo -f run.mp4 # ^C to stop- ffmpeg -ss 2.9 -t 1.1 -i run.mp4 -vf "fps=10,scale=200:-1,tile=11x1" -frames:v 1 strip.png+ xcrun simctl io <udid> recordVideo -f run.mp4 # ^C to stop+ ffmpeg -ss 7.4 -t 2.2 -i run.mp4 -vf "fps=5,scale=180:-1,tile=11x1" -frames:v 1 strip.png-It also lays every unit out once a second, which is what the transforms on Spotify's title row and-the narrowed marquee labels have to survive.+It also lays every unit out once a second, which is what the transforms on Spotify's title row and the+narrowed marquee labels have to survive.What it does not cover: the real element framework's autolayout, Spotify's own scrolled layout, the-player's open and close transition, and the artwork field behind the player (`PlayerField.x`).+player's open and close transition, and Spotify's real image loading.diff --git a/harness/player/build.sh b/harness/player/build.shindex 12910686..2cb61b9e 100755--- a/harness/player/build.sh+++ b/harness/player/build.sh@@ -1,27 +1,28 @@#!/bin/shset -e-SRC=/Users/vojta/Documents/quick/custom_spotify/custom_spotify/tweak/Sources-OUT=$(dirname "$0")/build+# SRC may point at another checkout of tweak/Sources (an older commit, to see a bug before its fix).+SRC=${SRC:-$(cd "$(dirname "$0")/../../tweak/Sources" && pwd)}+OUT=${OUT:-$(dirname "$0")/build}rm -rf "$OUT"; mkdir -p "$OUT/gen" "$OUT/PlayerHarness.app"for f in Redesigned/Player/PlayerLyrics.x Redesigned/Player/PlayerArtwork.x Redesigned/Player/PlayerFooter.x \- Redesigned/Player/PlayerScroll.x; do+ Redesigned/Player/PlayerScroll.x Redesigned/Player/PlayerField.x Redesigned/Kit/SGRBridges.x; doname=$(basename "$f" .x)"$THEOS/bin/logos.pl" -c generator=internal "$SRC/$f" > "$OUT/gen/$name.m"doneSDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -g -O0 \- -I"$SRC" -I"$SRC/Redesigned/Player" -I"$OUT/gen" -isysroot "$SDK" \+ -I"$SRC" -I"$SRC/Redesigned/Player" -I"$SRC/Redesigned/Kit" -I"$OUT/gen" -isysroot "$SDK" \-Wno-deprecated-declarations \"$(dirname "$0")/main.m" "$(dirname "$0")/stubs.m" \"$OUT"/gen/*.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \- "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m \+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m "$SRC"/Redesigned/Kit/SGRFlow.m \"$SRC"/Redesigned/Kit/SGRGlass.m "$SRC"/Redesigned/Kit/SGRGlyph.m "$SRC"/Redesigned/Kit/SGRRestyle.m \"$SRC"/Redesigned/Kit/SGRedesign.m \- "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/AdBlock/Protobuf.m \+ "$SRC"/Redesigned/Lyrics/SGRKaraokeView.m "$SRC"/Redesigned/Lyrics/LyricsText.m "$SRC"/Shared/Lyrics/KaraokeTiming.m "$SRC"/Shared/AdBlock/Protobuf.m \-framework UIKit -framework QuartzCore -framework CoreGraphics -framework CoreImage -framework Foundation -framework Symbols \-o "$OUT/PlayerHarness.app/PlayerHarness"diff --git a/harness/player/main.m b/harness/player/main.mindex cbf83209..fd210477 100644--- a/harness/player/main.m+++ b/harness/player/main.m@@ -2,12 +2,132 @@// from trees/clean/player/01.txt, so Redesigned/Player's lyrics state can be laid out, animated and// looked at on the Mac. Tapping the lyrics glyph in the footer works exactly as it does on the phone;// the harness also toggles it once by itself so a screenshot catches each state.+//+// HARNESS_SCENARIO (simctl launch passes it as SIMCTL_CHILD_HARNESS_SCENARIO) picks what it does:+// lyrics (default) the lyrics opened at 2 s, closed at 6, opened again at 10+// look one track playing, a second one from another album at 8 s, nothing opened+// artwork issue #58: tracks change while the covers on screen and the picture server lag behind,+// checked by colour at the end of each step; the log says PASS or FAIL+// HARNESS_VOLUME=0 leaves out the volume row the phone has (trees/clean/player/01.txt has none).#import <UIKit/UIKit.h>+#import <objc/runtime.h>#import "Shared/Lyrics/Lyrics.h"#import "Redesigned/Player/Player.h"+#import "Redesigned/Kit/SGRBridges.h"+#import "Redesigned/Kit/SGRField.h"-void SGRHarnessSetArtwork(UIImage *image);void SGRHarnessPlayFrom(NSInteger ms);+void SGRHarnessSetTrack(NSString *uri, NSString *imageURI, BOOL paused);++static NSString *scenario(void) {+ const char *value = getenv("HARNESS_SCENARIO");+ return value ? @(value) : @"lyrics";+}++#pragma mark - the picture server++// i.scdn.co as the harness wants it: each picture by the last 24 digits of its id, served after a delay+// of its own, or failed as if the phone were offline.+@interface SGRHarnessPicture : NSObject+@property (nonatomic, strong) UIImage *image;+@property (nonatomic) NSTimeInterval delay;+@property (nonatomic) BOOL fails;+@end+@implementation SGRHarnessPicture+@end++static NSMutableDictionary<NSString *, SGRHarnessPicture *> *sg_pictures;+static NSUInteger sg_served;++static void serve(NSString *imageURI, UIImage *image, NSTimeInterval delay, BOOL fails) {+ if (!sg_pictures) sg_pictures = [NSMutableDictionary dictionary];+ SGRHarnessPicture *picture = [SGRHarnessPicture new];+ picture.image = image;+ picture.delay = delay;+ picture.fails = fails;+ sg_pictures[[imageURI substringFromIndex:imageURI.length - 24]] = picture;+}++@interface SGRHarnessPictureServer : NSURLProtocol+@end++@implementation SGRHarnessPictureServer {+ BOOL _stopped;+}+++ (BOOL)canInitWithRequest:(NSURLRequest *)request {+ return [request.URL.host isEqualToString:@"i.scdn.co"];+}+++ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {+ return request;+}++- (void)startLoading {+ NSString *name = self.request.URL.lastPathComponent;+ SGRHarnessPicture *picture = name.length >= 24 ? sg_pictures[[name substringFromIndex:name.length - 24]] : nil;+ NSThread *thread = NSThread.currentThread;+ NSLog(@"[harness] picture server: %@ asked for, %@", name, picture ? (picture.fails ? @"will fail" : [NSString stringWithFormat:@"answers in %.1f s", picture.delay]) : @"unknown");+ NSData *data = picture.fails ? nil : UIImagePNGRepresentation(picture.image);+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(picture.delay * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{+ [self performSelector:@selector(answer:) onThread:thread withObject:data waitUntilDone:NO];+ });+}++- (void)answer:(NSData *)data {+ if (_stopped) return;+ if (!data) {+ [self.client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNotConnectedToInternet userInfo:nil]];+ return;+ }+ sg_served++;+ NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL statusCode:200 HTTPVersion:@"HTTP/1.1"+ headerFields:@{@"Content-Type": @"image/png"}];+ [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];+ [self.client URLProtocol:self didLoadData:data];+ [self.client URLProtocolDidFinishLoading:self];+}++- (void)stopLoading {+ _stopped = YES;+}++@end++// Every session the mod makes is handed the server first.+@implementation NSURLSessionConfiguration (SGRHarness)++ (NSURLSessionConfiguration *)sgr_harnessDefault {+ NSURLSessionConfiguration *configuration = [self sgr_harnessDefault];+ configuration.protocolClasses = [@[SGRHarnessPictureServer.class] arrayByAddingObjectsFromArray:configuration.protocolClasses ?: @[]];+ return configuration;+}+@end++#pragma mark - pictures++static UIImage *solid(UIColor *color) {+ UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];+ format.scale = 1;+ return [[[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(300, 300) format:format] imageWithActions:^(UIGraphicsImageRendererContext *ctx) {+ [color setFill];+ UIRectFill(CGRectMake(0, 0, 300, 300));+ }];+}++// The colour a picture is, by the pixel in its middle, named the way the checks name them.+static NSString *colorName(UIImage *image) {+ if (!image.CGImage) return @"none";+ uint8_t px[4] = {0};+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();+ CGContextRef context = CGBitmapContextCreate(px, 1, 1, 8, 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);+ CGColorSpaceRelease(space);+ CGFloat w = CGImageGetWidth(image.CGImage), h = CGImageGetHeight(image.CGImage);+ CGContextDrawImage(context, CGRectMake(-w / 2, -h / 2, w, h), image.CGImage);+ CGContextRelease(context);+ int r = px[0] > 128, g = px[1] > 128, b = px[2] > 128;+ NSArray *names = @[@"black", @"blue", @"green", @"cyan", @"red", @"magenta", @"yellow", @"white"];+ return names[r << 2 | g << 1 | b];+}#pragma mark - Spotify's classes, by name@@ -23,6 +143,9 @@ void SGRHarnessPlayFrom(NSInteger ms);@interface _TtC20NowPlaying_ModesImpl20FloatingElementsUnit : UIViewController @end@implementation _TtC20NowPlaying_ModesImpl20FloatingElementsUnit @end+@interface _TtC20NowPlaying_ModesImpl28PlaybackControlsElementsUnit : UIViewController @end+@implementation _TtC20NowPlaying_ModesImpl28PlaybackControlsElementsUnit @end+@interface _TtC20NowPlaying_ModesImpl18FooterElementsUnit : UIViewController @end@implementation _TtC20NowPlaying_ModesImpl18FooterElementsUnit @end@@ -32,9 +155,15 @@ void SGRHarnessPlayFrom(NSInteger ms);@interface _TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView : UICollectionView @end@implementation _TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView @end-@interface _TtC28NowPlaying_ContentLayersImpl16CoverArtCellImpl : UIView @end+@interface _TtC28NowPlaying_ContentLayersImpl16CoverArtCellImpl : UICollectionViewCell @end@implementation _TtC28NowPlaying_ContentLayersImpl16CoverArtCellImpl @end+@interface _TtC21NowPlaying_ScrollImpl27NPVBackgroundViewController : UIViewController @end+@implementation _TtC21NowPlaying_ScrollImpl27NPVBackgroundViewController @end++@interface _TtC18NowPlaying_BarImpl27NowPlayingBarViewController : UIViewController @end+@implementation _TtC18NowPlaying_BarImpl27NowPlayingBarViewController @end+@interface _TtC35CreativeWorkCommons_CoverArtTiltKit16CoverArtTiltView : UIView- (void)handleTap;@end@@ -96,6 +225,33 @@ static UIImage *artwork(void) {}];}+// A second album, busier: a warm sky over teal water with a sun in it, for the track change.+static UIImage *secondArtwork(void) {+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(354, 354)];+ return [renderer imageWithActions:^(UIGraphicsImageRendererContext *ctx) {+ CGContextRef c = ctx.CGContext;+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();+ CGFloat sky[] = {0.98, 0.55, 0.20, 1, 0.85, 0.22, 0.30, 1};+ CGGradientRef gradient = CGGradientCreateWithColorComponents(space, sky, NULL, 2);+ CGContextDrawLinearGradient(c, gradient, CGPointZero, CGPointMake(0, 200), 0);+ CGGradientRelease(gradient);+ CGFloat sea[] = {0.05, 0.45, 0.50, 1, 0.02, 0.12, 0.22, 1};+ gradient = CGGradientCreateWithColorComponents(space, sea, NULL, 2);+ CGContextSaveGState(c);+ CGContextClipToRect(c, CGRectMake(0, 200, 354, 154));+ CGContextDrawLinearGradient(c, gradient, CGPointMake(0, 200), CGPointMake(0, 354), 0);+ CGContextRestoreGState(c);+ CGGradientRelease(gradient);+ CGColorSpaceRelease(space);+ [[UIColor colorWithRed:1 green:0.9 blue:0.55 alpha:1] setFill];+ CGContextFillEllipseInRect(c, CGRectMake(210, 110, 90, 90));+ [@"LOW\nTIDE" drawAtPoint:CGPointMake(26, 230) withAttributes:@{+ NSFontAttributeName: [UIFont systemFontOfSize:40 weight:UIFontWeightHeavy],+ NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.9],+ }];+ }];+}+// A song with words timed inside each line, the shape SGRKaraokeView draws.static void loadLyrics(void) {NSArray<NSString *> *texts = @[@@ -142,6 +298,10 @@ static void loadLyrics(void) {@implementation SGRHarnessDelegate {NSArray<UIViewController *> *_units;+ UIImageView *_cover, *_barCover;+ UIViewController *_bar;+ UICollectionView *_covers;+ NSUInteger _failures, _checks;}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {@@ -157,7 +317,9 @@ static void loadLyrics(void) {// rows are placed as shares of it, the way Spotify's layout puts them.CGFloat headerTop = 62, headerHeight = 48;CGFloat bandTop = headerTop + headerHeight;- CGFloat bottomHeight = 236 + 44; // the tree's rows plus a volume row, as the phone has it+ const char *volumeEnv = getenv("HARNESS_VOLUME");+ BOOL hasVolume = !(volumeEnv && volumeEnv[0] == '0');+ CGFloat bottomHeight = 236 + (hasVolume ? 44 : 0); // the tree's rows plus a volume row, as the phone has itCGFloat bottomTop = H - bottomHeight - 61.33;CGFloat bandHeight = bottomTop - bandTop;CGFloat coverSide = MIN(354, W - 48);@@ -171,6 +333,12 @@ static void loadLyrics(void) {UIViewController *scrollUnit = [_TtC21NowPlaying_ScrollImpl23NPVScrollViewController new];scrollUnit.view = page;+ // NPVBackgroundViewController's plane, the field's home (trees/clean/player/01.txt:449), under the player.+ UIView *plane = box(list, UIView.class, CGRectMake(0, 0, W, H), nil);+ plane.backgroundColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.3 alpha:1];+ UIViewController *background = [_TtC21NowPlaying_ScrollImpl27NPVBackgroundViewController new];+ background.view = plane;+UIView *host = box(list, UIView.class, CGRectMake(0, 0, W, H), @"SPTNowPlayingView");// the content layers: the sideways list of covers@@ -187,11 +355,12 @@ static void loadLyrics(void) {UIView *tilt = box(inner, _TtC35CreativeWorkCommons_CoverArtTiltKit16CoverArtTiltView.class,CGRectMake(0, round((inner.bounds.size.height - coverSide) / 2), coverSide, coverSide), nil);tilt.accessibilityLabel = @"Inspect cover art";- UIView *coverElement = box(tilt, UIView.class, tilt.bounds, nil);+ // The Encore.ImageView holding the picture (01.txt:40), which PlayerField.x reads the cover from.+ UIView *coverElement = box(tilt, UIView.class, tilt.bounds, @"Encore.ImageView");UIImage *picture = artwork();- SGRHarnessSetArtwork(picture);UIImageView *cover = [[UIImageView alloc] initWithFrame:coverElement.bounds];cover.image = picture;+ _cover = cover;cover.contentMode = UIViewContentModeScaleAspectFill;[coverElement addSubview:cover];box(inner, _TtC22Lyrics_NPVContainerKit19LyricsContainerView.class, CGRectMake(0, inner.bounds.size.height, coverSide, 0), nil);@@ -241,12 +410,14 @@ static void loadLyrics(void) {glyphButton(controls, CGRectMake(W / 2 - 24, 14, 48, 60), @"pause.fill", nil);glyphButton(controls, CGRectMake(W / 2 + 82, 20, 48, 48), @"forward.fill", nil);- UIView *volume = box(bottom, UIView.class, CGRectMake(0, 192, W, 44), nil);- UIView *volumeTrack = box(volume, UIView.class, CGRectMake(44, 19, W - 88, 6), nil);- volumeTrack.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];- volumeTrack.layer.cornerRadius = 3;+ if (hasVolume) {+ UIView *volume = box(bottom, UIView.class, CGRectMake(0, 192, W, 44), nil);+ UIView *volumeTrack = box(volume, UIView.class, CGRectMake(44, 19, W - 88, 6), nil);+ volumeTrack.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];+ volumeTrack.layer.cornerRadius = 3;+ }- UIView *footerView = box(bottom, UIView.class, CGRectMake(0, 236, W, 44), nil);+ UIView *footerView = box(bottom, UIView.class, CGRectMake(0, hasVolume ? 236 : 192, W, 44), nil);UIView *footerRow = box(footerView, UIStackView.class, CGRectMake(12, 0, W - 24, 44), nil);UIView *connect = box(footerRow, UIView.class, CGRectMake(0, 4, 153.67, 36), nil);UIView *connectHolder = box(connect, UIView.class, connect.bounds, @"Components.ConnectButtonOutputSwitcher");@@ -257,6 +428,18 @@ static void loadLyrics(void) {glyphButton(footerRow, CGRectMake(279, 0, 44, 44), @"square.and.arrow.up", @"ShareButtonNowPlayingView");glyphButton(footerRow, CGRectMake(323, 6, 47, 32), @"list.bullet", @"QueueButtonNowPlaying");+ // The now playing bar, off screen: the Kit reads its 40pt cover (SPTNowPlayingBar > Encore.ImageView >+ // UIImageView, trees/clean/artist/01.txt).+ UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W, 64)];+ UIView *barCard = box(barView, UIView.class, CGRectMake(8, 8, W - 16, 48), @"SPTNowPlayingBar");+ UIView *barHolder = box(barCard, UIView.class, CGRectMake(8, 4, 40, 40), @"Encore.ImageView");+ UIImageView *barCover = [[UIImageView alloc] initWithFrame:barHolder.bounds];+ barCover.image = picture;+ [barHolder addSubview:barCover];+ _barCover = barCover;+ _bar = [_TtC18NowPlaying_BarImpl27NowPlayingBarViewController new];+ _bar.view = barView;+[self.window makeKeyAndVisible];UIViewController *info = [_TtC20NowPlaying_ModesImpl23InformationElementsUnit new];@@ -267,9 +450,13 @@ static void loadLyrics(void) {floating.view = floatingView;UIViewController *footer = [_TtC20NowPlaying_ModesImpl18FooterElementsUnit new];footer.view = footerView;+ UIViewController *playback = [_TtC20NowPlaying_ModesImpl28PlaybackControlsElementsUnit new];+ playback.view = controls;UIViewController *player = [_TtC19NowPlaying_ViewImpl24NowPlayingViewController new];player.view = host;- _units = @[info, duration, floating, footer, scrollUnit, player];+ _units = @[info, duration, floating, playback, footer, scrollUnit, background, player];+ _covers = covers;+ [self start];[self layOut];// Spotify lays its units out again as a track's elements arrive, which is what the redesign's// transforms and narrowed labels have to survive.@@ -288,8 +475,10 @@ static void loadLyrics(void) {NSLog(@"[harness] hit on the title: %@", NSStringFromClass([self.window hitTest:onTitle withEvent:nil].class));NSLog(@"[harness] hit on the lines: %@", NSStringFromClass([self.window hitTest:onLines withEvent:nil].class));});+ if ([scenario() isEqualToString:@"artwork"]) [self runArtworkChecks];+ else if ([scenario() isEqualToString:@"look"]) [self runLook];// Opened, closed and opened again, so a screenshot can be taken of each state and of the move itself.- for (NSNumber *at in @[@2, @6, @10]) {+ else for (NSNumber *at in @[@2, @6, @10]) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(at.doubleValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{NSLog(@"[harness] %@ the lyrics", SGRPlayerLyricsOpen() ? @"closing" : @"opening");SGRPlayerToggleLyrics();@@ -302,11 +491,144 @@ static void loadLyrics(void) {for (UIViewController *unit in _units) [unit viewDidLayoutSubviews];}+#pragma mark - tracks++static NSString *imageURI(NSString *digits) {+ // 16 digits of size, then the picture's own 24 (padded here from a short name).+ NSString *hash = [[digits stringByPaddingToLength:24 withString:@"0" startingAtIndex:0] substringToIndex:24];+ return [@"spotify:image:ab67616d0000b273" stringByAppendingString:hash];+}++static UIViewController *unitOf(UIView *view) {+ UIResponder *next = view.nextResponder;+ return [next isKindOfClass:UIViewController.class] ? (UIViewController *)next : nil;+}++static void after(NSTimeInterval seconds, dispatch_block_t block) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);+}++// What Spotify's screens do when a track changes: the list of covers moves to the new cell and lays out,+// still showing whatever picture that cell had, and the bar's cover stays what it was, until the+// pictures load and are set on the image views -- which lays nothing out.+- (void)playTrack:(NSString *)uri image:(NSString *)image {+ NSLog(@"[harness] track %@ (picture %@)", uri, [image substringFromIndex:image.length - 24]);+ SGRHarnessSetTrack(uri, image, NO);+ [_covers setNeedsLayout];+ [_covers layoutIfNeeded];+}++- (void)showOnScreen:(UIImage *)picture {+ _cover.image = picture;+ _barCover.image = picture;+}++- (void)start {+ BOOL checks = [scenario() isEqualToString:@"artwork"];+ UIImage *first = checks ? solid(UIColor.redColor) : _cover.image;+ [self showOnScreen:first];+ serve(imageURI(@"aaaa"), first, 0.2, NO);+ [self playTrack:@"spotify:track:harnessA" image:imageURI(@"aaaa")];+ // The bar lays out once as the app comes up.+ [_bar viewDidLayoutSubviews];+}++- (void)check:(NSString *)step want:(NSString *)want {+ NSString *uri = nil;+ UIImage *artwork = SGRNowPlayingArtwork(&uri, NULL);+ UIImage *drawn = [SGRPlayerField() valueForKey:@"image"];+ NSString *kit = colorName(artwork), *field = colorName(drawn);+ BOOL ok = [kit isEqualToString:want] && [field isEqualToString:want];+ _checks++;+ if (!ok) _failures++;+ NSLog(@"[harness] check %@: want %@, the Kit has %@ (for %@), the field draws %@ -- %@", step, want, kit, uri, field, ok ? @"ok" : @"WRONG");+}++// Issue #58: after a switch to another album the field kept the last one's picture until the next track.+- (void)runArtworkChecks {+ UIImage *green = solid(UIColor.greenColor), *blue = solid(UIColor.blueColor), *yellow = solid(UIColor.yellowColor),+ *magenta = solid(UIColor.magentaColor);+ after(1.5, ^{ [self check:@"1 first track" want:@"red"]; });++ // Another album: the screens go on showing the last picture for 3.5 s, well past the Kit's last look+ // at them, and the picture server answers in 1 s.+ after(2, ^{+ serve(imageURI(@"bbbb"), green, 1.0, NO);+ [self playTrack:@"spotify:track:harnessB" image:imageURI(@"bbbb")];+ });+ after(5.5, ^{ [self showOnScreen:green]; });+ after(7, ^{ [self check:@"2 album switch, the screens late" want:@"green"]; });++ // Two tracks in quick succession, the first one's picture answering last.+ after(7.5, ^{+ serve(imageURI(@"cccc"), blue, 2.0, NO);+ [self playTrack:@"spotify:track:harnessC" image:imageURI(@"cccc")];+ });+ after(7.7, ^{ [self showOnScreen:blue]; });+ after(7.8, ^{+ serve(imageURI(@"dddd"), yellow, 0.3, NO);+ [self playTrack:@"spotify:track:harnessD" image:imageURI(@"dddd")];+ });+ after(8.1, ^{ [self showOnScreen:yellow]; });+ after(11, ^{ [self check:@"3 skip twice, the older answer last" want:@"yellow"]; });++ // Offline: the server fails, and the screens show the new picture 0.8 s after the change.+ after(11.5, ^{+ serve(imageURI(@"eeee"), magenta, 0.1, YES);+ [self playTrack:@"spotify:track:harnessE" image:imageURI(@"eeee")];+ });+ after(12.3, ^{ [self showOnScreen:magenta]; });+ after(14.5, ^{ [self check:@"4 offline, the screens only" want:@"magenta"]; });++ after(15, ^{+ NSLog(@"[harness] artwork checks: %lu of %lu right -- %@", (unsigned long)(self->_checks - self->_failures), (unsigned long)self->_checks,+ self->_failures ? @"FAIL" : @"PASS");+ });+}++// One track, then another album's at 8 s, its picture on the screens 0.4 s later.+- (void)runLook {+ UIImage *second = secondArtwork();+ // Issue #54: the footer row moved down past the bottom stack's bounds must still take its touches,+ // at the bottom edge of each glyph as well as the middle.+ after(3, ^{+ CGFloat bottom = 0;+ for (NSString *symbol in @[@"list.bullet", @"airpods.pro"]) {+ __block UIView *found = nil;+ NSMutableArray *queue = [NSMutableArray arrayWithObject:self.window];+ while (queue.count && !found) {+ UIView *v = queue.firstObject;+ [queue removeObjectAtIndex:0];+ if ([v isKindOfClass:UIImageView.class] && [((UIImageView *)v).image isEqual:[UIImage systemImageNamed:symbol]]) found = v;+ [queue addObjectsFromArray:v.subviews];+ }+ CGRect drawn = [found convertRect:found.bounds toView:self.window];+ bottom = CGRectGetMaxY(drawn);+ for (NSNumber *dy in @[@0, @8]) {+ CGPoint at = CGPointMake(CGRectGetMidX(drawn), CGRectGetMidY(drawn) + dy.doubleValue);+ UIView *hit = [self.window hitTest:at withEvent:nil];+ BOOL inRow = NO;+ for (UIView *v = hit; v; v = v.superview) inRow = inRow || [NSStringFromClass(unitOf(v).class) containsString:@"FooterElementsUnit"] || [v.accessibilityIdentifier isEqualToString:@"QueueButtonNowPlaying"];+ NSLog(@"[harness] touch on %@ at y %.0f (+%.0f): %@ -- %@", symbol, at.y, dy.doubleValue, NSStringFromClass(hit.class), inRow ? @"the footer's" : @"MISSED");+ }+ }+ NSLog(@"[harness] footer glyphs end at %.0f of %.0f", bottom, self.window.bounds.size.height);+ });+ after(8, ^{+ serve(imageURI(@"ffff"), second, 0.25, NO);+ [self playTrack:@"spotify:track:harnessF" image:imageURI(@"ffff")];+ });+ after(8.4, ^{ [self showOnScreen:second]; });+}+@end-// Before every %ctor, so the redesign's gate reads on.+// Before every %ctor, so the redesign's gate reads on, and every session gets the picture server.__attribute__((constructor(101))) static void sgr_harnessDefaults(void) {[NSUserDefaults.standardUserDefaults setBool:YES forKey:@"spotifyglass.redesign"];+ Method original = class_getClassMethod(NSURLSessionConfiguration.class, @selector(defaultSessionConfiguration));+ Method harness = class_getClassMethod(NSURLSessionConfiguration.class, @selector(sgr_harnessDefault));+ method_exchangeImplementations(original, harness);}int main(int argc, char *argv[]) {diff --git a/harness/player/stubs.m b/harness/player/stubs.mindex ce0b43dc..f1c7790e 100644--- a/harness/player/stubs.m+++ b/harness/player/stubs.m@@ -1,9 +1,11 @@-// What the player harness does not compile: the Kit's hooks (SGRAccent.x, SGRRepaint.x, SGRBridges.x),-// the rest of the player's hooks, the lyrics store and the haptics. Everything here answers the way the-// phone would for one paused track with lyrics, so the redesign's own code is what is being looked at.+// What the player harness does not compile: the Kit's hooks (SGRAccent.x, SGRRepaint.x), the rest of+// the player's hooks, the lyrics store and the haptics. Everything here answers the way the phone would+// for one track with lyrics, so the redesign's own code is what is being looked at. The player itself+// is a mock the harness drives: SGRHarnessSetTrack reports a track change to every observer.#import <UIKit/UIKit.h>#import "Shared/Lyrics/Lyrics.h"#import "Headers/SPTPlayer.h"+#import "Shared/Player/PlayerState.h"#pragma mark - SGRAccent.x, SGRRepaint.x@@ -26,22 +28,47 @@ NSString *SGURIString(id uri) {if ([uri isKindOfClass:NSURL.class]) return ((NSURL *)uri).absoluteString;return nil;}-void SGAddPlayerStateObserver(id observer) {}-SPTPlayerState *SGPlayerState(void) { return nil; }--#pragma mark - Redesigned/Kit/SGRBridges.x--NSNotificationName const SGRNowPlayingArtworkDidChangeNotification = @"spotifyglass.redesign.artwork";-static UIImage *sg_artwork;-void SGRSetNowPlayingArtwork(UIImage *image, NSString *trackURI, NSInteger quality) { sg_artwork = image; }-UIImage *SGRNowPlayingArtwork(NSString **trackURI, NSString **identity) {- if (trackURI) *trackURI = @"spotify:track:harness";- if (identity) *identity = @"harness";- return sg_artwork;++// A player that plays what the harness tells it to, reporting on the main thread the way+// Shared/Player/PlayerState.x moves Spotify's reports onto it.+@implementation SPTPlayerTrack+@end+@implementation SPTPlayerOptions+@end+@implementation SPTPlayerState+@end++static NSHashTable *sg_observers;+static SPTPlayerState *sg_state;++void SGAddPlayerStateObserver(id observer) {+ if (!sg_observers) sg_observers = [NSHashTable weakObjectsHashTable];+ [sg_observers addObject:observer];+}+SPTPlayerState *SGPlayerState(void) { return sg_state; }++// `imageURI` is the xlarge picture as Spotify's metadata has it, spotify:image:<40 hex digits>, or nil for+// a track whose metadata names none.+void SGRHarnessSetTrack(NSString *uri, NSString *imageURI, BOOL paused) {+ SPTPlayerTrack *track = [SPTPlayerTrack new];+ [track setValue:uri forKey:@"URI"];+ NSMutableDictionary *metadata = [@{@"title": uri} mutableCopy];+ if (imageURI) {+ // The four sizes share the picture's last 24 digits (out/karaoke-diag.log:174-177 on the phone).+ NSString *hash = [imageURI substringFromIndex:imageURI.length - 24];+ metadata[@"image_xlarge_url"] = imageURI;+ metadata[@"image_large_url"] = imageURI;+ metadata[@"image_url"] = [@"spotify:image:ab67616d00001e02" stringByAppendingString:hash];+ metadata[@"image_small_url"] = [@"spotify:image:ab67616d00004851" stringByAppendingString:hash];+ }+ [track setValue:metadata forKey:@"metadata"];+ SPTPlayerState *state = [SPTPlayerState new];+ [state setValue:track forKey:@"track"];+ [state setValue:@(paused) forKey:@"isPaused"];+ [state setValue:@(!paused) forKey:@"isPlaying"];+ sg_state = state;+ for (id<SGPlayerStateObserver> observer in sg_observers.allObjects) [observer playerStateDidChange:state];}-void SGRHarnessSetArtwork(UIImage *image) { sg_artwork = image; }-BOOL SGRPlayerIsTransitioning(void) { return NO; }-void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)(id owner)) {}#pragma mark - Redesigned/Player/PlayerControls.xdiff --git a/harness/playlist/build.sh b/harness/playlist/build.shindex eb90ec30..646efe39 100755--- a/harness/playlist/build.sh+++ b/harness/playlist/build.sh@@ -17,7 +17,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$OUT"/gen/*.m \"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \- "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m \+ "$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRPalette.m "$SRC"/Redesigned/Kit/SGRField.m "$SRC"/Redesigned/Kit/SGRFlow.m \"$SRC"/Redesigned/Kit/SGRGlass.m "$SRC"/Redesigned/Kit/SGRGlyph.m "$SRC"/Redesigned/Kit/SGRRestyle.m \"$SRC"/Redesigned/Kit/SGRActionRow.m "$SRC"/Redesigned/Kit/SGRDownload.m "$SRC"/Redesigned/Kit/SGRHeaderInfo.m \"$SRC"/Redesigned/Kit/SGRedesign.m \diff --git a/harness/playlist/stubs.m b/harness/playlist/stubs.mindex 52541606..f6f16798 100644--- a/harness/playlist/stubs.m+++ b/harness/playlist/stubs.m@@ -6,3 +6,7 @@ __weak UIView *sgr_nowPlayingCard = nil;__weak UIView *sgr_lyricsCardRoot = nil;__weak UIView *sgr_lyricsPageRoot = nil;__weak UIView *sgr_playlistRoot = nil;++// Redesigned/Kit/SGRBridges.x: SGRField.m asks whether the player is opening or closing for its moving field.+BOOL SGRPlayerIsTransitioning(void) { return NO; }+void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)(id owner)) {}diff --git a/tweak/Sources/Redesigned/Kit/SGRBridges.h b/tweak/Sources/Redesigned/Kit/SGRBridges.hindex 37fa6fcf..8649dcd2 100644--- a/tweak/Sources/Redesigned/Kit/SGRBridges.h+++ b/tweak/Sources/Redesigned/Kit/SGRBridges.h@@ -2,9 +2,19 @@// hooks the player a second time.//// Player state is Shared/Player/PlayerState.h's, imported here so a redesigned screen needs one header.-// Artwork: the now playing bar's 40pt cover (trees/clean/artist/01.txt: id=SPTNowPlayingBar > Encore.ImageView >-// UIImageView 40x40), read after the bar's layout and after a track change; installed while the player-// is redesigned. Screens publish better copies of their own (the player's cover).+//+// Artwork is keyed on the picture the playing track names, not on the track or on where it was read+// (issue #58). The track's metadata carries its cover's image id (image_xlarge_url and its smaller+// sizes, spotify:image:<16 digits of size><24 digits of the picture>, out/karaoke-diag.log:174); two+// tracks of one album share it. On a change of picture the Kit fetches that picture itself, from+// i.scdn.co where Spotify's images live, and the answer is published whenever it lands, however late,+// unless the track has moved on to another picture meanwhile. Until it lands, and when it cannot+// (offline), the covers on screen stand in: the now playing bar's 40pt cover (trees/clean/artist/01.txt:+// id=SPTNowPlayingBar > Encore.ImageView > UIImageView 40x40), read after the bar's layout and a few+// times after a track change, and the player's own cover, which PlayerField.x publishes. A view read+// can be the last track's picture still -- a cell of the covers list that has not reloaded, a bar whose+// new picture is still loading -- so every picture published is remembered, weakly, with the picture it+// was published as, and a view showing one of them for another picture is not believed.//// Threading: everything here is main thread only; the player's reports are moved onto it.#import <UIKit/UIKit.h>@@ -14,15 +24,19 @@#pragma mark - now playing artworktypedef NS_ENUM(NSInteger, SGRArtworkQuality) {- SGRArtworkQualityLow, // the now playing bar's 40pt cover- SGRArtworkQualityHigh, // the player's own cover+ SGRArtworkQualityLow, // the now playing bar's 40pt cover+ SGRArtworkQualityHigh, // the player's own cover+ SGRArtworkQualityExact, // the picture the track's metadata names, fetched by the Kit};// Posted when the artwork changes, object nil, userInfo image, trackURI and quality.extern NSNotificationName const SGRNowPlayingArtworkDidChangeNotification;-// A lower quality image for the track already published is ignored, as is the same image again.+// A picture read off a screen for `trackURI`: ignored unless that track is still the one playing, when+// it is a picture already published for another track, when a better one is in for this picture, and+// when it is the same image again.void SGRSetNowPlayingArtwork(UIImage *image, NSString *trackURI, SGRArtworkQuality quality);-// The last artwork published; `trackURI` and `identity` (URI and quality, for-// -[SGRArtworkField setArtwork:identity:animated:]) are filled when asked for.+// The last artwork published; `trackURI` and `identity` (for -[SGRArtworkField setArtwork:identity:+// animated:]: the same identity is the same picture) are filled when asked for. Until the playing+// track's own picture is in, this is the last one.UIImage *SGRNowPlayingArtwork(NSString **trackURI, NSString **identity);#pragma mark - the player's open and closediff --git a/tweak/Sources/Redesigned/Kit/SGRBridges.x b/tweak/Sources/Redesigned/Kit/SGRBridges.xindex 60eb1b60..2310c825 100644--- a/tweak/Sources/Redesigned/Kit/SGRBridges.x+++ b/tweak/Sources/Redesigned/Kit/SGRBridges.x@@ -11,38 +11,179 @@NSNotificationName const SGRNowPlayingArtworkDidChangeNotification = @"spotifyglass.redesign.nowPlayingArtworkDidChange";+// The picture published, the track it was published for, the picture it is (its key) and how it was had.static UIImage *sg_artwork;-static NSString *sg_artworkURI;+static NSString *sg_artworkURI, *sg_artworkKey;static SGRArtworkQuality sg_artworkQuality;+static NSUInteger sg_artworkSerial;+// The playing track and the picture its metadata names.+static NSString *sg_wantedURI, *sg_wantedKey;+// Every picture published, held weakly, with the key it was published as. A view still showing one of+// these while another picture is wanted is showing the last track's.+static NSMapTable<UIImage *, NSString *> *sg_published;+static NSURLSessionDataTask *sg_fetch;-void SGRSetNowPlayingArtwork(UIImage *image, NSString *trackURI, SGRArtworkQuality quality) {- if (!image) return;- BOOL sameTrack = trackURI ? [trackURI isEqualToString:sg_artworkURI] : !sg_artworkURI;- if (sameTrack && (image == sg_artwork || quality < sg_artworkQuality)) return;+// Retries of a fetch that failed, and how long before each.+static const NSTimeInterval kFetchRetry = 2;+static const NSUInteger kFetchAttempts = 2;++static void artworkLog(NSString *format, ...) NS_FORMAT_FUNCTION(1, 2);+static void artworkLog(NSString *format, ...) {+ static NSUInteger logged;+ if (logged++ >= 40) return;+ va_list args;+ va_start(args, format);+ SGLog(@"redesign kit: artwork %@", [[NSString alloc] initWithFormat:format arguments:args]);+ va_end(args);+}++// The image id in a metadata value: spotify:image:<id>, or an https URL ending in it.+static NSString *imageIDIn(id value) {+ if (![value isKindOfClass:NSString.class] || ![value length]) return nil;+ NSString *string = value;+ if ([string hasPrefix:@"spotify:image:"]) return [string substringFromIndex:@"spotify:image:".length];+ if ([string hasPrefix:@"https://"]) return string.lastPathComponent;+ return nil;+}++// The picture a track names, largest first, and where it can be fetched from.+static NSString *pictureOf(SPTPlayerTrack *track, NSURL **url) {+ NSDictionary *metadata = [track respondsToSelector:@selector(metadata)] ? track.metadata : nil;+ if (![metadata isKindOfClass:NSDictionary.class]) return nil;+ for (NSString *field in @[@"image_xlarge_url", @"image_large_url", @"image_url", @"image_small_url"]) {+ NSString *value = metadata[field], *identifier = imageIDIn(value);+ if (!identifier) continue;+ if (url) *url = [value hasPrefix:@"https://"] ? [NSURL URLWithString:value]+ : [NSURL URLWithString:[@"https://i.scdn.co/image/" stringByAppendingString:identifier]];+ // Every size of one picture shares its last 24 digits; the first 16 are the size.+ return identifier.length == 40 ? [identifier substringFromIndex:16] : identifier;+ }+ return nil;+}++static void publish(UIImage *image, NSString *key, SGRArtworkQuality quality) {sg_artwork = image;- sg_artworkURI = [trackURI copy];+ sg_artworkURI = sg_wantedURI;+ sg_artworkKey = key;sg_artworkQuality = quality;+ sg_artworkSerial++;+ [sg_published setObject:key forKey:image];[NSNotificationCenter.defaultCenter postNotificationName:SGRNowPlayingArtworkDidChangeNotification object:nil userInfo:@{@"image": image,- @"trackURI": trackURI ?: @"",+ @"trackURI": sg_wantedURI ?: @"",@"quality": @(quality),}];}+static NSURLSession *artworkSession(void) {+ static NSURLSession *session;+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.defaultSessionConfiguration;+ // An image id is the picture's own digest, so a stored answer never goes stale.+ NSURL *caches = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject;+ configuration.URLCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:24 * 1024 * 1024+ directoryURL:[caches URLByAppendingPathComponent:@"spotifyglass-artwork"]];+ configuration.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;+ configuration.timeoutIntervalForRequest = 20;+ session = [NSURLSession sessionWithConfiguration:configuration];+ });+ return session;+}++static void fetchPicture(NSString *key, NSURL *url, NSUInteger attempt) {+ CFTimeInterval started = CACurrentMediaTime();+ sg_fetch = [artworkSession() dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;+ UIImage *image = data.length && status == 200 ? [UIImage imageWithData:data] : nil;+ // Decoded here, off the main thread, rather than on the first frame that draws it.+ image = image.imageByPreparingForDisplay ?: image;+ dispatch_async(dispatch_get_main_queue(), ^{+ if (error.code == NSURLErrorCancelled) return;+ // A newer track asked for another picture meanwhile: this one would put the old one back.+ if (![key isEqualToString:sg_wantedKey]) {+ artworkLog(@"%@ came after the track moved on to %@, dropped", key, sg_wantedKey);+ return;+ }+ if (!image) {+ artworkLog(@"%@ not fetched (%ld, %@), attempt %lu", key, (long)status, error.localizedDescription, (unsigned long)attempt + 1);+ if (attempt + 1 >= kFetchAttempts) return;+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kFetchRetry * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ if ([key isEqualToString:sg_wantedKey] && !([sg_artworkKey isEqualToString:key] && sg_artworkQuality == SGRArtworkQualityExact)) {+ fetchPicture(key, url, attempt + 1);+ }+ });+ return;+ }+ artworkLog(@"%@ fetched %.0fx%.0f in %.0f ms", key, image.size.width, image.size.height, (CACurrentMediaTime() - started) * 1000);+ publish(image, key, SGRArtworkQualityExact);+ });+ }];+ [sg_fetch resume];+}++// Brings the playing track's picture up to date with the player's state, and fetches a picture newly+// wanted. Called on every state report and before any view read is believed, whichever comes first.+static void followPlayer(void) {+ SPTPlayerTrack *track = SGPlayerState().track;+ NSString *uri = SGURIString(track.URI);+ if (!uri) return;+ // The same track is looked at again only while its metadata has named no picture yet.+ if ([uri isEqualToString:sg_wantedURI] && ![sg_wantedKey hasPrefix:@"track:"]) return;+ sg_wantedURI = uri;+ NSURL *url = nil;+ // A track whose metadata names no picture is its own key: only the screens can show it then.+ NSString *key = pictureOf(track, &url) ?: [@"track:" stringByAppendingString:uri];+ if ([key isEqualToString:sg_wantedKey]) return;+ sg_wantedKey = key;+ [sg_fetch cancel];+ sg_fetch = nil;+ if ([key isEqualToString:sg_artworkKey] && sg_artworkQuality == SGRArtworkQualityExact) return;+ if (url) fetchPicture(key, url, 0);+}++void SGRSetNowPlayingArtwork(UIImage *image, NSString *trackURI, SGRArtworkQuality quality) {+ if (!image || !trackURI) return;+ followPlayer();+ // Read for a track that is no longer playing.+ if (![trackURI isEqualToString:sg_wantedURI]) return;+ NSString *key = sg_wantedKey;+ NSString *publishedAs = [sg_published objectForKey:image];+ // The screen still shows a picture published for another one.+ if (publishedAs && ![publishedAs isEqualToString:key]) {+ artworkLog(@"%@ still on screen while %@ is wanted, not believed", publishedAs, key);+ return;+ }+ if ([key isEqualToString:sg_artworkKey] && (image == sg_artwork || quality < sg_artworkQuality)) return;+ publish(image, key, MIN(quality, SGRArtworkQualityHigh));+}+UIImage *SGRNowPlayingArtwork(NSString **trackURI, NSString **identity) {if (trackURI) *trackURI = sg_artworkURI;- if (identity) *identity = sg_artwork ? [NSString stringWithFormat:@"%@#%ld", sg_artworkURI ?: @"", (long)sg_artworkQuality] : nil;+ // A fetched picture is known for what it is; one read off a screen is only what it looked like then,+ // so each of those is a picture of its own and a field redraws it.+ if (identity) {+ *identity = !sg_artwork ? nil : sg_artworkQuality == SGRArtworkQualityExact+ ? [sg_artworkKey stringByAppendingString:@"#exact"]+ : [NSString stringWithFormat:@"%@#%lu", sg_artworkKey, (unsigned long)sg_artworkSerial];+ }return sg_artwork;}-static const CFTimeInterval kBarLead = 0.5;+// The player's reports, in the order they come.+@interface SGRArtworkFollower : NSObject <SGPlayerStateObserver>+@end++@implementation SGRArtworkFollower+- (void)playerStateDidChange:(SPTPlayerState *)state {+ followPlayer();+}+@end++static SGRArtworkFollower *sg_follower;+static char kBarCardKey, kBarImageKey;static __weak UIView *sg_barView;-// The bar's picture and when it appeared, and when the track last changed: a picture that was there-// well before the change is the last track's cover still, while one that turned up just before it-// was set by the same state the Kit is told about a moment later.-static __weak UIImage *sg_barImage;-static CFTimeInterval sg_barImageSince, sg_trackChangedAt;static void publishBarArtwork(void) {UIView *card = SGRFindByIdentifier(sg_barView, @"SPTNowPlayingBar", &kBarCardKey);@@ -54,16 +195,11 @@ static void publishBarArtwork(void) {UIImage *image = cover.image;NSString *uri = SGURIString(SGPlayerState().track.URI);if (!image || !uri) return;- if (image != sg_barImage) {- sg_barImage = image;- sg_barImageSince = CACurrentMediaTime();- }- if (sg_barImageSince < sg_trackChangedAt - kBarLead) return;SGRSetNowPlayingArtwork(image, uri, SGRArtworkQualityLow);}// The bar sets its picture when the image has loaded, which lays nothing out, so a track change looks-// again a few times while the picture comes in.+// again a few times while the picture comes in. One that loads later still is the fetch's to bring.@interface SGRBarArtworkWatcher : NSObject <SGPlayerStateObserver>@end@@ -74,8 +210,6 @@ static void publishBarArtwork(void) {- (void)playerStateDidChange:(SPTPlayerState *)state {NSString *track = SGURIString(state.track.URI);if (!track || [track isEqualToString:_track]) return;- // The first track of the launch has no last cover to mistake for its own.- if (_track) sg_trackChangedAt = CACurrentMediaTime();_track = track;for (NSNumber *delay in @[@0, @0.3, @1, @2.5]) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay.doubleValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ publishBarArtwork(); });@@ -143,6 +277,12 @@ void SGRObservePlayerTransition(id owner, void (^began)(id owner), void (^ended)%ctor {if (!SGRedesignedUI()) return;+ // By pointer: two images of one picture are still two reads.+ sg_published = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPointerPersonality+ valueOptions:NSPointerFunctionsStrongMemory capacity:16];+ // Ahead of the watchers, so the picture a track wants is known before any screen is read for it.+ sg_follower = [SGRArtworkFollower new];+ SGAddPlayerStateObserver(sg_follower);sg_barWatcher = [SGRBarArtworkWatcher new];SGAddPlayerStateObserver(sg_barWatcher);%init(SGRBarArtworkHooks);diff --git a/tweak/Sources/Redesigned/Kit/SGRField.h b/tweak/Sources/Redesigned/Kit/SGRField.hindex 3f863249..4b7110f3 100644--- a/tweak/Sources/Redesigned/Kit/SGRField.h+++ b/tweak/Sources/Redesigned/Kit/SGRField.h@@ -21,6 +21,13 @@ extern NSNotificationName const SGRFieldColorDidChangeNotification;// that does not clip): positive values draw outside. The field never clips.@property (nonatomic) UIEdgeInsets bleed;@property (nonatomic) BOOL showsBackdrop;+// The player's moving field instead of the still backdrop (SGRFlow.h): the artwork's colours drifting+// over the whole of the bounds, with no fade to black. It moves only while the field is in a window,+// the app is in front, the player is not opening or closing, Reduce Motion and Low Power Mode are off+// and nothing holds it (motionHeld); otherwise it stays still where it was.+@property (nonatomic) BOOL flows;+// Held still by the owner (the player while playback is paused).+@property (nonatomic) BOOL motionHeld;// The backdrop's height in points from the top of the bounds; 0 is the window's height.@property (nonatomic) CGFloat backdropHeight;// SGRNeutralField until a colour arrives.diff --git a/tweak/Sources/Redesigned/Kit/SGRField.m b/tweak/Sources/Redesigned/Kit/SGRField.mindex ad9fbedc..4c9062c3 100644--- a/tweak/Sources/Redesigned/Kit/SGRField.m+++ b/tweak/Sources/Redesigned/Kit/SGRField.m@@ -1,5 +1,7 @@#import "Core/SGCore.h"#import "SGRField.h"+#import "SGRFlow.h"+#import "SGRBridges.h"#import "SGRPalette.h"#import "SGRTokens.h"@@ -32,6 +34,8 @@ static NSDictionary *noActions(void) {CALayer *_solid;CAGradientLayer *_black;CALayer *_backdrop;+ SGRFlowLayer *_flow;+ BOOL _watching;UIColor *_color;UIColor *_preferred; // the page's own colour, made fit; wins over the artwork'sUIImage *_image;@@ -97,9 +101,72 @@ static NSDictionary *noActions(void) {_black.frame = painted;_black.locations = @[@(MIN(1, from / total)), @(MIN(1, to / total))];_backdrop.frame = CGRectMake(0, 0, bounds.size.width, [self backdropHeightNow]);+ _flow.frame = CGRectMake(0, 0, bounds.size.width, [self backdropHeightNow]);[CATransaction commit];}+#pragma mark - the moving field++- (void)setFlows:(BOOL)flows {+ if (flows == _flows) return;+ _flows = flows;+ if (flows && !_flow) {+ _flow = [SGRFlowLayer layer];+ _flow.hidden = YES;+ [self.layer insertSublayer:_flow above:_solid];+ }+ // The moving field is the whole picture: no still backdrop over it, no fade to black under it.+ _black.hidden = flows;+ if (flows) _backdrop.hidden = YES;+ else _flow.hidden = YES;+ [self watch];+ [self updateMotion];+ [self setNeedsLayout];+}++- (void)setMotionHeld:(BOOL)held {+ if (held == _motionHeld) return;+ _motionHeld = held;+ [self updateMotion];+}++- (void)watch {+ if (_watching || !_flows) return;+ _watching = YES;+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;+ for (NSNotificationName name in @[UIApplicationDidBecomeActiveNotification, UIApplicationWillResignActiveNotification,+ NSProcessInfoPowerStateDidChangeNotification, UIAccessibilityReduceMotionStatusDidChangeNotification]) {+ [center addObserver:self selector:@selector(updateMotionSoon) name:name object:nil];+ }+ SGRObservePlayerTransition(self, ^(id owner) { [owner updateMotion]; }, ^(id owner) { [owner updateMotion]; });+}++- (void)dealloc {+ [NSNotificationCenter.defaultCenter removeObserver:self];+}++// The power state is reported off the main thread.+- (void)updateMotionSoon {+ dispatch_async(dispatch_get_main_queue(), ^{ [self updateMotion]; });+}++- (void)updateMotion {+ if (!_flow) return;+ // A locked phone keeps the player in its window, so being in front counts as much as being in one.+ BOOL front = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;+ BOOL moving = _flows && !_flow.hidden && self.window && front && !SGRPlayerIsTransitioning() && !SGRReduceMotion()+ && !NSProcessInfo.processInfo.lowPowerModeEnabled && !_motionHeld;+ if (moving == _flow.moving) return;+ _flow.moving = moving;+ static NSUInteger logged;+ if (logged++ < 6) SGLog(@"redesign kit: moving field %@", moving ? @"moves" : @"holds still");+}++- (void)didMoveToWindow {+ [super didMoveToWindow];+ [self updateMotion];+}+- (void)applyColor:(UIColor *)color animated:(BOOL)animated {if (!color || CGColorEqualToColor(color.CGColor, _color.CGColor)) return;CALayer *shown = _solid.presentationLayer ?: _solid;@@ -139,8 +206,8 @@ static NSDictionary *noActions(void) {_image = image;_identity = [identity copy];NSUInteger generation = ++_generation;- SGRPaletteRequest request = {CGSizeZero, NO, YES};- if (_showsBackdrop) request.backdropSize = CGSizeMake(self.bounds.size.width > 0 ? self.bounds.size.width : 402, [self backdropHeightNow]);+ SGRPaletteRequest request = {CGSizeZero, NO, YES, _flows};+ if (_showsBackdrop && !_flows) request.backdropSize = CGSizeMake(self.bounds.size.width > 0 ? self.bounds.size.width : 402, [self backdropHeightNow]);__weak SGRArtworkField *weakSelf = self;[SGRPalette paletteForImage:image request:request completion:^(SGRPalette *palette) {SGRArtworkField *field = weakSelf;@@ -151,7 +218,15 @@ static NSDictionary *noActions(void) {- (void)applyPalette:(SGRPalette *)palette animated:(BOOL)animated {_read = YES;- if (_showsBackdrop && palette.backdrop) {+ if (_flows && palette.flowColors) {+ [_flow setColors:palette.flowColors animated:animated && !_flow.hidden];+ _flow.hidden = NO;+ [self updateMotion];+ // Past the moving field's edges (the pull that dismisses the player) the colour under it goes on.+ [self applyColor:_preferred ?: _flow.baseColor animated:animated];+ return;+ }+ if (_showsBackdrop && !_flows && palette.backdrop) {[CATransaction begin];[CATransaction setDisableActions:YES];if (animated) {diff --git a/tweak/Sources/Redesigned/Kit/SGRFlow.h b/tweak/Sources/Redesigned/Kit/SGRFlow.hnew file mode 100644index 00000000..973095df--- /dev/null+++ b/tweak/Sources/Redesigned/Kit/SGRFlow.h@@ -0,0 +1,26 @@+// A moving field of the artwork's colours, the Music app's animated background behind its player+// (issue #59): five soft discs of colour, one for each quarter of the artwork and one for the whole of+// it, each drifting on a slow loop of its own and breathing in size, over the artwork's main colour and+// under a shade that deepens towards the bottom, where the controls are.+//+// All of it is Core Animation: radial gradient layers moved by repeating animations that the render+// server plays, so the app itself does no work per frame -- no display link, no timer, nothing redrawn+// -- and a paused layer tree costs nothing at all. The animations ask for 30 frames a second (the discs+// are soft and slow; more would only spend the GPU) and allow up to 60, so they can fall in with other+// sources rather than holding them down.+//+// Stopping holds every disc where it is and starting again carries on from there, so the owner can stop+// it as often as it likes (the player's transition, the app leaving the screen) without a jump.+//+// Threading: main thread only.+#import <UIKit/UIKit.h>++@interface SGRFlowLayer : CALayer+// Five colours, as -[SGRPalette flowColors] gives them: top left, top right, bottom left, bottom right,+// the whole artwork's. Animated, they blend into the new ones where the discs are.+- (void)setColors:(NSArray<UIColor *> *)colors animated:(BOOL)animated;+// The colour under the discs, darker than the artwork's main one; nil before any colours are set.+@property (nonatomic, readonly) UIColor *baseColor;+// Drifting, or held still where it is. Off to begin with.+@property (nonatomic) BOOL moving;+@enddiff --git a/tweak/Sources/Redesigned/Kit/SGRFlow.m b/tweak/Sources/Redesigned/Kit/SGRFlow.mnew file mode 100644index 00000000..74359735--- /dev/null+++ b/tweak/Sources/Redesigned/Kit/SGRFlow.m@@ -0,0 +1,217 @@+#import "SGRFlow.h"++enum { kDiscs = 5 };++// Each disc: where it rests, as shares of the layer (top left, top right, bottom left, bottom right,+// middle), its diameter as a share of the layer's longer side, the loop it drifts on (radii as shares of+// width and height), how long one loop takes and which way it runs, and how long a breath takes.+// The periods share no factor, so the picture they make together does not come round again for hours.+typedef struct {+ CGFloat x, y, diameter, driftX, driftY;+ CFTimeInterval loop;+ BOOL clockwise;+ CFTimeInterval breath;+} SGRDisc;++static const SGRDisc kDisc[kDiscs] = {+ {0.22, 0.20, 0.80, 0.26, 0.10, 29, YES, 13},+ {0.80, 0.28, 0.75, 0.22, 0.12, 37, NO, 17},+ {0.20, 0.74, 0.78, 0.24, 0.11, 31, NO, 19},+ {0.78, 0.84, 0.80, 0.26, 0.09, 41, YES, 11},+ {0.50, 0.50, 0.75, 0.18, 0.14, 53, YES, 23},+};+// How far a breath takes a disc's size either way, and how long new colours take to blend in.+static const CGFloat kBreathSmall = 0.85, kBreathLarge = 1.18;+static const CFTimeInterval kRecolor = 1.2;+// The colour under the discs is the artwork's main colour at this share of its light.+static const CGFloat kBaseShade = 0.55;+// The shade over everything: clear down to this share of the height, then this much black at the bottom.+static const CGFloat kShadeFrom = 0.45, kShadeBottom = 0.35;++static NSDictionary *noActions(void) {+ static NSDictionary *none;+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ NSNull *off = NSNull.null;+ none = @{@"bounds": off, @"position": off, @"frame": off, @"transform": off, @"backgroundColor": off,+ @"colors": off, @"hidden": off, @"opacity": off};+ });+ return none;+}++static UIColor *shaded(UIColor *color, CGFloat share) {+ CGFloat r = 0, g = 0, b = 0, a = 1;+ if (![color getRed:&r green:&g blue:&b alpha:&a]) return color;+ return [UIColor colorWithRed:r * share green:g * share blue:b * share alpha:1];+}++static NSArray *discColors(UIColor *color) {+ return @[(id)color.CGColor, (id)[color colorWithAlphaComponent:0.55].CGColor, (id)[color colorWithAlphaComponent:0].CGColor];+}++@implementation SGRFlowLayer {+ CALayer *_base;+ CAGradientLayer *_discs[kDiscs];+ CAGradientLayer *_shade;+ CGSize _laidOutFor;+ // Seconds of drift so far, and since when it has been running.+ CFTimeInterval _elapsed, _since;+ BOOL _moving;+ UIColor *_baseColor;+}++- (instancetype)init {+ if (!(self = [super init])) return nil;+ self.actions = noActions();+ _base = [CALayer layer];+ _base.actions = noActions();+ [self addSublayer:_base];+ for (int i = 0; i < kDiscs; i++) {+ CAGradientLayer *disc = [CAGradientLayer layer];+ disc.actions = noActions();+ disc.type = kCAGradientLayerRadial;+ disc.startPoint = CGPointMake(0.5, 0.5);+ disc.endPoint = CGPointMake(1, 1);+ disc.locations = @[@0, @0.45, @1];+ disc.colors = discColors(UIColor.clearColor);+ // The whole artwork's colour lies under the four quarters', so it fills between them rather than over them.+ if (i == kDiscs - 1) [self insertSublayer:disc above:_base];+ else [self addSublayer:disc];+ _discs[i] = disc;+ }+ _shade = [CAGradientLayer layer];+ _shade.actions = noActions();+ _shade.colors = @[(id)[UIColor colorWithWhite:0 alpha:0].CGColor, (id)[UIColor colorWithWhite:0 alpha:kShadeBottom].CGColor];+ _shade.locations = @[@(kShadeFrom), @1];+ [self addSublayer:_shade];+ return self;+}++- (UIColor *)baseColor {+ return _baseColor;+}++- (BOOL)moving {+ return _moving;+}++#pragma mark - where the discs are++- (void)layoutSublayers {+ [super layoutSublayers];+ CGRect bounds = self.bounds;+ _base.frame = bounds;+ _shade.frame = bounds;+ if (CGSizeEqualToSize(bounds.size, _laidOutFor) || bounds.size.width < 1 || bounds.size.height < 1) return;+ _laidOutFor = bounds.size;+ BOOL moving = _moving;+ if (moving) [self stop];+ CGFloat side = MAX(bounds.size.width, bounds.size.height);+ for (int i = 0; i < kDiscs; i++) {+ CGFloat diameter = round(side * kDisc[i].diameter);+ _discs[i].bounds = CGRectMake(0, 0, diameter, diameter);+ _discs[i].position = [self restOf:i];+ _discs[i].transform = CATransform3DIdentity;+ }+ if (moving) [self start];+}++- (CGPoint)restOf:(int)i {+ CGSize size = self.bounds.size;+ return CGPointMake(size.width * kDisc[i].x, size.height * kDisc[i].y);+}++// The disc's loop: an ellipse about its resting place, passing through it at the start.+- (CGPathRef)newLoopOf:(int)i {+ CGSize size = self.bounds.size;+ CGPoint rest = [self restOf:i];+ CGFloat rx = size.width * kDisc[i].driftX, ry = size.height * kDisc[i].driftY;+ CGMutablePathRef path = CGPathCreateMutable();+ // Round about a centre one radius to the side, so the loop starts and ends at the resting place.+ CGPathAddArc(path, NULL, 0, 0, 1, M_PI, M_PI + (kDisc[i].clockwise ? -2 : 2) * M_PI, kDisc[i].clockwise);+ CGAffineTransform place = CGAffineTransformMake(rx, 0, 0, ry, rest.x + rx, rest.y);+ CGPathRef loop = CGPathCreateCopyByTransformingPath(path, &place);+ CGPathRelease(path);+ return loop;+}++#pragma mark - moving++- (void)setMoving:(BOOL)moving {+ if (moving == _moving) return;+ _moving = moving;+ if (moving) [self start];+ else [self stop];+}++- (void)start {+ if (self.bounds.size.width < 1) return;+ _since = CACurrentMediaTime();+ CAFrameRateRange rate = CAFrameRateRangeMake(15, 60, 30);+ for (int i = 0; i < kDiscs; i++) {+ CAGradientLayer *disc = _discs[i];+ CGPathRef loop = [self newLoopOf:i];+ CAKeyframeAnimation *drift = [CAKeyframeAnimation animationWithKeyPath:@"position"];+ drift.path = loop;+ CGPathRelease(loop);+ drift.calculationMode = kCAAnimationPaced;+ drift.duration = kDisc[i].loop;+ drift.repeatCount = HUGE_VALF;+ drift.timeOffset = fmod(_elapsed, kDisc[i].loop);+ drift.preferredFrameRateRange = rate;+ [disc addAnimation:drift forKey:@"drift"];++ CABasicAnimation *breath = [CABasicAnimation animationWithKeyPath:@"transform.scale"];+ breath.fromValue = @(kBreathSmall);+ breath.toValue = @(kBreathLarge);+ breath.duration = kDisc[i].breath;+ breath.autoreverses = YES;+ breath.repeatCount = HUGE_VALF;+ breath.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];+ breath.timeOffset = fmod(_elapsed, 2 * kDisc[i].breath);+ breath.preferredFrameRateRange = rate;+ [disc addAnimation:breath forKey:@"breath"];+ }+}++// Every disc held where it is drawn now, so starting again carries on from the same picture.+- (void)stop {+ if (_since > 0) _elapsed += CACurrentMediaTime() - _since;+ _since = 0;+ for (int i = 0; i < kDiscs; i++) {+ CAGradientLayer *disc = _discs[i];+ CALayer *shown = disc.presentationLayer;+ if (shown) {+ disc.position = shown.position;+ disc.transform = shown.transform;+ }+ [disc removeAnimationForKey:@"drift"];+ [disc removeAnimationForKey:@"breath"];+ }+}++#pragma mark - colours++- (void)setColors:(NSArray<UIColor *> *)colors animated:(BOOL)animated {+ if (colors.count < kDiscs) return;+ _baseColor = shaded(colors[4], kBaseShade);+ [CATransaction begin];+ [CATransaction setDisableActions:YES];+ [self recolor:_base key:@"backgroundColor" to:(__bridge id)_baseColor.CGColor animated:animated];+ for (int i = 0; i < kDiscs; i++) [self recolor:_discs[i] key:@"colors" to:discColors(colors[i]) animated:animated];+ [CATransaction commit];+}++- (void)recolor:(CALayer *)layer key:(NSString *)key to:(id)value animated:(BOOL)animated {+ id from = [(layer.presentationLayer ?: layer) valueForKey:key];+ [layer setValue:value forKey:key];+ if (!animated || !from) return;+ CABasicAnimation *blend = [CABasicAnimation animationWithKeyPath:key];+ blend.fromValue = from;+ blend.toValue = value;+ blend.duration = kRecolor;+ blend.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];+ [layer addAnimation:blend forKey:key];+}++@enddiff --git a/tweak/Sources/Redesigned/Kit/SGRKit.h b/tweak/Sources/Redesigned/Kit/SGRKit.hindex e10f52b5..899b6bed 100644--- a/tweak/Sources/Redesigned/Kit/SGRKit.h+++ b/tweak/Sources/Redesigned/Kit/SGRKit.h@@ -5,6 +5,7 @@// SGRTokens.h colours, type, spacing, radii, motion, the accessibility settings// SGRPalette.h the artwork's edge colour, the field colour and the pre-blurred bitmaps// SGRField.h the artwork field behind a page+// SGRFlow.h the player's moving field of the artwork's colours// SGRGlass.h glass inside Spotify's round controls// SGRGlyph.h bare glyph overlays and glyph buttons// SGRActionRow.h the Play capsule and the stand-in button of a page's action row@@ -22,6 +23,7 @@#import "SGRTokens.h"#import "SGRPalette.h"#import "SGRField.h"+#import "SGRFlow.h"#import "SGRGlass.h"#import "SGRGlyph.h"#import "SGRActionRow.h"diff --git a/tweak/Sources/Redesigned/Kit/SGRPalette.h b/tweak/Sources/Redesigned/Kit/SGRPalette.hindex 08b38d72..6b62b5a8 100644--- a/tweak/Sources/Redesigned/Kit/SGRPalette.h+++ b/tweak/Sources/Redesigned/Kit/SGRPalette.h@@ -20,6 +20,8 @@ typedef struct {// from 85%, to lay over the sharp picture with the same aspect fill.BOOL dissolve;BOOL amoled;+ // The colours of a moving field (SGRFlow.h): the artwork's main colour in each quarter and overall.+ BOOL flow;} SGRPaletteRequest;@interface SGRPalette : NSObject@@ -27,6 +29,10 @@ typedef struct {@property (nonatomic, readonly) UIColor *fieldColor;@property (nonatomic, readonly) UIImage *backdrop; // nil unless asked for, at most 160px wide@property (nonatomic, readonly) UIImage *dissolve; // nil unless asked for, 96px wide+// nil unless asked for: top left, top right, bottom left, bottom right, then the whole artwork's, each+// the dominant colour there, its saturation lifted a little and its luminance held between+// kFlowLuminanceMin and kFlowLuminanceMax, so white text keeps better than 5.5:1 on any of them.+@property (nonatomic, readonly) NSArray<UIColor *> *flowColors;// nil to the completion when the image has no bitmap to read (a symbol, a CIImage).+ (void)paletteForImage:(UIImage *)image request:(SGRPaletteRequest)request completion:(void (^)(SGRPalette *palette))completion;diff --git a/tweak/Sources/Redesigned/Kit/SGRPalette.m b/tweak/Sources/Redesigned/Kit/SGRPalette.mindex 33dae2da..2306e763 100644--- a/tweak/Sources/Redesigned/Kit/SGRPalette.m+++ b/tweak/Sources/Redesigned/Kit/SGRPalette.m@@ -13,12 +13,17 @@ static const CGFloat kFadeFrom = 0.55, kDissolveOpaque = 0.85;// A tint: the artwork's dominant colour brought down to this luminance, then this share of it mixed into// the surface it tints.static const CGFloat kTintLuminance = 0.05, kTintShare = 0.35;+// A moving field's colours: luminance held between these (the ceiling 0.07 with Increase Contrast, as a+// field's), saturation lifted by this much up to a cap, and a near-black one lifted by at most this factor.+static const CGFloat kFlowLuminanceMin = 0.03, kFlowLuminanceMax = 0.13;+static const CGFloat kFlowSaturationLift = 1.25, kFlowSaturationMax = 0.9, kFlowMaxLift = 4;@interface SGRPalette ()@property (nonatomic, readwrite) UIColor *edgeColor;@property (nonatomic, readwrite) UIColor *fieldColor;@property (nonatomic, readwrite) UIImage *backdrop;@property (nonatomic, readwrite) UIImage *dissolve;+@property (nonatomic, readwrite) NSArray<UIColor *> *flowColors;@endstatic dispatch_queue_t paletteQueue(void) {@@ -163,16 +168,14 @@ static UIImage *finished(CGImageRef blurred, BOOL dim, CGFloat dimBottom, CGFloa// The artwork's dominant colour in linear light: its pixels binned 3 bits a channel, each bin scored by its// count weighted towards colourful pixels, so a busy cover gives its main colour instead of the grey an// average makes. Near-black and near-white count for little unless they are nearly all there is.-static BOOL dominantColorOf(CGImageRef image, CGFloat out[3]) {- CGContextRef context = newBitmap(kSample, kSample);- if (!context) return NO;- CGContextSetInterpolationQuality(context, kCGInterpolationMedium);- CGContextDrawImage(context, CGRectMake(0, 0, kSample, kSample), image);- const uint8_t *px = CGBitmapContextGetData(context);+// The same over the rows y0..y1 and columns x0..x1 of a kSample square bitmap (rows top down).+static BOOL dominantIn(const uint8_t *px, size_t x0, size_t y0, size_t x1, size_t y1, CGFloat out[3]) {enum { kBins = 512 };double sums[kBins][3] = {{0}}, scores[kBins] = {0};size_t counts[kBins] = {0};for (size_t i = 0; px && i < kSample * kSample; i++) {+ size_t x = i % kSample, y = i / kSample;+ if (x < x0 || x >= x1 || y < y0 || y >= y1) continue;const uint8_t *p = px + i * 4;if (p[3] < 128) continue;int bin = (p[0] >> 5) << 6 | (p[1] >> 5) << 3 | (p[2] >> 5);@@ -182,7 +185,6 @@ static BOOL dominantColorOf(CGImageRef image, CGFloat out[3]) {counts[bin]++;sums[bin][0] += toLinear(p[0] / 255.0), sums[bin][1] += toLinear(p[1] / 255.0), sums[bin][2] += toLinear(p[2] / 255.0);}- CGContextRelease(context);int best = 0;for (int i = 1; i < kBins; i++) {if (scores[i] > scores[best]) best = i;@@ -192,6 +194,52 @@ static BOOL dominantColorOf(CGImageRef image, CGFloat out[3]) {return YES;}+static BOOL dominantColorOf(CGImageRef image, CGFloat out[3]) {+ CGContextRef context = newBitmap(kSample, kSample);+ if (!context) return NO;+ CGContextSetInterpolationQuality(context, kCGInterpolationMedium);+ CGContextDrawImage(context, CGRectMake(0, 0, kSample, kSample), image);+ BOOL found = dominantIn(CGBitmapContextGetData(context), 0, 0, kSample, kSample, out);+ CGContextRelease(context);+ return found;+}++#pragma mark - flow++// A dominant colour in linear light made fit for a moving field: a little more colourful, and within+// the luminance band that keeps white text readable on it.+static UIColor *flowColorFor(const CGFloat linear[3], CGFloat ceiling) {+ CGFloat r = toEncoded(linear[0]), g = toEncoded(linear[1]), b = toEncoded(linear[2]), h = 0, s = 0, v = 0, a = 1;+ [[UIColor colorWithRed:r green:g blue:b alpha:1] getHue:&h saturation:&s brightness:&v alpha:&a];+ [[UIColor colorWithHue:h saturation:MIN(kFlowSaturationMax, MAX(s, s * kFlowSaturationLift)) brightness:v alpha:1] getRed:&r green:&g blue:&b alpha:&a];+ CGFloat lr = toLinear(r), lg = toLinear(g), lb = toLinear(b);+ CGFloat luminance = 0.2126 * lr + 0.7152 * lg + 0.0722 * lb;+ CGFloat k = 1;+ if (luminance > ceiling) k = ceiling / luminance;+ else if (luminance > 0 && luminance < kFlowLuminanceMin) k = MIN(kFlowMaxLift, kFlowLuminanceMin / luminance);+ return [UIColor colorWithRed:toEncoded(MIN(1, lr * k)) green:toEncoded(MIN(1, lg * k)) blue:toEncoded(MIN(1, lb * k)) alpha:1];+}++// The artwork's main colour in each quarter, then over the whole of it: the field keeps the artwork's+// colours roughly where the artwork has them.+static NSArray<UIColor *> *flowColorsOf(CGImageRef image, CGFloat ceiling) {+ CGContextRef context = newBitmap(kSample, kSample);+ if (!context) return nil;+ CGContextSetInterpolationQuality(context, kCGInterpolationMedium);+ CGContextDrawImage(context, CGRectMake(0, 0, kSample, kSample), image);+ const uint8_t *px = CGBitmapContextGetData(context);+ size_t half = kSample / 2;+ size_t regions[5][4] = {{0, 0, half, half}, {half, 0, kSample, half}, {0, half, half, kSample}, {half, half, kSample, kSample}, {0, 0, kSample, kSample}};+ NSMutableArray<UIColor *> *colors = [NSMutableArray arrayWithCapacity:5];+ CGFloat linear[3];+ for (int i = 0; i < 5; i++) {+ if (!dominantIn(px, regions[i][0], regions[i][1], regions[i][2], regions[i][3], linear)) break;+ [colors addObject:flowColorFor(linear, ceiling)];+ }+ CGContextRelease(context);+ return colors.count == 5 ? colors : nil;+}+static UIColor *tintOf(CGImageRef image, UIColor *surface) {CGFloat linear[3];CGFloat r = 0, g = 0, b = 0, a = 1;@@ -228,6 +276,7 @@ static UIColor *tintOf(CGImageRef image, UIColor *surface) {+ (void)paletteForImage:(UIImage *)image request:(SGRPaletteRequest)request completion:(void (^)(SGRPalette *palette))completion {if (!completion) return;CGFloat ceiling = SGRIncreaseContrast() ? kMaxLuminanceContrast : kMaxLuminance;+ CGFloat flowCeiling = SGRIncreaseContrast() ? kMaxLuminance : kFlowLuminanceMax;dispatch_async(paletteQueue(), ^{SGRPalette *palette = nil;CGImageRef cg = image.CGImage;@@ -244,6 +293,7 @@ static UIColor *tintOf(CGImageRef image, UIColor *surface) {if (blurred) palette.backdrop = finished(blurred, YES, request.amoled ? 0.55 : 0.45, kFadeFrom, 1, 1, 0);CGImageRelease(blurred);}+ if (request.flow) palette.flowColors = flowColorsOf(cg, flowCeiling);if (request.dissolve) {CGFloat aspect = (CGFloat)CGImageGetHeight(cg) / CGImageGetWidth(cg);size_t height = (size_t)MIN(kDissolveWidth * 2, MAX(kDissolveWidth / 2, round(kDissolveWidth * aspect)));diff --git a/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBarSettings.m b/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBarSettings.mindex 51f92c54..5e99b36d 100644--- a/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBarSettings.m+++ b/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBarSettings.m@@ -1,12 +1,17 @@-// The Now playing page of the redesign, under Player (App/Pages.m puts it there).+// The Now playing page of the redesign, under Player (App/Pages.m puts it there): the bar and the+// player behind it.#import "Core/SGCore.h"#import "Settings/SGModPage.h"#import "NowPlayingBar.h"+#import "Redesigned/Player/Player.h"UIViewController *SGRNowPlayingBarSettingsPage(void) {return [[SGModPage alloc] initWithTitle:@"Now playing" intro:SGRestartNote sections:@[SGSection(nil, @[SGHideRow(@"Hide the device button", @"The speaker icon on the now playing bar", SGRHideBarConnect),]),+ SGNotedSection(nil, @[+ SGSwitchRow(@"Moving background", @"The artwork's colours drift slowly behind the player", SGRKeyPlayerMotion),+ ], @"Off, the blurred artwork stays still. The colours hold still anyway while a song is paused, with Reduce Motion and in Low Power Mode."),] footer:nil];}diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.hindex 9cd8dc3b..ce5e1ea5 100644--- a/tweak/Sources/Redesigned/Player/Player.h+++ b/tweak/Sources/Redesigned/Player/Player.h@@ -25,6 +25,10 @@@class SGRArtworkField;+// The artwork's colours moving behind the player (on until switched off), or the blurred artwork held+// still; the row is on the Now playing page (Redesigned/NowPlayingBar/NowPlayingBarSettings.m).+#define SGRKeyPlayerMotion @"spotifyglass.redesign.player.movingBackground"+// The field behind the player, nil until the player has laid out once (PlayerField.x).SGRArtworkField *SGRPlayerField(void);diff --git a/tweak/Sources/Redesigned/Player/PlayerField.x b/tweak/Sources/Redesigned/Player/PlayerField.xindex 70d1fc57..dd16708d 100644--- a/tweak/Sources/Redesigned/Player/PlayerField.x+++ b/tweak/Sources/Redesigned/Player/PlayerField.x@@ -8,7 +8,11 @@// The field goes on top of the plane's own subviews, so Spotify's gradients are covered rather than// fought over, and nothing depends on a repaint hook.//-// The picture comes from the Kit's now playing artwork: the now playing bar's 40pt cover, published by+// With Moving background on (SGRKeyPlayerMotion, the default) the field is the Kit's moving field of the+// artwork's colours (SGRFlow.h) rather than the still blurred artwork; a paused song holds it still.+//+// The picture comes from the Kit's now playing artwork, keyed on the picture the playing track names+// (SGRBridges.h, issue #58): the Kit's own fetch of it, the now playing bar's 40pt cover, published by// the Kit, and the player's own 354pt cover, published here from the cell in the middle of the// sideways list of covers (AccessibleCollectionView, 01.txt:28, one CoverArtCellImpl per queued track,// the ones out of view hidden). Until the first picture has been read the field takes the colour@@ -50,6 +54,9 @@ static SGRArtworkField *fieldIn(UIView *plane) {if (field) return field;field = [[SGRArtworkField alloc] initWithFrame:plane.bounds];field.showsBackdrop = YES;+ field.flows = SGEnabled(SGRKeyPlayerMotion);+ // A paused song holds the colours still, the way it rests the cover (PlayerArtwork.x).+ field.motionHeld = SGPlayerState().isPaused;field.bleed = kBleed;objc_setAssociatedObject(plane, &kFieldKey, field, OBJC_ASSOCIATION_RETAIN_NONATOMIC);sg_field = field;@@ -135,6 +142,7 @@ static void publishCover(void) {}- (void)playerStateDidChange:(SPTPlayerState *)state {+ sg_field.motionHeld = state.isPaused;NSString *track = SGURIString(state.track.URI);if (!track || [track isEqualToString:_track]) return;_track = track;