Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: declutter the album page and put its blurred cover behind the header
Vojtěch Škopekf414321444ed
diff --git a/tweak/Sources/Features/Album/Album.h b/tweak/Sources/Features/Album/Album.hnew file mode 100644index 00000000..221295c6--- /dev/null+++ b/tweak/Sources/Features/Album/Album.h@@ -0,0 +1,18 @@+// Album: parts of the album page hidden one switch each, and the cover behind its header (Album.x).+// An unset switch is off.+#import <UIKit/UIKit.h>++#define SGKeyAlbumBackdrop @"spotifyglass.albumBackdrop"++#define SGHideAlbumExplore @"spotifyglass.hide.albumExplore"+#define SGHideAlbumAddTo @"spotifyglass.hide.albumAddTo"+#define SGHideAlbumDownload @"spotifyglass.hide.albumDownload"+#define SGHideAlbumMore @"spotifyglass.hide.albumMore"++#define SGHideAlbumMoreBy @"spotifyglass.hide.albumMoreBy"+#define SGHideAlbumVideos @"spotifyglass.hide.albumVideos"+#define SGHideAlbumConcerts @"spotifyglass.hide.albumConcerts"+#define SGHideAlbumMerch @"spotifyglass.hide.albumMerch"+#define SGHideAlbumYouMightLike @"spotifyglass.hide.albumYouMightLike"++UIViewController *SGAlbumSettingsPage(void); // opened from the Home & Library pagediff --git a/tweak/Sources/Features/Album/Album.x b/tweak/Sources/Features/Album/Album.xnew file mode 100644index 00000000..fa804798--- /dev/null+++ b/tweak/Sources/Features/Album/Album.x@@ -0,0 +1,198 @@+// Album: hides parts of the album page, one switch each in Mod Settings > Home & Library > Album,+// and puts the blurred cover behind its header.+//+// Trees (trees/continuous/*.txt): the page is a CreativeWorkTemplateView with that identifier (the+// artist page is TemplateKit's TemplateView instead). Its first child is the header's background,+// a HeaderView holding Spotify's GradientView; the header itself scrolls with the list, its buttons+// one UIStackView of element views, each holding a button with an accessibility identifier.+//+// Below the tracks every row is an Element_List cell, sectioned the way Artist/Artist.x describes: a+// heading cell (SectionHeadingHome), the carousel or card, a 16pt spacer. The copyright closes the+// page right after the last section, so it counts as a heading of its own and stays.+#import "Core/SGCore.h"+#import "Album.h"++static NSString *const kTemplate = @"CreativeWorkPlatform.CreativeWorkTemplateView";++static UIView *albumPageOf(UIView *view) {+ for (UIView *v = view.superview; v; v = v.superview) {+ if ([v.accessibilityIdentifier isEqualToString:kTemplate]) return v;+ }+ return nil;+}++static UIView *identNamed(UIView *root, NSString *ident) {+ __block UIView *found = nil;+ SGForEachView(root, ^(UIView *v) {+ if (!found && [v.accessibilityIdentifier hasPrefix:ident]) found = v;+ });+ return found;+}++#pragma mark - header buttons++static const struct { __unsafe_unretained NSString *ident, *key; } buttons[] = {+ {@"Components.UI.WatchFeedEntityExplorerButton", SGHideAlbumExplore},+ {@"Components.UI.AddToButton", SGHideAlbumAddTo},+ {@"DownloadButton.Granular", SGHideAlbumDownload},+ {@"Components.UI.ContextMenuButton", SGHideAlbumMore},+};++static NSString *keyFor(UIView *action) {+ NSString *ident = action.subviews.firstObject.accessibilityIdentifier;+ for (size_t i = 0; ident && i < sizeof(buttons) / sizeof(buttons[0]); i++) {+ if ([ident hasPrefix:buttons[i].ident]) return buttons[i].key;+ }+ return nil;+}++// The buttons arrive after the page has laid out, and a row that keeps its size lays nothing else out.+%hook UIStackView+- (void)layoutSubviews {+ %orig;+ BOOL row = NO;+ for (UIView *action in self.subviews) {+ if (keyFor(action)) row = YES;+ }+ if (!row || !albumPageOf(self)) return;+ for (UIView *action in self.subviews) {+ NSString *key = keyFor(action);+ if (key && SGHidden(key)) action.hidden = YES;+ }+}+%end++#pragma mark - the cover behind the header++static char kBackdropKey, kSourceKey;++static void applyBackdrop(UIView *page) {+ if (!SGFlag(SGKeyAlbumBackdrop, NO)) return;+ __block UIImage *image = nil;+ SGForEachView(identNamed(page, @"CreativeWorkPlatform.Components.UI.ArtWorkElement.WithCoverArt"), ^(UIView *v) {+ if (!image && [v isKindOfClass:UIImageView.class]) image = ((UIImageView *)v).image;+ });+ UIView *header = page.subviews.firstObject;+ if (!image || ![NSStringFromClass(header.class) containsString:@"HeaderView"]) 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 _TtC28CreativeWorkPlatform_PageKit24CreativeWorkTemplateView+- (void)layoutSubviews {+ %orig;+ applyBackdrop((UIView *)self);+}+%end++// The cover loads after the page is laid out, and a new image lays nothing out. Only the 248pt+// cover is wide enough to be worth walking up from.+%hook UIImageView+- (void)setImage:(UIImage *)image {+ %orig;+ if (!image || self.bounds.size.width < 200 || !SGFlag(SGKeyAlbumBackdrop, NO)) return;+ UIView *page = albumPageOf(self);+ if (page) applyBackdrop(page);+}+%end++#pragma mark - sections++static const struct { __unsafe_unretained NSString *title, *key; BOOL prefix; } sections[] = {+ {@"More by ", SGHideAlbumMoreBy, YES},+ {@"Related Music Videos", SGHideAlbumVideos, NO},+ {@"Concerts", SGHideAlbumConcerts, NO},+ {@"Merch", SGHideAlbumMerch, NO},+ {@"You might also like", SGHideAlbumYouMightLike, NO},+};++static BOOL sectionHidden(NSString *title) {+ for (size_t i = 0; i < sizeof(sections) / sizeof(sections[0]); i++) {+ NSString *wanted = sections[i].title;+ BOOL match = sections[i].prefix+ ? [title.lowercaseString hasPrefix:wanted.lowercaseString]+ : [title caseInsensitiveCompare:wanted] == NSOrderedSame;+ if (match) return SGHidden(sections[i].key);+ }+ return NO;+}++static BOOL anySectionSwitch(void) {+ for (size_t i = 0; i < sizeof(sections) / sizeof(sections[0]); i++) {+ if (SGHidden(sections[i].key)) return YES;+ }+ return NO;+}++// The heading's title, the copyright's identifier, or @"" for any other cell.+static NSString *headingIn(UIView *cell) {+ __block NSString *title = nil;+ SGForEachView(cell, ^(UIView *v) {+ if (title) return;+ if ([v.accessibilityIdentifier isEqualToString:@"Album.Copyright"]) title = v.accessibilityIdentifier;+ else if (v.accessibilityLabel.length && [NSStringFromClass(v.class) containsString:@"SectionHeadingHome"]) title = v.accessibilityLabel;+ });+ return title ?: @"";+}++static NSMutableDictionary<NSIndexPath *, NSString *> *headingsOf(UICollectionView *list) {+ static char key;+ NSMutableDictionary *headings = objc_getAssociatedObject(list, &key);+ if (!headings) {+ headings = [NSMutableDictionary dictionary];+ objc_setAssociatedObject(list, &key, headings, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ }+ return headings;+}++// nil above the first heading, or where a cell between this one and its heading is not sized yet.+static NSString *sectionOf(NSIndexPath *path, NSDictionary<NSIndexPath *, NSString *> *headings) {+ for (NSInteger item = path.item; item >= 0; item--) {+ NSString *title = headings[[NSIndexPath indexPathForItem:item inSection:path.section]];+ if (!title) return nil;+ if (title.length) return title;+ }+ return nil;+}++static BOOL collapsed(UICollectionViewCell *cell, NSIndexPath *path) {+ if (!path || !anySectionSwitch()) return NO;+ UICollectionView *list = nil;+ for (UIView *v = cell.superview; v; v = v.superview) {+ if ([v isKindOfClass:cell.class]) return NO; // a card inside a carousel: its row decides+ if (!list && [v isKindOfClass:UICollectionView.class]) list = (UICollectionView *)v;+ }+ if (!list || !albumPageOf(list)) return NO;+ NSMutableDictionary *headings = headingsOf(list);+ headings[path] = headingIn(cell);+ NSString *section = sectionOf(path, headings);+ return section && sectionHidden(section);+}++%hook _TtC12Element_List18CollectionViewCell+- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)attributes {+ UICollectionViewLayoutAttributes *result = %orig;+ if (!collapsed((UICollectionViewCell *)self, attributes.indexPath)) return result;+ result.size = CGSizeMake(result.size.width, 0);+ ((UIView *)self).clipsToBounds = YES;+ return result;+}+%end++%ctor {+ %init;+ SGRequireClasses(@[+ @"_TtC28CreativeWorkPlatform_PageKit24CreativeWorkTemplateView",+ @"_TtC12Element_List18CollectionViewCell",+ ]);+}diff --git a/tweak/Sources/Features/Album/AlbumSettings.m b/tweak/Sources/Features/Album/AlbumSettings.mnew file mode 100644index 00000000..4b981cff--- /dev/null+++ b/tweak/Sources/Features/Album/AlbumSettings.m@@ -0,0 +1,25 @@+#import "Core/SGCore.h"+#import "Settings/SGModPage.h"+#import "Album.h"++UIViewController *SGAlbumSettingsPage(void) {+ NSArray<SGModSection *> *sections = @[+ SGSection(@"Header", @[+ SGOptionRow(@"Artwork background", nil, SGKeyAlbumBackdrop),+ ]),+ SGSection(@"Hide in the header", @[+ SGHideRow(@"Explore (video deck)", nil, SGHideAlbumExplore),+ SGHideRow(@"Add to library", nil, SGHideAlbumAddTo),+ SGHideRow(@"Download", nil, SGHideAlbumDownload),+ SGHideRow(@"More options", nil, SGHideAlbumMore),+ ]),+ SGNotedSection(@"Hide on the page", @[+ SGHideRow(@"More by the artist", nil, SGHideAlbumMoreBy),+ SGHideRow(@"Related music videos", nil, SGHideAlbumVideos),+ SGHideRow(@"Concerts", nil, SGHideAlbumConcerts),+ SGHideRow(@"Merch", nil, SGHideAlbumMerch),+ SGHideRow(@"You might also like", nil, SGHideAlbumYouMightLike),+ ], @"Sections are found by their English titles, so these do nothing while Spotify is in another language."),+ ];+ return [[SGModPage alloc] initWithTitle:@"Album" intro:nil sections:sections footer:nil];+}diff --git a/tweak/Sources/Features/Home/HomeSettings.m b/tweak/Sources/Features/Home/HomeSettings.mindex 67487da7..42bf81f3 100644--- a/tweak/Sources/Features/Home/HomeSettings.m+++ b/tweak/Sources/Features/Home/HomeSettings.m@@ -4,6 +4,7 @@#import "Features/Declutter/Declutter.h"#import "Features/Playlist/Playlist.h"#import "Features/Artist/Artist.h"+#import "Features/Album/Album.h"static SGModRow *choiceRow(NSString *title, NSString *subtitle, SGHomeChoice choice) {return SGChoiceRow(title, subtitle, SGHomeChoiceKey(choice), SGHomeChoiceNames(choice),@@ -43,10 +44,6 @@ static SGModRow *bare(SGModRow *row) {return row;}-static UIViewController *emptyPage(NSString *title) {- return [[SGModPage alloc] initWithTitle:title intro:nil sections:@[] footer:nil];-}-UIViewController *SGHomeSettingsPage(void) {// The row reads its own state out, so the section says which colour is set without being opened.SGModRow *gradient = SGPageRow(@"Gradient", ^UIViewController *{ return SGHomeGradientPage(); });@@ -59,7 +56,7 @@ UIViewController *SGHomeSettingsPage(void) {SGSection(nil, @[SGWithSymbol(SGPageRow(@"Playlists", ^UIViewController *{ return SGPlaylistSettingsPage(); }), @"music.note.list"),SGWithSymbol(SGPageRow(@"Library", ^UIViewController *{ return libraryPage(); }), @"books.vertical"),- SGWithSymbol(SGPageRow(@"Album", ^UIViewController *{ return emptyPage(@"Album"); }), @"square.stack"),+ SGWithSymbol(SGPageRow(@"Album", ^UIViewController *{ return SGAlbumSettingsPage(); }), @"square.stack"),SGWithSymbol(SGPageRow(@"Artist", ^UIViewController *{ return SGArtistSettingsPage(); }), @"music.mic"),]),SGSection(@"Home", @[