Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Fluid artwork gets Speed, Warp, Blur, Saturation and Brightness sliders
My-Name-Is-Jeff4e00fd025a5f
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 9927b9e0..c5f45dd7 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -658,6 +658,13 @@ for Animated while the player has no clip, the card looks the playing track's upis in, then the clip over it, poster first (harness/player/ `preview`);under it a segmented control of the five backgrounds (Still, Colors, Fluid, Animated, Visualizer, changing the card at once)with a note on what the one picked does, Artwork sources and Download in Low Data Mode while Animated is chosen,+while Fluid is chosen its five sliders and a red Reset (Redesigned/Kit/SGRFluid.h, spotifyglass.redesign.fluid.*):+Speed (25-300 %, how fast the four blurred copies turn, picked up from the angle each is at), Warp (0-100 %, how+far from the middle they turn and how much the upper three show; at none one copy turns alone), Blur (2-24, the+radius at 1.25 px a step on the 128 px cover), Saturation (0-250 %) and Brightness (40-150 %, a gain on linear+light that the luminance ceiling for the player's text, 0.07 or 0.04 with Increase Contrast, still caps); the+card and the player follow each step at once through SGRFluidSettingsDidChangeNotification, the last three by+blurring the cover again off the main thread, and Animated and Visualizer's Fluid with them,and Mini player: Apple Music style (the tab bar's minimize on scroll), Device button (the now playing bar keepsSpotify's device button in that minimized row, off by default) and the device button hidden on the full bar,checked in the simulator against harness/player/ (`settings`). Vibrations, a page of its own under eitherdiff --git a/tweak/Sources/Redesigned/Kit/SGRFluid.h b/tweak/Sources/Redesigned/Kit/SGRFluid.hindex 5ca2b450..203106f7 100644--- a/tweak/Sources/Redesigned/Kit/SGRFluid.h+++ b/tweak/Sources/Redesigned/Kit/SGRFluid.h@@ -6,9 +6,28 @@// Like SGRFlow.h it is all Core Animation: repeating rotations the render server plays, nothing drawn// per frame by the app. Stopping holds every copy where it is, and starting again carries on from there.//+// The Player page tunes it with five sliders (Redesigned/Player/PlayerSettings.m), stored as whole numbers under+// spotifyglass.redesign.fluid.<speed|warp|blur|saturation|brightness>. Every layer follows a change at once.+//// Threading: main thread only; the blur runs off it.#import <UIKit/UIKit.h>+typedef NS_ENUM(NSInteger, SGRFluidSetting) {+ SGRFluidSpeed, // percent of the turns' speed+ SGRFluidWarp, // percent of how far apart the copies turn and how much the upper three show+ SGRFluidBlur, // the blur's strength, 1.25 px of radius each on the 128 px cover+ SGRFluidSaturation, // percent+ SGRFluidBrightness, // percent, under the ceiling that keeps the player's text legible+ SGRFluidSettingCount,+};+typedef struct { NSInteger least, most, step, standard; } SGRFluidLimits;+SGRFluidLimits SGRFluidLimitsOf(SGRFluidSetting setting);+NSInteger SGRFluidValue(SGRFluidSetting setting); // held to its limits+// Stores the value and posts SGRFluidSettingsDidChangeNotification on the main thread.+void SGRSetFluidValue(SGRFluidSetting setting, NSInteger value);+void SGRResetFluidSettings(void); // all five back to their standard values, one notification+extern NSNotificationName const SGRFluidSettingsDidChangeNotification;+@interface SGRFluidLayer : CALayer// The cover, blurred off the main thread and crossfaded in. The same image again is a no-op.- (void)setArtwork:(UIImage *)image animated:(BOOL)animated;diff --git a/tweak/Sources/Redesigned/Kit/SGRFluid.m b/tweak/Sources/Redesigned/Kit/SGRFluid.mindex 5de05790..0ed70239 100644--- a/tweak/Sources/Redesigned/Kit/SGRFluid.m+++ b/tweak/Sources/Redesigned/Kit/SGRFluid.m@@ -12,12 +12,46 @@ static const struct { CGFloat side, x, y; CFTimeInterval turn; } kCopy[] = {{1.8, 0.70, 0.85, -54},};enum { kCopies = sizeof(kCopy) / sizeof(kCopy[0]) };-// The blurred cover is small: it is drawn several times the screen's size, so detail is lost anyway.-static const CGFloat kBlurSide = 128, kBlurRadius = 10, kSaturation = 1.5, kBrightness = -0.08;+// The blurred cover is small: it is drawn several times the screen's size, so detail is lost anyway. The Blur+// setting is its radius in steps of 1.25 px, so the standard 8 is 10 px, a twelfth of the side.+static const CGFloat kBlurSide = 128, kBlurPerStep = 1.25, kBrightness = -0.08;+// The upper three copies' opacity at full Warp.+static const float kOverOpacity = 0.7;// The shade over the bottom of the visible rect, where the controls are.static const CGFloat kShadeFrom = 0.45, kShadeAlpha = 0.55;-static UIImage *blurred(UIImage *image, BOOL contrast) {+NSNotificationName const SGRFluidSettingsDidChangeNotification = @"spotifyglass.redesign.fluidSettingsDidChange";++// In SGRFluidSetting's order.+static const struct { __unsafe_unretained NSString *key; SGRFluidLimits limits; } kSetting[] = {+ {@"spotifyglass.redesign.fluid.speed", {25, 300, 25, 100}},+ {@"spotifyglass.redesign.fluid.warp", {0, 100, 5, 100}},+ {@"spotifyglass.redesign.fluid.blur", {2, 24, 1, 8}},+ {@"spotifyglass.redesign.fluid.saturation", {0, 250, 10, 150}},+ {@"spotifyglass.redesign.fluid.brightness", {40, 150, 5, 100}},+};++SGRFluidLimits SGRFluidLimitsOf(SGRFluidSetting setting) {+ return kSetting[setting].limits;+}++NSInteger SGRFluidValue(SGRFluidSetting setting) {+ SGRFluidLimits l = kSetting[setting].limits;+ return MIN(l.most, MAX(l.least, SGInt(kSetting[setting].key, l.standard)));+}++void SGRSetFluidValue(SGRFluidSetting setting, NSInteger value) {+ SGSetInt(kSetting[setting].key, value);+ [NSNotificationCenter.defaultCenter postNotificationName:SGRFluidSettingsDidChangeNotification object:nil];+}++void SGRResetFluidSettings(void) {+ for (NSInteger i = 0; i < SGRFluidSettingCount; i++) [NSUserDefaults.standardUserDefaults removeObjectForKey:kSetting[i].key];+ [NSNotificationCenter.defaultCenter postNotificationName:SGRFluidSettingsDidChangeNotification object:nil];+}++// `gain` is the Brightness setting as a factor on linear light, applied under the ceiling below.+static UIImage *blurred(UIImage *image, BOOL contrast, CGFloat radius, CGFloat saturation, CGFloat gain) {CGImageRef source = image.CGImage;if (!source) return nil;CIImage *input = [CIImage imageWithCGImage:source];@@ -25,8 +59,8 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {input = [input imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)];CGRect extent = input.extent;CIImage *output = [[[input imageByClampingToExtent]- imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey: @(kBlurRadius)}]- imageByApplyingFilter:@"CIColorControls" withInputParameters:@{kCIInputSaturationKey: @(kSaturation), kCIInputBrightnessKey: @(kBrightness)}];+ imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey: @(radius)}]+ imageByApplyingFilter:@"CIColorControls" withInputParameters:@{kCIInputSaturationKey: @(saturation), kCIInputBrightnessKey: @(kBrightness)}];static CIContext *context;static dispatch_once_t once;dispatch_once(&once, ^{ context = [CIContext contextWithOptions:nil]; });@@ -34,7 +68,7 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {if (!cg) return nil;// Each blurred pixel is capped at a linear luminance (0.04 with Increase Contrast) that keeps the player's// white text at about 8.8:1 and its 65% white at about 4.7:1 on the brightest; an average alone would leave- // a bright patch of a cover behind the text.+ // a bright patch of a cover behind the text. The Brightness gain comes first, so the ceiling still caps it.size_t width = CGImageGetWidth(cg), height = CGImageGetHeight(cg);CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space,@@ -50,12 +84,12 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {CGFloat rgb[3];for (int c = 0; c < 3; c++) {CGFloat v = p[c] / 255.0;- rgb[c] = v <= 0.04045 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4);+ rgb[c] = gain * (v <= 0.04045 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4));}CGFloat light = 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2];- CGFloat gain = light > ceiling ? ceiling / light : 1;+ CGFloat cap = light > ceiling ? ceiling / light : 1;for (int c = 0; c < 3; c++) {- CGFloat v = rgb[c] * gain;+ CGFloat v = MIN(1, rgb[c] * cap);p[c] = floor(255 * (v <= 0.0031308 ? v * 12.92 : 1.055 * pow(v, 1 / 2.4) - 0.055));}p[3] = 255;@@ -73,7 +107,8 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {UIImage *_source;NSUInteger _generation;BOOL _moving;- CFTimeInterval _since, _elapsed;+ CFTimeInterval _since, _elapsed; // _elapsed in turns at the standard speed+ CGFloat _rate, _warp; // Speed and Warp as factors, read when the copies are laid out or startedCGRect _laidOutFor;}@@ -84,14 +119,17 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {for (int i = 0; i < kCopies; i++) {_copies[i] = [CALayer layer];_copies[i].contentsGravity = kCAGravityResizeAspectFill;- _copies[i].opacity = i == 0 ? 1 : 0.7;[self addSublayer:_copies[i]];}+ [self applyWarp];_shade = [CAGradientLayer layer];_shade.colors = @[(id)[UIColor colorWithWhite:0 alpha:0].CGColor, (id)[UIColor colorWithWhite:0 alpha:kShadeAlpha].CGColor];[self addSublayer:_shade];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(contrastChanged)name:UIAccessibilityDarkerSystemColorsStatusDidChangeNotification object:nil];+ // Posted on the main thread by the Player page's sliders, so the change applies at once.+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(settingsChanged)+ name:SGRFluidSettingsDidChangeNotification object:nil];return self;}@@ -100,11 +138,35 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {}- (void)contrastChanged {- dispatch_async(dispatch_get_main_queue(), ^{- UIImage *source = self->_source;- self->_source = nil;- [self setArtwork:source animated:NO];- });+ dispatch_async(dispatch_get_main_queue(), ^{ [self reblur]; });+}++// The same cover blurred again, for a new Blur, Saturation, Brightness or contrast.+- (void)reblur {+ UIImage *source = _source;+ _source = nil;+ [self setArtwork:source animated:NO];+}++// Warp is how far from the visible rect's middle the copies turn, and how much the upper three show: at none,+// one copy turns about the middle, a still picture turning, with nothing to flow through it.+- (void)applyWarp {+ _warp = SGRFluidValue(SGRFluidWarp) / 100.0;+ [CATransaction begin];+ [CATransaction setDisableActions:YES];+ for (int i = 1; i < kCopies; i++) _copies[i].opacity = kOverOpacity * (float)_warp;+ [CATransaction commit];+}++- (void)settingsChanged {+ // Restarted at the new speed from the angle each copy is at, and laid out again for the new warp.+ BOOL moving = _moving;+ if (moving) [self stop];+ [self applyWarp];+ _laidOutFor = CGRectNull;+ [self setNeedsLayout];+ if (moving) [self start];+ [self reblur];}- (BOOL)moving {@@ -135,8 +197,8 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {for (int i = 0; i < kCopies; i++) {CGFloat side = round(longer * kCopy[i].side);_copies[i].bounds = CGRectMake(0, 0, side, side);- _copies[i].position = CGPointMake(CGRectGetMinX(visible) + visible.size.width * kCopy[i].x,- CGRectGetMinY(visible) + visible.size.height * kCopy[i].y);+ _copies[i].position = CGPointMake(CGRectGetMidX(visible) + visible.size.width * (kCopy[i].x - 0.5) * _warp,+ CGRectGetMidY(visible) + visible.size.height * (kCopy[i].y - 0.5) * _warp);}if (moving) [self start];}@@ -148,9 +210,11 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {_source = image;NSUInteger generation = ++_generation;BOOL contrast = SGRIncreaseContrast();+ CGFloat radius = kBlurPerStep * SGRFluidValue(SGRFluidBlur), saturation = SGRFluidValue(SGRFluidSaturation) / 100.0;+ CGFloat gain = SGRFluidValue(SGRFluidBrightness) / 100.0;__weak SGRFluidLayer *weakSelf = self;dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{- UIImage *blur = blurred(image, contrast);+ UIImage *blur = blurred(image, contrast, radius, saturation, gain);dispatch_async(dispatch_get_main_queue(), ^{SGRFluidLayer *layer = weakSelf;if (!blur || !layer || generation != layer->_generation) return;@@ -186,6 +250,7 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {- (void)start {if (self.bounds.size.width < 1) return;_since = CACurrentMediaTime();+ _rate = SGRFluidValue(SGRFluidSpeed) / 100.0;CAFrameRateRange rate = CAFrameRateRangeMake(15, 60, 30);for (int i = 0; i < kCopies; i++) {CFTimeInterval length = fabs(kCopy[i].turn);@@ -196,10 +261,10 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {[CATransaction commit];CABasicAnimation *turn = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];turn.byValue = @(kCopy[i].turn > 0 ? 2 * M_PI : -2 * M_PI);- turn.duration = length;+ turn.duration = length / _rate;turn.repeatCount = HUGE_VALF;turn.additive = YES;- turn.timeOffset = fmod(_elapsed, length);+ turn.timeOffset = fmod(_elapsed, length) / _rate;turn.preferredFrameRateRange = rate;[_copies[i] addAnimation:turn forKey:@"turn"];}@@ -207,7 +272,7 @@ static UIImage *blurred(UIImage *image, BOOL contrast) {// Every copy held at the angle it is drawn at now.- (void)stop {- if (_since > 0) _elapsed += CACurrentMediaTime() - _since;+ if (_since > 0) _elapsed += (CACurrentMediaTime() - _since) * _rate;_since = 0;[CATransaction begin];[CATransaction setDisableActions:YES];diff --git a/tweak/Sources/Redesigned/Player/PlayerSettings.m b/tweak/Sources/Redesigned/Player/PlayerSettings.mindex b220ae72..97beea36 100644--- a/tweak/Sources/Redesigned/Player/PlayerSettings.m+++ b/tweak/Sources/Redesigned/Player/PlayerSettings.m@@ -17,15 +17,16 @@// width, and every choice stays one tap away with the card showing it at once. A note under the control says what the// choice does; it is given the room of the longest note, so the rows under it hold still as it changes.//-// Under the header: the rows that follow the choice (Animated's sources and its Low Data Mode switch;-// Fluid has no settings of its own), the Mini player section (Redesigned/NowPlayingBar/-// NowPlayingBarSettings.m), then the sections either look shares.+// Under the header: the rows that follow the choice (Animated's sources and its Low Data Mode switch; Fluid's+// five sliders and their Reset, which the card and the player follow as they move, SGRFluid.h), the Mini+// player section (Redesigned/NowPlayingBar/NowPlayingBarSettings.m), then the sections either look shares.//// Threading: main thread only.#import "Core/SGCore.h"#import "Settings/SGModPage.h"#import "Settings/SGPageStyle.h"#import "Redesigned/Kit/SGRKit.h"+#import "Redesigned/Kit/SGRFluid.h"#import "Redesigned/NowPlayingBar/NowPlayingBar.h"#import "Shared/AnimatedArtwork/AnimatedArtwork.h"#import "Shared/Player/SGLastTrack.h"@@ -316,6 +317,14 @@ static NSArray<NSString *> *backgroundNotes(void) {@end+// One of Fluid's settings on a slider, written out with `unit` after the number.+static SGModRow *fluidSlider(NSString *title, SGRFluidSetting setting, NSString *unit) {+ SGRFluidLimits l = SGRFluidLimitsOf(setting);+ return SGSliderRow(title, nil, l.least, l.most, l.step, ^double { return SGRFluidValue(setting); },+ ^(double value) { SGRSetFluidValue(setting, lround(value)); },+ ^NSString *(double value) { return [NSString stringWithFormat:@"%.0f%@", value, unit]; });+}+UIViewController *SGRPlayerSettingsPage(NSArray *more) {SGRPlayerShowcase *showcase = [[SGRPlayerShowcase alloc] initWithFrame:CGRectMake(0, 0, 320, kCardMinHeight)];UISegmentedControl *backgrounds = [[UISegmentedControl alloc] initWithItems:SGRPlayerBackgroundNames()];@@ -328,8 +337,22 @@ UIViewController *SGRPlayerSettingsPage(NSArray *more) {SGModRow *sources = SGMotionSourcesRow();sources.visible = lowData.visible;+ __block __weak UITableViewController *weakPage;+ SGModRow *reset = SGActionRow(@"Reset", nil, ^{+ SGRResetFluidSettings();+ [weakPage.tableView reloadData]; // the sliders at once, rather than at the page's next tick+ });+ reset.color = SGRed();+ NSArray<SGModRow *> *fluid = @[+ fluidSlider(@"Speed", SGRFluidSpeed, @"%"), fluidSlider(@"Warp", SGRFluidWarp, @"%"), fluidSlider(@"Blur", SGRFluidBlur, @""),+ fluidSlider(@"Saturation", SGRFluidSaturation, @"%"), fluidSlider(@"Brightness", SGRFluidBrightness, @"%"), reset,+ ];+ for (SGModRow *row in fluid) row.visible = ^BOOL { return SGRPlayerBackground() == SGRPlayerBackgroundFluid; };+NSMutableArray<SGModSection *> *sections = [NSMutableArray arrayWithObjects:SGSection(nil, @[sources, lowData]),+ SGNotedSection(nil, fluid, @"Brightness above 100% can make white text harder to read. A paused song holds the "+ @"background still. Animated and Visualizer use these where they show Fluid."),SGNotedSection(@"Mini player", SGRNowPlayingBarRows(),@"Apple Music style moves the now playing bar in between two tabs as you scroll down."), nil];[sections addObjectsFromArray:more];@@ -337,6 +360,7 @@ UIViewController *SGRPlayerSettingsPage(NSArray *more) {// on the field's layout); Still and Colors are a field of another kind, made once a launch (PlayerField.x).NSString *footer = @"Fluid, Animated and Visualizer change with the next song. Still, Colors and Hide on the player apply after you restart Spotify.";SGRPlayerPage *page = [[SGRPlayerPage alloc] initWithTitle:@"Player" intro:nil sections:sections footer:footer];+ weakPage = page;page.showcase = showcase;page.backgrounds = backgrounds;[backgrounds addTarget:page action:@selector(backgroundPicked) forControlEvents:UIControlEventValueChanged];