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: AI DJ player in the redesign (#36)

Andrew7c3067f65b8b
diff --git a/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBar.x b/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBar.x
index d6364f30..10f33d2d 100644
--- a/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBar.x
+++ b/tweak/Sources/Redesigned/NowPlayingBar/NowPlayingBar.x
@@ -429,7 +429,24 @@ void SGRNowPlayingBarFollowTabBar(void) {
[container.view layoutIfNeeded];
}
+// The same bar class also stands at the top of Spotify's sheets (the DJ request one: Up next, device, play).
+// It is not the tab bar's card there: the sheet has its own blue ground and corners, and a dark 24pt pane of
+// the bar's full width on it reads as a frame round the track. It is left as Spotify draws it, and it must not
+// become the bar this file tracks (sg_container, sg_cardGlass, sgr_nowPlayingRoot): the player takes its card
+// frame from those.
+static BOOL inSheet(UIView *view) {
+ for (UIView *v = view.superview; v; v = v.superview) {
+ if ([v.accessibilityIdentifier isEqualToString:@"sheet-view"] || [NSStringFromClass(v.class) containsString:@"SheetImpl"]) return YES;
+ }
+ return NO;
+}
+
static void styleNowPlayingBar(UIViewController *container) {
+ if (container.isViewLoaded && inSheet(container.view)) {
+ UIVisualEffectView *pane = objc_getAssociatedObject(container.view, &kGlassKey);
+ if (pane) [pane removeFromSuperview];
+ return;
+ }
UIViewController *barVC = container.childViewControllers.firstObject;
UIView *bar = barVC.viewIfLoaded ?: container.view;
sg_container = container;
@@ -527,14 +544,14 @@ static void styleNowPlayingBar(UIViewController *container) {
%hook _TtC18NowPlaying_BarImpl17InformationCellV2
- (void)layoutSubviews {
%orig;
- if (sgr_nowPlayingRoot) placeText((UIView *)self);
+ if (sgr_nowPlayingRoot && [(UIView *)self isDescendantOfView:sgr_nowPlayingRoot]) placeText((UIView *)self);
}
%end
%hook _TtC18NowPlaying_BarImpl15InformationCell
- (void)layoutSubviews {
%orig;
- if (sgr_nowPlayingRoot) placeText((UIView *)self);
+ if (sgr_nowPlayingRoot && [(UIView *)self isDescendantOfView:sgr_nowPlayingRoot]) placeText((UIView *)self);
}
%end
diff --git a/tweak/Sources/Redesigned/Player/DJPlayer.x b/tweak/Sources/Redesigned/Player/DJPlayer.x
new file mode 100644
index 00000000..ef1661e8
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/DJPlayer.x
@@ -0,0 +1,248 @@
+// The AI DJ's player screen drawn the way the redesign draws the ordinary one: previous, play and next as bare
+// glyphs, and one row of glyphs under them (Lyrics, Connect, Queue, the DJ button) without the share button.
+// The redesign hooks the ordinary player's units and the DJ has its own, so each hook here is a second one on the
+// DJ's unit that calls the ordinary player's function: the play glyph (SGRPlayerPlayGlyphIn, PlayerControls.x), the
+// lowered row with the volume row and Follow the cover (SGRPlayerLowerRow), Connect (SGRPlayerConnectGlyphIn, SGRPlayerCoverConnect) and the
+// Lyrics glyph (SGRPlayerLyricsGlyphIn, PlayerFooter.x). What is the DJ's own is kept here: the skip glyphs, the
+// four places of the footer and its Queue button.
+//
+// Controls: DJMHeadUnitViewController's view holds the previous, next and play buttons directly. The glyphs go
+// inside the buttons over their images, which go transparent; the buttons keep their actions and accessibility.
+//
+// Footer: DJMFooterUnitView holds the share button, the DJ button, Connect and an empty view, not in a stack.
+// Connect and the DJ button stay Spotify's and are only moved, by a transform that survives relayout. Lyrics
+// and Queue are the Kit's glyph buttons; Queue opens spotify:now-playing:queue.
+#import "Core/SGCore.h"
+#import "Redesigned/Kit/SGRKit.h"
+#import "Shared/Navigation/Links.h"
+#import "Shared/Navigation/TabIcons.h"
+#import "Player.h"
+
+static const CGFloat kSkipGlyphSize = 32, kFooterGlyphSize = 20;
+// Where the four footer glyphs sit, as parts of the footer's width: Lyrics, Connect, Queue, the DJ button.
+static const CGFloat kSlots[4] = {0.2, 0.4, 0.6, 0.8};
+// The DJ button's blue disc is 48pt, a good deal more than the glyphs beside it; drawn this much of that.
+static const CGFloat kDJButtonScale = 0.88;
+static char kPrevKey, kNextKey, kPlayKey, kGlyphKey;
+static char kShareKey, kConnectKey, kDJKey, kQueueKey, kEncoreQueueKey, kEncoreMissKey;
+
+#pragma mark - previous and next
+
+static SGRGlyphView *glyphFor(UIView *owner, NSString *symbol, CGFloat size) {
+ SGRGlyphView *glyph = objc_getAssociatedObject(owner, &kGlyphKey);
+ if (!glyph) {
+ glyph = [[SGRGlyphView alloc] initWithSymbol:symbol pointSize:size weight:UIImageSymbolWeightRegular];
+ objc_setAssociatedObject(owner, &kGlyphKey, glyph, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ return glyph;
+}
+
+// On top inside `host` and centred by the autoresizing mask as well as here: the unit lays out before the
+// buttons in it have a size, so a centre set then is the middle of nothing.
+static void keepOnTop(UIView *view, UIView *host, CGFloat dy) {
+ if (view.superview != host) {
+ view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
+ | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
+ [host addSubview:view];
+ } else if (host.subviews.lastObject != view) {
+ [host bringSubviewToFront:view];
+ }
+ view.center = CGPointMake(CGRectGetMidX(host.bounds), CGRectGetMidY(host.bounds) + dy);
+}
+
+// How far below its own middle a previous/next glyph must sit to share a line with the play glyph. Spotify's
+// head unit puts the 56pt skip buttons and the 64pt play view on one top edge (y=16), so their middles are
+// 44 and 48 apart by 4pt, and the glyphs, centred in their buttons, would be too. Measured in the unit's
+// coordinates so it follows whatever sizes the unit lays out with; 0 until both are in place.
+static CGFloat skipDrop(UIView *host, UIView *button) {
+ UIView *play = SGRFindByIdentifier(host, @"SPTNowPlayingPlayButton", &kPlayKey);
+ if (!play || !button.superview || !play.superview) return 0;
+ CGFloat skipMid = [host convertPoint:CGPointMake(0, CGRectGetMidY(button.frame)) fromView:button.superview].y;
+ CGFloat playMid = [host convertPoint:CGPointMake(0, CGRectGetMidY(play.frame)) fromView:play.superview].y;
+ CGFloat dy = playMid - skipMid;
+ return fabs(dy) < 12 ? dy : 0;
+}
+
+static void skipGlyph(UIView *host, NSString *identifier, const void *findKey, NSString *symbol) {
+ UIView *button = SGRFindByIdentifier(host, identifier, findKey);
+ if (!button) return;
+ static Class icon;
+ if (!icon) icon = NSClassFromString(@"SPTEncoreIconView");
+ SGRGlyphView *glyph = glyphFor(button, symbol, kSkipGlyphSize);
+ // Spotify's own icon, a Swift view SGRSuppress cannot keep (PlayerControls.x), or a plain image view.
+ SGForEachView(button, ^(UIView *view) {
+ if (view == glyph) return;
+ if (icon && [view isKindOfClass:icon]) SGRPlayerVanish(view);
+ else if ([view isKindOfClass:UIImageView.class] && view.superview == button) SGRSuppress(view);
+ });
+ keepOnTop(glyph, button, skipDrop(host, button));
+}
+
+static void styleControls(UIViewController *unit) {
+ UIView *host = unit.viewIfLoaded;
+ if (!host || host.bounds.size.width < 100) return;
+ skipGlyph(host, @"SPTNowPlayingPreviousTrackButton", &kPrevKey, @"backward.fill");
+ skipGlyph(host, @"SPTNowPlayingNextTrackButton", &kNextKey, @"forward.fill");
+ SGRPlayerPlayGlyphIn(host);
+
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ SGLog(@"AI DJ player: glyphs over previous, play and next in %@", NSStringFromClass(host.class)); });
+}
+
+#pragma mark - the footer
+
+// The view of `host`'s own that holds `view`.
+static UIView *holderIn(UIView *host, UIView *view) {
+ for (UIView *v = view; v && v != host; v = v.superview) {
+ if (v.superview == host) return v;
+ }
+ return nil;
+}
+
+// Where `point` of `view` is in the host with the holder's own transform left out.
+static CGFloat untransformedX(UIView *holder, UIView *view, CGPoint point) {
+ CGPoint local = [view convertPoint:point toView:holder];
+ return holder.center.x + local.x - CGRectGetMidX(holder.bounds);
+}
+
+static void placeHolder(UIView *holder, CGFloat from, CGFloat to, CGFloat scale) {
+ CGAffineTransform transform = CGAffineTransformMake(scale, 0, 0, scale, round(to - from), 0);
+ if (!CGAffineTransformEqualToTransform(holder.transform, transform)) holder.transform = transform;
+}
+
+static SGRGlyphButton *glyphButtonIn(UIView *host, const void *key, NSString *symbol, NSString *title, UIColor *tint, void (^tap)(void)) {
+ SGRGlyphButton *button = objc_getAssociatedObject(host, key);
+ if (!button) {
+ button = [SGRGlyphButton buttonWithSymbol:symbol pointSize:kFooterGlyphSize title:title];
+ button.glyph.tintColor = tint;
+ button.onTap = tap;
+ objc_setAssociatedObject(host, key, button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ if (button.superview != host) [host addSubview:button];
+ return button;
+}
+
+// Spotify's own queue glyph (SPTEncoreIcon "queue", what the main player's queue button and the Queue tab
+// draw) over the Kit button's list.bullet, which is only what the button is made with. The button keeps its
+// touch and large content viewer; the glyph is its icon's stand-in, so the symbol hides rather than goes. With
+// no Encore (the class gone, or no such glyph) SGTabIconView answers a star symbol, and the bullet list stays.
+// A touch smaller on the iOS < 26 fallback, where the glyph sits among the Kit's own 20pt glyphs and read heavier.
+static CGFloat queueIconSize(void) {
+ if (@available(iOS 26.0, *)) return 24;
+ return 21;
+}
+
+static void useSpotifyQueueIcon(SGRGlyphButton *button) {
+ if (objc_getAssociatedObject(button, &kEncoreMissKey)) return;
+ UIView *icon = objc_getAssociatedObject(button, &kEncoreQueueKey);
+ if (!icon) {
+ UIView *made = SGTabIconView(@"queue", NO, SGRPrimary());
+ if (!made || [made isKindOfClass:UIImageView.class]) {
+ // No such glyph in this build: not asked again on every layout pass.
+ objc_setAssociatedObject(button, &kEncoreMissKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ return;
+ }
+ icon = made;
+ icon.userInteractionEnabled = NO;
+ icon.isAccessibilityElement = NO;
+ objc_setAssociatedObject(button, &kEncoreQueueKey, icon, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ [button addSubview:icon];
+ }
+ button.glyph.hidden = YES;
+ CGFloat side = queueIconSize();
+ icon.bounds = CGRectMake(0, 0, side, side);
+ icon.center = CGPointMake(CGRectGetMidX(button.bounds), CGRectGetMidY(button.bounds));
+}
+
+#pragma mark the row
+
+static void styleFooter(UIViewController *unit) {
+ UIView *host = unit.viewIfLoaded;
+ if (!host || host.bounds.size.width < 100) return;
+ CGFloat width = host.bounds.size.width, middleY = CGRectGetMidY(host.bounds);
+ BOOL rtl = host.effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
+ CGFloat (^slot)(NSUInteger) = ^CGFloat(NSUInteger i) { return round(width * (rtl ? 1 - kSlots[i] : kSlots[i])); };
+
+ // No share button; its holder goes too, since a view that takes touches swallows them with nothing on it.
+ UIView *share = SGRFindByIdentifier(host, @"ShareButtonNowPlayingView", &kShareKey);
+ SGRPlayerVanish(share);
+ SGRPlayerVanish(holderIn(host, share));
+
+ SGRGlyphButton *lyrics = SGRPlayerLyricsGlyphIn(host);
+ lyrics.bounds = CGRectMake(0, 0, 44, 44);
+ lyrics.center = CGPointMake(slot(0), middleY);
+ SGRPlayerLyricsChanged();
+
+ // Connect: the glyph alone, its device name gone, at the second place; left where Spotify put it until the glyph has a size.
+ UIView *connect = SGRFindByIdentifier(host, @"Components.ConnectButtonOutputSwitcher", &kConnectKey);
+ UIView *connectHolder = holderIn(host, connect);
+ UIView *glyph = SGRPlayerConnectGlyphIn(host, connect);
+ if (connectHolder && glyph) {
+ CGFloat from = untransformedX(connectHolder, glyph, CGPointMake(CGRectGetMidX(glyph.bounds), CGRectGetMidY(glyph.bounds)));
+ placeHolder(connectHolder, from, slot(1), 1);
+ // Its view reaches far to the right of the glyph, over Queue and the DJ button: touches only over the glyph.
+ SGRPlayerCoverConnect(connectHolder, glyph);
+ }
+
+ SGRGlyphButton *queue = glyphButtonIn(host, &kQueueKey, @"list.bullet", @"Queue", SGRPrimary(), ^{
+ SGOpenSpotifyURI([NSURL URLWithString:@"spotify:now-playing:queue"]);
+ });
+ queue.bounds = CGRectMake(0, 0, 44, 44);
+ queue.center = CGPointMake(slot(2), middleY);
+ useSpotifyQueueIcon(queue);
+
+ // The DJ button at the last place. Connect's view reaches a long way to the right of its glyph, so the
+ // DJ's goes above it in the stack, or Connect would take its touches.
+ UIView *dj = SGRFindByIdentifier(host, @"Components.UI.NeffleButtonNowPlayingContainerView", &kDJKey);
+ UIView *djHolder = holderIn(host, dj);
+ if (djHolder) {
+ placeHolder(djHolder, djHolder.center.x, slot(3), kDJButtonScale);
+ NSArray<UIView *> *order = host.subviews;
+ if (connectHolder && [order indexOfObject:djHolder] < [order indexOfObject:connectHolder]) [host bringSubviewToFront:djHolder];
+ }
+
+ SGRPlayerLowerRow(host);
+
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ SGLog(@"AI DJ player: footer %.0f wide, lyrics %.0f, connect %@ to %.0f, queue %.0f, DJ %@ to %.0f, share %@", width, slot(0),
+ glyph ? @"glyph" : @"button", slot(1), slot(2), djHolder ? @"found" : @"not found", slot(3), share ? @"gone" : @"not found");
+ });
+}
+
+#pragma mark - hooks
+
+// Groups only, installed when the build has the DJ's classes.
+%group SGRDJHeadHooks
+%hook SGRDJHeadUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ styleControls((UIViewController *)self);
+}
+%end
+%end
+
+%group SGRDJFooterHooks
+%hook SGRDJFooterUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ styleFooter((UIViewController *)self);
+}
+%end
+%end
+
+%ctor {
+ if (!SGRedesignedUI()) return;
+ Class head = SGRDJClass(@"DJMHeadUnitViewController");
+ if (head) {
+ %init(SGRDJHeadHooks, SGRDJHeadUnit = head);
+ } else {
+ SGLog(@"class DJMHeadUnitViewController not found, the AI DJ controls keep Spotify's own");
+ }
+ Class footer = SGRDJClass(@"DJMFooterUnitViewController");
+ if (footer) {
+ %init(SGRDJFooterHooks, SGRDJFooterUnit = footer);
+ } else {
+ SGLog(@"class DJMFooterUnitViewController not found, the AI DJ footer keeps Spotify's own");
+ }
+}
diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.h
index 0e736d07..e8fb74ca 100644
--- a/tweak/Sources/Redesigned/Player/Player.h
+++ b/tweak/Sources/Redesigned/Player/Player.h
@@ -10,6 +10,8 @@
// PlayerHeader.x glass behind the close and more buttons
// PlayerControls.x previous, play and next as bare glyphs, monospaced times
// PlayerFooter.x share gone, lyrics, Connect and queue as one row of three glyphs
+// DJPlayer.x the AI DJ's player (its own units): skip glyphs, a four glyph footer row, over the shared
+// play glyph, lowered row and Connect of PlayerControls.x and PlayerFooter.x
// PlayerCards.x every card under the player collapsed, so the list closes up
// PlayerScroll.x the list held at its top, so the player is one screen and cannot be scrolled up,
// and its pull held off while the progress bar is scrubbed
@@ -87,6 +89,18 @@ 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 lyrics glyph of the footer, made on first ask in `host` and kept there; the ordinary player's footer
+// (PlayerFooter.x) and the AI DJ's (DJPlayer.x) share it. Only the one asked for last is followed by
+// SGRPlayerLyricsChanged, which is the footer on screen: each layout of a footer asks again.
+@class SGRGlyphButton;
+SGRGlyphButton *SGRPlayerLyricsGlyphIn(UIView *host);
+// An Endless_DJMusicImpl class of the AI DJ's player by its Swift name, under the mangled spelling or the
+// dotted one the trees print; nil when the app has neither.
+static inline Class SGRDJClass(NSString *name) {
+ NSString *module = @"Endless_DJMusicImpl";
+ NSString *mangled = [NSString stringWithFormat:@"_TtC%lu%@%lu%@", (unsigned long)module.length, module, (unsigned long)name.length, name];
+ return NSClassFromString(mangled) ?: NSClassFromString([NSString stringWithFormat:@"%@.%@", module, name]);
+}
// The controls under the lines fade a few seconds after the last touch while the lyrics play, and the
// lines grow down into their room (PlayerLyrics.x). On until switched off; a scroll through the lines
// hides them either way.
@@ -162,6 +176,20 @@ UIViewController *SGRPlayerSettingsPage(NSArray *more);
// which SGRSuppress cannot keep (PlayerControls.x).
void SGRPlayerVanish(UIView *view);
+// The play glyph over the play button in `host`, a controls unit's view: the ordinary player's
+// (PlayerControls.x) and the AI DJ's (DJPlayer.x) share it, with its state, spinner and tap handling.
+void SGRPlayerPlayGlyphIn(UIView *host);
+
+// The footer's shared parts (PlayerFooter.x), for the AI DJ's footer (DJPlayer.x). `row` is the footer unit's view:
+// moved down over the home indicator with the controls following, the volume row and Follow the cover placed.
+void SGRPlayerLowerRow(UIView *row);
+// Connect's glyph in `connect`, laid out and with the device name hidden; nil while it has no size yet, and `host`
+// then lays out again on the next turn of the run loop. Leave Connect where it is while it is nil.
+UIView *SGRPlayerConnectGlyphIn(UIView *host, UIView *connect);
+// Connect's touches only in a 44pt square over its glyph: `holder` is the view of the footer that holds Connect, whose
+// own view reaches far past the glyph (over Queue and the DJ button once the glyph is moved).
+void SGRPlayerCoverConnect(UIView *holder, UIView *glyph);
+
// The tap to seek around the progress bar (PlayerControls.x), apart for the harness: whether a tap at `point`
// in the duration unit's view seeks, and the share of the song a point on the slider stands for, measured
// over the thumb's travel (NAN while the slider has no width).
diff --git a/tweak/Sources/Redesigned/Player/PlayerControls.x b/tweak/Sources/Redesigned/Player/PlayerControls.x
index 916308a7..94a6d67a 100644
--- a/tweak/Sources/Redesigned/Player/PlayerControls.x
+++ b/tweak/Sources/Redesigned/Player/PlayerControls.x
@@ -133,7 +133,9 @@ static void playTapped(void) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kTapTrust * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ refreshPlayGlyph(YES); });
}
-static void playGlyph(UIView *host) {
+void SGRPlayerPlayGlyphIn(UIView *host) {
+ // A state that lands after the layout asks this unit to lay out again (the watcher below).
+ sg_controlsHost = host;
UIView *play = SGRFindByIdentifier(host, @"SPTNowPlayingPlayButton", &kPlayKey);
if (!play) {
logMissing(@"SPTNowPlayingPlayButton");
@@ -182,10 +184,9 @@ static void playGlyph(UIView *host) {
if (!host) return;
// The unit lays out before its row does, and the glyphs are centered on the buttons in it.
[SGRowIn(host) layoutIfNeeded];
- sg_controlsHost = host;
skipGlyph(host, @"SPTNowPlayingPreviousTrackButton", &kPreviousKey, @"backward.fill");
skipGlyph(host, @"SPTNowPlayingNextTrackButton", &kNextKey, @"forward.fill");
- playGlyph(host);
+ SGRPlayerPlayGlyphIn(host);
}
%end
@@ -265,7 +266,8 @@ CGFloat SGRSeekShareAt(UISlider *slider, CGPoint point) {
// Whether a tap at `point` in the duration unit's view seeks.
BOOL SGRSeekTapLands(UIView *unit, UISlider *slider, CGPoint point) {
- if (!slider.window || !slider.enabled || ![slider isDescendantOfView:unit] || !CGRectContainsPoint(unit.bounds, point)) return NO;
+ // The AI DJ's unit keeps a slider in a hidden element on a live stream: nothing to seek there.
+ if (!slider.window || !slider.enabled || ![slider isDescendantOfView:unit] || !SGShownUnder(slider, unit) || !CGRectContainsPoint(unit.bounds, point)) return NO;
CGRect bar = [unit convertRect:slider.bounds fromView:slider];
if (point.x < CGRectGetMinX(bar) - kSeekReach || point.x > CGRectGetMaxX(bar) + kSeekReach) return NO;
CGRect bounds = slider.bounds;
@@ -386,10 +388,9 @@ static void watchSlider(UIView *host) {
}
%end
-%hook _TtC20NowPlaying_ModesImpl19DurationElementUnit
-- (void)viewDidLayoutSubviews {
- %orig;
- UIView *host = ((UIViewController *)self).viewIfLoaded;
+// The ordinary player's duration unit and the AI DJ's (DJMDurationElementsUnit) hold the same progress bar and times.
+static void durationLaidOut(UIViewController *unit) {
+ UIView *host = unit.viewIfLoaded;
if (!host) return;
watchSlider(host);
UILabel *taken = monospaced(host, @"now-playing-time-take-label-internal", &kTakeKey);
@@ -399,11 +400,29 @@ static void watchSlider(UIView *host) {
static dispatch_once_t once;
dispatch_once(&once, ^{ SGLog(@"redesign player: monospaced times in %@ %.0fpt, features %@", taken.font.fontName, taken.font.pointSize, taken.font.fontDescriptor.fontAttributes[UIFontDescriptorFeatureSettingsAttribute]); });
}
+
+%hook _TtC20NowPlaying_ModesImpl19DurationElementUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ durationLaidOut((UIViewController *)self);
+}
+%end
+
+// The DJ unit is another class, hooked only when the build has it (see %ctor).
+%group SGRDJControlsDurationHooks
+%hook SGRDJControlsDurationUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ durationLaidOut((UIViewController *)self);
+}
+%end
%end
%ctor {
if (!SGRedesignedUI()) return;
%init;
+ Class djDuration = SGRDJClass(@"DJMDurationElementsUnit");
+ if (djDuration) { %init(SGRDJControlsDurationHooks, SGRDJControlsDurationUnit = djDuration); }
sg_controlsWatcher = [SGRPlayerControlsWatcher new];
SGAddPlayerStateObserver(sg_controlsWatcher);
SGRequireClasses(@[
diff --git a/tweak/Sources/Redesigned/Player/PlayerFooter.x b/tweak/Sources/Redesigned/Player/PlayerFooter.x
index 4bd57170..cc344312 100644
--- a/tweak/Sources/Redesigned/Player/PlayerFooter.x
+++ b/tweak/Sources/Redesigned/Player/PlayerFooter.x
@@ -6,7 +6,7 @@
// tint, the Jam avatars) and are only moved, by a translation of the arranged view that holds each: a
// transform survives the stack view laying them out again, and moving the holder keeps its touches
// inside its own bounds. The device name goes transparent, since the glyph alone sits in the middle, and
-// Connect's holder takes touches only in a square over the glyph (coverConnect).
+// Connect's holder takes touches only in a square over the glyph (SGRPlayerCoverConnect).
// Spotify has no lyrics control down here, so that one is the Kit's glyph button, and it turns the
// lyrics in the player on and off (PlayerLyrics.x): filled while they are up, dimmed and dead for a
// track that has none.
@@ -93,7 +93,7 @@ void SGRPlayerLyricsChanged(void) {
else mark();
}
-static SGRGlyphButton *lyricsGlyphIn(UIView *host) {
+SGRGlyphButton *SGRPlayerLyricsGlyphIn(UIView *host) {
SGRGlyphButton *glyph = objc_getAssociatedObject(host, &kLyricsGlyphKey);
if (!glyph) {
glyph = [SGRGlyphButton buttonWithSymbol:kLyricsSymbol pointSize:kLyricsGlyphSize title:@"Lyrics"];
@@ -122,27 +122,55 @@ static UIView *connectGlyphIn(UIView *holder) {
// of the row's other two; Spotify's own views inside still handle them. The square is kept on that view, and
// its class answers from it, as it does for every other view of the class.
static char kConnectHitKey;
-static BOOL (*sg_origPointInside)(UIView *, SEL, CGPoint, UIEvent *);
+static NSMutableDictionary<NSString *, NSValue *> *sg_origPointInside; // class name -> original IMP
static BOOL connectPointInside(UIView *self, SEL _cmd, CGPoint point, UIEvent *event) {
NSValue *square = objc_getAssociatedObject(self, &kConnectHitKey);
if (square) return CGRectContainsPoint(square.CGRectValue, point);
- return sg_origPointInside(self, _cmd, point, event);
+ // The nearest hooked class up the chain holds the original this view would have run.
+ for (Class cls = self.class; cls; cls = class_getSuperclass(cls)) {
+ NSValue *orig = sg_origPointInside[NSStringFromClass(cls)];
+ // A subclass hooked after its parent got the parent's hook as its original; that one is skipped.
+ if (orig && orig.pointerValue != (void *)connectPointInside)
+ return ((BOOL (*)(UIView *, SEL, CGPoint, UIEvent *))orig.pointerValue)(self, _cmd, point, event);
+ }
+ return NO;
}
-static void coverConnect(UIView *arranged, UIView *glyph) {
+void SGRPlayerCoverConnect(UIView *arranged, UIView *glyph) {
if (!arranged || !glyph) return;
- static Class hooked;
- if (!hooked) {
- hooked = arranged.class;
- MSHookMessageEx(hooked, @selector(pointInside:withEvent:), (IMP)connectPointInside, (IMP *)&sg_origPointInside);
+ // The ordinary footer and the DJ's can hand over views of different classes; each class is hooked once.
+ if (!sg_origPointInside) sg_origPointInside = [NSMutableDictionary new];
+ NSString *name = NSStringFromClass(arranged.class);
+ if (!sg_origPointInside[name]) {
+ IMP orig = NULL;
+ MSHookMessageEx(arranged.class, @selector(pointInside:withEvent:), (IMP)connectPointInside, &orig);
+ sg_origPointInside[name] = [NSValue valueWithPointer:(void *)orig];
}
- if (arranged.class != hooked) return;
CGRect drawn = [glyph convertRect:glyph.bounds toView:arranged];
CGRect square = CGRectMake(CGRectGetMidX(drawn) - 22, CGRectGetMidY(drawn) - 22, 44, 44);
objc_setAssociatedObject(arranged, &kConnectHitKey, [NSValue valueWithCGRect:square], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+// Connect's glyph, ready to be moved: Connect laid out first, since the glyph is unsized until it is (the first time the
+// player opens), and its device name hidden. The name is a MarqueeLabel on the phone's speaker and a plain Encore label
+// on AirPods; Connect holds nothing else that draws text. With no glyph yet the caller leaves Connect where Spotify put
+// it, and `host` looks again on the next turn of the run loop.
+UIView *SGRPlayerConnectGlyphIn(UIView *host, UIView *connect) {
+ [connect layoutIfNeeded];
+ UIView *glyph = connectGlyphIn(connect);
+ static NSUInteger lookedAgain; // a Connect with no glyph at all is left alone after this many
+ if (glyph) lookedAgain = 0;
+ if (connect && !glyph && lookedAgain++ < 20) {
+ __weak UIView *weakHost = host;
+ dispatch_async(dispatch_get_main_queue(), ^{ [weakHost setNeedsLayout]; });
+ }
+ SGForEachView(connect, ^(UIView *view) {
+ if ([view isKindOfClass:UILabel.class] || [NSStringFromClass(view.class) containsString:@"MarqueeLabel"]) SGRPlayerVanish(view);
+ });
+ return glyph;
+}
+
#pragma mark - lower down
// Moved down, the row is drawn partly below the bottom stack it is arranged in, and UIKit does not look
@@ -170,8 +198,13 @@ static UIViewController *unitOf(UIView *view) {
static UIView *rowAbove(UIView *row) {
UIView *best = nil;
CGFloat top = row.center.y - row.bounds.size.height / 2, bestBottom = -CGFLOAT_MAX;
+ // The volume row lives in the same stack, between the controls and the footer: it is not what is above, or the
+ // controls would be taken for something else whenever it ends within a point of the footer (the AI DJ's gap does).
+ static Class volumeRow;
+ if (!volumeRow) volumeRow = NSClassFromString(@"SGRVolumeRow");
for (UIView *sibling in row.superview.subviews) {
if (sibling == row || sibling.hidden || sibling.alpha < 0.01 || sibling.bounds.size.height < 1) continue;
+ if (volumeRow && [sibling isKindOfClass:volumeRow]) continue;
CGFloat bottom = sibling.center.y + sibling.bounds.size.height / 2;
if (bottom <= top + 1 && bottom > bestBottom) {
best = sibling;
@@ -181,7 +214,7 @@ static UIView *rowAbove(UIView *row) {
return best;
}
-static void lowerRow(UIView *row) {
+void SGRPlayerLowerRow(UIView *row) {
UIView *stack = row.superview, *player = stack.superview;
UIWindow *window = row.window;
if (![stack isKindOfClass:UIStackView.class] || !player || !window) return;
@@ -195,7 +228,9 @@ static void lowerRow(UIView *row) {
// Only the controls, and only straight above: a volume row between them keeps its place and theirs.
UIView *above = rowAbove(row);
- BOOL controls = [NSStringFromClass(unitOf(above).class) containsString:@"PlaybackControlsElementsUnit"];
+ // The ordinary player's controls unit, or the AI DJ's head unit (DJPlayer.x), which holds the same buttons.
+ NSString *unit = NSStringFromClass(unitOf(above).class);
+ BOOL controls = [unit containsString:@"PlaybackControlsElementsUnit"] || [unit containsString:@"DJMHeadUnitViewController"];
// With the volume row, the controls stay where Spotify put them and the row takes the room the footer leaves.
// The volume row is placed against the controls where they would stand for it; one that does not show leaves them
// following the footer as they do without it.
@@ -256,36 +291,20 @@ static void lowerRow(UIView *row) {
SGRPlayerVanish(sharedBy);
SGRPlayerVanish(arrangedAround(sharedBy, host));
- SGRGlyphButton *lyrics = lyricsGlyphIn(host);
+ SGRGlyphButton *lyrics = SGRPlayerLyricsGlyphIn(host);
lyrics.bounds = CGRectMake(0, 0, 44, 44);
lyrics.center = CGPointMake(round(width * (rtl ? kTrailing : kLeading)), middleY);
SGRPlayerLyricsChanged();
UIView *connect = SGRFindByIdentifier(host, @"Components.ConnectButtonOutputSwitcher", &kConnectKey);
- // The unit lays out before Connect has sized its glyph, the first time the player opens: Connect is laid
- // out first so the glyph is there to move to the middle. Should it still be missing, Connect stays where
- // Spotify put it, since centering the whole control would leave the glyph well off the middle, and the unit
- // looks again on the next turn of the run loop.
- [connect layoutIfNeeded];
- UIView *glyph = connectGlyphIn(connect);
- static NSUInteger lookedAgain; // a Connect with no glyph at all is left alone after this many
- if (glyph) lookedAgain = 0;
- if (connect && !glyph && lookedAgain++ < 20) {
- __weak UIView *weakHost = host;
- dispatch_async(dispatch_get_main_queue(), ^{ [weakHost setNeedsLayout]; });
- }
- // The name is a MarqueeLabel on the phone's speaker and a plain Encore label on AirPods; Connect holds
- // nothing else that draws text.
- SGForEachView(connect, ^(UIView *view) {
- if ([view isKindOfClass:UILabel.class] || [NSStringFromClass(view.class) containsString:@"MarqueeLabel"]) SGRPlayerVanish(view);
- });
+ UIView *glyph = SGRPlayerConnectGlyphIn(host, connect);
CGFloat connectFrom = glyph ? moveTo(arrangedAround(connect, host), glyph, CGPointMake(CGRectGetMidX(glyph.bounds), CGRectGetMidY(glyph.bounds)), host, round(width * kMiddle)) : -1;
UIView *queue = SGRFindByIdentifier(host, @"QueueButtonNowPlaying", &kQueueKey);
CGFloat queueFrom = moveTo(arrangedAround(queue, host), queue, CGPointMake(CGRectGetMidX(queue.bounds), CGRectGetMidY(queue.bounds)), host, round(width * (rtl ? kLeading : kTrailing)));
- lowerRow(host);
- coverConnect(arrangedAround(connect, host), glyph);
+ SGRPlayerLowerRow(host);
+ SGRPlayerCoverConnect(arrangedAround(connect, host), glyph);
static dispatch_once_t once;
dispatch_once(&once, ^{
diff --git a/tweak/Sources/Redesigned/Player/PlayerHeader.x b/tweak/Sources/Redesigned/Player/PlayerHeader.x
index 20af9492..eff24fa5 100644
--- a/tweak/Sources/Redesigned/Player/PlayerHeader.x
+++ b/tweak/Sources/Redesigned/Player/PlayerHeader.x
@@ -61,8 +61,9 @@ static void pressFrom(UIControl *control, BOOL throughMenu) {
[control addTarget:press action:@selector(up:) forControlEvents:throughMenu ? up : up | UIControlEventTouchCancel];
}
-// `identifiers` holds, for each button, the identifiers it may carry, the first found winning.
-static void glassInside(UIViewController *unit, NSArray<NSArray<NSString *> *> *identifiers, const void **findKeys) {
+// `identifiers` holds, for each button, the identifiers it may carry, the first found winning. `watchMenu`: the ⋯ is
+// handed to PlayerMenu.m, which takes it for the ordinary player's; the AI DJ's ⋯ keeps Spotify's own menu.
+static void glassInside(UIViewController *unit, NSArray<NSArray<NSString *> *> *identifiers, const void **findKeys, BOOL watchMenu) {
UIView *host = unit.viewIfLoaded;
if (!host) return;
NSUInteger found = 0;
@@ -88,7 +89,7 @@ static void glassInside(UIViewController *unit, NSArray<NSArray<NSString *> *> *
button.accessibilityIdentifier, button.bounds.size.width, button.bounds.size.height,
button.hidden || button.alpha < 0.01 ? @"hidden" : @"shown");
}
- if ([name isEqualToString:@"Context menu"]) {
+ if (watchMenu && [name isEqualToString:@"Context menu"]) {
SGRPlayerMenuWatch(button);
}
UIControl *front = SGRSystemMenuFront(button);
@@ -105,16 +106,33 @@ static void glassInside(UIViewController *unit, NSArray<NSArray<NSString *> *> *
}
}
+static void glassHeader(UIViewController *unit, BOOL watchMenu) {
+ static const void *keys[] = {&kCloseKey, &kMoreKey};
+ glassInside(unit, @[@[@"now-playing-minimize-button", @"mobile-nowplaying-close-button"], @[@"Context menu"]], keys, watchMenu);
+}
+
%hook _TtC20NowPlaying_ModesImpl18HeaderElementsUnit
- (void)viewDidLayoutSubviews {
%orig;
- static const void *keys[] = {&kCloseKey, &kMoreKey};
- glassInside((UIViewController *)self, @[@[@"now-playing-minimize-button", @"mobile-nowplaying-close-button"], @[@"Context menu"]], keys);
+ glassHeader((UIViewController *)self, YES);
+}
+%end
+
+// The AI DJ's player has a header unit of its own with the same two buttons, which the circles never reached.
+// Its ⋯ gets the glass but is not watched: the menu it opens (feedback, language, voice) is not the player's.
+%group SGRDJHeaderHooks
+%hook SGRDJHeaderUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ glassHeader((UIViewController *)self, NO);
}
%end
+%end
%ctor {
if (!SGRedesignedUI()) return;
%init;
+ Class djHeader = SGRDJClass(@"DJMHeaderElementsUnitViewController");
+ if (djHeader) { %init(SGRDJHeaderHooks, SGRDJHeaderUnit = djHeader); }
SGRequireClasses(@[@"_TtC20NowPlaying_ModesImpl18HeaderElementsUnit"]);
}
diff --git a/tweak/Sources/Redesigned/Player/PlayerLyrics.x b/tweak/Sources/Redesigned/Player/PlayerLyrics.x
index 9ca14a7e..5f66a8a7 100644
--- a/tweak/Sources/Redesigned/Player/PlayerLyrics.x
+++ b/tweak/Sources/Redesigned/Player/PlayerLyrics.x
@@ -718,10 +718,9 @@ static void replace(void) {
}
%end
-%hook _TtC20NowPlaying_ModesImpl23InformationElementsUnit
-- (void)viewDidLayoutSubviews {
- %orig;
- UIViewController *unit = (UIViewController *)self;
+// The ordinary player's units and the AI DJ's (DJMInformationUnitViewController, DJMDurationElementsUnit, which
+// hold the same title row and progress bar) are measured the same way, so the DJ's player has these lyrics too.
+static void informationUnitLaidOut(UIViewController *unit) {
sg_info = unit;
UIView *host = unit.viewIfLoaded;
// The title and the artist are two labels of one arranged element view, which is what moves.
@@ -737,13 +736,23 @@ static void replace(void) {
}
replace();
}
+
+static void durationUnitLaidOut(UIViewController *unit) {
+ sg_duration = unit;
+ replace();
+}
+
+%hook _TtC20NowPlaying_ModesImpl23InformationElementsUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ informationUnitLaidOut((UIViewController *)self);
+}
%end
%hook _TtC20NowPlaying_ModesImpl19DurationElementUnit
- (void)viewDidLayoutSubviews {
%orig;
- sg_duration = (UIViewController *)self;
- replace();
+ durationUnitLaidOut((UIViewController *)self);
}
%end
@@ -757,6 +766,36 @@ static void replace(void) {
}
%end
+// The DJ player units are other classes, hooked only when the build has them (see %ctor).
+%group SGRDJInformationHooks
+%hook SGRDJInformationUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ informationUnitLaidOut((UIViewController *)self);
+}
+%end
+%end
+
+// The DJ's header row fades with the rest while the lines are alone.
+%group SGRDJHeaderFadeHooks
+%hook SGRDJHeaderFadeUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ sg_header = (UIViewController *)self;
+ replace();
+}
+%end
+%end
+
+%group SGRDJDurationHooks
+%hook SGRDJDurationUnit
+- (void)viewDidLayoutSubviews {
+ %orig;
+ durationUnitLaidOut((UIViewController *)self);
+}
+%end
+%end
+
// The chips over the title (Switch to video and whatever else a track brings) sit in the middle of the
// room the lines take, so they go while the lines are up. The row keeps its height: the rest of the
// bottom stack stays where it was, which is the whole point of lifting only the title out of it.
@@ -847,6 +886,12 @@ BOOL SGRPlayerLyricsNone(void) {
%ctor {
if (!SGRedesignedUI()) return;
%init;
+ Class djInformation = SGRDJClass(@"DJMInformationUnitViewController");
+ if (djInformation) { %init(SGRDJInformationHooks, SGRDJInformationUnit = djInformation); }
+ Class djDuration = SGRDJClass(@"DJMDurationElementsUnit");
+ if (djDuration) { %init(SGRDJDurationHooks, SGRDJDurationUnit = djDuration); }
+ Class djHeader = SGRDJClass(@"DJMHeaderElementsUnitViewController");
+ if (djHeader) { %init(SGRDJHeaderFadeHooks, SGRDJHeaderFadeUnit = djHeader); }
sg_watcher = [SGRPlayerLyricsWatcher new];
SGAddPlayerStateObserver(sg_watcher);
// In the background the controls stay up; back in front the clock starts again.
diff --git a/tweak/Sources/Redesigned/Player/PlayerVolume.m b/tweak/Sources/Redesigned/Player/PlayerVolume.m
index d1c730e4..e338f62a 100644
--- a/tweak/Sources/Redesigned/Player/PlayerVolume.m
+++ b/tweak/Sources/Redesigned/Player/PlayerVolume.m
@@ -88,7 +88,9 @@ static UIImageView *glyph(NSString *symbol) {
@end
-static char kVolumeKey, kPlayKey;
+static char kVolumeKey, kPlayKey, kSettleKey;
+// Passes the footer's layout is asked for again while the row's place keeps changing, at most.
+static const NSUInteger kSettlePasses = 6;
// Read at launch, as the player's other switches are: the footer asks on every layout pass.
BOOL SGRPlayerVolumeOn(void) {
@@ -98,6 +100,28 @@ BOOL SGRPlayerVolumeOn(void) {
return on;
}
+// The footer lays out before Spotify's bottom stack has arranged its views the first time the player opens (the
+// controls then stand near the stack's top), and nothing lays the footer out again once the stack has: the row was
+// placed against the controls' old place and stayed there, over their buttons. A row that moved asks the footer's
+// layout once more on the next turn of the run loop, which finds the settled places; one that did not move ends it.
+static void settle(UIView *stack, UIView *row, BOOL moved, CGRect frame) {
+ NSUInteger passes = [objc_getAssociatedObject(stack, &kSettleKey) unsignedIntegerValue];
+ if (!moved) {
+ if (passes) objc_setAssociatedObject(stack, &kSettleKey, @0, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ return;
+ }
+ if (passes >= kSettlePasses) return;
+ objc_setAssociatedObject(stack, &kSettleKey, @(passes + 1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ if (passes > 0) SGLog(@"redesign player: volume row placed again at %.0f (pass %lu)", frame.origin.y, (unsigned long)passes + 1);
+ __weak UIView *weakStack = stack, *weakRow = row;
+ dispatch_async(dispatch_get_main_queue(), ^{
+ UIView *settledStack = weakStack, *settledRow = weakRow;
+ if (!settledStack.window || !settledRow.window) return;
+ [settledStack layoutIfNeeded];
+ SGRPlayerLowerRow(settledRow);
+ });
+}
+
BOOL SGRPlayerPlaceVolume(UIView *stack, UIView *controls, UIView *row, CGFloat margin) {
SGRVolumeRow *volume = objc_getAssociatedObject(stack, &kVolumeKey);
if (!SGRPlayerVolumeOn() || !controls) {
@@ -126,7 +150,9 @@ BOOL SGRPlayerPlaceVolume(UIView *stack, UIView *controls, UIView *row, CGFloat
CGFloat below = [row convertPoint:CGPointMake(CGRectGetMidX(row.bounds), CGRectGetMidY(row.bounds)) toView:stack].y - kGlyphsHalf;
CGFloat height = kRowHeight, room = below - above;
CGRect frame = CGRectMake(margin, round((above + below - height) / 2), MAX(0, stack.bounds.size.width - 2 * margin), height);
- if (!CGRectEqualToRect(volume.frame, frame)) volume.frame = frame;
+ BOOL moved = !CGRectEqualToRect(volume.frame, frame);
+ if (moved) volume.frame = frame;
+ settle(stack, row, moved, frame);
// The lyrics fade the stack's views by their alpha and bring back only the ones they faded.
// The track and thumb are 14pt of the row's 34: it fits with the glyphs' and buttons' own clearance around it.
BOOL fits = room >= height * 0.6;