Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: put the blurred cover behind the playlist header
Vojtěch Škopekc3039dacf3b6
diff --git a/tweak/Sources/Core/SGBackdrop.h b/tweak/Sources/Core/SGBackdrop.hnew file mode 100644index 00000000..498c3c31--- /dev/null+++ b/tweak/Sources/Core/SGBackdrop.h@@ -0,0 +1,9 @@+// The cover art as a background: shrunk until no shape of it survives, blurred, under a scrim+// darkening towards the bottom. NowPlaying/Player.x puts one behind the player, Playlist/Playlist.x+// behind the playlist header.+#import <UIKit/UIKit.h>++UIImage *SGBackdropSample(UIImage *cover);+// The scrim runs from 0.40 black at the top to `bottomAlpha` at the bottom.+UIView *SGBackdropMake(CGRect frame, CGFloat bottomAlpha);+UIImageView *SGBackdropImageView(UIView *backdrop);diff --git a/tweak/Sources/Core/SGBackdrop.m b/tweak/Sources/Core/SGBackdrop.mnew file mode 100644index 00000000..d5a723bd--- /dev/null+++ b/tweak/Sources/Core/SGBackdrop.m@@ -0,0 +1,60 @@+#import "Core/SGBackdrop.h"++// Small enough that no shape of the cover survives the scaling back up, large enough to keep the+// colours where they were in it.+static const CGFloat kCoverSample = 48;++@interface SGScrimView : UIView+@end++@implementation SGScrimView++ (Class)layerClass {+ return CAGradientLayer.class;+}+@end++UIImage *SGBackdropSample(UIImage *cover) {+ UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];+ format.scale = 1;+ format.opaque = YES;+ CGRect square = CGRectMake(0, 0, kCoverSample, kCoverSample);+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:square.size format:format];+ return [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {+ [cover drawInRect:square];+ }];+}++UIView *SGBackdropMake(CGRect frame, CGFloat bottomAlpha) {+ UIViewAutoresizing fill = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;+ UIView *backdrop = [[UIView alloc] initWithFrame:frame];+ backdrop.userInteractionEnabled = NO;+ backdrop.clipsToBounds = YES;+ backdrop.autoresizingMask = fill;++ UIImageView *cover = [[UIImageView alloc] initWithFrame:backdrop.bounds];+ cover.contentMode = UIViewContentModeScaleAspectFill;+ cover.autoresizingMask = fill;+ [backdrop addSubview:cover];++ // The scaled up cover comes back with the seams of its 48 pixels in it; the blur takes those out.+ UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark]];+ blur.frame = backdrop.bounds;+ blur.autoresizingMask = fill;+ [backdrop addSubview:blur];++ SGScrimView *scrim = [[SGScrimView alloc] initWithFrame:backdrop.bounds];+ scrim.autoresizingMask = fill;+ CAGradientLayer *fade = (CAGradientLayer *)scrim.layer;+ fade.colors = @[+ (id)[UIColor colorWithWhite:0 alpha:0.40].CGColor,+ (id)[UIColor colorWithWhite:0 alpha:0.60].CGColor,+ (id)[UIColor colorWithWhite:0 alpha:bottomAlpha].CGColor,+ ];+ fade.locations = @[@0, @0.5, @1];+ [backdrop addSubview:scrim];+ return backdrop;+}++UIImageView *SGBackdropImageView(UIView *backdrop) {+ return (UIImageView *)backdrop.subviews.firstObject;+}diff --git a/tweak/Sources/Core/SGCore.h b/tweak/Sources/Core/SGCore.hindex 8014aecd..b3e7ffe7 100644--- a/tweak/Sources/Core/SGCore.h+++ b/tweak/Sources/Core/SGCore.h@@ -5,3 +5,4 @@#import "Core/SGPrefs.h"#import "Core/SGViewTree.h"#import "Core/SGGlass.h"+#import "Core/SGBackdrop.h"diff --git a/tweak/Sources/Features/NowPlaying/Player.x b/tweak/Sources/Features/NowPlaying/Player.xindex b20be784..86db38e9 100644--- a/tweak/Sources/Features/NowPlaying/Player.x+++ b/tweak/Sources/Features/NowPlaying/Player.x@@ -26,9 +26,6 @@static const CGFloat kButtonMin = 36, kButtonMax = 48;static const CGFloat kArtRadius = 12;-// Small enough that no shape of the cover survives the scaling back up, large enough to keep the-// colours where they were in it.-static const CGFloat kCoverSample = 48;static BOOL backdropOn(void) {return SGFlag(SGKeyPlayerBackdrop, NO);@@ -67,36 +64,16 @@ static void glassBehindRoundButtons(UIViewController *unit) {#pragma mark - the cover behind the player-@interface SGScrimView : UIView-@end--@implementation SGScrimView-+ (Class)layerClass {- return CAGradientLayer.class;-}-@end-static char kBackdropKey;static UIImage *sg_cover; // the cover the backdrop stands on, compared by pointerstatic UIImage *sg_coverSmall;static __weak UIImageView *sg_coverView;-static UIImage *shrunk(UIImage *cover) {- UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];- format.scale = 1;- format.opaque = YES;- CGRect square = CGRectMake(0, 0, kCoverSample, kCoverSample);- UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:square.size format:format];- return [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {- [cover drawInRect:square];- }];-}-static void showCover(UIImage *cover) {if (!cover || cover == sg_cover) return;sg_cover = cover;- sg_coverSmall = shrunk(cover);+ sg_coverSmall = SGBackdropSample(cover);UIImageView *view = sg_coverView;if (!view) return;[UIView transitionWithView:view duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{@@ -127,37 +104,11 @@ static UIView *backdropIn(UIView *plane) {UIView *backdrop = objc_getAssociatedObject(plane, &kBackdropKey);if (backdrop) return backdrop;- UIViewAutoresizing fill = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;- backdrop = [[UIView alloc] initWithFrame:plane.bounds];- backdrop.userInteractionEnabled = NO;- backdrop.clipsToBounds = YES;- backdrop.autoresizingMask = fill;-- UIImageView *cover = [[UIImageView alloc] initWithFrame:backdrop.bounds];- cover.contentMode = UIViewContentModeScaleAspectFill;- cover.autoresizingMask = fill;+ backdrop = SGBackdropMake(plane.bounds, SGFlag(SGKeyAmoled, NO) ? 1 : 0.94);+ UIImageView *cover = SGBackdropImageView(backdrop);cover.image = sg_coverSmall;- [backdrop addSubview:cover];sg_coverView = cover;- // The scaled up cover comes back with the seams of its 48 pixels in it; the blur takes those- // out and is the same material as everything else the mod puts on this screen.- UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark]];- blur.frame = backdrop.bounds;- blur.autoresizingMask = fill;- [backdrop addSubview:blur];-- SGScrimView *scrim = [[SGScrimView alloc] initWithFrame:backdrop.bounds];- scrim.autoresizingMask = fill;- CAGradientLayer *fade = (CAGradientLayer *)scrim.layer;- fade.colors = @[- (id)[UIColor colorWithWhite:0 alpha:0.40].CGColor,- (id)[UIColor colorWithWhite:0 alpha:0.60].CGColor,- (id)[UIColor colorWithWhite:0 alpha:SGFlag(SGKeyAmoled, NO) ? 1 : 0.94].CGColor,- ];- fade.locations = @[@0, @0.5, @1];- [backdrop addSubview:scrim];-objc_setAssociatedObject(plane, &kBackdropKey, backdrop, OBJC_ASSOCIATION_RETAIN_NONATOMIC);return backdrop;}diff --git a/tweak/Sources/Features/Playlist/Playlist.h b/tweak/Sources/Features/Playlist/Playlist.hindex c2416bb4..23359f5e 100644--- a/tweak/Sources/Features/Playlist/Playlist.h+++ b/tweak/Sources/Features/Playlist/Playlist.h@@ -12,5 +12,6 @@#define SGHidePlaylistMore @"spotifyglass.hide.playlistMore"#define SGHidePlaylistPills @"spotifyglass.hide.playlistPills"#define SGHidePlaylistFind @"spotifyglass.hide.playlistFind"+#define SGKeyPlaylistBackdrop @"spotifyglass.playlistBackdrop"UIViewController *SGPlaylistSettingsPage(void); // opened from the Home & Library pagediff --git a/tweak/Sources/Features/Playlist/Playlist.x b/tweak/Sources/Features/Playlist/Playlist.xindex d1286034..71be7018 100644--- a/tweak/Sources/Features/Playlist/Playlist.x+++ b/tweak/Sources/Features/Playlist/Playlist.x@@ -137,6 +137,42 @@ static void collapseCover(UIViewController *headerVC, UIView *layout, UIView *codispatch_once(&once, ^{ SGLog(@"playlist header: layout %.0f, header %.0f -> %.0f, guide %.0f", full, before, target, guide.constant); });}+static char kBackdropKey, kSourceKey;++// The cover blurred behind the header in place of Spotify's colour wash. The header's background+// (trees/test6.txt) is a HeaderView under _backgroundViewContainer holding the wash, then a black+// view that fades in as the page scrolls; the backdrop goes between them so the scroll still darkens+// it, and its scrim ends in the black of the rows below. No cover, no backdrop: the wash stays.+static void applyBackdrop(UIView *layout) {+ if (!SGFlag(SGKeyPlaylistBackdrop, NO)) return;+ UIImage *image = nil;+ for (UIView *v in identNamed(layout, @"EditableHeaderArtworkElement.ImageView").subviews) {+ if ([v isKindOfClass:UIImageView.class] && ((UIImageView *)v).image) image = ((UIImageView *)v).image;+ }+ if (!image) return;++ UIView *container = nil;+ for (UIView *v = layout.superview; v && !container; v = v.superview) {+ for (UIView *sub in v.subviews) {+ if ([sub.accessibilityIdentifier isEqualToString:@"_backgroundViewContainer"]) container = sub;+ }+ }+ UIView *header = container.subviews.firstObject;+ if (header.subviews.count < 2) return;++ UIView *backdrop = objc_getAssociatedObject(header, &kBackdropKey);+ if (!backdrop) {+ backdrop = SGBackdropMake(header.bounds, 1);+ objc_setAssociatedObject(header, &kBackdropKey, backdrop, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ }+ if (backdrop.superview != header) [header insertSubview:backdrop atIndex:1];++ UIImageView *sample = SGBackdropImageView(backdrop);+ if (objc_getAssociatedObject(sample, &kSourceKey) == image) return;+ objc_setAssociatedObject(sample, &kSourceKey, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ sample.image = SGBackdropSample(image);+}+%hook _TtC28EncoreConsumerMobile_BaseKit19HeaderContentLayout- (void)layoutSubviews {%orig;@@ -145,10 +181,24 @@ static void collapseCover(UIViewController *headerVC, UIView *layout, UIView *coUIView *layout = (UIView *)self, *cover = identNamed(layout, @"Components.Header.UI.ArtworkImage");hide(cover, SGHidePlaylistArtwork);applyColumn(layout);+ applyBackdrop(layout);if (cover.hidden) collapseCover(headerVC, layout, cover);}%end+// The cover loads after the header is laid out, and a new image does not lay the header out again.+%hook UIImageView+- (void)setImage:(UIImage *)image {+ %orig;+ if (!image || ![self.superview.accessibilityIdentifier isEqualToString:@"EditableHeaderArtworkElement.ImageView"]) return;+ for (UIView *v = self.superview; v; v = v.superview) {+ if (![NSStringFromClass(v.class) containsString:@"HeaderContentLayout"]) continue;+ if (playlistHeaderOf(v)) applyBackdrop(v);+ return;+ }+}+%end+// The buttons fill in after the header is laid out, and a row that keeps its size never makes the// header lay out again, so they stay up until something else does -- the cover loading, a second on.%hook UIStackViewdiff --git a/tweak/Sources/Features/Playlist/PlaylistSettings.m b/tweak/Sources/Features/Playlist/PlaylistSettings.mindex cb8f16a2..4d2b8817 100644--- a/tweak/Sources/Features/Playlist/PlaylistSettings.m+++ b/tweak/Sources/Features/Playlist/PlaylistSettings.m@@ -3,6 +3,9 @@UIViewController *SGPlaylistSettingsPage(void) {return [[SGModPage alloc] initWithTitle:@"Playlists" intro:nil sections:@[+ SGSection(@"Header", @[+ SGOptionRow(@"Artwork background", nil, SGKeyPlaylistBackdrop),+ ]),SGSection(@"Hide in the playlist header", @[SGHideRow(@"Cover artwork", nil, SGHidePlaylistArtwork),SGHideRow(@"Description", nil, SGHidePlaylistDescription),