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: animated player artwork follows playback and gives way to Spotify's video

My-Name-Is-Jeffa2b62937d294
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 302563c2..4180519e 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -354,7 +354,10 @@ Redesigned:
cross over as one comes and the other goes, the clip from its poster frame before the video has
decoded one, and the Fluid field under a clip holds still and, once the clip has faded in, is hidden
(SGRArtworkField's covered); the menu switches it between Animated
- and Fluid without a restart. Checked in the simulator against harness/player/ (`motion`)
+ and Fluid without a restart. The clip holds its frame while the song is paused, goes while
+ Spotify's music video shows, is given up when it has drawn nothing in 5 s on screen, and on a skip
+ stays a moment for the next track's to cross over it. Checked in the simulator against
+ harness/player/ (`motion`)
Lyrics/ the full screen lyrics page on glass with Apple Music style lyrics over it, always on (SGRKaraokeView,
which the player shows in itself too, Player/PlayerLyrics.x): lines sung over each other lit together,
the stack moving on once the first is sung out; an instrumental break of 7 s or more held by three dots
diff --git a/harness/player/README.md b/harness/player/README.md
index a7dd654c..28347eb5 100644
--- a/harness/player/README.md
+++ b/harness/player/README.md
@@ -59,8 +59,11 @@ come late, out of order, or not at all.
out, the lyrics open and close, the background is built again, and the ⋯ menu's switch goes off, on, and
off with the player out of its window. Each step is checked (the clip on the field, the cover hidden, the
poster under the video and the Fluid field held under it, the foot and the blur, the thumbnail, the clip
- and the cover crossing over half way through the switch) and the log ends with
- `motion checks: n of 11 right -- PASS` or `FAIL`.
+ and the cover crossing over half way through the switch). Then the song pauses and plays (the clip's rate
+ 0, then 1), a mock of Spotify's video unit attaches and detaches its video (the clip goes and comes back),
+ a track comes with no Canvas and then again with one (the clip comes in late), and a skip to a track whose
+ clip is in the store keeps the last clip until the new one is over it, the cover hidden throughout. The log
+ ends with `motion checks: n of 20 right -- PASS` or `FAIL`.
- `settings` opens the redesign's Player page (`PlayerSettings.m`, with the real `Settings/` framework) over the
player at 3 s, with the clip of `motion` in. It checks the card leads the page with the four backgrounds'
diff --git a/harness/player/main.m b/harness/player/main.m
index 461a4999..ee5a68bf 100644
--- a/harness/player/main.m
+++ b/harness/player/main.m
@@ -24,6 +24,7 @@
// Animated again, the note under it changing and the rows under the header holding still;
// each step checked, the log ends with settings checks n of m right -- PASS or FAIL
// HARNESS_VOLUME=0 leaves out the volume row the phone has (trees/clean/player/01.txt has none).
+#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import "Shared/Lyrics/Lyrics.h"
@@ -186,6 +187,13 @@ static NSString *colorName(UIImage *image) {
@interface _TtC21NowPlaying_ScrollImpl27NPVBackgroundViewController : UIViewController @end
@implementation _TtC21NowPlaying_ScrollImpl27NPVBackgroundViewController @end
+// One of the units Spotify shows its music video in (Switch to video); PlayerMotion.x hooks its video surface.
+@interface _TtC22NowPlaying_ElementsKit14VideoElementUI : NSObject @end
+@implementation _TtC22NowPlaying_ElementsKit14VideoElementUI
+- (void)videoSurfaceDidAttachVideo:(id)surface {}
+- (void)videoSurfaceDidDetachVideo:(id)surface {}
+@end
+
@interface _TtC18NowPlaying_BarImpl27NowPlayingBarViewController : UIViewController @end
@implementation _TtC18NowPlaying_BarImpl27NowPlayingBarViewController @end
@@ -905,7 +913,59 @@ static BOOL fieldCovered(void) {
[self expect:!self->_covers.layer.mask step:@"switched off with the player closed" detail:self->_covers.layer.mask ? @"cover still hidden" : @"cover back"];
[self->_list addSubview:self->_host];
});
- after(15.7, ^{
+ after(15.7, ^{ SGPlayerMenuSetAnimatedArtwork(YES); });
+ // The song paused and played again: the clip holds its frame, then moves on.
+ after(17.2, ^{
+ AVPlayer *player = [motionView() valueForKey:@"_player"];
+ AVPlayerLayer *clip = [motionView() valueForKey:@"_clip"];
+ [self expect:player.rate > 0 && clip.readyForDisplay step:@"playing" detail:[NSString stringWithFormat:@"clip at rate %.1f, %@", player.rate,
+ clip.readyForDisplay ? @"drawn" : @"not drawn"]];
+ SGRHarnessSetTrack(@"spotify:track:harnessA", imageURI(@"aaaa"), YES);
+ AVPlayer *paused = [motionView() valueForKey:@"_player"];
+ [self expect:motionView() && paused.rate == 0 step:@"paused" detail:[NSString stringWithFormat:@"clip %@ at rate %.1f",
+ motionView() ? @"there" : @"gone", paused.rate]];
+ SGRHarnessSetTrack(@"spotify:track:harnessA", imageURI(@"aaaa"), NO);
+ [self expect:paused.rate > 0 step:@"played again" detail:[NSString stringWithFormat:@"rate %.1f", paused.rate]];
+ });
+ // Spotify's music video comes and goes: the clip goes for it, and comes back after.
+ __block id video = nil;
+ after(17.5, ^{
+ video = [NSClassFromString(@"_TtC22NowPlaying_ElementsKit14VideoElementUI") new];
+ [video videoSurfaceDidAttachVideo:nil];
+ });
+ after(18.5, ^{
+ [self expect:!SGRPlayerMotionShowing() step:@"Spotify's video showing" detail:SGRPlayerMotionShowing() ? @"the clip stayed" : @"the clip went"];
+ [video videoSurfaceDidDetachVideo:nil];
+ });
+ after(19.5, ^{
+ [self expect:SGRPlayerMotionShowing() && self->_covers.layer.mask step:@"Spotify's video gone"
+ detail:SGRPlayerMotionShowing() ? @"the clip back" : @"no clip"];
+ // A track whose first state names no Canvas, as on a skip before Spotify's extended metadata.
+ unsetenv("HARNESS_CANVAS");
+ SGRHarnessSetTrack(@"spotify:track:harnessB", imageURI(@"bbbb"), NO);
+ });
+ after(20.5, ^{
+ [self expect:!SGRPlayerMotionShowing() step:@"a track with no Canvas yet" detail:SGRPlayerMotionShowing() ? @"a clip left" : @"no clip"];
+ setenv("HARNESS_CANVAS", "https://canvas.harness/clip.mp4", 1);
+ SGRHarnessSetTrack(@"spotify:track:harnessB", imageURI(@"bbbb"), NO);
+ });
+ // A skip to a track whose clip is in the store: the clip stays for the next one to cross over it.
+ __block UIView *before = nil;
+ after(21.5, ^{
+ [self expect:SGRPlayerMotionShowing() step:@"its Canvas came late" detail:SGRPlayerMotionShowing() ? @"the clip in" : @"no clip"];
+ before = motionView();
+ SGRHarnessSetTrack(@"spotify:track:harnessC", imageURI(@"cccc"), NO);
+ });
+ after(21.6, ^{
+ [self expect:SGRPlayerMotionShowing() && self->_covers.layer.mask step:@"skipped, the clip kept a moment"
+ detail:SGRPlayerMotionShowing() ? @"a clip in" : @"the cover back"];
+ });
+ after(22.3, ^{
+ CALayer *mask = self->_covers.layer.mask;
+ [self expect:SGRPlayerMotionShowing() && motionView() != before && mask && mask.opacity == 0 step:@"the next clip over the last"
+ detail:[NSString stringWithFormat:@"%@, cover %@", motionView() != before ? @"a new clip" : @"the same clip", mask && mask.opacity == 0 ? @"still hidden" : @"back"]];
+ });
+ after(23, ^{
NSLog(@"[harness] motion checks: %lu of %lu right -- %@", (unsigned long)(self->_checks - self->_failures), (unsigned long)self->_checks,
self->_failures ? @"FAIL" : @"PASS");
});
diff --git a/tweak/Sources/Redesigned/Player/PlayerMotion.x b/tweak/Sources/Redesigned/Player/PlayerMotion.x
index bb32abe7..3d9e0232 100644
--- a/tweak/Sources/Redesigned/Player/PlayerMotion.x
+++ b/tweak/Sources/Redesigned/Player/PlayerMotion.x
@@ -13,12 +13,18 @@
// Spotify built again, gets it.
//
// The clip holds still when the moving field does (SGRField.m): out of a window, with the app not in front,
-// while the player opens or closes, under Reduce Motion and in Low Power Mode. While a clip is showing, the
-// Fluid field under it is held still too, and once the clip has faded in it is hidden (the field's covered):
-// the field's colour is what shows above the clip on the pull that dismisses.
+// while the player opens or closes, under Reduce Motion and in Low Power Mode, and while the song is paused.
+// While a clip is showing, the Fluid field under it is held still too, and once the clip has faded in it is
+// hidden (the field's covered): the field's colour is what shows above the clip on the pull that dismisses.
//
// The clip and the cover cross over: the clip fades in from its first frame (the poster, under the video
-// until the video has decoded one) as the cover fades out, and fades out as the cover comes back.
+// until the video has decoded one) as the cover fades out, and fades out as the cover comes back. A clip
+// that has drawn no frame of its own after kGiveUp seconds on screen is given up, and the cover comes back.
+// On a skip the last clip stays a moment (kHandOver), so a clip already in the store (the next track's is
+// fetched ahead, SGMotionFollower) crosses over it with no cover between.
+//
+// Spotify's music video (Switch to video) draws in one of three of its units, each told by its video surface
+// as the video comes and goes: while one shows it, the clip goes and the cover comes back, as for Fluid.
#import <AVFoundation/AVFoundation.h>
#import "Core/SGCore.h"
#import "Redesigned/Kit/SGRKit.h"
@@ -36,9 +42,15 @@ static const CGFloat kCanvasAspect = 1.5;
// The blur starts at the seam, or this share of the screen down where that is higher, so it is under the
// controls whatever the clip's shape; it is whole kBlurRamp points below its start.
static const CGFloat kBlurByControls = 0.6, kBlurRamp = 96;
+// Seconds a clip may be on screen in front without drawing a frame before it is given up.
+static const NSTimeInterval kGiveUp = 5;
+// Seconds the last track's clip stays on a skip for the next one's to come in over it.
+static const NSTimeInterval kHandOver = 0.4;
-@interface SGRPlayerMotionView : UIView
+@interface SGRPlayerMotionView : UIView <SGPlayerStateObserver>
@property (nonatomic) CGFloat screenHeight;
+// Runs when the clip has drawn nothing kGiveUp seconds after it was first on screen in front.
+@property (nonatomic, copy) void (^gaveUp)(void);
- (void)playFile:(NSURL *)file poster:(UIImage *)poster;
- (void)appear:(BOOL)animated then:(void (^)(void))done;
- (void)setBlurred:(BOOL)blurred animated:(BOOL)animated;
@@ -54,6 +66,7 @@ static const CGFloat kBlurByControls = 0.6, kBlurRamp = 96;
CALayer *_foot;
UIVisualEffectView *_seamBlur, *_lyricsBlur;
CGFloat _aspect;
+ BOOL _timing; // the give-up's count is running
}
// What fades is the effect views' effect, never their alpha or an ancestor's: UIKit draws a blur under a
@@ -104,9 +117,14 @@ static UIBlurEffect *blurEffect(void) {
[center addObserver:self selector:@selector(updateMotionSoon) name:name object:nil];
}
SGRObservePlayerTransition(self, ^(id owner) { [owner updateMotion]; }, ^(id owner) { [owner updateMotion]; });
+ SGAddPlayerStateObserver(self);
return self;
}
+- (void)playerStateDidChange:(SPTPlayerState *)state {
+ [self updateMotion];
+}
+
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}
@@ -196,9 +214,23 @@ static UIBlurEffect *blurEffect(void) {
// A locked phone keeps the player in its window, so being in front counts as much as being in one.
- (void)updateMotion {
BOOL front = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;
- BOOL may = self.window && front && !SGRPlayerIsTransitioning() && !SGRReduceMotion() && !NSProcessInfo.processInfo.lowPowerModeEnabled;
+ BOOL may = self.window && front && !SGRPlayerIsTransitioning() && !SGRReduceMotion() && !NSProcessInfo.processInfo.lowPowerModeEnabled
+ && !SGPlayerState().isPaused;
if (may) [_player play];
else [_player pause];
+ // A paused player still draws the clip's first frame, so the count runs whenever it is on screen in front.
+ if (_player && self.window && front && !_timing && !_clip.readyForDisplay) {
+ _timing = YES;
+ __weak SGRPlayerMotionView *weakSelf = self;
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kGiveUp * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ SGRPlayerMotionView *view = weakSelf;
+ if (!view) return;
+ view->_timing = NO;
+ // Taken off screen meanwhile, the count starts again when it is back.
+ BOOL shown = view.window && UIApplication.sharedApplication.applicationState == UIApplicationStateActive;
+ if (shown && !view->_clip.readyForDisplay && view.gaveUp) view.gaveUp();
+ });
+ }
}
- (void)didMoveToWindow {
@@ -209,7 +241,9 @@ static UIBlurEffect *blurEffect(void) {
@end
static SGRPlayerMotionView *sg_motion;
-static NSString *sg_track;
+static NSString *sg_track; // the track playing
+static NSString *sg_shown; // the track sg_motion is the clip of
+static NSUInteger sg_begun; // counts the walks begun, so a poster read for an earlier one is dropped
// The clip sg_motion plays and its poster, for the Player page's showcase (SGRPlayerMotionPreview).
static NSURL *sg_motionFile;
static UIImage *sg_motionPoster;
@@ -316,10 +350,20 @@ UIView *SGRPlayerMotionPreview(void) {
return preview;
}
+static SGMotionFollower *sg_follower;
+
+// Spotify's units that are showing its music video, held weakly: one that goes without saying so lets go.
+static NSHashTable *sg_videos;
+
+static BOOL videoShowing(void) {
+ return sg_videos.allObjects.count > 0;
+}
+
// The clip fades out over the cover coming back where the player is on screen, and goes at once where not.
static void clear(BOOL animated) {
SGRPlayerMotionView *old = sg_motion;
sg_motion = nil;
+ sg_shown = nil;
sg_motionFile = nil;
sg_motionPoster = nil;
// Uncovered before the clip starts to fade, so the field is there under it.
@@ -333,10 +377,19 @@ static void clear(BOOL animated) {
static void show(NSString *track, NSURL *file) {
if (!file || ![track isEqualToString:sg_track]) return;
+ NSUInteger begun = sg_begun;
SGMotionPoster(file, ^(UIImage *poster) {
- if (!poster || poster.size.width <= 0 || ![track isEqualToString:sg_track]) return;
+ // A walk begun while the poster was read (switched off, Spotify's video) has the say.
+ if (!poster || poster.size.width <= 0 || begun != sg_begun) return;
clear(YES);
sg_motion = [[SGRPlayerMotionView alloc] initWithFrame:CGRectZero];
+ sg_shown = track;
+ SGRPlayerMotionView *motion = sg_motion;
+ motion.gaveUp = ^{
+ if (sg_motion != motion) return;
+ SGLog(@"redesign player: animated artwork %@ drew nothing in %.0f s, given up", file.lastPathComponent, kGiveUp);
+ clear(YES);
+ };
[sg_motion playFile:file poster:poster];
sg_motionFile = file;
sg_motionPoster = poster;
@@ -344,7 +397,6 @@ static void show(NSString *track, NSURL *file) {
holdField();
[sg_motion setBlurred:SGRPlayerLyricsOpen() animated:NO];
BOOL animated = sg_motion.window != nil;
- SGRPlayerMotionView *motion = sg_motion;
[motion appear:animated then:^{
if (sg_motion == motion) cover(YES);
}];
@@ -357,7 +409,35 @@ void SGRPlayerMotionLyricsChanged(void) {
[sg_motion setBlurred:SGRPlayerLyricsOpen() animated:sg_motion.window != nil];
}
-static SGMotionFollower *sg_follower;
+static void videoSurface(id unit, BOOL attached) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ BOOL was = videoShowing();
+ if (attached) [sg_videos addObject:unit];
+ else [sg_videos removeObject:unit];
+ if (videoShowing() == was) return;
+ SGLog(@"redesign player: Spotify's video %@", was ? @"gone, the clip looked up again" : @"showing, the clip away");
+ [sg_follower restart];
+ });
+}
+
+// A skip on screen leaves the last clip a moment for the next one's to cross over, and the same track
+// walked again (its Canvas came late) keeps its clip until the new one does. Otherwise the clip goes at
+// once: off screen, for Spotify's video, and when the background is not Animated.
+static BOOL beginTrack(NSString *track) {
+ sg_track = track;
+ sg_begun++;
+ // Read on every track, since the ⋯ menu switches it.
+ BOOL wanted = SGRPlayerBackground() == SGRPlayerBackgroundAnimated && !videoShowing();
+ if (wanted && [track isEqualToString:sg_shown]) return YES;
+ if (!wanted || !sg_motion.window) {
+ clear(YES);
+ return wanted;
+ }
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kHandOver * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ if ([track isEqualToString:sg_track] && sg_motion && ![track isEqualToString:sg_shown]) clear(YES);
+ });
+ return YES;
+}
#pragma mark - the ⋯ menu's switch (Shared/Player/SpeedPitch.h)
@@ -382,17 +462,51 @@ void SGPlayerMenuSetAnimatedArtwork(BOOL on) {
[sg_follower restart];
}
+%group SGRVideoSurfaces
+%hook _TtC28NowPlaying_ContentLayersImpl24HorizontalVideoViewModel
+- (void)videoSurfaceDidAttachVideo:(id)surface {
+ %orig;
+ videoSurface(self, YES);
+}
+- (void)videoSurfaceDidDetachVideo:(id)surface {
+ %orig;
+ videoSurface(self, NO);
+}
+%end
+
+%hook _TtC28NowPlaying_ContentLayersImpl31VerticalVideoCellImplementation
+- (void)videoSurfaceDidAttachVideo:(id)surface {
+ %orig;
+ videoSurface(self, YES);
+}
+- (void)videoSurfaceDidDetachVideo:(id)surface {
+ %orig;
+ videoSurface(self, NO);
+}
+%end
+
+%hook _TtC22NowPlaying_ElementsKit14VideoElementUI
+- (void)videoSurfaceDidAttachVideo:(id)surface {
+ %orig;
+ videoSurface(self, YES);
+}
+- (void)videoSurfaceDidDetachVideo:(id)surface {
+ %orig;
+ videoSurface(self, NO);
+}
+%end
+%end
+
%ctor {
if (!SGRedesignedUI()) return;
SGRPlayerBackgroundKind background = SGRPlayerBackground();
if (background != SGRPlayerBackgroundFluid && background != SGRPlayerBackgroundAnimated) return;
+ sg_videos = [NSHashTable weakObjectsHashTable];
sg_follower = [[SGMotionFollower alloc] initWithBegin:^BOOL(NSString *uri, SPTPlayerState *state) {
- sg_track = uri;
- clear(YES);
- // Read on every track, since the ⋯ menu switches it.
- return SGRPlayerBackground() == SGRPlayerBackgroundAnimated;
+ return beginTrack(uri);
} found:^(NSString *uri, NSURL *file) {
if (file) show(uri, file);
else clear(YES);
}];
+ %init(SGRVideoSurfaces);
}