Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Home's shortcut tiles hold their cover inset on a surface tinted faintly by it, instead of the stretched blur
Vojtěch Škopek3de8ea1431eb
diff --git a/tweak/Sources/Redesigned/Home/HomeTiles.m b/tweak/Sources/Redesigned/Home/HomeTiles.mindex aa1a7530..c0227995 100644--- a/tweak/Sources/Redesigned/Home/HomeTiles.m+++ b/tweak/Sources/Redesigned/Home/HomeTiles.m@@ -1,33 +1,39 @@-// Home redesign: a shortcut tile runs its own picture across itself. The sharp cover keeps its place at the-// leading edge and fades, from 40% of its width, into the same picture blurred, which carries on to the-// trailing edge as its own edge stretched out and dims under the title until white text on it passes WCAG AA-// (Kit/SGRPalette.h, +extensionForImage:). Until the picture has loaded, the tile is Spotify's grey.+// Home redesign: a shortcut tile the way an Apple list row holds artwork. The cover sits inset from the tile's+// edges with its own corners, the title follows it in, and the tile is a dark surface tinted faintly towards the+// cover's dominant colour (Kit/SGRPalette.h, +tintForImage:), so the grid is not one grey and white text always+// reads. Until the cover has loaded, the tile is the untinted surface.//-// The picture sits behind Spotify's stack, over the tile's own fill, so the title, the playing indicator and-// the button's touches stay Spotify's. It is made off the main thread once per picture and follows the cover-// as Spotify sets it (SGRObserveImage), since a cover lands after the tile has laid out.+// The surface sits behind Spotify's stack, over the tile's own fill, so the title, the playing indicator and+// the button's touches stay Spotify's. The inset and the title's move are transforms, which Spotify's layout+// never reads, so its constraints are left as they are. The tint is worked out off the main thread once per+// picture and follows the cover as Spotify sets it (SGRObserveImage), since a cover lands after layout.//-// Tree (trees/continuous/1.txt:3454-3465, 2026-09-17): InteractableLayoutBackingButton id=Shortcut.Card.Home+// Tree (trees/home 3 more scrolled.txt:36-50, 2026-09-17): InteractableLayoutBackingButton id=Shortcut.Card.Home// 181x48 clips > UIView 181x48 bg=#[email protected] (the fill), then Encore.StackView > AutoLayoutStackView >-// UIView > UIView {0, 0} 48x48 bg=#[email protected] r=4 (the cover's square) > Encore.ImageView > UIImageView 48x48-// and the PlaceholderView; the title's stack starts at x 56.-#import <QuartzCore/QuartzCore.h>+// UIView 173x48 (the row) > UIView {0, 0} 48x48 bg=#[email protected] r=4 (the cover's square) > Encore.ImageView >+// UIImageView 48x48 and the PlaceholderView; then the row's 8pt spacer and the title's stack at x 56.#import "Core/SGCore.h"#import "Redesigned/Kit/SGRKit.h"#import "Home.h"-// Where the sharp cover starts to fade, as a share of its width.-static const CGFloat kFadeFrom = 0.4;+// How far the cover sits in from the tile's top, bottom and leading edges, and its corners there.+static const CGFloat kInset = 5, kCoverRadius = 4;-static char kBackdropKey, kImageKey, kMaskKey, kShownKey;+static char kSurfaceKey, kImageKey, kShownKey;-// Pictures already worked out, by the image object while it lives (main thread only).-static NSMapTable<UIImage *, UIImage *> *extensions(void) {+// Tints already worked out, by the image object while it lives (main thread only).+static NSMapTable<UIImage *, UIColor *> *tints(void) {static NSMapTable *table;if (!table) table = [NSMapTable weakToStrongObjectsMapTable];return table;}+static UIColor *untinted(void) {+ static UIColor *color;+ if (!color) color = SGRElevated(UIColor.blackColor);+ return color;+}+@interface SGRTileParts : NSObject@property (nonatomic, weak) UIView *fill;@property (nonatomic, weak) UIView *square;@@ -49,67 +55,57 @@ static SGRTileParts *partsOf(UIView *tile) {return parts;}-static UIView *backdropIn(UIView *tile, UIView *fill) {- UIView *backdrop = objc_getAssociatedObject(tile, &kBackdropKey);- if (!backdrop) {- backdrop = [UIView new];- backdrop.userInteractionEnabled = NO;- backdrop.accessibilityElementsHidden = YES;- backdrop.layer.contentsGravity = kCAGravityResize;- backdrop.hidden = YES;- objc_setAssociatedObject(tile, &kBackdropKey, backdrop, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+static UIView *surfaceIn(UIView *tile, UIView *fill) {+ UIView *surface = objc_getAssociatedObject(tile, &kSurfaceKey);+ if (!surface) {+ surface = [UIView new];+ surface.userInteractionEnabled = NO;+ surface.accessibilityElementsHidden = YES;+ surface.backgroundColor = untinted();+ objc_setAssociatedObject(tile, &kSurfaceKey, surface, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- if (backdrop.superview != tile) {- if (fill) [tile insertSubview:backdrop aboveSubview:fill];- else [tile insertSubview:backdrop atIndex:0];+ if (surface.superview != tile) {+ if (fill) [tile insertSubview:surface aboveSubview:fill];+ else [tile insertSubview:surface atIndex:0];}- if (!CGRectEqualToRect(backdrop.frame, tile.bounds)) backdrop.frame = tile.bounds;- return backdrop;+ if (!CGRectEqualToRect(surface.frame, tile.bounds)) surface.frame = tile.bounds;+ return surface;}-static void fadeCover(UIView *square, BOOL on) {- if (!square) return;- if (!on) {- if (square.layer.mask) square.layer.mask = nil;- return;- }- CAGradientLayer *mask = objc_getAssociatedObject(square, &kMaskKey);- if (!mask) {- mask = [CAGradientLayer layer];- mask.startPoint = CGPointMake(0, 0.5);- mask.endPoint = CGPointMake(1, 0.5);- mask.colors = @[(id)UIColor.blackColor.CGColor, (id)UIColor.clearColor.CGColor];- mask.locations = @[@(kFadeFrom), @1];- mask.actions = @{@"bounds": NSNull.null, @"position": NSNull.null};- objc_setAssociatedObject(square, &kMaskKey, mask, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+// The square scaled about its centre so it clears the tile's edges by kInset, and everything after it in the+// row moved in by what that took off its trailing side.+static void inset(UIView *square) {+ CGFloat side = square.bounds.size.height;+ if (side <= kInset * 4) return;+ CGFloat scale = (side - kInset * 2) / side;+ CGAffineTransform shrink = CGAffineTransformMakeScale(scale, scale);+ if (!CGAffineTransformEqualToTransform(square.transform, shrink)) square.transform = shrink;+ CALayer *layer = square.layer;+ // The radius is drawn scaled with the square.+ CGFloat radius = kCoverRadius / scale;+ if (layer.cornerRadius != radius) layer.cornerRadius = radius;+ if (layer.cornerCurve != kCACornerCurveContinuous) layer.cornerCurve = kCACornerCurveContinuous;+ if (!layer.masksToBounds) layer.masksToBounds = YES;++ CGAffineTransform follow = CGAffineTransformMakeTranslation(-square.bounds.size.width * (1 - scale) / 2, 0);+ BOOL after = NO;+ for (UIView *sibling in square.superview.subviews) {+ if (sibling == square) {+ after = YES;+ continue;+ }+ if (after && !CGAffineTransformEqualToTransform(sibling.transform, follow)) sibling.transform = follow;}- if (!CGRectEqualToRect(mask.frame, square.bounds)) mask.frame = square.bounds;- if (square.layer.mask != mask) square.layer.mask = mask;}-static void show(UIView *tile, UIView *backdrop, UIView *square, UIImage *extension, BOOL animated) {- if (!extension) {- backdrop.hidden = YES;- backdrop.layer.contents = nil;- fadeCover(square, NO);- return;- }- if (backdrop.layer.contents == (__bridge id)extension.CGImage && !backdrop.hidden) {- fadeCover(square, YES);+static void tint(UIView *tile, UIView *surface, UIColor *color, BOOL animated) {+ color = color ?: untinted();+ if ([surface.backgroundColor isEqual:color]) return;+ if (!animated || !tile.window) {+ surface.backgroundColor = color;return;}- [CATransaction begin];- [CATransaction setDisableActions:YES];- if (animated && tile.window && !backdrop.hidden) {- CATransition *fade = [CATransition animation];- fade.type = kCATransitionFade;- fade.duration = SGRCrossfade;- [backdrop.layer addAnimation:fade forKey:@"contents"];- }- backdrop.layer.contents = (__bridge id)extension.CGImage;- backdrop.hidden = NO;- [CATransaction commit];- fadeCover(square, YES);+ SGRAnimate(SGRMotionFade, ^{ surface.backgroundColor = color; }, nil);}static void refresh(UIView *tile) {@@ -117,33 +113,31 @@ static void refresh(UIView *tile) {UIImageView *cover = parts.cover;UIView *square = parts.square;if (!cover || !square || square.bounds.size.width < 20 || tile.bounds.size.width <= square.bounds.size.width) return;- UIView *backdrop = backdropIn(tile, parts.fill);+ UIView *surface = surfaceIn(tile, parts.fill);+ inset(square);UIImage *image = cover.image;- // The cover's placeholder shows while there is no picture, over Spotify's grey.if (!image) {objc_setAssociatedObject(tile, &kShownKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);- show(tile, backdrop, square, nil, NO);+ tint(tile, surface, nil, NO);return;}- UIImage *known = [extensions() objectForKey:image];- if (known && CGSizeEqualToSize(known.size, CGSizeMake(ceil(tile.bounds.size.width), ceil(tile.bounds.size.height)))) {+ UIColor *known = [tints() objectForKey:image];+ if (known) {objc_setAssociatedObject(tile, &kShownKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);- show(tile, backdrop, square, known, YES);+ tint(tile, surface, known, YES);return;}if (objc_getAssociatedObject(tile, &kShownKey) == image) return;objc_setAssociatedObject(tile, &kShownKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);- CGFloat art = CGRectGetMaxX([square convertRect:square.bounds toView:tile]);__weak UIView *weakTile = tile;- [SGRPalette extensionForImage:image size:tile.bounds.size artWidth:art completion:^(UIImage *extension) {+ [SGRPalette tintForImage:image surface:untinted() completion:^(UIColor *color) {UIView *strongTile = weakTile;- if (extension) [extensions() setObject:extension forKey:image];+ if (color) [tints() setObject:color forKey:image];// A tile reused for another cover in the meantime has asked for that one.if (!strongTile || objc_getAssociatedObject(strongTile, &kShownKey) != image) return;- SGRTileParts *now = partsOf(strongTile);- show(strongTile, backdropIn(strongTile, now.fill), now.square, extension, YES);+ tint(strongTile, surfaceIn(strongTile, partsOf(strongTile).fill), color, YES);}];}diff --git a/tweak/Sources/Redesigned/Kit/SGRPalette.h b/tweak/Sources/Redesigned/Kit/SGRPalette.hindex 207d2cff..1e3381e8 100644--- a/tweak/Sources/Redesigned/Kit/SGRPalette.h+++ b/tweak/Sources/Redesigned/Kit/SGRPalette.h@@ -30,14 +30,10 @@ typedef struct {// nil to the completion when the image has no bitmap to read (a symbol, a CIImage).+ (void)paletteForImage:(UIImage *)image request:(SGRPaletteRequest)request completion:(void (^)(SGRPalette *palette))completion;-// A strip `size` points across with the artwork `artWidth` wide at its leading edge, carried on to the-// trailing edge by the artwork's own trailing edge stretched out, the way a background extension continues-// a picture, and blurred. From the artwork's trailing edge on it is dimmed until white text on it passes-// WCAG AA (its 90th percentile luminance at 0.16, 0.08 with Increase Contrast, and never less than a-// 15% dim), so a row can run its picture on behind its text; under the artwork itself it is the same-// picture blurred, for the sharp one to fade into. One pixel a point, since it is blurred. nil to the-// completion when the image has no bitmap to read.-+ (void)extensionForImage:(UIImage *)image size:(CGSize)size artWidth:(CGFloat)artWidth completion:(void (^)(UIImage *extension))completion;+// `surface` tinted a little towards the artwork's dominant colour (brought down to a luminance of 0.05, then+// 35% of it mixed in), so a tile or row on the surface quietly takes its cover's colour while white text on+// it keeps its contrast. nil to the completion when the image has no bitmap to read.++ (void)tintForImage:(UIImage *)image surface:(UIColor *)surface completion:(void (^)(UIColor *tint))completion;@end// Any colour made fit to be a field, the same way the edge colour is: for a colour Spotify hands overdiff --git a/tweak/Sources/Redesigned/Kit/SGRPalette.m b/tweak/Sources/Redesigned/Kit/SGRPalette.mindex 6d5a55ce..b1be05ff 100644--- a/tweak/Sources/Redesigned/Kit/SGRPalette.m+++ b/tweak/Sources/Redesigned/Kit/SGRPalette.m@@ -10,11 +10,9 @@ static const CGFloat kMaxLuminance = 0.07, kMaxLuminanceContrast = 0.04;static const CGFloat kBackdropWidth = 160, kBackdropMaxHeight = 400, kBackdropSigma = 12;static const CGFloat kDissolveWidth = 96, kDissolveSigma = 5;static const CGFloat kFadeFrom = 0.55, kDissolveOpaque = 0.85;-static const CGFloat kExtensionSigma = 8, kExtensionLuminance = 0.16, kExtensionLuminanceContrast = 0.08, kExtensionMinDim = 0.15;-// The share of the artwork's width, at its trailing edge, stretched over the rest of the strip.-static const CGFloat kExtensionEdge = 0.04;-// The dim starts at this share of the artwork's width and is full this far past it, where a row's text begins.-static const CGFloat kExtensionRampFrom = 0.4, kExtensionRampOut = 8;+// A tint: the artwork's dominant colour brought down to this luminance, then this share of it mixed into+// the surface it tints.+static const CGFloat kTintLuminance = 0.05, kTintShare = 0.35;@interface SGRPalette ()@property (nonatomic, readwrite) UIColor *edgeColor;@@ -160,97 +158,53 @@ static UIImage *finished(CGImageRef blurred, BOOL dim, CGFloat dimBottom, CGFloareturn image;}-#pragma mark - extension+#pragma mark - tint-// The artwork filling `rect` and clipped to it.-static void drawFillingRect(CGContextRef context, CGImageRef image, CGRect rect) {- CGFloat iw = CGImageGetWidth(image), ih = CGImageGetHeight(image);- CGFloat scale = MAX(rect.size.width / iw, rect.size.height / ih);- CGContextSaveGState(context);- CGContextClipToRect(context, rect);- CGContextDrawImage(context, CGRectMake(CGRectGetMidX(rect) - iw * scale / 2, CGRectGetMidY(rect) - ih * scale / 2, iw * scale, ih * scale), image);- CGContextRestoreGState(context);-}--// The luminance under which 90% of the pixels right of `fromX` fall, read off a histogram.-static CGFloat upperLuminance(const uint8_t *px, size_t width, size_t height, size_t fromX) {- static CGFloat linear[256];- static dispatch_once_t once;- dispatch_once(&once, ^{- for (int i = 0; i < 256; i++) linear[i] = toLinear(i / 255.0);- });- enum { kBins = 64 };- size_t bins[kBins] = {0}, total = 0;- for (size_t y = 0; y < height; y++) {- for (size_t x = fromX; x < width; x++) {- const uint8_t *p = px + (y * width + x) * 4;- CGFloat luminance = 0.2126 * linear[p[0]] + 0.7152 * linear[p[1]] + 0.0722 * linear[p[2]];- bins[MIN(kBins - 1, (size_t)(luminance * kBins))]++;- total++;- }- }- size_t seen = 0;- for (size_t i = 0; i < kBins; i++) {- seen += bins[i];- if (seen * 10 >= total * 9) return (CGFloat)(i + 1) / kBins;- }- return 1;-}--static UIImage *extensionOf(CGImageRef image, CGSize size, CGFloat artWidth, CGFloat ceiling) {- size_t width = (size_t)ceil(size.width), height = (size_t)ceil(size.height);- CGFloat art = MIN(MAX(1, round(artWidth)), width);- CGContextRef context = newBitmap(width, height);- if (!context) return nil;+// The artwork's dominant colour in linear light: its pixels binned 3 bits a channel, each bin scored by its+// count weighted towards colourful pixels, so a busy cover gives its main colour instead of the grey an+// average makes. Near-black and near-white count for little unless they are nearly all there is.+static BOOL dominantColorOf(CGImageRef image, CGFloat out[3]) {+ CGContextRef context = newBitmap(kSample, kSample);+ if (!context) return NO;CGContextSetInterpolationQuality(context, kCGInterpolationMedium);- drawFillingRect(context, image, CGRectMake(0, 0, art, height));- if (art < width) {- // The part of the picture that shows once it fills the artwork's place, and the strip at its trailing- // edge; copies laid side by side instead repeat whatever stands out in the picture along the row.- CGFloat iw = CGImageGetWidth(image), ih = CGImageGetHeight(image);- CGFloat scale = MAX(art / iw, height / ih);- CGFloat shownWidth = art / scale, shownHeight = height / scale;- CGFloat edge = MAX(2, round(shownWidth * kExtensionEdge));- CGRect strip = CGRectMake(floor((iw + shownWidth) / 2 - edge), floor((ih - shownHeight) / 2), edge, floor(shownHeight));- CGImageRef column = CGImageCreateWithImageInRect(image, strip);- if (column) CGContextDrawImage(context, CGRectMake(art, 0, width - art, height), column);- CGImageRelease(column);+ CGContextDrawImage(context, CGRectMake(0, 0, kSample, kSample), image);+ const uint8_t *px = CGBitmapContextGetData(context);+ enum { kBins = 512 };+ double sums[kBins][3] = {{0}}, scores[kBins] = {0};+ size_t counts[kBins] = {0};+ for (size_t i = 0; px && i < kSample * kSample; i++) {+ const uint8_t *p = px + i * 4;+ if (p[3] < 128) continue;+ int bin = (p[0] >> 5) << 6 | (p[1] >> 5) << 3 | (p[2] >> 5);+ CGFloat high = MAX(p[0], MAX(p[1], p[2])) / 255.0, low = MIN(p[0], MIN(p[1], p[2])) / 255.0;+ CGFloat saturation = high > 0 ? (high - low) / high : 0;+ scores[bin] += (0.25 + saturation) * (high < 0.12 ? 0.3 : 1) * (low > 0.88 ? 0.3 : 1);+ counts[bin]++;+ sums[bin][0] += toLinear(p[0] / 255.0), sums[bin][1] += toLinear(p[1] / 255.0), sums[bin][2] += toLinear(p[2] / 255.0);}- CGImageRef tiled = CGBitmapContextCreateImage(context);CGContextRelease(context);- if (!tiled) return nil;- CIImage *input = [CIImage imageWithCGImage:tiled];- CGImageRelease(tiled);- CIImage *output = [[input imageByClampingToExtent] imageByApplyingGaussianBlurWithSigma:kExtensionSigma];- CGImageRef blurred = [blurContext() createCGImage:output fromRect:input.extent format:kCIFormatRGBA8 colorSpace:sRGB()];- if (!blurred) return nil;-- context = newBitmap(width, height);- if (!context) {- CGImageRelease(blurred);- return nil;+ int best = 0;+ for (int i = 1; i < kBins; i++) {+ if (scores[i] > scores[best]) best = i;}- CGContextDrawImage(context, CGRectMake(0, 0, width, height), blurred);- CGImageRelease(blurred);- const uint8_t *px = CGBitmapContextGetData(context);- size_t textFrom = (size_t)MIN(width - 1, art + kExtensionRampOut);- CGFloat upper = px ? upperLuminance(px, width, height, textFrom) : 1;- // A black over sRGB takes the encoded channels down linearly, the luminance by about that to the 2.4.- CGFloat keep = MIN(1, ceiling / MAX(upper, 0.0001));- CGFloat dim = MAX(kExtensionMinDim, 1 - pow(keep, 1 / 2.4));- CGFloat components[] = {0, 0, 0, 0, 0, 0, 0, dim};- CGFloat locations[] = {0, 1};- CGGradientRef gradient = CGGradientCreateWithColorComponents(sRGB(), components, locations, 2);- CGContextDrawLinearGradient(context, gradient, CGPointMake(art * kExtensionRampFrom, 0), CGPointMake(art + kExtensionRampOut, 0), kCGGradientDrawsAfterEndLocation);- CGGradientRelease(gradient);- CGImageRef result = CGBitmapContextCreateImage(context);- CGContextRelease(context);- UIImage *extension = result ? [UIImage imageWithCGImage:result scale:1 orientation:UIImageOrientationUp] : nil;- CGImageRelease(result);+ if (!counts[best]) return NO;+ for (int k = 0; k < 3; k++) out[k] = sums[best][k] / counts[best];+ return YES;+}- static dispatch_once_t once;- dispatch_once(&once, ^{ SGLog(@"redesign kit: first extension %zux%zu, luminance p90 %.3f, dim %.2f", width, height, upper, dim); });- return extension;+static UIColor *tintOf(CGImageRef image, UIColor *surface) {+ CGFloat linear[3];+ CGFloat r = 0, g = 0, b = 0, a = 1;+ if (!dominantColorOf(image, linear) || ![surface getRed:&r green:&g blue:&b alpha:&a]) return nil;+ CGFloat luminance = 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2];+ if (luminance > kTintLuminance) {+ for (int k = 0; k < 3; k++) linear[k] *= kTintLuminance / luminance;+ }+ CGFloat t = kTintShare;+ return [UIColor colorWithRed:r * (1 - t) + toEncoded(linear[0]) * t+ green:g * (1 - t) + toEncoded(linear[1]) * t+ blue:b * (1 - t) + toEncoded(linear[2]) * t+ alpha:1];}@implementation SGRPalette@@ -289,16 +243,14 @@ static UIImage *extensionOf(CGImageRef image, CGSize size, CGFloat artWidth, CGF});}-+ (void)extensionForImage:(UIImage *)image size:(CGSize)size artWidth:(CGFloat)artWidth completion:(void (^)(UIImage *extension))completion {++ (void)tintForImage:(UIImage *)image surface:(UIColor *)surface completion:(void (^)(UIColor *tint))completion {if (!completion) return;- CGFloat ceiling = SGRIncreaseContrast() ? kExtensionLuminanceContrast : kExtensionLuminance;dispatch_async(paletteQueue(), ^{CGImageRef cg = image.CGImage;- UIImage *extension = nil;- if (cg && CGImageGetWidth(cg) && CGImageGetHeight(cg) && size.width >= 1 && size.height >= 1 && artWidth >= 1) {- extension = extensionOf(cg, size, artWidth, ceiling);- }- dispatch_async(dispatch_get_main_queue(), ^{ completion(extension); });+ UIColor *tint = cg && CGImageGetWidth(cg) && CGImageGetHeight(cg) ? tintOf(cg, surface) : nil;+ static dispatch_once_t once;+ if (tint) dispatch_once(&once, ^{ SGLog(@"redesign kit: first tint %@", tint); });+ dispatch_async(dispatch_get_main_queue(), ^{ completion(tint); });});}