Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
// 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);
}