Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: the redesigned player has a volume slider under its controls

My-Name-Is-Jeff16bb8aac682d
diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.h
index 060fb5a8..db7b0b4e 100644
--- a/tweak/Sources/Redesigned/Player/Player.h
+++ b/tweak/Sources/Redesigned/Player/Player.h
@@ -91,6 +91,14 @@ void SGRPlayerLyricsChanged(void);
// hides them either way.
#define SGRKeyLyricsAutoHide @"spotifyglass.redesign.lyrics.autoHide"
+// A volume slider under the controls (PlayerVolume.m), the system's: this iPhone's, or the Connect device's.
+// On until switched off. Placed by the footer's layout, between the controls (`controls`) and the footer row
+// (`row`), both arranged in Spotify's bottom stack, `margin` in from each side.
+#define SGRKeyPlayerVolume @"spotifyglass.redesign.player.volume"
+BOOL SGRPlayerVolumeOn(void);
+// YES when the row shows; NO when it is off, has no controls to sit under or no room between them and the footer.
+BOOL SGRPlayerPlaceVolume(UIView *stack, UIView *controls, UIView *row, CGFloat margin);
+
// The lyrics turn sideways with the phone onto a landscape screen of their own (PlayerLandscape.x). On
// until switched off. Shown or put away by hand for the harness.
#define SGRKeyLyricsLandscape @"spotifyglass.redesign.lyrics.landscape"
diff --git a/tweak/Sources/Redesigned/Player/PlayerFooter.x b/tweak/Sources/Redesigned/Player/PlayerFooter.x
index be41a581..5b13e34e 100644
--- a/tweak/Sources/Redesigned/Player/PlayerFooter.x
+++ b/tweak/Sources/Redesigned/Player/PlayerFooter.x
@@ -40,6 +40,10 @@ static const CGFloat kRowAboveSafeArea = 20, kRowMinBottom = 34;
// The controls follow a share of the row's move when nothing stands between them, so the gaps above
// and below them even out instead of all the room opening under them.
static const CGFloat kControlsShare = 0.3;
+// The volume row's sides, the player's own margin.
+static const CGFloat kVolumeMargin = 24;
+// And the controls go up this much over it, so the gaps above and below the volume row match.
+static const CGFloat kVolumeLift = 10;
static char kConnectKey, kShareKey, kTrimmerKey, kSharedByKey, kQueueKey, kLyricsGlyphKey, kReachKey;
static __weak SGRGlyphButton *sg_lyricsGlyph;
@@ -192,8 +196,20 @@ static void lowerRow(UIView *row) {
// Only the controls, and only straight above: a volume row between them keeps its place and theirs.
UIView *above = rowAbove(row);
BOOL controls = [NSStringFromClass(unitOf(above).class) containsString:@"PlaybackControlsElementsUnit"];
- CGAffineTransform follow = CGAffineTransformMakeTranslation(0, controls ? round(move * kControlsShare) : 0);
+ // With the volume row, the controls stay where Spotify put them and the row takes the room the footer leaves.
+ // The volume row is placed against the controls where they would stand for it; one that does not show leaves them
+ // following the footer as they do without it.
+ BOOL volume = SGRPlayerVolumeOn() && controls;
+ CGFloat share = volume ? 0 : kControlsShare, lift = volume ? kVolumeLift : 0;
+ CGAffineTransform follow = CGAffineTransformMakeTranslation(0, controls ? round(move * share) - lift : 0);
if (above && controls && !CGAffineTransformEqualToTransform(above.transform, follow)) above.transform = follow;
+ if (volume && !SGRPlayerPlaceVolume(stack, above, row, kVolumeMargin)) {
+ share = kControlsShare;
+ follow = CGAffineTransformMakeTranslation(0, round(move * share));
+ if (!CGAffineTransformEqualToTransform(above.transform, follow)) above.transform = follow;
+ } else if (!volume) {
+ SGRPlayerPlaceVolume(stack, nil, row, kVolumeMargin);
+ }
SGRFooterReach *reach = objc_getAssociatedObject(row, &kReachKey);
if (!reach) {
@@ -210,8 +226,8 @@ static void lowerRow(UIView *row) {
static dispatch_once_t once;
dispatch_once(&once, ^{
- SGLog(@"redesign player: footer row from %.0f to %.0f of %.0f (safe area %.0f), controls %@ %.0f", middle, middle + move, height,
- window.safeAreaInsets.bottom, controls ? @"follow" : @"stay", controls ? round(move * kControlsShare) : 0);
+ SGLog(@"redesign player: footer row from %.0f to %.0f of %.0f (safe area %.0f), controls %@ %.0f, volume %@", middle, middle + move, height,
+ window.safeAreaInsets.bottom, controls ? @"follow" : @"stay", controls ? round(move * share) : 0, SGRPlayerVolumeOn() ? @"on" : @"off");
});
}
diff --git a/tweak/Sources/Redesigned/Player/PlayerSettings.m b/tweak/Sources/Redesigned/Player/PlayerSettings.m
index 97beea36..4f69da11 100644
--- a/tweak/Sources/Redesigned/Player/PlayerSettings.m
+++ b/tweak/Sources/Redesigned/Player/PlayerSettings.m
@@ -353,6 +353,8 @@ UIViewController *SGRPlayerSettingsPage(NSArray *more) {
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(@"Controls", @[SGSwitchRow(@"Volume", nil, SGRKeyPlayerVolume)],
+ @"A slider under the controls for this iPhone's volume, or the speaker's while Spotify plays on another device."),
SGNotedSection(@"Mini player", SGRNowPlayingBarRows(),
@"Apple Music style moves the now playing bar in between two tabs as you scroll down."), nil];
[sections addObjectsFromArray:more];
diff --git a/tweak/Sources/Redesigned/Player/PlayerVolume.m b/tweak/Sources/Redesigned/Player/PlayerVolume.m
new file mode 100644
index 00000000..55ee4e85
--- /dev/null
+++ b/tweak/Sources/Redesigned/Player/PlayerVolume.m
@@ -0,0 +1,128 @@
+// Player redesign: a volume slider under the controls, as the Music app has. It is MPVolumeView, as the HIG asks
+// (sliders.md, playing-audio.md), so it is the system's volume: the hardware buttons move it, and while Spotify plays
+// on a Connect device it is that device's, which Spotify hands iOS through its remote volume. No route button: the
+// footer's Connect glyph is the route.
+//
+// It sits in Spotify's bottom stack, outside its arranged views, between the controls and the footer row, so the
+// lyrics fade it with the rest of the stack (PlayerLyrics.x) and its touches stay inside the stack. The footer's
+// lowerRow (PlayerFooter.x) places it after the moves, and keeps the controls where Spotify put them to make the room.
+#import <MediaPlayer/MediaPlayer.h>
+#import <objc/runtime.h>
+#import "Core/SGCore.h"
+#import "Redesigned/Kit/SGRKit.h"
+#import "Player.h"
+
+// The track's height, the glyphs' point size and the gap between a glyph and the track.
+static const CGFloat kRowHeight = 34, kGlyphSize = 13, kGlyphGap = 10, kTrack = 4;
+// Half the play button's height and half the footer glyphs': below this much room between them it does not fit.
+static const CGFloat kButtonsHalf = 24, kGlyphsHalf = 14;
+// The slider draws its track this far above its own middle (6.5px measured on the phone), so the glyphs go up with it.
+static const CGFloat kTrackAbove = 2;
+
+@interface SGRVolumeRow : UIView
+@end
+
+@implementation SGRVolumeRow {
+ MPVolumeView *_volume;
+ UIImageView *_quiet, *_loud;
+}
+
+static UIImage *trackImage(UIColor *color) {
+ CGFloat side = kTrack;
+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(side, side)];
+ UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {
+ [color setFill];
+ [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, side, side) cornerRadius:side / 2] fill];
+ }];
+ return [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, side / 2, 0, side / 2)];
+}
+
+static UIImage *thumbImage(void) {
+ CGFloat side = 14;
+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(side, side)];
+ return [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {
+ [UIColor.whiteColor setFill];
+ [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)] fill];
+ }];
+}
+
+static UIImageView *glyph(NSString *symbol) {
+ UIImageSymbolConfiguration *configuration = [UIImageSymbolConfiguration configurationWithPointSize:kGlyphSize weight:UIImageSymbolWeightSemibold];
+ UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:symbol withConfiguration:configuration]];
+ view.tintColor = [UIColor colorWithWhite:1 alpha:0.6];
+ view.contentMode = UIViewContentModeCenter;
+ view.isAccessibilityElement = NO;
+ return view;
+}
+
+- (instancetype)initWithFrame:(CGRect)frame {
+ if (!(self = [super initWithFrame:frame])) return nil;
+ _volume = [MPVolumeView new];
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+ _volume.showsRouteButton = NO;
+#pragma clang diagnostic pop
+ [_volume setMinimumVolumeSliderImage:trackImage(UIColor.whiteColor) forState:UIControlStateNormal];
+ [_volume setMaximumVolumeSliderImage:trackImage([UIColor colorWithWhite:1 alpha:0.22]) forState:UIControlStateNormal];
+ [_volume setVolumeThumbImage:thumbImage() forState:UIControlStateNormal];
+ _quiet = glyph(@"speaker.fill");
+ _loud = glyph(@"speaker.wave.3.fill");
+ [self addSubview:_quiet];
+ [self addSubview:_volume];
+ [self addSubview:_loud];
+ return self;
+}
+
+- (void)layoutSubviews {
+ [super layoutSubviews];
+ CGRect bounds = self.bounds;
+ CGFloat side = kRowHeight, middle = CGRectGetMidY(bounds), glyphs = middle - kTrackAbove;
+ _quiet.frame = CGRectMake(0, glyphs - side / 2, side / 2 + kGlyphSize / 2, side);
+ _loud.frame = CGRectMake(CGRectGetMaxX(bounds) - side / 2 - kGlyphSize, glyphs - side / 2, side / 2 + kGlyphSize, side);
+ CGFloat left = CGRectGetMaxX(_quiet.frame) + kGlyphGap, right = CGRectGetMinX(_loud.frame) - kGlyphGap;
+ // MPVolumeView draws its slider at the top of its bounds, not in the middle: it gets the slider's own height,
+ // centered, so the track lines up with the glyphs (it sat 9pt above them).
+ CGFloat sliderHeight = MIN(side, [_volume sizeThatFits:CGSizeMake(MAX(0, right - left), side)].height ?: side);
+ _volume.frame = CGRectMake(left, round(middle - sliderHeight / 2), MAX(0, right - left), sliderHeight);
+}
+
+@end
+
+static char kVolumeKey, kPlayKey;
+
+// Read at launch, as the player's other switches are: the footer asks on every layout pass.
+BOOL SGRPlayerVolumeOn(void) {
+ static BOOL on;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{ on = SGEnabled(SGRKeyPlayerVolume); });
+ return on;
+}
+
+BOOL SGRPlayerPlaceVolume(UIView *stack, UIView *controls, UIView *row, CGFloat margin) {
+ SGRVolumeRow *volume = objc_getAssociatedObject(stack, &kVolumeKey);
+ if (!SGRPlayerVolumeOn() || !controls) {
+ if (volume) volume.alpha = 0;
+ return NO;
+ }
+ if (!volume) {
+ volume = [SGRVolumeRow new];
+ objc_setAssociatedObject(stack, &kVolumeKey, volume, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ if (volume.superview != stack) [stack addSubview:volume];
+ // Halfway between the play button's bottom and the footer glyphs' top, each where it is drawn (convertRect: takes
+ // the moves' transforms in): the controls' unit reaches above its buttons, so its own middle sat the row 16pt high.
+ // Without the button, the unit's middle stands in for it.
+ UIView *play = SGRFindByIdentifier(controls, @"SPTNowPlayingPlayButton", &kPlayKey);
+ CGFloat above = play ? CGRectGetMaxY([play convertRect:play.bounds toView:stack])
+ : controls.center.y + controls.transform.ty + kButtonsHalf;
+ CGFloat below = [row convertPoint:CGPointMake(CGRectGetMidX(row.bounds), CGRectGetMidY(row.bounds)) toView:stack].y - kGlyphsHalf;
+ CGFloat height = kRowHeight, room = below - above;
+ CGRect frame = CGRectMake(margin, round((above + below - height) / 2), MAX(0, stack.bounds.size.width - 2 * margin), height);
+ if (!CGRectEqualToRect(volume.frame, frame)) volume.frame = frame;
+ // The lyrics fade the stack's views by their alpha and bring back only the ones they faded.
+ // The track and thumb are 14pt of the row's 34: it fits with the glyphs' and buttons' own clearance around it.
+ BOOL fits = room >= height * 0.6;
+ if (!fits) volume.alpha = 0;
+ else if (volume.alpha < 0.01 && controls.alpha > 0.99) volume.alpha = 1;
+ return fits;
+}