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: Apple Music style lyrics only in the redesign and always on there, the native karaoke copy gone, and AGENTS.md and CLAUDE.md for agents

Vojtěch Škopekcc24a25e29ea
diff --git a/AGENTS.md b/AGENTS.md
new file mode 100644
index 00000000..f8993dcb
--- /dev/null
+++ b/AGENTS.md
@@ -0,0 +1,55 @@
+# spoti.pw
+
+A Theos tweak (Objective-C + Logos) injected into the decrypted Spotify iOS app. The full guide is
+`docs/tweaks.md`; read it before changing code.
+
+## Two looks, never both
+
+**Redesigned UI** (Mod Settings → Appearance) picks one of two looks, read once at launch:
+
+- **Native**: Spotify's own screens with the mod's tweaks on them (hide switches, glass header buttons,
+ Home gradient, AMOLED switch, accent colour...).
+- **Redesigned**: the mod's own Liquid Glass look, from a clean sheet. It includes the glass tab bar,
+ search field and now playing bar, the redesigned player and lyrics page with Apple Music style lyrics
+ always on, black throughout, and its own accent colour. No native tweak runs.
+
+Anything that doesn't draw on Spotify's screens works the same under both: ads, privacy, lyrics
+sources, gestures, blocked artists, flags.
+
+## Where code goes (`tweak/Sources/`)
+
+| Layer | What | Gate at the top of every `%ctor` |
+|---|---|---|
+| `Shared/` | behaviour and data, either look | none |
+| `Native/` | Spotify's screens tweaked | `if (!SGNativeUI()) return;` |
+| `Redesigned/` | the redesign (`Kit/` + parts) | `if (!SGRedesignedUI()) return;` |
+| `App/` | Mod Settings root and combined pages, Mod page, tour | none |
+| `Core/`, `Settings/`, `Headers/`, `Diagnostics/` | infrastructure | none |
+
+Rules:
+
+- Imports run one way: `Core <- Settings <- Shared <- Native | Redesigned <- App`. Native and
+ Redesigned never import each other. `scripts/check-layers.sh` runs before every build and fails on a
+ violation. Don't work around it.
+- Change one look without touching the other. When both need the same thing, each side keeps its own
+ copy under its own names (`SG…` native, `SGR…` redesign) and its own keys. Examples are the tab bar
+ composition and editor, AMOLED and the accent colour. Don't merge copies back into
+ a shared file.
+- A lower layer that needs something from a higher one gets it through a registry in `Core`
+ (forced flags: `Core/SGFlagForce.h`) or a function declared low and defined high.
+- Settings: each layer builds its own rows and sections. `App/Pages.m` and `App/ModSettings.x`
+ decide what to show from `SGRedesignedUIStored()`, so native-only pages disappear in the redesign.
+- Keys start with `spotifyglass.` (redesign-only ones with `spotifyglass.redesign.`). Switches are
+ read at launch, so a change needs a restart.
+
+## Working on it
+
+- Build: `make install` (signs and pushes to the phone), `make release` (IPA only). Tweak only:
+ `env -u MAKELEVEL gmake -C tweak clean package`.
+- Look at Spotify's views through recorded trees (`make session` records clean ones into
+ `trees/clean/`) before hooking anything. Prove every class and selector against the tree or the binary.
+- Device log: `make log` (`[spotifyglass]` lines).
+- Known traps: anything pushed onto Spotify's nav stack must conform to `SPTPageController`
+ (`Settings/SGPage.m`). Setting `hidden` on views inside Spotify's `OverflowStackView` or its Encore
+ stacks crashes, so use alpha. A `CADisplayLink` capped at 60 Hz drags the player's 120 Hz
+ transitions down with it.
diff --git a/CLAUDE.md b/CLAUDE.md
new file mode 100644
index 00000000..43c994c2
--- /dev/null
+++ b/CLAUDE.md
@@ -0,0 +1 @@
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 105cd9e1..8394a32e 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -39,8 +39,8 @@ screens works under both. So the sources are four layers, each a directory of fe
The two looks never run together, so each hooks the same Spotify class in its own way, and a part of
the look is edited on its own side without touching the other: where both need the same thing, each
-has its own copy (the karaoke view, the tab bar's composition and its editor, the lyrics page's glass,
-the soft top edge), under its own names (SG… native, SGR… redesign) and its own keys. The imports run
+has its own copy (the tab bar's composition and its editor, the lyrics page's glass, the soft top edge,
+AMOLED, the accent colour), under its own names (SG… native, SGR… redesign) and its own keys. The imports run
one way, Core <- Settings <- Shared <- Native | Redesigned <- App, and `scripts/check-layers.sh`, run
by tweak/Makefile before every build, fails on any other. A layer below that needs something from one
above takes it through a registry in Core (forced flags, SGFlagForce.h) or a function declared low and
@@ -64,8 +64,9 @@ Shared:
ArtistBlock/ tracks by blocked artists skipped as they start (ArtistSkip.x), the list and the Blocked artists page under Player
Flags/ Spotify's remote-config flags: the provider hook, the generated table, the All flags page and the Labs page
Gestures/ the double tap zones on the player: the grid, what each cell does, the recognizer (each look hooks it on)
- Lyrics/ Apple Music style lyrics' engine: lines read from color-lyrics and the player's clock (KaraokeSource.x),
- words timed by estimate inside Spotify's line times (KaraokeTiming.m), and the Lyrics page's rows
+ Lyrics/ the lyrics engine for the redesign's Apple Music style lyrics and the lock screen: lines read from
+ color-lyrics and the player's clock (KaraokeSource.x), words timed by estimate inside Spotify's line
+ times (KaraokeTiming.m), and the Lyrics page's parts
LyricsSources/ the sources lyrics come from, asked in the order the Lyrics page puts them in and merged into the
best answer (LyricsSources.m, the list to drag in LyricsSourcesPage.m): Apple Music's TTML from
BiniLyrics.m and Unison.m, read by SGTTML.m, which is the only shape carrying a second voice and the
@@ -91,8 +92,8 @@ Native:
Navbar/ Spotify's tab bar composed (Navbar.x, NavbarLayout.m, hooked from TabBarHooks.x), the Navbar and Add a tab pages
NowPlayingBar/ the device button hidden, the bar's flags
Player/ the full screen player (Player.x), its cards and buttons hidden (PlayerDeclutter.x), the glass lyrics card
- (LyricsCard.x), karaoke on the card (KaraokeCard.x), the gestures' hookup, the Queue & devices flags
- Lyrics/ the full screen lyrics page on glass (LyricsPage.x), karaoke over it (KaraokePage.x, KaraokeView.m)
+ (LyricsCard.x), the gestures' hookup, the Queue & devices flags
+ Lyrics/ the full screen lyrics page on glass (LyricsPage.x)
Home/ the Home gradient, Home's sections and pills hidden (HomeDeclutter.x), the Home & Library page
Playlist/ the playlist header and pills, hidden one switch each
Album/, Artist/ their pages' parts hidden, and the cover or photo behind their headers
@@ -105,7 +106,8 @@ Redesigned:
Navbar/ the glass tab bar (TabBar.x) over its own composition (Navbar.x, NavbarLayout.m) and editor, the glass search field
NowPlayingBar/ the glass now playing bar
Player/ the redesigned full screen player (Player.h lists its files)
- Lyrics/ the full screen lyrics page on glass and its own karaoke view (SGRKaraokeView)
+ Lyrics/ the full screen lyrics page on glass with Apple Music style lyrics over it, always on (SGRKaraokeView,
+ also over the card under the player, Player/KaraokeCard.x)
App:
@@ -146,9 +148,8 @@ is set. Redesigned UI is the one switch between the two looks (see Layers): it g
(Settings/SGGlowSwitch), its ⓘ says what it changes, and flipping it offers to restart Spotify.
The pages show only what the stored look has: a page opened after flipping the switch already shows
what the restart will bring. Then a card of parts. Navbar: the tab editor of the stored look, each with
-its own list of tabs. Player: Gestures, Lyrics (Apple Music style, the lock screen, in the native look
-glass lyrics, the ordered list of lyrics sources, lyrics for every track, naming the source, the lyrics
-flags), Blocked artists (with the count on the row) and Lock screen widget, which work with either look;
+its own list of tabs. Player: Gestures, Lyrics (the ordered list of lyrics sources, lyrics for every track,
+naming the source in the redesign, the lock screen, and glass lyrics in the native look), Blocked artists (with the count on the row) and Lock screen widget, which work with either look;
in the native look also Now playing bar (its device button and its flags), Queue & devices, and
Spotify's own player screen (artwork background, glass header buttons, Disable Canvas and the sheet,
header, slider and sticky header flags, the cards under the player and the lyrics preview and player
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index ee97ca6b..2ddeb93e 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -47,10 +47,13 @@ UIViewController *SGNavbarPage(void) {
return SGRedesignedUIStored() ? SGRNavbarSettingsPage() : SGNavbarSettingsPage();
}
+// The redesign always draws Apple Music style lyrics, and only it names their source; the native look
+// has its glass card and page instead.
static UIViewController *lyricsPage(void) {
- NSMutableArray<SGModRow *> *look = [SGLyricsLookRows() mutableCopy];
- if (!SGRedesignedUIStored()) [look insertObject:SGGlassLyricsRow() atIndex:1];
- NSArray<SGModSection *> *sections = [@[SGSection(nil, look)] arrayByAddingObjectsFromArray:SGLyricsSourceSections()];
+ BOOL redesigned = SGRedesignedUIStored();
+ NSMutableArray<SGModRow *> *more = [NSMutableArray arrayWithObject:SGLockScreenLyricsRow()];
+ if (!redesigned) [more insertObject:SGGlassLyricsRow() atIndex:0];
+ NSArray<SGModSection *> *sections = @[SGLyricsSourcesSection(redesigned), SGSection(nil, more)];
return [[SGModPage alloc] initWithTitle:@"Lyrics" intro:SGRestartNote sections:sections footer:nil];
}
diff --git a/tweak/Sources/Core/SGViewTree.m b/tweak/Sources/Core/SGViewTree.m
index eb57b47b..1714a25c 100644
--- a/tweak/Sources/Core/SGViewTree.m
+++ b/tweak/Sources/Core/SGViewTree.m
@@ -41,8 +41,7 @@ BOOL SGKeepsColor(UIView *view) {
void SGStripBackgrounds(UIView *view) {
if ([view isKindOfClass:UIVisualEffectView.class]) return;
// The karaoke views paint nothing to strip and hold a label for every word in sight.
- NSString *name = NSStringFromClass(view.class);
- if ([name hasPrefix:@"SGKaraoke"] || [name hasPrefix:@"SGRKaraoke"]) return;
+ if ([NSStringFromClass(view.class) hasPrefix:@"SGRKaraoke"]) return;
if (!SGKeepsColor(view)) view.layer.backgroundColor = NULL;
if ([view.layer isKindOfClass:CAGradientLayer.class] || [NSStringFromClass(view.class) containsString:@"GradientView"]) view.hidden = YES;
for (CALayer *layer in view.layer.sublayers) {
diff --git a/tweak/Sources/Native/Lyrics/KaraokePage.x b/tweak/Sources/Native/Lyrics/KaraokePage.x
deleted file mode 100644
index 135d19df..00000000
--- a/tweak/Sources/Native/Lyrics/KaraokePage.x
+++ /dev/null
@@ -1,31 +0,0 @@
-// The karaoke view goes over the lyrics part of the full screen lyrics page (trees/lyrics.txt:
-// Lyrics_FullscreenElementPageImpl.LyricsView, between the header and the controls), so Spotify's
-// header, close button and controls stay working. It is laid over rather than swapped in: Spotify's
-// list keeps running underneath and comes back the moment a track has no synced lyrics.
-#import "Core/SGCore.h"
-#import "Native/Lyrics/KaraokeView.h"
-
-static char kKaraokeKey;
-
-%hook _TtC32Lyrics_FullscreenElementPageImpl10LyricsView
-- (void)layoutSubviews {
- %orig;
- UIView *host = (UIView *)self;
- SGKaraokeView *view = objc_getAssociatedObject(host, &kKaraokeKey);
- if (!view) {
- SGLog(@"karaoke: page opened, playing %@", SGKaraokePlayingTrack());
- view = [[SGKaraokeView alloc] initWithFrame:host.bounds];
- objc_setAssociatedObject(host, &kKaraokeKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- if (view.superview == host) [host bringSubviewToFront:view];
- else [host addSubview:view];
- view.frame = host.bounds;
- [view syncSiblings];
-}
-%end
-
-%ctor {
- if (!SGNativeUI() || !SGFlag(SGKeyKaraokeLyrics, NO)) return;
- %init;
- SGRequireClasses(@[@"_TtC32Lyrics_FullscreenElementPageImpl10LyricsView"]);
-}
diff --git a/tweak/Sources/Native/Lyrics/KaraokeView.h b/tweak/Sources/Native/Lyrics/KaraokeView.h
deleted file mode 100644
index e764c205..00000000
--- a/tweak/Sources/Native/Lyrics/KaraokeView.h
+++ /dev/null
@@ -1,13 +0,0 @@
-// The native look's Apple Music style lyrics view, over Spotify's full screen lyrics page
-// (KaraokePage.x) and, compact, over the card under the player (Native/Player/KaraokeCard.x). The
-// lines and the clock are Shared/Lyrics/Lyrics.h's.
-#import <UIKit/UIKit.h>
-#import "Shared/Lyrics/Lyrics.h"
-
-@interface SGKaraokeView : UIView
-// Compact is the card under the player: Spotify's own type size, no seeking by tap and no margin of
-// its own, since the card already insets what it holds. initWithFrame: is the full screen page.
-- (instancetype)initWithFrame:(CGRect)frame compact:(BOOL)compact;
-// Hides Spotify's own lyrics next to this view while it has lyrics to show, and brings them back when not.
-- (void)syncSiblings;
-@end
diff --git a/tweak/Sources/Native/Lyrics/KaraokeView.m b/tweak/Sources/Native/Lyrics/KaraokeView.m
deleted file mode 100644
index 0f4f88ed..00000000
--- a/tweak/Sources/Native/Lyrics/KaraokeView.m
+++ /dev/null
@@ -1,742 +0,0 @@
-#import "Core/SGCore.h"
-#import "Native/Lyrics/KaraokeView.h"
-#import "Shared/LyricsSources/LyricsSources.h"
-#import "Shared/Player/PlayerEvents.h"
-
-static const CGFloat kFontSize = 30, kMargin = 24, kLineGap = 24, kRowTighten = 2;
-// The card under the player is a seventh of the page's height, so it gets Spotify's own card type
-// size and the spacing its rows sit on, and no blur: at that size the dimming does the work, and a
-// blur filter on every line in sight, under the card's glass, made the player stutter as it opened.
-static const CGFloat kCardFontSize = 21, kCardMargin = 0, kCardLineGap = 16;
-static const CGFloat kDimAlpha = 0.3, kFillEdge = 22, kLift = 2.5, kDimScale = 0.97;
-static const CGFloat kAnchor = 0.28; // where the sung line rests, as a share of the height
-static const CGFloat kEdgeFade = 0.1; // the lines fade out over this share at the top and bottom
-static const CGFloat kBlurPerLine = 1.4, kMaxBlur = 6;
-// The (oh, aye) hanging under a line: smaller, a little dimmer, and just clear of it.
-static const CGFloat kBackingScale = 0.62, kBackingAlpha = 0.8, kBackingGap = 4;
-// The line naming the source, under the lyrics and outside the fade so it does not dim with them.
-static const CGFloat kCreditSize = 12, kCreditAlpha = 0.4, kCreditBottom = 10;
-static const NSTimeInterval kBrowseHold = 3; // after scrolling by hand, how long until it follows the song again
-static const double kFloatMinMs = 700, kFloatLeadMs = 80; // a short word still floats up this slowly
-static const double kClockSnapMs = 250, kClockPull = 0.08;
-// Lines get views this far outside the visible part, in screen heights: half a screen above it
-// and below, and a quarter more before a view is let go. The card under the player is in the tree
-// for every open and close of the player, and every view it holds is one more for the window to
-// take in and let go; a line comes into view about once in three seconds, and a page flung by
-// hand fills in at a few lines a frame.
-static const CGFloat kSightBehind = 0.5, kSightAhead = 0.5, kSightSlack = 0.25;
-static const NSTimeInterval kTransitionSlack = 0.05; // after the player's animation, before the link is back
-static NSString *const kBlurPath = @"filters.gaussianBlur.inputRadius";
-
-@interface CAFilter : NSObject
-+ (instancetype)filterWithType:(NSString *)type;
-@end
-
-static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect frame) {
- UILabel *label = [[UILabel alloc] initWithFrame:frame];
- label.text = text;
- label.font = font;
- label.textColor = color;
- return label;
-}
-
-#pragma mark - a word
-
-// The word twice: dim underneath, white on top behind a mask whose feathered edge slides across it.
-@interface SGKaraokeWordView : UIView
-@property (nonatomic, readonly) SGKaraokeWord *word;
-@property (nonatomic, readonly) UILabel *lit;
-@property (nonatomic) CGFloat offset; // where the word starts along its line, rows laid end to end
-- (void)fillTo:(CGFloat)cursor;
-- (void)floatAt:(double)ms;
-- (void)settle;
-@end
-
-@implementation SGKaraokeWordView {
- CAGradientLayer *_fill;
- CGFloat _filled, _lift;
-}
-
-- (instancetype)initWithWord:(SGKaraokeWord *)word font:(UIFont *)font {
- CGSize size = [word.text sizeWithAttributes:@{NSFontAttributeName: font}];
- self = [super initWithFrame:CGRectMake(0, 0, ceil(size.width), ceil(font.lineHeight))];
- if (!self) return nil;
- _word = word;
- [self addSubview:wordLabel(word.text, font, [UIColor colorWithWhite:1 alpha:kDimAlpha], self.bounds)];
- _lit = wordLabel(word.text, font, UIColor.whiteColor, self.bounds);
- // Hidden until the line is sung: a masked layer is drawn offscreen every frame even when the
- // mask leaves nothing of it, and a song has hundreds of words waiting their turn.
- _lit.hidden = YES;
- [self addSubview:_lit];
-
- CGFloat width = self.bounds.size.width + kFillEdge;
- _fill = [CAGradientLayer layer];
- _fill.colors = @[(id)UIColor.whiteColor.CGColor, (id)UIColor.whiteColor.CGColor, (id)UIColor.clearColor.CGColor];
- _fill.locations = @[@0, @((width - kFillEdge) / width), @1];
- _fill.startPoint = CGPointMake(0, 0.5);
- _fill.endPoint = CGPointMake(1, 0.5);
- _lit.layer.mask = _fill;
- _filled = NAN;
- [self fillTo:-CGFLOAT_MAX];
- return self;
-}
-
-// The cursor is in line units and the feathered edge is centred on it, so the edge runs on through
-// the space into the next word instead of starting over at each one.
-- (void)fillTo:(CGFloat)cursor {
- CGFloat width = self.bounds.size.width, height = self.bounds.size.height;
- CGFloat local = MAX(-kFillEdge / 2, MIN(width + kFillEdge / 2, cursor - _offset));
- if (local == _filled) return;
- _filled = local;
- [CATransaction begin];
- [CATransaction setDisableActions:YES];
- _fill.frame = CGRectMake(local - kFillEdge / 2 - width, -height / 2, width + kFillEdge, height * 2);
- [CATransaction commit];
-}
-
-// Rises like a critically damped spring let go as the word starts: no jolt, a long soft landing.
-// x = 5 at the end of the word is 96 % of the way up.
-- (void)floatAt:(double)ms {
- double x = MAX(0, ms - _word.start + kFloatLeadMs) / MAX(_word.end - _word.start, kFloatMinMs) * 5;
- CGFloat lift = kLift * (1 - (1 + x) * exp(-x));
- if (lift == _lift) return;
- _lift = lift;
- self.transform = CGAffineTransformMakeTranslation(0, -lift);
-}
-
-- (void)settle {
- _lift = 0;
- self.transform = CGAffineTransformIdentity;
-}
-
-@end
-
-#pragma mark - a line
-
-@interface SGKaraokeLineView : UIView
-@property (nonatomic, readonly) SGKaraokeLine *line;
-@property (nonatomic) BOOL active;
-@property (nonatomic) CGFloat blur;
-- (instancetype)initWithLine:(SGKaraokeLine *)line width:(CGFloat)width font:(UIFont *)font backing:(BOOL)backing blurred:(BOOL)blurred;
-- (void)showTime:(double)ms;
-@end
-
-typedef struct {
- double ms, x, slope;
-} SGSweepKnot;
-
-@implementation SGKaraokeLineView {
- NSArray<SGKaraokeWordView *> *_words;
- NSUInteger _generation;
- SGSweepKnot *_knots;
- NSUInteger _knotCount;
- SGKaraokeLineView *_backing;
-}
-
-- (instancetype)initWithLine:(SGKaraokeLine *)line width:(CGFloat)width font:(UIFont *)font backing:(BOOL)backing blurred:(BOOL)blurred {
- self = [super initWithFrame:CGRectZero];
- if (!self) return nil;
- _line = line;
- CGFloat space = ceil([@" " sizeWithAttributes:@{NSFontAttributeName: font}].width);
- CGFloat row = ceil(font.lineHeight) - kRowTighten, x = 0, y = 0, offset = 0;
- NSMutableArray<SGKaraokeWordView *> *words = [NSMutableArray array];
- NSMutableArray<NSMutableArray<SGKaraokeWordView *> *> *rows = [NSMutableArray arrayWithObject:[NSMutableArray array]];
- for (SGKaraokeWord *word in line.words) {
- SGKaraokeWordView *view = [[SGKaraokeWordView alloc] initWithWord:word font:font];
- CGSize size = view.bounds.size;
- // A joined word follows the one before it flush: the scripts that do not space their words
- // would otherwise read with a gap between every syllable.
- CGFloat lead = x > 0 && !word.joined ? space : 0;
- if (x > 0 && x + lead + size.width > width) {
- x = lead = 0;
- y += row;
- [rows addObject:[NSMutableArray array]];
- }
- x += lead;
- offset += lead;
- view.center = CGPointMake(x + size.width / 2, y + size.height / 2);
- view.offset = offset;
- x += size.width;
- offset += size.width;
- [self addSubview:view];
- [words addObject:view];
- [rows.lastObject addObject:view];
- }
- // A second voice is laid against the far edge, as Apple Music sets the two sides of a duet apart.
- if (line.align == SGKaraokeAlignTrailing) {
- for (NSArray<SGKaraokeWordView *> *wrapped in rows) {
- if (!wrapped.count) continue;
- CGFloat shift = width - CGRectGetMaxX(wrapped.lastObject.frame);
- if (shift <= 0) continue;
- for (SGKaraokeWordView *view in wrapped) view.center = CGPointMake(view.center.x + shift, view.center.y);
- }
- }
- _words = words;
-
- CGFloat height = y + ceil(font.lineHeight);
- if (line.backing.words.count && !backing) {
- UIFont *smaller = [UIFont systemFontOfSize:round(font.pointSize * kBackingScale) weight:UIFontWeightBold];
- _backing = [[SGKaraokeLineView alloc] initWithLine:line.backing width:width font:smaller backing:YES blurred:NO];
- _backing.alpha = kBackingAlpha;
- _backing.frame = CGRectMake(0, height + kBackingGap, width, _backing.bounds.size.height);
- [self addSubview:_backing];
- height = CGRectGetMaxY(_backing.frame);
- }
- self.frame = CGRectMake(0, 0, width, height);
- [self buildSweep];
-
- // Scales toward the edge its text is aligned to, and a backing row rides its line's blur.
- if (backing) return self;
- self.layer.anchorPoint = CGPointMake(line.align == SGKaraokeAlignTrailing ? 1 : 0, 0.5);
- if (!blurred) return self;
- CAFilter *blur = [NSClassFromString(@"CAFilter") filterWithType:@"gaussianBlur"];
- if (blur) self.layer.filters = @[blur];
- return self;
-}
-
-// The height a line takes at a width, its rows counted the way initWithLine:width:font:backing:blurred:
-// lays them, so the page can place every line of a song while making views for the few in sight.
-static CGFloat lineHeight(SGKaraokeLine *line, CGFloat width, UIFont *font, BOOL backing) {
- CGFloat space = ceil([@" " sizeWithAttributes:@{NSFontAttributeName: font}].width);
- CGFloat row = ceil(font.lineHeight) - kRowTighten, x = 0, y = 0;
- for (SGKaraokeWord *word in line.words) {
- CGFloat wordWidth = ceil([word.text sizeWithAttributes:@{NSFontAttributeName: font}].width);
- CGFloat lead = x > 0 && !word.joined ? space : 0;
- if (x > 0 && x + lead + wordWidth > width) {
- x = lead = 0;
- y += row;
- }
- x += lead + wordWidth;
- }
- CGFloat height = y + ceil(font.lineHeight);
- if (line.backing.words.count && !backing) {
- UIFont *smaller = [UIFont systemFontOfSize:round(font.pointSize * kBackingScale) weight:UIFontWeightBold];
- height += kBackingGap + lineHeight(line.backing, width, smaller, YES);
- }
- return height;
-}
-
-// Where each line starts in the stack, from the heights alone. Measuring text is safe off the main
-// thread, and a song's worth of it is kept off it: the first card of a track lays out while the
-// player is opening, and a frame that measured every line then was a frame the animation lost.
-static NSArray<NSNumber *> *topsOf(NSArray<SGKaraokeLine *> *lines, CGFloat width, UIFont *font, CGFloat gap) {
- NSMutableArray<NSNumber *> *tops = [NSMutableArray arrayWithCapacity:lines.count];
- CGFloat top = 0;
- for (SGKaraokeLine *line in lines) {
- [tops addObject:@(top)];
- top += lineHeight(line, width, font, NO) + gap;
- }
- return tops;
-}
-
-// How many line views are made in one frame: the rest follow on the next, nearest the sung line first.
-static const NSUInteger kLinesPerFrame = 4;
-
-- (void)dealloc {
- free(_knots);
-}
-
-static double secant(SGSweepKnot *knots, NSUInteger i) {
- return (knots[i + 1].x - knots[i].x) / (knots[i + 1].ms - knots[i].ms);
-}
-
-// The fill cursor reaches each word's left edge as the word starts and clears the last word as it
-// ends, on a monotone cubic through those points, so the pace changes between words without a kink.
-- (void)buildSweep {
- _knots = calloc(_words.count + 1, sizeof(SGSweepKnot));
- if (!_words.count) return;
- for (NSUInteger i = 0; i <= _words.count; i++) {
- SGKaraokeWordView *word = _words[MIN(i, _words.count - 1)];
- double ms = i < _words.count ? word.word.start : word.word.end;
- double x = i == 0 ? -kFillEdge / 2 : i < _words.count ? word.offset : word.offset + word.bounds.size.width + kFillEdge / 2;
- if (_knotCount && ms <= _knots[_knotCount - 1].ms) {
- _knots[_knotCount - 1].x = x;
- continue;
- }
- _knots[_knotCount++] = (SGSweepKnot){ms, x, 0};
- }
- // Fritsch-Carlson slopes: capped so the cursor never runs backwards or overshoots a word.
- for (NSUInteger k = 0; k < _knotCount; k++) {
- BOOL first = k == 0, last = k + 1 == _knotCount;
- if (first && last) break;
- if (first || last) {
- _knots[k].slope = secant(_knots, first ? 0 : k - 1);
- continue;
- }
- double before = secant(_knots, k - 1), after = secant(_knots, k);
- double h0 = _knots[k].ms - _knots[k - 1].ms, h1 = _knots[k + 1].ms - _knots[k].ms;
- _knots[k].slope = MIN(MIN(2 * before, 2 * after), (before * h1 + after * h0) / (h0 + h1));
- }
-}
-
-- (CGFloat)cursorAt:(double)ms {
- if (!_knotCount) return -CGFLOAT_MAX;
- if (ms <= _knots[0].ms) return _knots[0].x;
- if (_knotCount == 1 || ms >= _knots[_knotCount - 1].ms) return _knots[_knotCount - 1].x;
- NSUInteger k = 0;
- while (ms >= _knots[k + 1].ms) k++;
- SGSweepKnot a = _knots[k], b = _knots[k + 1];
- double h = b.ms - a.ms, u = (ms - a.ms) / h, u2 = u * u, u3 = u2 * u;
- return (2 * u3 - 3 * u2 + 1) * a.x + (u3 - 2 * u2 + u) * h * a.slope
- + (3 * u2 - 2 * u3) * b.x + (u3 - u2) * h * b.slope;
-}
-
-// Eases from wherever the blur is on screen, so a line coming into focus sharpens instead of snapping.
-- (void)setBlur:(CGFloat)blur {
- if (blur == _blur) return;
- id shown = [self.layer.presentationLayer valueForKeyPath:kBlurPath];
- CGFloat from = [shown isKindOfClass:NSNumber.class] ? [shown doubleValue] : _blur;
- _blur = blur;
- if (!self.layer.filters) return;
- [self.layer setValue:@(blur) forKeyPath:kBlurPath];
- CABasicAnimation *ease = [CABasicAnimation animationWithKeyPath:kBlurPath];
- ease.fromValue = @(from);
- ease.toValue = @(blur);
- ease.duration = 0.6;
- ease.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- [self.layer addAnimation:ease forKey:@"blur"];
-}
-
-- (void)setActive:(BOOL)active {
- if (active == _active) return;
- _active = active;
- _backing.active = active;
- NSUInteger generation = ++_generation;
- if (active) {
- for (SGKaraokeWordView *word in _words) {
- [word.layer removeAllAnimations];
- [word.lit.layer removeAllAnimations];
- word.lit.alpha = 1;
- word.lit.hidden = NO;
- [word fillTo:-CGFLOAT_MAX];
- [word settle];
- }
- return;
- }
- // A sung line fades back to dim rather than dropping its fill at once, and its words sink back
- // on a spring slow enough to still be seen doing it.
- [UIView animateWithDuration:0.9 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0
- options:UIViewAnimationOptionAllowUserInteraction
- animations:^{
- for (SGKaraokeWordView *word in self->_words) [word settle];
- } completion:nil];
- [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
- for (SGKaraokeWordView *word in self->_words) word.lit.alpha = 0;
- } completion:^(BOOL finished) {
- if (generation != self->_generation) return;
- for (SGKaraokeWordView *word in self->_words) {
- [word fillTo:-CGFLOAT_MAX];
- word.lit.hidden = YES;
- word.lit.alpha = 1;
- }
- }];
-}
-
-- (void)showTime:(double)ms {
- CGFloat cursor = [self cursorAt:ms];
- for (SGKaraokeWordView *word in _words) {
- [word fillTo:cursor];
- [word floatAt:ms];
- }
- [_backing showTime:ms]; // timed on its own, so it lags the line as it is sung
-}
-
-@end
-
-#pragma mark - the page
-
-@interface SGKaraokeView () <UIScrollViewDelegate>
-@end
-
-@implementation SGKaraokeView {
- UIScrollView *_scroll;
- BOOL _browsing;
- CADisplayLink *_link;
- NSString *_track;
- NSArray<SGKaraokeLine *> *_lines;
- // The song is placed from its lines' heights alone; views exist for the lines in and near sight.
- NSArray<NSNumber *> *_tops; // where each line starts in the stack
- NSMutableDictionary<NSNumber *, SGKaraokeLineView *> *_shown; // the views there are, by line
- CGFloat _focusTop; // the top of the line the stack is arranged around
- CGFloat _sightOffset, _sightFocus; // what the views in sight were last chosen for
- NSInteger _sightActive;
- NSUInteger _build; // counts the songs and widths measured, so a measurement that is late is dropped
- UIFont *_font;
- NSInteger _active;
- CGFloat _builtWidth;
- BOOL _showing;
- CAGradientLayer *_fade;
- UILabel *_credit;
- CGFloat _fontSize, _margin, _lineGap, _blurPerLine, _maxBlur;
- BOOL _compact;
- BOOL _crediting; // the switch is read once: the page asks for the source on every frame until it has one
- double _clock;
- NSInteger _reported;
- CFTimeInterval _clockTime;
-}
-
-- (instancetype)initWithFrame:(CGRect)frame {
- return [self initWithFrame:frame compact:NO];
-}
-
-- (instancetype)initWithFrame:(CGRect)frame compact:(BOOL)compact {
- self = [super initWithFrame:frame];
- if (!self) return nil;
- self.hidden = YES;
- _active = -1;
- _compact = compact;
- _fontSize = compact ? kCardFontSize : kFontSize;
- _margin = compact ? kCardMargin : kMargin;
- _lineGap = compact ? kCardLineGap : kLineGap;
- _blurPerLine = compact ? 0 : kBlurPerLine;
- _maxBlur = compact ? 0 : kMaxBlur;
- _shown = [NSMutableDictionary dictionary];
- _sightActive = -2;
- _fade = [CAGradientLayer layer];
- _fade.colors = @[(id)UIColor.clearColor.CGColor, (id)UIColor.whiteColor.CGColor,
- (id)UIColor.whiteColor.CGColor, (id)UIColor.clearColor.CGColor];
- _fade.locations = @[@0, @(kEdgeFade), @(1 - kEdgeFade), @1];
- _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
- _scroll.layer.mask = _fade;
- _scroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- _scroll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- _scroll.showsVerticalScrollIndicator = NO;
- _scroll.alwaysBounceVertical = YES;
- _scroll.scrollsToTop = NO;
- _scroll.delegate = self;
- [self addSubview:_scroll];
- _credit = [[UILabel alloc] initWithFrame:CGRectZero];
- _credit.font = [UIFont systemFontOfSize:kCreditSize weight:UIFontWeightSemibold];
- _credit.textColor = [UIColor colorWithWhite:1 alpha:kCreditAlpha];
- _credit.hidden = YES;
- _crediting = !compact && SGFlag(SGKeyLyricsCredit, NO); // the card has no room to name a source
- [self addSubview:_credit];
- // A tap on the card is Spotify's, and opens the full screen page; seeking by tap is the page's.
- if (compact) self.userInteractionEnabled = NO;
- else [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]];
- [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionNotification object:nil];
- [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionEndedNotification object:nil];
- return self;
-}
-
-- (void)dealloc {
- [NSNotificationCenter.defaultCenter removeObserver:self];
-}
-
-- (void)tapped:(UITapGestureRecognizer *)tap {
- CGPoint point = [tap locationInView:_scroll];
- for (SGKaraokeLineView *view in _shown.allValues) {
- if (!CGRectContainsPoint(CGRectInset(view.frame, -_margin, -_lineGap / 2), point)) continue;
- SGKaraokeSeek(view.line.start);
- [self followSong];
- return;
- }
-}
-
-#pragma mark - scrolling by hand
-
-// Lines are placed for a content offset of 0, so following the song means scrolling back to 0.
-// While the user browses, placement stands still and every line is sharp.
-- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];
- _browsing = YES;
- for (SGKaraokeLineView *view in _shown.allValues) view.blur = 0;
-}
-
-- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- [self showLinesInSight];
-}
-
-- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- if (!decelerate) [self performSelector:@selector(followSong) withObject:nil afterDelay:kBrowseHold];
-}
-
-- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- [self performSelector:@selector(followSong) withObject:nil afterDelay:kBrowseHold];
-}
-
-- (void)followSong {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];
- if (!_browsing) return;
- _browsing = NO;
- [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:0
- options:UIViewAnimationOptionAllowUserInteraction
- animations:^{ self->_scroll.contentOffset = CGPointZero; } completion:nil];
- [self placeLinesAnimated:YES];
-}
-
-- (void)didMoveToWindow {
- [super didMoveToWindow];
- if (!self.window) {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];
- _browsing = NO;
- }
- [self scheduleLink];
-}
-
-// The player opens and closes in animations that run at 120 Hz, and while a display link asked
-// for 30 to 60, the range Apple's ProMotion guide says Core Animation gives priority to, those
-// animations ran rough; the player and the lyrics themselves were smooth throughout. The card's
-// link now asks for 60 but takes 120, and is put down for as long as the player animates, which
-// NowPlayingBar.x announces as each animation starts and again as it ends; the card holds still
-// under it, where nobody sees it. The timer is for an end that is never announced.
-- (void)scheduleLink {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startLink) object:nil];
- [_link invalidate];
- _link = nil;
- if (!self.window) return;
- NSTimeInterval wait = SGPlayerTransitionEnds() - CACurrentMediaTime();
- if (wait <= 0) [self startLink];
- else [self performSelector:@selector(startLink) withObject:nil afterDelay:wait + kTransitionSlack inModes:@[NSRunLoopCommonModes]];
-}
-
-- (void)startLink {
- if (!self.window || _link) return;
- _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
- // The card stays in the window behind the full screen page, so both sweep at once; the card is
- // a few lines tall and is content with 60, the page keeps the high refresh rate.
- _link.preferredFrameRateRange = _compact ? CAFrameRateRangeMake(30, 120, 60) : CAFrameRateRangeMake(80, 120, 120);
- [_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
-}
-
-- (void)playerTransitionChanged:(NSNotification *)note {
- [self scheduleLink];
-}
-
-// The mask sits in the scroll view's own coordinates, which move with the content as it scrolls, so
-// it is put back over the visible part on every frame, where the presentation layer says the
-// content is: that holds through a drag and through the animated scroll home alike. Left where
-// layout put it, it covered the first screenful of lines only, and a scroll past them showed nothing.
-- (void)alignFade {
- CALayer *shown = (CALayer *)_scroll.layer.presentationLayer ?: _scroll.layer;
- CGRect frame = CGRectMake(0, shown.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
- if (CGRectEqualToRect(frame, _fade.frame)) return;
- [CATransaction begin];
- [CATransaction setDisableActions:YES];
- _fade.frame = frame;
- [CATransaction commit];
-}
-
-- (void)layoutSubviews {
- [super layoutSubviews];
- [self alignFade];
- _scroll.contentSize = self.bounds.size;
- [_credit sizeToFit];
- _credit.frame = CGRectMake(_margin, self.bounds.size.height - _credit.bounds.size.height - kCreditBottom,
- _credit.bounds.size.width, _credit.bounds.size.height);
- if (_lines && self.bounds.size.width != _builtWidth) [self rebuild];
-}
-
-- (void)dropLineViews {
- for (UIView *view in _shown.allValues) [view removeFromSuperview];
- [_shown removeAllObjects];
- _tops = nil;
- _sightActive = -2;
- _build++;
-}
-
-// Measures the song for the width and, once that is in, places it; Spotify's own lines stay in view
-// until then, since the page shows nothing of its own before it has lines to show.
-- (void)rebuild {
- [self dropLineViews];
- _active = -1;
- _browsing = NO;
- _scroll.contentOffset = CGPointZero;
- _builtWidth = self.bounds.size.width;
- CGFloat width = _builtWidth - 2 * _margin;
- if (width <= 0) return;
- _font = [UIFont systemFontOfSize:_fontSize weight:UIFontWeightBold];
- UIFont *font = _font;
- NSArray<SGKaraokeLine *> *lines = _lines;
- CGFloat gap = _lineGap;
- NSUInteger build = _build;
- dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
- NSArray<NSNumber *> *tops = topsOf(lines, width, font, gap);
- dispatch_async(dispatch_get_main_queue(), ^{
- if (build != self->_build) return; // the song or the width moved on meanwhile
- self->_tops = tops;
- [self placeLinesAnimated:NO];
- });
- });
-}
-
-// Where a line starts on the page, for the stack as it is arranged now.
-- (CGFloat)topOfLine:(NSInteger)index {
- return self.bounds.size.height * kAnchor + _tops[index].doubleValue - _focusTop;
-}
-
-// The view of a line, made the first time it is asked for and placed where the stack has it.
-- (SGKaraokeLineView *)viewForLine:(NSInteger)index {
- SGKaraokeLineView *view = _shown[@(index)];
- if (view || !_tops || index < 0 || index >= (NSInteger)_tops.count) return view;
- view = [[SGKaraokeLineView alloc] initWithLine:_lines[index] width:_builtWidth - 2 * _margin font:_font backing:NO blurred:_maxBlur > 0];
- [_scroll addSubview:view];
- _shown[@(index)] = view;
- [self placeLine:view at:index animated:NO];
- return view;
-}
-
-// Views for the lines just outside the visible part as well as in it, wherever that is: around the
-// sung line while following the song, around wherever the page has been scrolled to while browsing.
-// The ones further off are let go, so a long song costs a couple of dozen line views rather than
-// one for every line, and the lines in sight are the only ones the compositor has to draw.
-- (void)showLinesInSight {
- if (!_tops.count) return;
- CGFloat offset = ((CALayer *)_scroll.layer.presentationLayer ?: _scroll.layer).bounds.origin.y;
- if (offset == _sightOffset && _focusTop == _sightFocus && _active == _sightActive) return;
- _sightOffset = offset;
- _sightFocus = _focusTop;
- _sightActive = _active;
- CGFloat height = self.bounds.size.height;
- CGFloat from = offset - kSightBehind * height, to = offset + (1 + kSightAhead) * height, slack = kSightSlack * height;
- NSMutableArray<NSNumber *> *gone = [NSMutableArray array];
- for (NSNumber *key in _shown) {
- NSInteger index = key.integerValue;
- CGFloat top = [self topOfLine:index], bottom = top + _shown[key].bounds.size.height;
- if (index != _active && (bottom < from - slack || top > to + slack)) [gone addObject:key];
- }
- for (NSNumber *key in gone) {
- [_shown[key] removeFromSuperview];
- [_shown removeObjectForKey:key];
- }
- NSInteger count = (NSInteger)_tops.count;
- NSMutableArray<NSNumber *> *wanted = [NSMutableArray array];
- for (NSInteger index = 0; index < count; index++) {
- if (_shown[@(index)]) continue;
- CGFloat top = [self topOfLine:index];
- CGFloat bottom = index + 1 < count ? [self topOfLine:index + 1] - _lineGap : top + height;
- if (bottom < from || top > to) continue;
- [wanted addObject:@(index)];
- }
- // A few a frame, the nearest the middle of the visible part first, so a page scrolled by hand
- // fills in what is in view before what is not; the next frame picks up where this one left off.
- CGFloat middle = offset + height / 2;
- [wanted sortUsingComparator:^NSComparisonResult(NSNumber *a, NSNumber *b) {
- CGFloat da = fabs([self topOfLine:a.integerValue] - middle), db = fabs([self topOfLine:b.integerValue] - middle);
- return da < db ? NSOrderedAscending : da > db ? NSOrderedDescending : NSOrderedSame;
- }];
- NSUInteger made = 0;
- for (NSNumber *index in wanted) {
- if (made++ == kLinesPerFrame) {
- _sightOffset = -CGFLOAT_MAX;
- break;
- }
- [self viewForLine:index.integerValue];
- }
-}
-
-// One line's place in the stack: the anchor sits on the edge its text is aligned to, so it scales
-// toward its own text, and it dims and blurs with its distance from the sung line.
-- (void)placeLine:(SGKaraokeLineView *)view at:(NSInteger)index animated:(BOOL)animated {
- CGFloat height = self.bounds.size.height;
- NSInteger distance = index - _active;
- CGRect frame = CGRectMake(_margin, [self topOfLine:index], view.bounds.size.width, view.bounds.size.height);
- BOOL trailing = view.line.align == SGKaraokeAlignTrailing;
- CGPoint center = CGPointMake(trailing ? CGRectGetMaxX(frame) : _margin, CGRectGetMidY(frame));
- CGFloat scale = distance == 0 ? 1 : kDimScale;
- CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
- view.blur = distance == 0 || _browsing ? 0 : MIN(_maxBlur, labs(distance) * _blurPerLine);
- BOOL near = CGRectIntersectsRect(CGRectInset(self.bounds, 0, -height / 2), frame)
- || CGRectIntersectsRect(CGRectInset(self.bounds, 0, -height / 2), view.frame);
- if (!animated || !near) {
- view.center = center;
- view.transform = transform;
- return;
- }
- NSTimeInterval delay = distance > 0 ? MIN(0.3, distance * 0.04) : 0;
- [UIView animateWithDuration:0.7 delay:delay usingSpringWithDamping:0.86 initialSpringVelocity:0
- options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
- animations:^{
- view.center = center;
- view.transform = transform;
- } completion:nil];
-}
-
-// The sung line rests at the anchor and the rest stack around it; lines below it follow a beat
-// later, the further down the later, as Apple Music's do.
-- (void)placeLinesAnimated:(BOOL)animated {
- if (_browsing || !_tops.count) return;
- _focusTop = _tops[(NSUInteger)MAX(_active, 0)].doubleValue;
- [self showLinesInSight];
- for (NSNumber *key in _shown) [self placeLine:_shown[key] at:key.integerValue animated:animated];
- // Room to scroll until the first line or the last one reaches the anchor.
- CGFloat lastTop = _tops.lastObject.doubleValue;
- _scroll.contentInset = UIEdgeInsetsMake(_focusTop, 0, MAX(0, lastTop - _focusTop), 0);
-}
-
-- (void)creditTo:(NSString *)source {
- NSString *text = source.length && _crediting ? [NSString stringWithFormat:@"Lyrics from %@", source] : nil;
- if (text == _credit.text || [text isEqualToString:_credit.text]) return;
- _credit.text = text;
- _credit.hidden = !_showing || !text.length;
- [self setNeedsLayout];
-}
-
-- (void)syncSiblings {
- for (UIView *sibling in self.superview.subviews) {
- if (sibling != self && _showing) sibling.alpha = 0;
- }
-}
-
-- (void)setShowing:(BOOL)showing {
- if (showing == _showing) return;
- _showing = showing;
- self.hidden = !showing;
- _credit.hidden = !showing || !_credit.text.length;
- for (UIView *sibling in self.superview.subviews) {
- if (sibling != self) sibling.alpha = showing ? 0 : 1;
- }
-}
-
-// The player's position run on by the frame times the display will show and eased toward each new
-// reading, so the sweep follows neither the callback's jitter nor the small jumps of the core's
-// corrections. A seek or a new track is too far off to ease and is taken at once.
-- (double)clockMs {
- NSInteger raw = SGKaraokePositionMs();
- CFTimeInterval shown = _link.targetTimestamp;
- BOOL running = raw != _reported; // a paused player reports the same position every frame
- double reported = raw + (running ? (shown - CACurrentMediaTime()) * 1000 : 0);
- double predicted = _clock + (running ? (shown - _clockTime) * 1000 : 0);
- double error = reported - predicted;
- _clock = raw < 0 || fabs(error) > kClockSnapMs ? reported : predicted + error * kClockPull;
- _reported = raw;
- _clockTime = shown;
- return _clock;
-}
-
-- (void)tick {
- NSString *track = SGKaraokePlayingTrack();
- if (!(track == _track || [track isEqualToString:_track])) {
- SGLog(@"karaoke: page shows track %@, lyrics %@", track, SGKaraokeLinesForTrack(track) ? @"captured" : @"not captured yet");
- _track = track;
- _lines = nil;
- _builtWidth = 0;
- [self creditTo:nil];
- [self dropLineViews];
- }
- if (!_lines && track && (_lines = SGKaraokeLinesForTrack(track))) {
- SGLog(@"karaoke: showing %lu lines of %@", (unsigned long)_lines.count, track);
- [self setNeedsLayout];
- }
- [self setShowing:_tops != nil];
- // The source is settled a moment after the lines are, so it is asked for until it answers.
- if (_crediting && _lines && !_credit.text.length) [self creditTo:SGLyricsCreditFor(track)];
- if (!_tops) return;
- [self alignFade];
-
- double now = [self clockMs];
- NSInteger active = -1;
- for (NSUInteger i = 0; i < _lines.count && _lines[i].start <= now; i++) active = i;
- if (active != _active) {
- if (_active >= 0) _shown[@(_active)].active = NO;
- if (active >= 0) [self viewForLine:active].active = YES;
- BOOL jump = labs(active - _active) > 2; // a seek, not the song moving on
- _active = active;
- [self placeLinesAnimated:!jump];
- } else {
- [self showLinesInSight]; // the page may be scrolling by hand, or springing back
- }
- if (active >= 0) [_shown[@(active)] showTime:now];
-}
-
-@end
diff --git a/tweak/Sources/Native/Player/KaraokeCard.x b/tweak/Sources/Native/Player/KaraokeCard.x
deleted file mode 100644
index c71b1287..00000000
--- a/tweak/Sources/Native/Player/KaraokeCard.x
+++ /dev/null
@@ -1,43 +0,0 @@
-// The same Apple Music style lyrics on the card under the player, so the card sweeps word by word
-// rather than scrolling whole lines while the page it expands into sweeps. It is the page's view in
-// compact: the card is a seventh of the height, so it takes Spotify's own type size and spacing.
-//
-// The host is Lyrics_CardElementImpl.CardContentView, the card's own content view. The table inside
-// it, Lyrics_TextElementImpl.LyricsTextView, is the wrong thing to hook: the full screen page is
-// built from that same class, and KaraokePage.x is already over it there. CardContentView belongs
-// to the card alone, and is still in the tree while the page is open, behind it.
-//
-// The view is laid over rather than swapped in, as on the page: Spotify's table keeps running
-// underneath and comes back the moment a track has no synced lyrics. It takes no touches, so the
-// tap that opens the full screen page is still Spotify's to answer.
-#import "Core/SGCore.h"
-#import "Native/Lyrics/KaraokeView.h"
-
-// Below this the card is collapsed, by Declutter or while it is still being laid out.
-static const CGFloat kLivingHeight = 40;
-
-static char kKaraokeKey;
-
-%hook _TtC22Lyrics_CardElementImpl15CardContentView
-- (void)layoutSubviews {
- %orig;
- UIView *host = (UIView *)self;
- if (host.bounds.size.height < kLivingHeight) return;
- SGKaraokeView *view = objc_getAssociatedObject(host, &kKaraokeKey);
- if (!view) {
- SGLog(@"karaoke: card laid out, playing %@", SGKaraokePlayingTrack());
- view = [[SGKaraokeView alloc] initWithFrame:host.bounds compact:YES];
- objc_setAssociatedObject(host, &kKaraokeKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- if (view.superview == host) [host bringSubviewToFront:view];
- else [host addSubview:view];
- view.frame = host.bounds;
- [view syncSiblings];
-}
-%end
-
-%ctor {
- if (!SGNativeUI() || !SGFlag(SGKeyKaraokeLyrics, NO)) return;
- %init;
- SGRequireClasses(@[@"_TtC22Lyrics_CardElementImpl15CardContentView"]);
-}
diff --git a/tweak/Sources/Native/Player/NowPlaying.h b/tweak/Sources/Native/Player/NowPlaying.h
index 1c964920..b97acbd0 100644
--- a/tweak/Sources/Native/Player/NowPlaying.h
+++ b/tweak/Sources/Native/Player/NowPlaying.h
@@ -1,6 +1,5 @@
// The native look's player: Spotify's full screen player with the artwork background and glass header
-// buttons (Player.x), the glass lyrics card (LyricsCard.x), karaoke on the card (KaraokeCard.x), the
-// gestures' hookup (PlayerGestures.x) and the parts of it to hide (PlayerDeclutter.x). The glass
+// buttons (Player.x), the glass lyrics card (LyricsCard.x), the gestures' hookup (PlayerGestures.x) and the parts of it to hide (PlayerDeclutter.x). The glass
// lyrics page is Native/Lyrics/LyricsPage.x's and shares SGKeyLyricsCard ("Glass lyrics").
#import <UIKit/UIKit.h>
diff --git a/tweak/Sources/Redesigned/Lyrics/KaraokePage.x b/tweak/Sources/Redesigned/Lyrics/KaraokePage.x
index 3b6093ad..75b57542 100644
--- a/tweak/Sources/Redesigned/Lyrics/KaraokePage.x
+++ b/tweak/Sources/Redesigned/Lyrics/KaraokePage.x
@@ -25,7 +25,7 @@ static char kKaraokeKey;
%end
%ctor {
- if (!SGRedesignedUI() || !SGFlag(SGKeyKaraokeLyrics, NO)) return;
+ if (!SGRedesignedUI()) return;
%init;
SGRequireClasses(@[@"_TtC32Lyrics_FullscreenElementPageImpl10LyricsView"]);
}
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
index 5692ad63..1599a577 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.h
@@ -1,6 +1,7 @@
-// The redesign's Apple Music style lyrics view, over Spotify's full screen lyrics page (KaraokePage.x)
-// and, compact, over the card under the redesigned player (Redesigned/Player/KaraokeCard.x). The
-// lines and the clock are Shared/Lyrics/Lyrics.h's.
+// The redesign's Apple Music style lyrics view, always on, over Spotify's full screen lyrics page
+// (KaraokePage.x) and, compact, over the card under the redesigned player (Redesigned/Player/KaraokeCard.x).
+// A track without synced lyrics keeps Spotify's own lines. The lines and the clock are
+// Shared/Lyrics/Lyrics.h's.
#import <UIKit/UIKit.h>
#import "Shared/Lyrics/Lyrics.h"
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 644ccef0..d1c60cf7 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1,5 +1,5 @@
-// The redesign's copy of Native/Lyrics/KaraokeView.m, to be changed for the redesign without touching
-// the native look's.
+// The redesign's Apple Music style lyrics, always on: the line being sung lights up word by word, the
+// rest dim and blur with distance. The lines and the clock are Shared/Lyrics/Lyrics.h's.
#import "Core/SGCore.h"
#import "SGRKaraokeView.h"
#import "Shared/LyricsSources/LyricsSources.h"
diff --git a/tweak/Sources/Redesigned/Player/KaraokeCard.x b/tweak/Sources/Redesigned/Player/KaraokeCard.x
index 6900d7d7..f461f451 100644
--- a/tweak/Sources/Redesigned/Player/KaraokeCard.x
+++ b/tweak/Sources/Redesigned/Player/KaraokeCard.x
@@ -37,7 +37,7 @@ static char kKaraokeKey;
%end
%ctor {
- if (!SGRedesignedUI() || !SGFlag(SGKeyKaraokeLyrics, NO)) return;
+ if (!SGRedesignedUI()) return;
%init;
SGRequireClasses(@[@"_TtC22Lyrics_CardElementImpl15CardContentView"]);
}
diff --git a/tweak/Sources/Redesigned/Player/PlayerCards.x b/tweak/Sources/Redesigned/Player/PlayerCards.x
index a33ddc6a..b729d71e 100644
--- a/tweak/Sources/Redesigned/Player/PlayerCards.x
+++ b/tweak/Sources/Redesigned/Player/PlayerCards.x
@@ -1,5 +1,5 @@
-// Player redesign: of the cards under the player only lyrics is kept, and it has no card of its own when
-// Apple Music style lyrics draw it: the lines sit straight on the field, with no edge where the card was;
+// Player redesign: of the cards under the player only lyrics is kept, with no card of its own: the Apple
+// Music style lines sit straight on the field, with no edge where the card was;
// every other card (about the artist, videos, SongDNA, events, explore, credits, merch, anything Spotify
// adds later) reports no height, so the list closes up around it.
//
@@ -16,7 +16,6 @@
#import "Core/SGCore.h"
#import "Redesigned/Kit/SGRKit.h"
#import "Redesigned/Kit/SGRRepaint.h"
-#import "Shared/Lyrics/Lyrics.h"
#import "Player.h"
// Below this a cell is collapsed, or still being laid out.
@@ -25,16 +24,6 @@ static const CGFloat kLivingHeight = 40;
static char kShareKey, kRevalidatedKey;
static __weak UIView *sg_lyricsCard;
-// Spotify draws the lines still to come in black, for its card in the album colour (lyrics/01.txt:1054),
-// so they read on the dark field only with Apple Music style lyrics drawn over them; without those the
-// card keeps Spotify's own colour.
-static BOOL clearWanted(void) {
- static BOOL wanted;
- static dispatch_once_t once;
- dispatch_once(&once, ^{ wanted = SGFlag(SGKeyKaraokeLyrics, NO); });
- return wanted;
-}
-
// In a window and not in a hidden cell: the list keeps the cells it has put aside for reuse in it, hidden
// (player/02.txt:521), each with the card it last held.
static BOOL showing(UIView *card) {
@@ -121,12 +110,12 @@ static UIView *cellAround(UIView *view) {
objc_setAssociatedObject(cell, &kRevalidatedKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Spotify paints the cell the album colour again at every track change, which is no layout pass
// of the card's; as the root of the lyrics card, Redesigned/Kit/SGRRepaint.x keeps it clear in between.
- if (clearWanted()) {
- sgr_lyricsCardRoot = cell;
- SGStripBackgrounds(cell);
- static dispatch_once_t once;
- dispatch_once(&once, ^{ SGLog(@"redesign player: lyrics card cleared onto the field"); });
- }
+ // Spotify draws the lines still to come in black for that colour (lyrics/01.txt:1054); the Apple
+ // Music style lyrics drawn over them (KaraokeCard.x) are what read on the field.
+ sgr_lyricsCardRoot = cell;
+ SGStripBackgrounds(cell);
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ SGLog(@"redesign player: lyrics card cleared onto the field"); });
return;
}
// Sized before its card was in it, so it was collapsed as an unknown card: the list is asked once to
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index d3488bd7..a7734f41 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -246,8 +246,8 @@ static void prefetch(SPTPlayerTrack *track, NSString *trackID, SPTPlayerState *s
%ctor {
// The sources that search by name learn the name from the player, so the player is caught
- // whenever one is on, not only for the karaoke page and the lock screen.
- if (!SGFlag(SGKeyKaraokeLyrics, NO) && !SGFlag(SGKeyLockScreenLyrics, NO) && !SGLyricsEnabled()) return;
+ // whenever one is on, not only for the redesign's lyrics and the lock screen.
+ if (!SGRedesignedUI() && !SGFlag(SGKeyLockScreenLyrics, NO) && !SGLyricsEnabled()) return;
sg_seenTracks = [NSMutableDictionary dictionary];
sg_lyrics = [NSMutableDictionary dictionary];
sg_requested = [NSMutableSet set];
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index 2b8a1e5f..5f4b2ceb 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -1,17 +1,15 @@
-// Apple Music style lyrics, the engine both looks draw from: the line being sung lights up word by
-// word. Spotify only times whole lines, so the words inside a line are timed by an estimate
-// (KaraokeTiming.m), unless a source of the mod's times them. The lines are read from the color-lyrics
-// response as it arrives, or handed over by Shared/LyricsSources, and the position from the player's
-// state (KaraokeSource.x); a track without synced lyrics keeps Spotify's own page. What draws them is
-// each look's own: Native/Lyrics/KaraokeView.h and Redesigned/Lyrics/SGRKaraokeView.h.
+// The lyrics engine: timed lines and the player's clock, for the redesign's Apple Music style lyrics
+// (Redesigned/Lyrics/SGRKaraokeView.h) and the lock screen (Shared/LockScreenLyrics). Spotify only
+// times whole lines, so the words inside a line are timed by an estimate (KaraokeTiming.m), unless a
+// source of the mod's times them. The lines are read from the color-lyrics response as it arrives, or
+// handed over by Shared/LyricsSources, and the position from the player's state (KaraokeSource.x).
#import <UIKit/UIKit.h>
-#define SGKeyKaraokeLyrics @"spotifyglass.karaokeLyrics"
-
@class SGModRow, SGModSection;
-// LyricsSettings.m: the Lyrics page's rows for either look.
-NSArray<SGModRow *> *SGLyricsLookRows(void);
-NSArray<SGModSection *> *SGLyricsSourceSections(void);
+// LyricsSettings.m: the Lyrics page's parts (App/Pages.m puts the page together): where lyrics come
+// from, naming the source (read by the redesign's lyrics view only), and the lock screen.
+SGModSection *SGLyricsSourcesSection(BOOL namingSource);
+SGModRow *SGLockScreenLyricsRow(void);
// Which edge a line is laid against. Apple Music puts a duet's second voice against the far one, so
// the two sides of the song read apart; a track sung by one voice stays leading throughout.
diff --git a/tweak/Sources/Shared/Lyrics/LyricsSettings.m b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
index 13d2075c..b25731f9 100644
--- a/tweak/Sources/Shared/Lyrics/LyricsSettings.m
+++ b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
@@ -1,19 +1,11 @@
-// What the Lyrics page holds for either look: Apple Music style, the lock screen, where lyrics come from
-// and Spotify's lyrics flags. The page itself is App/Pages.m's, which adds the native look's glass row.
+// The Lyrics page's parts. The page itself is App/Pages.m's, which adds what only one look has.
#import "Core/SGCore.h"
#import "Settings/SGModPage.h"
#import "Lyrics.h"
#import "Shared/LockScreenLyrics/LockScreenLyrics.h"
#import "Shared/LyricsSources/LyricsSources.h"
-NSArray<SGModRow *> *SGLyricsLookRows(void) {
- return @[
- SGOptionRow(@"Apple Music style", @"Word by word on the full screen page; timing inside a line is estimated unless Musixmatch has it", SGKeyKaraokeLyrics),
- SGOptionRow(@"Lyrics on the lock screen", @"The line being sung in place of the artist, also in the Dynamic Island, Control Center and CarPlay", SGKeyLockScreenLyrics),
- ];
-}
-
-NSArray<SGModSection *> *SGLyricsSourceSections(void) {
+SGModSection *SGLyricsSourcesSection(BOOL namingSource) {
// The row reads the order out, so which sources are on is visible without opening it.
SGModRow *sources = SGPageRow(@"Lyrics sources", ^UIViewController *{ return SGLyricsSourcesPage(); });
sources.subtitle = @"BiniLyrics, Musixmatch, Unison, NetEase and LRCLIB, in the order you put them";
@@ -22,20 +14,12 @@ NSArray<SGModSection *> *SGLyricsSourceSections(void) {
for (NSString *key in SGLyricsOrder()) [names addObject:SGLyricsProviderFor(key).name];
return names.count ? [names componentsJoinedByString:@", "] : @"Off";
};
- return @[
- SGNotedSection(@"Where lyrics come from", @[
- sources,
- SGOptionRow(@"Lyrics for every track", @"Offers the lyrics card on tracks Spotify has no lyrics for; needs a source above", SGKeyLyricsAllTracks),
- SGOptionRow(@"Name the source", @"Reads out which source the lines on the full screen page came from", SGKeyLyricsCredit),
- ], @"With no source on, Spotify's own lyrics are left alone."),
- SGSection(@"Spotify's flags", @[
- SGFlagRow(@"Translations in the player", @"ios-feature-lyrics.enable_lyrics_multilanguage_npv"),
- SGFlagRow(@"Translations full screen", @"ios-feature-lyrics.enable_lyrics_multilanguage_fullscreen"),
- SGFlagRow(@"Keep lyrics offline", @"ios-feature-lyrics.lyrics_offline_enabled"),
- SGFlagRow(@"Dynamic colours", @"ios-feature-lyrics.enable_dynamic_colors"),
- SGFlagRow(@"Centre a single line", @"ios-feature-lyrics.is_single_line_centering_enabled"),
- SGFlagRow(@"Full screen on track change", @"ios-feature-lyrics.enable_fullscreen_track_change"),
- SGFlagRow(@"Lyrics toggle in the context menu", @"ios-feature-lyrics.lyrics_context_menu_toggle_enabled"),
- ]),
- ];
+ NSMutableArray<SGModRow *> *rows = [NSMutableArray arrayWithObjects:sources,
+ SGOptionRow(@"Lyrics for every track", @"Offers the lyrics card on tracks Spotify has no lyrics for; needs a source above", SGKeyLyricsAllTracks), nil];
+ if (namingSource) [rows addObject:SGOptionRow(@"Name the source", @"Reads out which source the lines on the full screen page came from", SGKeyLyricsCredit)];
+ return SGNotedSection(@"Where lyrics come from", rows, @"With no source on, Spotify's own lyrics are left alone.");
+}
+
+SGModRow *SGLockScreenLyricsRow(void) {
+ return SGOptionRow(@"Lyrics on the lock screen", @"The line being sung in place of the artist, also in the Dynamic Island, Control Center and CarPlay", SGKeyLockScreenLyrics);
}