Skip to content

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

C

feat: the redesigned player's lyrics fill the screen while left alone

My-Name-Is-Jeff60335bb9244c
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index 566a5033..1b79fbea 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -1,4 +1,5 @@
#import "Core/SGCore.h"
+#import "Redesigned/Player/Player.h"
#import "Settings/SGModPage.h"
#import "Settings/SGPageStyle.h"
#import "Pages.h"
@@ -77,7 +78,8 @@ static UIViewController *lyricsPage(void) {
if (!redesigned) [more insertObject:SGGlassLyricsRow() atIndex:0];
NSMutableArray<SGModSection *> *sections = [NSMutableArray arrayWithObject:SGLyricsSourcesSection(redesigned)];
if (redesigned) {
- [sections addObject:SGSection(@"Display", @[SGLyricsWordTimingRow(), SGRLyricsTextSizesRow(), SGLyricsTranslationLanguageRow(), SGLyricsMeaningsRow()])];
+ [sections addObject:SGSection(@"Display", @[SGLyricsWordTimingRow(), SGRLyricsTextSizesRow(), SGLyricsTranslationLanguageRow(), SGLyricsMeaningsRow(),
+ SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide)])];
}
[sections addObject:SGSection(nil, more)];
return [[SGModPage alloc] initWithTitle:@"Lyrics" intro:SGRestartNote sections:sections footer:nil];
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
index 4bff6a04..ea41fdbc 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
@@ -11,4 +11,8 @@
@interface SGRKaraokeView : UIView
// Hides Spotify's own lyrics next to this view while it has lyrics to show, and brings them back when not.
- (void)syncSiblings;
+// Called as a finger starts to scroll the lines, for a host that hides its controls then.
+@property (nonatomic, copy) void (^browsingBegan)(void);
+// The pronunciation and translation button goes with the host's controls when they are hidden.
+@property (nonatomic) BOOL extrasHidden;
@end
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 9e9ac706..62cabc48 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1187,9 +1187,16 @@ typedef struct {
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];
_browsing = YES;
+ if (self.browsingBegan) self.browsingBegan();
for (SGRKaraokeLineView *view in _shown.allValues) view.blur = 0;
}
+- (void)setExtrasHidden:(BOOL)hidden {
+ _extrasHidden = hidden;
+ _extras.alpha = hidden ? 0 : 1;
+ _extras.userInteractionEnabled = !hidden;
+}
+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self showLinesInSight];
}
@@ -1423,6 +1430,7 @@ typedef struct {
_extras.showsMenuAsPrimaryAction = YES;
_extras.preferredMenuElementOrder = UIContextMenuConfigurationElementOrderFixed;
_extras.accessibilityLabel = @"Pronunciation and translation";
+ self.extrasHidden = _extrasHidden;
[self addSubview:_extras];
}
NSMutableArray<UIMenuElement *> *items = [NSMutableArray array];
diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.h
index 909f51e2..a515f37b 100644
--- a/tweak/Sources/Redesigned/Player/Player.h
+++ b/tweak/Sources/Redesigned/Player/Player.h
@@ -67,6 +67,10 @@ void SGRPlayerToggleLyrics(void);
// Called by PlayerLyrics.x whenever either of those two changed, so the footer's lyrics glyph follows
// (PlayerFooter.x). It returns at once when nothing changed.
void SGRPlayerLyricsChanged(void);
+// The controls fade a few seconds after the last touch while the lyrics play, and the lines take the
+// whole player (PlayerLyrics.x). On until switched off; a scroll through the lines hides them either way.
+#define SGRKeyLyricsAutoHide @"spotifyglass.redesign.lyrics.autoHide"
+
// The animated artwork follows the lyrics: blurred behind them (PlayerMotion.x).
void SGRPlayerMotionLyricsChanged(void);
diff --git a/tweak/Sources/Redesigned/Player/PlayerControls.x b/tweak/Sources/Redesigned/Player/PlayerControls.x
index 86088f10..e019824d 100644
--- a/tweak/Sources/Redesigned/Player/PlayerControls.x
+++ b/tweak/Sources/Redesigned/Player/PlayerControls.x
@@ -18,6 +18,7 @@
// inside the unit's view never reaches.
#import "Core/SGCore.h"
#import "Redesigned/Kit/SGRKit.h"
+#import "Shared/Lyrics/Lyrics.h"
#import "Player.h"
static const CGFloat kSkipGlyphSize = 32, kPlayGlyphSize = 44;
@@ -228,11 +229,42 @@ static UILabel *monospaced(UIView *host, NSString *identifier, const void *findK
}
%end
+#pragma mark - tap to seek
+
+static char kSliderKey, kSeekTapKey;
+
+// Spotify's slider only drags. A tap on it seeks to where it landed; the tap cancels the slider's own
+// tracking, so the two never both seek.
+@interface SGRSeekTap : UITapGestureRecognizer
+@end
+
+@implementation SGRSeekTap
+- (void)sgr_seek {
+ UIView *slider = self.view;
+ SPTPlayerState *state = SGPlayerState();
+ CGFloat width = slider.bounds.size.width;
+ if (!state || width < 1 || state.duration <= 0) return;
+ CGFloat share = MIN(1, MAX(0, [self locationInView:slider].x / width));
+ SGKaraokeSeek((NSInteger)(share * state.duration * 1000));
+}
+@end
+
+static void watchSlider(UIView *host) {
+ UIView *slider = SGRFindByIdentifier(host, @"SPTNowPlayingSliderV2", &kSliderKey);
+ if (!slider || objc_getAssociatedObject(slider, &kSeekTapKey)) return;
+ SGRSeekTap *tap = [[SGRSeekTap alloc] init];
+ [tap addTarget:tap action:@selector(sgr_seek)];
+ [slider addGestureRecognizer:tap];
+ objc_setAssociatedObject(slider, &kSeekTapKey, tap, OBJC_ASSOCIATION_ASSIGN);
+ SGLog(@"redesign player: a tap on the progress bar seeks");
+}
+
%hook _TtC20NowPlaying_ModesImpl19DurationElementUnit
- (void)viewDidLayoutSubviews {
%orig;
UIView *host = ((UIViewController *)self).viewIfLoaded;
if (!host) return;
+ watchSlider(host);
UILabel *taken = monospaced(host, @"now-playing-time-take-label-internal", &kTakeKey);
monospaced(host, @"now-playing-time-remaning-label-internal", &kRemainingKey);
// Whether Spotify's font has digits of one width shows in the descriptor's feature settings.
diff --git a/tweak/Sources/Redesigned/Player/PlayerLyrics.x b/tweak/Sources/Redesigned/Player/PlayerLyrics.x
index 2a6f15be..396c0009 100644
--- a/tweak/Sources/Redesigned/Player/PlayerLyrics.x
+++ b/tweak/Sources/Redesigned/Player/PlayerLyrics.x
@@ -24,6 +24,7 @@
// The geometry is re-applied on every layout pass of the units, since a new track rebuilds the
// elements inside them, and it is undone before the player closes: the bar morphs back into a full
// size cover, which a thumbnail would not match.
+#import <UIKit/UIGestureRecognizerSubclass.h>
#import "Core/SGCore.h"
#import "Redesigned/Kit/SGRKit.h"
#import "Redesigned/Lyrics/SGRKaraokeView.h"
@@ -45,6 +46,10 @@ static const NSTimeInterval kLyricsIn = 0.3, kLyricsInDelay = 0.12, kLyricsOut =
static const NSTimeInterval kLyricsGrace = 3;
// Below this the player has not laid out yet and nothing can be measured from it.
static const CGFloat kLivingHeight = 200;
+// The lines on their own: the controls fade after this long without a touch, and the lines then keep this
+// far from the safe area's edges.
+static const NSTimeInterval kRest = 4;
+static const CGFloat kAloneInset = 24;
static char kOverlayKey, kPlateKey, kTitleKey;
static BOOL sg_open;
@@ -52,6 +57,10 @@ static BOOL sg_moving; // the transition is in flight, so n
static __weak UIView *sg_host; // SPTNowPlayingView
static __weak UIViewController *sg_info, *sg_duration, *sg_floating;
static __weak UIView *sg_titleElement; // the arranged element view holding the title and the artist
+static __weak UIViewController *sg_player; // NowPlayingViewController, whose units the controls are
+static BOOL sg_alone; // the lines have the player to themselves
+static NSTimer *sg_rest;
+static UIView *sg_shield; // over the player while alone: the touch that brings the controls back
#pragma mark - the overlay
@@ -73,7 +82,10 @@ static __weak UIView *sg_titleElement; // the arranged element view holding
- (instancetype)initWithFrame:(CGRect)frame {
if (!(self = [super initWithFrame:frame])) return nil;
_thumb = [[UIView alloc] initWithFrame:CGRectZero];
- _thumb.userInteractionEnabled = NO;
+ // A tap on the cover beside the lines brings the full player back.
+ [_thumb addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sgr_thumbTapped)]];
+ _thumb.accessibilityLabel = @"Show the player";
+ _thumb.accessibilityTraits = UIAccessibilityTraitButton;
_cover = [[UIImageView alloc] initWithFrame:CGRectZero];
_cover.contentMode = UIViewContentModeScaleAspectFill;
_cover.clipsToBounds = YES;
@@ -86,6 +98,10 @@ static __weak UIView *sg_titleElement; // the arranged element view holding
return self;
}
+- (void)sgr_thumbTapped {
+ if (SGRPlayerLyricsOpen()) SGRPlayerToggleLyrics();
+}
+
- (UIView *)thumb { return _thumb; }
- (UIImageView *)cover { return _cover; }
- (UIView *)stage { return _stage; }
@@ -281,6 +297,138 @@ static CGFloat thumbRadius(SGRLyricsLayout l, BOOL open) {
return scale > 0 ? SGRRadiusCover / scale : SGRRadiusArtwork;
}
+#pragma mark - the lines on their own
+
+// After kRest without a touch while the song plays, the player's controls fade and the lines grow to its
+// whole height, the way the Music app leaves its lyrics alone. A touch anywhere brings the controls back
+// and is not a seek; a pause brings them back and keeps them; VoiceOver, a sheet over the player and the
+// app in the background keep them up. A scroll through the lines hides them at once.
+
+// The units the controls are: the header row, the title row, the progress bar, the buttons, the footer.
+static NSArray<UIView *> *controlViews(void) {
+ static NSArray<NSString *> *names;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ names = @[@"HeaderElementsUnit", @"InformationElementsUnit", @"DurationElementUnit",
+ @"PlaybackControlsElementsUnit", @"FooterElementsUnit"];
+ });
+ // A unit's view answers to its controller as its next responder, wherever the unit sits in the tree.
+ NSMutableArray<UIView *> *views = [NSMutableArray array];
+ SGForEachView(sg_host, ^(UIView *view) {
+ UIResponder *owner = view.nextResponder;
+ if (![owner isKindOfClass:UIViewController.class] || ((UIViewController *)owner).viewIfLoaded != view) return;
+ NSString *name = NSStringFromClass(owner.class);
+ for (NSString *unit in names) {
+ if ([name hasSuffix:unit]) [views addObject:view];
+ }
+ });
+ return views;
+}
+
+// Where the lines go on their own: the player's whole height inside its safe area.
+static CGRect aloneStage(UIView *host, SGRLyricsLayout l) {
+ CGRect safe = UIEdgeInsetsInsetRect(host.bounds, host.safeAreaInsets);
+ return CGRectMake(CGRectGetMinX(l.stage), CGRectGetMinY(safe) + kAloneInset, l.stage.size.width,
+ safe.size.height - 2 * kAloneInset);
+}
+
+static void setAlone(BOOL alone, BOOL animated);
+
+static BOOL mayRest(void) {
+ SPTPlayerState *state = SGPlayerState();
+ UIView *host = sg_host;
+ return SGEnabled(SGRKeyLyricsAutoHide) && sg_open && !sg_alone && host.window && state && !state.isPaused
+ && !UIAccessibilityIsVoiceOverRunning()
+ && UIApplication.sharedApplication.applicationState == UIApplicationStateActive
+ && !sg_player.presentedViewController;
+}
+
+// The clock starts again at every touch, and stops for anything that keeps the controls up.
+static void restartRest(void) {
+ [sg_rest invalidate];
+ sg_rest = nil;
+ if (!mayRest()) return;
+ sg_rest = [NSTimer scheduledTimerWithTimeInterval:kRest repeats:NO block:^(NSTimer *timer) {
+ sg_rest = nil;
+ if (mayRest()) setAlone(YES, YES);
+ }];
+}
+
+@interface SGRPlayerTouchWatcher : UIGestureRecognizer
+@end
+
+// Sees every touch on the player and takes none: it fails at once, so nothing under it loses one.
+@implementation SGRPlayerTouchWatcher
+- (instancetype)init {
+ if (!(self = [super initWithTarget:nil action:nil])) return nil;
+ self.cancelsTouchesInView = NO;
+ self.delaysTouchesBegan = NO;
+ self.delaysTouchesEnded = NO;
+ return self;
+}
+- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
+ if (!sg_alone) restartRest();
+ self.state = UIGestureRecognizerStateFailed;
+}
+@end
+
+static void shieldTapped(void) {
+ setAlone(NO, YES);
+ restartRest();
+}
+
+@interface SGRPlayerShield : UIView
+@end
+
+@implementation SGRPlayerShield
+- (instancetype)initWithFrame:(CGRect)frame {
+ if (!(self = [super initWithFrame:frame])) return nil;
+ self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+ self.accessibilityElementsHidden = YES;
+ [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sgr_tapped)]];
+ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(sgr_tapped)];
+ [self addGestureRecognizer:pan];
+ return self;
+}
+- (void)sgr_tapped {
+ shieldTapped();
+}
+@end
+
+static void setAlone(BOOL alone, BOOL animated) {
+ UIView *host = sg_host;
+ if (alone == sg_alone || !host || (alone && !sg_open)) return;
+ SGRLyricsLayout l = layoutIn(host);
+ if (!l.ok) return;
+ sg_alone = alone;
+ if (alone) {
+ [sg_rest invalidate];
+ sg_rest = nil;
+ l.stage = aloneStage(host, l);
+ }
+ SGRPlayerLyricsOverlay *overlay = objc_getAssociatedObject(host, &kOverlayKey);
+ NSArray<UIView *> *controls = controlViews();
+ void (^apply)(void) = ^{
+ if (overlay.superview) {
+ place(overlay, host, l);
+ overlay.thumb.transform = thumbTransform(l);
+ overlay.thumb.alpha = alone ? 0 : 1;
+ overlay.lyrics.extrasHidden = alone;
+ }
+ for (UIView *view in controls) view.alpha = alone ? 0 : 1;
+ };
+ if (alone && !sg_shield) {
+ sg_shield = [[SGRPlayerShield alloc] initWithFrame:host.bounds];
+ [host addSubview:sg_shield];
+ } else if (!alone) {
+ [sg_shield removeFromSuperview];
+ sg_shield = nil;
+ }
+ if (animated) SGRAnimate(SGRMotionLayout, apply, nil);
+ else apply();
+ SGLog(@"redesign player: the lines %@", alone ? @"on their own" : @"with the controls");
+}
+
static void setOpen(BOOL open, BOOL animated) {
UIView *host = sg_host;
if (open == sg_open) return;
@@ -300,6 +448,7 @@ static void setOpen(BOOL open, BOOL animated) {
SGLog(@"redesign player: no artwork read yet, the lyrics stay down");
return;
}
+ if (!open) setAlone(NO, NO);
sg_open = open;
SGRPlayerLyricsChanged();
@@ -313,7 +462,7 @@ static void setOpen(BOOL open, BOOL animated) {
overlay.cover.image = SGRNowPlayingArtwork(NULL, NULL);
overlay.stage.alpha = 0;
overlay.stage.transform = CGAffineTransformMakeScale(kLyricsEnterScale, kLyricsEnterScale);
- [overlay lyrics];
+ overlay.lyrics.browsingBegan = ^{ setAlone(YES, YES); };
// Spotify's cover goes the moment the redesign's own takes its place: the same picture at the
// same size with the same corners, so there is nothing to see in the swap. Coming back it waits
// for the thumbnail to land on it, or the two would be on screen at once, one of them half size.
@@ -350,6 +499,11 @@ static void setOpen(BOOL open, BOOL animated) {
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:show completion:nil];
}
+ if (open) restartRest();
+ else {
+ [sg_rest invalidate];
+ sg_rest = nil;
+ }
SGLog(@"redesign player: lyrics %@, thumbnail %.0fx%.0f at %.0f,%.0f, title row up %.0f and right %.0f, lines %.0fx%.0f",
open ? @"up" : @"away", l.thumb.size.width, l.thumb.size.height, l.thumb.origin.x, l.thumb.origin.y,
-l.lift, l.shift, l.stage.size.width, l.stage.size.height);
@@ -372,6 +526,12 @@ static void replace(void) {
if (!sg_open) return;
SGRPlayerLyricsOverlay *overlay = objc_getAssociatedObject(host, &kOverlayKey);
if (!overlay.superview) return;
+ if (sg_alone) {
+ l.stage = aloneStage(host, l);
+ for (UIView *view in controlViews()) view.alpha = 0;
+ if (sg_shield.superview != host) [host addSubview:sg_shield];
+ else [host bringSubviewToFront:sg_shield];
+ }
place(overlay, host, l);
overlay.thumb.transform = thumbTransform(l);
overlay.cover.layer.cornerRadius = thumbRadius(l, YES);
@@ -386,8 +546,10 @@ static void replace(void) {
%orig;
UIView *host = ((UIViewController *)self).viewIfLoaded;
if (!host || host.bounds.size.height < kLivingHeight) return;
+ sg_player = (UIViewController *)self;
if (sg_host != host) {
sg_host = host;
+ [host addGestureRecognizer:[SGRPlayerTouchWatcher new]];
SGLog(@"redesign player: the lyrics have the player's view %.0fx%.0f", host.bounds.size.width, host.bounds.size.height);
}
replace();
@@ -450,6 +612,14 @@ static void replace(void) {
}
- (void)playerStateDidChange:(SPTPlayerState *)state {
+ // A pause brings the controls back and keeps them; playing again starts the clock.
+ if (state.isPaused) {
+ setAlone(NO, YES);
+ [sg_rest invalidate];
+ sg_rest = nil;
+ } else if (!sg_rest && !sg_alone) {
+ restartRest();
+ }
NSString *track = SGURIString(state.track.URI);
if (!track || [track isEqualToString:_track]) return;
_track = track;
@@ -475,6 +645,15 @@ static SGRPlayerLyricsWatcher *sg_watcher;
%init;
sg_watcher = [SGRPlayerLyricsWatcher new];
SGAddPlayerStateObserver(sg_watcher);
+ // In the background the controls stay up; back in front the clock starts again.
+ [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
+ setAlone(NO, NO);
+ [sg_rest invalidate];
+ sg_rest = nil;
+ }];
+ [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
+ restartRest();
+ }];
[NSNotificationCenter.defaultCenter addObserverForName:SGRNowPlayingArtworkDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
if (!sg_open) return;
SGRPlayerLyricsOverlay *overlay = objc_getAssociatedObject(sg_host, &kOverlayKey);