Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
refactor: the reads of Spotify's tab bar move to Shared/Navigation/TabItems.h and the room under it to TabBarRoom.m (#205)
Andrew7659c36b6e74
diff --git a/harness/tabbar/build.sh b/harness/tabbar/build.shindex e89de390..343acdb7 100755--- a/harness/tabbar/build.sh+++ b/harness/tabbar/build.sh@@ -19,7 +19,7 @@ xcrun -sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-ar"$SRC"/Core/SGLog.m "$SRC"/Core/SGPrefs.m "$SRC"/Core/SGViewTree.m "$SRC"/Core/SGGlass.m \"$SRC"/Core/SGBackdrop.m "$SRC"/Core/SGFlagForce.m "$SRC"/Core/SGUIMode.m \"$SRC"/Redesigned/Kit/SGRTokens.m "$SRC"/Redesigned/Kit/SGRGlass.m \- "$SRC"/Redesigned/Navbar/NavbarLayout.m "$SRC"/Shared/Navigation/TabIcons.m "$SRC"/Settings/SGMarquee.m \+ "$SRC"/Redesigned/Navbar/NavbarLayout.m "$SRC"/Redesigned/Navbar/TabBarRoom.m "$SRC"/Shared/Navigation/TabIcons.m "$SRC"/Settings/SGMarquee.m \-framework UIKit -framework IOKit -framework QuartzCore -framework CoreGraphics -framework Foundation \-o "$OUT/TabBarHarness.app/TabBarHarness"diff --git a/tweak/Sources/Native/Navbar/TabBarHooks.x b/tweak/Sources/Native/Navbar/TabBarHooks.xindex 98843cd3..56322e72 100644--- a/tweak/Sources/Native/Navbar/TabBarHooks.x+++ b/tweak/Sources/Native/Navbar/TabBarHooks.x@@ -6,6 +6,7 @@#import "Core/SGCore.h"#import "Navbar.h"#import "Settings/SGPage.h"+#import "Shared/Navigation/TabItems.h"@interface SGHomeHold : UILongPressGestureRecognizer@end@@ -26,12 +27,6 @@ static void holdHome(UIView *stockBar) {[home addGestureRecognizer:[[SGHomeHold alloc] initWithTarget:SGHomeHold.class action:@selector(held:)]];}-static UIView *tabBarOf(UIView *item) {- Class barClass = NSClassFromString(@"_TtC23NavigationUI_TabBarImpl10TabBarView");- for (UIView *v = item.superview; v; v = v.superview) if ([v isKindOfClass:barClass]) return v;- return nil;-}-%hook _TtC23NavigationUI_TabBarImpl10TabBarView- (void)layoutSubviews {%orig;diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex 9ad55f96..98a49c57 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -1827,13 +1827,6 @@ typedef struct {_dots = nil;}-static BOOL hasTranslations(NSArray<SGKaraokeLine *> *lines) {- for (SGKaraokeLine *line in lines) {- if (line.translation.length) return YES;- }- return NO;-}-// A line with words to translate: not an empty one, nor a ♪ holding a break.static BOOL hasWords(SGKaraokeLine *line) {for (SGKaraokeWord *word in line.words) {@@ -2470,7 +2463,7 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {NSArray<SGKaraokeLine *> *kept = _lines && track ? [self linesFor:track] : nil;if (kept && kept != _lines && kept != _passedOver) {if (!_hasTranslation && kept.count == _lines.count && SGKaraokeLinesTiming(kept) == SGKaraokeLinesTiming(_lines)- && hasTranslations(kept)) {+ && SGKaraokeHasTranslations(kept)) {// Musixmatch's translations come in a moment after the lines, as copies of them: the song is// restyled with them where it stands.SGLog(@"karaoke: translations of %@ came in", track);diff --git a/tweak/Sources/Redesigned/Navbar/Navbar.h b/tweak/Sources/Redesigned/Navbar/Navbar.hindex cf9670a1..42dcb706 100644--- a/tweak/Sources/Redesigned/Navbar/Navbar.h+++ b/tweak/Sources/Redesigned/Navbar/Navbar.h@@ -38,6 +38,14 @@ void SGRComposeTabBar(UIView *tabBar);void SGRLogTabBarRow(UIView *tabBar);// Lays the bar out again after the Navbar page changes something, so it does not wait for a touch.void SGRRefreshTabBar(void);++// TabBarRoom.m. The hold on Spotify's Home that opens Mod Settings, added once.+void SGRHoldHomeForModSettings(UIView *stockBar);+// The room made under Spotify's bar for the taller bar of the mod's (see "room for the glass bar" there): the bottom+// safe area `container` (TabBarContainerImpl) is given, and what UIKit's `bar` asks for with no room made.+CGFloat SGRTabBarRoom(void);+CGFloat SGRTabBarGlassHeight(UITabBar *bar, UIView *stockBar);+void SGRMakeTabBarRoom(UIViewController *container, UIView *stockBar, UITabBar *bar);// Whether a tab of the composed row is one of the split tabs at its trailing end, for TabBar.x's second bar.BOOL SGRTabIsApart(UIView *item);// The tab of the mod's own whose page is on screen, which the glass bar shows selected; nil when none is.diff --git a/tweak/Sources/Redesigned/Navbar/TabBar.x b/tweak/Sources/Redesigned/Navbar/TabBar.xindex bb78e97d..cea19bfb 100644--- a/tweak/Sources/Redesigned/Navbar/TabBar.x+++ b/tweak/Sources/Redesigned/Navbar/TabBar.x@@ -16,6 +16,7 @@#import "Redesigned/Kit/SGRTokens.h"#import "Redesigned/NowPlayingBar/NowPlayingBar.h"#import "Settings/SGPage.h"+#import "Shared/Navigation/TabItems.h"#import "Headers/SPTEncoreIconView.h"#import "Shared/Navigation/TabIcons.h"#import <objc/message.h>@@ -24,7 +25,6 @@ static char kBarKey, kApartBarKey, kHostKey, kFadeKey;// On a UITabBarItem: the names of the Encore icons its image and selectedImage were drawn from (see fillBar).static char kGlyphNamesKey;static __weak UIView *sg_stockBar;-static CGFloat sg_room, sg_glassHeight; // see "room for the glass bar"// The glass row in the window as the now playing card was last told of it (syncBar), CGRectNull for none.static CGRect sg_glassRow = {{INFINITY, INFINITY}, {0, 0}};// See "minimized". sg_keepApart holds the second bar on screen while the main one grows back under it.@@ -45,68 +45,6 @@ static void syncBar(UIView *stockBar);#pragma mark - reading Spotify's items-// The items the bar shows, left to right as Navbar/Navbar.x placed them.-static NSArray<UIView *> *tabItems(UIView *tabBar) {- NSMutableArray<UIView *> *items = [NSMutableArray array];- for (UIView *item in SGRowIn(tabBar).arrangedSubviews) {- if (!item.hidden && item.bounds.size.width >= 20) [items addObject:item];- }- return [items sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {- return [@(SGFrameIn(a, tabBar).origin.x) compare:@(SGFrameIn(b, tabBar).origin.x)];- }];-}--// Navbar.x never reorders Spotify's row and appends the mod's own tabs after it, so Home stays first.-static BOOL isHome(UIView *item, UIView *tabBar) {- return item && item == SGRowIn(tabBar).arrangedSubviews.firstObject;-}--static UILabel *labelIn(UIView *item) {- __block UILabel *label = nil;- SGForEachView(item, ^(UIView *v) {- if (!label && [v isKindOfClass:UILabel.class] && ((UILabel *)v).text.length) label = (UILabel *)v;- });- return label;-}--static UIView *iconIn(UIView *item) {- __block UIView *icon = nil;- SGForEachView(item, ^(UIView *v) {- if (icon || v.bounds.size.width < 2) return;- if ([v isKindOfClass:UIImageView.class] || [NSStringFromClass(v.class) containsString:@"IconView"]) icon = v;- });- return icon;-}--// Spotify paints the selected tab's label white and the rest #B3B3B3.-static BOOL isActive(UIView *item) {- UIColor *color = labelIn(item).textColor;- CGFloat white = 0, alpha = 0, r, g, b;- if (![color getWhite:&white alpha:&alpha] && [color getRed:&r green:&g blue:&b alpha:&alpha]) white = MIN(r, MIN(g, b));- return white > 0.95;-}--static BOOL hasInk(UIImage *image) {- CGImageRef cg = image.CGImage;- size_t width = CGImageGetWidth(cg), height = CGImageGetHeight(cg);- if (!width || !height) return NO;- NSMutableData *pixels = [NSMutableData dataWithLength:width * height];- CGContextRef context = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);- CGContextDrawImage(context, CGRectMake(0, 0, width, height), cg);- CGContextRelease(context);- const uint8_t *alpha = pixels.bytes;- for (size_t i = 0; i < pixels.length; i++) if (alpha[i] > 16) return YES;- return NO;-}--static UIImage *renderLayer(CALayer *layer, CGSize size) {- UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size];- UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {- [layer renderInContext:context.CGContext];- }];- return hasInk(image) ? [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] : nil;-}-// The SPTEncoreIcon an icon view was built with. Encore keeps it in a Swift ivar with no getter.static id encoreIconOf(UIView *view) {Ivar ivar = class_getInstanceVariable(view.class, "icon");@@ -192,51 +130,6 @@ UIImage *SGRNavbarGlyph(NSDictionary *entry, BOOL active) {return nil;}-#pragma mark - passing a tap on--// NavigationUI_TabBarImpl's TabBarItemElementUI answers a tap recognizer (-handleTap), so the tap is-// replayed through the recognizer's own target-action pairs, the same call a real touch ends in.-static BOOL fireTapRecognizers(UIView *view) {- Ivar targetsIvar = class_getInstanceVariable(UIGestureRecognizer.class, "_targets");- if (!targetsIvar) return NO;- BOOL fired = NO;- for (UIGestureRecognizer *recognizer in view.gestureRecognizers) {- if (![recognizer isKindOfClass:UITapGestureRecognizer.class] || !recognizer.enabled) continue;- for (id pair in object_getIvar(recognizer, targetsIvar)) {- Ivar targetIvar = class_getInstanceVariable([pair class], "_target");- Ivar actionIvar = class_getInstanceVariable([pair class], "_action");- if (!targetIvar || !actionIvar) continue;- id target = object_getIvar(pair, targetIvar);- SEL action = *(SEL *)((char *)(__bridge void *)pair + ivar_getOffset(actionIvar));- if (!target || !action || ![target respondsToSelector:action]) continue;- SGLog(@"tab bar: tap -> %@ %@", NSStringFromClass([target class]), NSStringFromSelector(action));- ((void (*)(id, SEL, id))objc_msgSend)(target, action, recognizer);- fired = YES;- }- }- return fired;-}--static void forwardTap(UIView *item) {- __block BOOL sent = NO;- SGForEachView(item, ^(UIView *v) {- if (!sent) sent = fireTapRecognizers(v);- });- SGForEachView(item, ^(UIView *v) {- if (sent || ![v isKindOfClass:UIControl.class]) return;- SGLog(@"tab bar: tap -> control %@", NSStringFromClass(v.class));- [(UIControl *)v sendActionsForControlEvents:UIControlEventTouchUpInside];- sent = YES;- });- if (!sent) {- NSMutableString *out = [NSMutableString stringWithFormat:@"tab bar: nothing to tap in %@", NSStringFromClass(item.class)];- SGForEachView(item, ^(UIView *v) {- for (UIGestureRecognizer *r in v.gestureRecognizers) [out appendFormat:@"\n %@ on %@", r, NSStringFromClass(v.class)];- });- SGLogLong(@"navbar", out);- }-}-#pragma mark - the system bar// A name too long for its tab (one of the user's own, a split tab's circle) shrinks a little before UIKit cuts@@ -341,30 +234,11 @@ void SGRShrinkTabTitles(UIView *bar) {@implementation SGRTabBarHost- (UIEdgeInsets)safeAreaInsets {UIEdgeInsets insets = [super safeAreaInsets];- insets.bottom = MAX(0, insets.bottom - sg_room);+ insets.bottom = MAX(0, insets.bottom - SGRTabBarRoom());return insets;}@end-@interface SGRHomeHold : UILongPressGestureRecognizer-@end--@implementation SGRHomeHold-+ (void)held:(SGRHomeHold *)hold {- if (hold.state == UIGestureRecognizerStateBegan) SGOpenModSettings(hold.view);-}-@end--// On Spotify's own bar a hold that begins fails the item's tap recognizer, so Home is not tapped too.-static void holdHome(UIView *stockBar) {- UIView *home = SGRowIn(stockBar).arrangedSubviews.firstObject;- if (!home) return;- for (UIGestureRecognizer *recognizer in home.gestureRecognizers) {- if ([recognizer isKindOfClass:SGRHomeHold.class]) return;- }- [home addGestureRecognizer:[[SGRHomeHold alloc] initWithTarget:SGRHomeHold.class action:@selector(held:)]];-}-static void logBarOnce(UITabBar *bar) {static dispatch_once_t once;dispatch_once(&once, ^{@@ -415,51 +289,10 @@ static void placeFade(UIView *stockBar, CGRect glass, BOOL minimized) {#pragma mark - room for the glass bar-// UIKit's glass bar asks for 83 pt, the platter the top 62 of it, over no more safe area than a Face ID-// phone's 34 (simulator, iOS 26.5 and 27). Spotify's bar is its 49 pt row over the bottom safe area of-// TabBarContainerImpl's view: a guide from 49 pt above the safe area's bottom to the view's bottom sets-// its height (its viewDidLoad, 0x100840a2c), the now playing bar stands on that guide's top-// (MainUIContainer's chrome bottom anchor, 0x100ae0178), the bar slides away by the inset plus 49 when-// Spotify hides it (0x1037169a4) and the pages get 49 on top of the inset (0x10707bde4). A Face ID-// phone gives the view 34 and the two bars match. A phone with a home button gives it none, and so does-// Spotify's message bar (LimitedExperienceIndicatorBar: Offline, Private Session) coming in under the-// tab bar, which takes the home indicator's inset for itself: the glass bar stood 34 pt above-// Spotify's, over the now playing bar. So the view gets the rest of the glass bar's height as safe-// area, and Spotify lays its bar, the now playing bar, the pages and the hide out for the glass bar-// itself, and moves them all with the message bar.-static const CGFloat kStockRow = 49;--// UIKit asks for 62 + max(21, inset) on a phone with a home button, max(83, 49 + inset) on a Face ID-// phone, by the safe area of the view the bar stands in. SGRTabBarHost keeps the room out of that; if-// it ever reached the bar again, the bar would ask for more room every pass, so what it asks for with-// no room made is what is kept.-static CGFloat glassHeight(UITabBar *bar, UIView *stockBar) {- if (sg_room < 0.5 || sg_glassHeight <= 0) sg_glassHeight = [bar sizeThatFits:CGSizeMake(stockBar.bounds.size.width, kStockRow)].height;- return sg_glassHeight;-}--static UIViewController *containerOf(UIView *stockBar) {- Class containerClass = NSClassFromString(@"_TtC23NavigationUI_TabBarImpl19TabBarContainerImpl");- for (UIResponder *r = stockBar.nextResponder; r; r = r.nextResponder) {- if ([r isKindOfClass:containerClass]) return (UIViewController *)r;- }- return nil;-}-+// The room itself is TabBarRoom.m's.static void makeRoom(UIViewController *container) {UIView *stockBar = sg_stockBar;- UITabBar *bar = stockBar ? objc_getAssociatedObject(stockBar, &kBarKey) : nil;- if (!bar.window || !container.isViewLoaded || ![stockBar isDescendantOfView:container.view]) return;- UIEdgeInsets extra = container.additionalSafeAreaInsets;- CGFloat inset = container.view.safeAreaInsets.bottom - extra.bottom;- CGFloat height = glassHeight(bar, stockBar);- // Spotify's regular width bar is a fixed 76 pt that ignores the inset.- BOOL compact = container.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact;- CGFloat room = compact ? MAX(0, ceil(height - kStockRow - inset)) : 0;- if (fabs(extra.bottom - room) < 0.5) return;- sg_room = extra.bottom = room;- container.additionalSafeAreaInsets = extra;- SGLog(@"tab bar: %.0f pt of room made under Spotify's bar for the glass bar's %.0f, over an inset of %.0f", room, height, inset);+ SGRMakeTabBarRoom(container, stockBar, stockBar ? objc_getAssociatedObject(stockBar, &kBarKey) : nil);}static SGRSystemTabBar *makeBar(UIView *stockBar) {@@ -616,7 +449,7 @@ static void syncBar(UIView *stockBar) {CGRect bounds = stockBar.bounds;CGFloat width = bounds.size.width;- CGFloat height = MAX(bounds.size.height, glassHeight(bar, stockBar));+ CGFloat height = MAX(bounds.size.height, SGRTabBarGlassHeight(bar, stockBar));CGRect frame = CGRectMake(0, CGRectGetMaxY(bounds) - height, width, height);if (!CGRectEqualToRect(host.frame, frame)) host.frame = frame;placeFade(stockBar, frame, minimized);@@ -931,12 +764,6 @@ CGRect SGRTabBarGlassRow(UIView *host) {#pragma mark - hooks-static UIView *tabBarOf(UIView *item) {- Class barClass = NSClassFromString(@"_TtC23NavigationUI_TabBarImpl10TabBarView");- for (UIView *v = item.superview; v; v = v.superview) if ([v isKindOfClass:barClass]) return v;- return nil;-}-// The bar in its own pass, and whether one of its items laid out during it.static __weak UIView *sg_passBar;static BOOL sg_itemsLaidOut;@@ -957,7 +784,7 @@ static BOOL sg_itemsLaidOut;if (sg_itemsLaidOut) SGRComposeTabBar(bar);sg_passBar = outer;sg_itemsLaidOut = outerLaidOut;- holdHome(bar);+ SGRHoldHomeForModSettings(bar);syncBar(bar);SGRLogTabBarRow(bar);}@@ -973,7 +800,7 @@ static void itemDidLayOut(UIView *item) {return;}SGRComposeTabBar(bar);- holdHome(bar);+ SGRHoldHomeForModSettings(bar);syncBar(bar);SGRLogTabBarRow(bar);}diff --git a/tweak/Sources/Redesigned/Navbar/TabBarRoom.m b/tweak/Sources/Redesigned/Navbar/TabBarRoom.mnew file mode 100644index 00000000..b289248a--- /dev/null+++ b/tweak/Sources/Redesigned/Navbar/TabBarRoom.m@@ -0,0 +1,71 @@+// What the redesign's tab bar does to Spotify's own bar apart from drawing itself (TabBar.x): the hold on Home that+// opens Mod Settings, and the room made under Spotify's bar for the taller bar of the mod's.+// Threading: main thread only.+#import "Core/SGCore.h"+#import "Navbar.h"+#import "Settings/SGPage.h"++#pragma mark - the hold on Home++@interface SGRHomeHold : UILongPressGestureRecognizer+@end++@implementation SGRHomeHold++ (void)held:(SGRHomeHold *)hold {+ if (hold.state == UIGestureRecognizerStateBegan) SGOpenModSettings(hold.view);+}+@end++// On Spotify's own bar a hold that begins fails the item's tap recognizer, so Home is not tapped too.+void SGRHoldHomeForModSettings(UIView *stockBar) {+ UIView *home = SGRowIn(stockBar).arrangedSubviews.firstObject;+ if (!home) return;+ for (UIGestureRecognizer *recognizer in home.gestureRecognizers) {+ if ([recognizer isKindOfClass:SGRHomeHold.class]) return;+ }+ [home addGestureRecognizer:[[SGRHomeHold alloc] initWithTarget:SGRHomeHold.class action:@selector(held:)]];+}++#pragma mark - room for the glass bar++// UIKit's glass bar asks for 83 pt, the platter the top 62 of it, over no more safe area than a Face ID+// phone's 34 (simulator, iOS 26.5 and 27). Spotify's bar is its 49 pt row over the bottom safe area of+// TabBarContainerImpl's view: a guide from 49 pt above the safe area's bottom to the view's bottom sets+// its height (its viewDidLoad, 0x100840a2c), the now playing bar stands on that guide's top+// (MainUIContainer's chrome bottom anchor, 0x100ae0178), the bar slides away by the inset plus 49 when+// Spotify hides it (0x1037169a4) and the pages get 49 on top of the inset (0x10707bde4). A Face ID+// phone gives the view 34 and the two bars match. A phone with a home button gives it none, and so does+// Spotify's message bar (LimitedExperienceIndicatorBar: Offline, Private Session) coming in under the+// tab bar, which takes the home indicator's inset for itself, so the glass bar would stand over the now+// playing bar. So the view gets the rest of the glass bar's height as safe area, and Spotify lays its bar,+// the now playing bar, the pages and the hide out for the glass bar itself, and moves them all with the+// message bar.+static const CGFloat kStockRow = 49;+static CGFloat sg_room, sg_glassHeight;++CGFloat SGRTabBarRoom(void) {+ return sg_room;+}++// UIKit asks for 62 + max(21, inset) on a phone with a home button, max(83, 49 + inset) on a Face ID+// phone, by the safe area of the view the bar stands in. The bar's host keeps the room out of that; if+// it ever reached the bar again, the bar would ask for more room every pass, so what it asks for with+// no room made is what is kept.+CGFloat SGRTabBarGlassHeight(UITabBar *bar, UIView *stockBar) {+ if (sg_room < 0.5 || sg_glassHeight <= 0) sg_glassHeight = [bar sizeThatFits:CGSizeMake(stockBar.bounds.size.width, kStockRow)].height;+ return sg_glassHeight;+}++void SGRMakeTabBarRoom(UIViewController *container, UIView *stockBar, UITabBar *bar) {+ if (!bar.window || !container.isViewLoaded || ![stockBar isDescendantOfView:container.view]) return;+ UIEdgeInsets extra = container.additionalSafeAreaInsets;+ CGFloat inset = container.view.safeAreaInsets.bottom - extra.bottom;+ CGFloat height = SGRTabBarGlassHeight(bar, stockBar);+ // Spotify's regular width bar is a fixed 76 pt that ignores the inset.+ BOOL compact = container.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact;+ CGFloat room = compact ? MAX(0, ceil(height - kStockRow - inset)) : 0;+ if (fabs(extra.bottom - room) < 0.5) return;+ sg_room = extra.bottom = room;+ container.additionalSafeAreaInsets = extra;+ SGLog(@"tab bar: %.0f pt of room made under Spotify's bar for the glass bar's %.0f, over an inset of %.0f", room, height, inset);+}diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.xindex 30734ad4..babe6c75 100644--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x@@ -95,13 +95,6 @@ static SPTPlayerTrack *upNextIn(SPTPlayerState *state) {return [next isKindOfClass:objc_getClass("SPTPlayerTrack")] ? next : nil;}-static BOOL hasTranslations(NSArray<SGKaraokeLine *> *lines) {- for (SGKaraokeLine *line in lines) {- if (line.translation.length) return YES;- }- return NO;-}-// Lines with no translation of their own take Musixmatch's community ones in the Lyrics page's language,// whichever source they came from, Spotify's own included, matched by their text. Only the redesign shows// translations, so the native look asks for none. The translated lines are copies kept in place of the@@ -109,11 +102,11 @@ static BOOL hasTranslations(NSArray<SGKaraokeLine *> *lines) {// Musixmatch answers at once once it has answered, so lines kept again take the translations straight away.static void translate(NSString *track) {NSString *language = SGRedesignedUI() ? SGLyricsTranslationLanguage() : nil;- if (!language || hasTranslations(sg_lyrics[track])) return;+ if (!language || SGKaraokeHasTranslations(sg_lyrics[track])) return;SGMusixmatchTranslations(track, language, ^(NSDictionary<NSString *, NSString *> *byLine) {// Whatever is kept by now, which may be Spotify's timed lines in place of the plain ones asked for.NSArray<SGKaraokeLine *> *kept = sg_lyrics[track];- if (!byLine || hasTranslations(kept)) return;+ if (!byLine || SGKaraokeHasTranslations(kept)) return;NSArray<SGKaraokeLine *> *translated = SGMusixmatchTranslatedLines(kept, byLine);if (translated) sg_lyrics[track] = translated;});diff --git a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m b/tweak/Sources/Shared/Lyrics/KaraokeTiming.mindex 583e840c..cfbe4f3f 100644--- a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m+++ b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m@@ -375,3 +375,10 @@ NSArray<SGKaraokeLine *> *SGKaraokeLinesFromBody(NSData *body) {if (!body.length) return nil;return ((const uint8_t *)body.bytes)[0] == '{' ? fromJSON(body) : fromProtobuf(body);}++BOOL SGKaraokeHasTranslations(NSArray<SGKaraokeLine *> *lines) {+ for (SGKaraokeLine *line in lines) {+ if (line.translation.length) return YES;+ }+ return NO;+}diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.hindex cd618f11..bd092efd 100644--- a/tweak/Sources/Shared/Lyrics/Lyrics.h+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h@@ -93,6 +93,8 @@ typedef NS_ENUM(NSUInteger, SGKaraokeAlign) {@property (nonatomic) BOOL translationMade;@end+// Whether any of the lines carries a translation.+BOOL SGKaraokeHasTranslations(NSArray<SGKaraokeLine *> *lines);// The line as one string, a space between the words that are not joined.NSString *SGKaraokeLineText(SGKaraokeLine *line);// When the singing of a line is over: its own end, or its backing's when that runs on past it.diff --git a/tweak/Sources/Shared/Navigation/TabItems.h b/tweak/Sources/Shared/Navigation/TabItems.hnew file mode 100644index 00000000..40f30e98--- /dev/null+++ b/tweak/Sources/Shared/Navigation/TabItems.h@@ -0,0 +1,126 @@+// Reads of Spotify's tab bar that both looks' bars make: the items it shows, the one that is Home or active, the+// glyphs it draws and the bar or container an item stands in, and a tap passed on to one of its items. The redesign's TabBar.x and the+// Native bar both take them from here.+#import <UIKit/UIKit.h>+#import <objc/message.h>+#import <objc/runtime.h>+#import "Core/SGLog.h"+#import "Core/SGViewTree.h"++// The items the bar shows, left to right as Navbar/Navbar.x placed them.+static inline NSArray<UIView *> *tabItems(UIView *tabBar) {+ NSMutableArray<UIView *> *items = [NSMutableArray array];+ for (UIView *item in SGRowIn(tabBar).arrangedSubviews) {+ if (!item.hidden && item.bounds.size.width >= 20) [items addObject:item];+ }+ return [items sortedArrayUsingComparator:^NSComparisonResult(UIView *a, UIView *b) {+ return [@(SGFrameIn(a, tabBar).origin.x) compare:@(SGFrameIn(b, tabBar).origin.x)];+ }];+}++// Navbar.x never reorders Spotify's row and appends the mod's own tabs after it, so Home stays first.+static inline BOOL isHome(UIView *item, UIView *tabBar) {+ return item && item == SGRowIn(tabBar).arrangedSubviews.firstObject;+}++static inline UILabel *labelIn(UIView *item) {+ __block UILabel *label = nil;+ SGForEachView(item, ^(UIView *v) {+ if (!label && [v isKindOfClass:UILabel.class] && ((UILabel *)v).text.length) label = (UILabel *)v;+ });+ return label;+}++static inline UIView *iconIn(UIView *item) {+ __block UIView *icon = nil;+ SGForEachView(item, ^(UIView *v) {+ if (icon || v.bounds.size.width < 2) return;+ if ([v isKindOfClass:UIImageView.class] || [NSStringFromClass(v.class) containsString:@"IconView"]) icon = v;+ });+ return icon;+}++// Spotify paints the selected tab's label white and the rest #B3B3B3.+static inline BOOL isActive(UIView *item) {+ UIColor *color = labelIn(item).textColor;+ CGFloat white = 0, alpha = 0, r, g, b;+ if (![color getWhite:&white alpha:&alpha] && [color getRed:&r green:&g blue:&b alpha:&alpha]) white = MIN(r, MIN(g, b));+ return white > 0.95;+}++static inline BOOL hasInk(UIImage *image) {+ CGImageRef cg = image.CGImage;+ size_t width = CGImageGetWidth(cg), height = CGImageGetHeight(cg);+ if (!width || !height) return NO;+ NSMutableData *pixels = [NSMutableData dataWithLength:width * height];+ CGContextRef context = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), cg);+ CGContextRelease(context);+ const uint8_t *alpha = pixels.bytes;+ for (size_t i = 0; i < pixels.length; i++) if (alpha[i] > 16) return YES;+ return NO;+}++static inline UIImage *renderLayer(CALayer *layer, CGSize size) {+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size];+ UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {+ [layer renderInContext:context.CGContext];+ }];+ return hasInk(image) ? [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] : nil;+}++static inline UIViewController *containerOf(UIView *stockBar) {+ Class containerClass = NSClassFromString(@"_TtC23NavigationUI_TabBarImpl19TabBarContainerImpl");+ for (UIResponder *r = stockBar.nextResponder; r; r = r.nextResponder) {+ if ([r isKindOfClass:containerClass]) return (UIViewController *)r;+ }+ return nil;+}++static inline UIView *tabBarOf(UIView *item) {+ Class barClass = NSClassFromString(@"_TtC23NavigationUI_TabBarImpl10TabBarView");+ for (UIView *v = item.superview; v; v = v.superview) if ([v isKindOfClass:barClass]) return v;+ return nil;+}++// NavigationUI_TabBarImpl's TabBarItemElementUI answers a tap recognizer (-handleTap), so the tap is replayed through+// the recognizer's own target-action pairs, the same call a real touch ends in.+static inline BOOL fireTapRecognizers(UIView *view) {+ Ivar targetsIvar = class_getInstanceVariable(UIGestureRecognizer.class, "_targets");+ if (!targetsIvar) return NO;+ BOOL fired = NO;+ for (UIGestureRecognizer *recognizer in view.gestureRecognizers) {+ if (![recognizer isKindOfClass:UITapGestureRecognizer.class] || !recognizer.enabled) continue;+ for (id pair in object_getIvar(recognizer, targetsIvar)) {+ Ivar targetIvar = class_getInstanceVariable([pair class], "_target");+ Ivar actionIvar = class_getInstanceVariable([pair class], "_action");+ if (!targetIvar || !actionIvar) continue;+ id target = object_getIvar(pair, targetIvar);+ SEL action = *(SEL *)((char *)(__bridge void *)pair + ivar_getOffset(actionIvar));+ if (!target || !action || ![target respondsToSelector:action]) continue;+ ((void (*)(id, SEL, id))objc_msgSend)(target, action, recognizer);+ fired = YES;+ }+ }+ return fired;+}++// A tap on Spotify's `item`: through its tap recognizers, else the first control in it. What it holds is logged when+// neither is there.+static inline void forwardTap(UIView *item) {+ __block BOOL sent = NO;+ SGForEachView(item, ^(UIView *v) {+ if (!sent) sent = fireTapRecognizers(v);+ });+ SGForEachView(item, ^(UIView *v) {+ if (sent || ![v isKindOfClass:UIControl.class]) return;+ [(UIControl *)v sendActionsForControlEvents:UIControlEventTouchUpInside];+ sent = YES;+ });+ if (sent) return;+ NSMutableString *out = [NSMutableString stringWithFormat:@"tab bar: nothing to tap in %@", NSStringFromClass(item.class)];+ SGForEachView(item, ^(UIView *v) {+ for (UIGestureRecognizer *r in v.gestureRecognizers) [out appendFormat:@"\n %@ on %@", r, NSStringFromClass(v.class)];+ });+ SGLogLong(@"navbar", out);+}