Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: a tap around the redesigned player's progress bar seeks the way a drag does
My-Name-Is-Jeff43182d620f7b
diff --git a/harness/player/README.md b/harness/player/README.mdindex a2e26d47..5f8a1f71 100644--- a/harness/player/README.md+++ b/harness/player/README.md@@ -14,7 +14,7 @@ too. `SRC=<another checkout>/tweak/Sources OUT=<dir> ./build.sh` builds it againsources, for example an older commit, to see a bug before its fix.`build.sh` runs `logos.pl -c generator=internal` over `PlayerLyrics.x`, `PlayerArtwork.x`,-`PlayerFooter.x`, `PlayerScroll.x`, `PlayerField.x` and the Kit's `SGRBridges.x`, and links them with+`PlayerControls.x`, `PlayerFooter.x`, `PlayerScroll.x`, `PlayerField.x` and the Kit's `SGRBridges.x`, and links them withthe real `Core/`, `Redesigned/Kit/`, `SGRKaraokeView`, `Settings/` and the Player page. `stubs.m` stands in for the hooks the harnessdoes not compile (the Kit's accent and repaint, the rest of the player, the lyrics store, the haptics,Sing's mic) and plays a mock player: `SGRHarnessSetTrack` reports a track, with the image ids Spotify's@@ -75,6 +75,13 @@ come late, out of order, or not at all.Colours, Still and Animated again, with a note for each and the header the same height throughout. The logends with `settings checks: n of 7 right -- PASS` or `FAIL`.+- `seek` is the tap around the progress bar (`PlayerControls.x`): a mock slider with Spotify's identifier and+ the two times beside it. It checks that a tap counts below the bar and up to 12 pt past its ends, not on the+ thumb or the times, that the ends of the thumb's travel are the song's start and end, that a tap is played+ to the slider as touch down, value changed and touch up with the slider still tracking, that a drag nobody+ seeks for is seeked directly after a second, and that the slider lets go once the position is there. The+ log ends with `seek checks: n of 9 right -- PASS` or `FAIL`.+`HARNESS_VOLUME=0` leaves out the volume row that the phone has and the tree does not.A screen recording is the way to see a move:diff --git a/harness/player/build.sh b/harness/player/build.shindex 2b26d36f..9186d082 100755--- a/harness/player/build.sh+++ b/harness/player/build.sh@@ -6,7 +6,7 @@ OUT=${OUT:-$(dirname "$0")/build}rm -rf "$OUT"; mkdir -p "$OUT/gen" "$OUT/PlayerHarness.app"for f in Redesigned/Player/PlayerLyrics.x Redesigned/Player/PlayerArtwork.x Redesigned/Player/PlayerFooter.x \- Redesigned/Player/PlayerScroll.x Redesigned/Player/PlayerField.x Redesigned/Player/PlayerMotion.x Redesigned/Player/PlayerLandscape.x Redesigned/Kit/SGRBridges.x; do+ Redesigned/Player/PlayerScroll.x Redesigned/Player/PlayerField.x Redesigned/Player/PlayerMotion.x Redesigned/Player/PlayerLandscape.x Redesigned/Player/PlayerControls.x Redesigned/Kit/SGRBridges.x; doname=$(basename "$f" .x)"$THEOS/bin/logos.pl" -c generator=internal "$SRC/$f" > "$OUT/gen/$name.m"donediff --git a/harness/player/main.m b/harness/player/main.mindex 7409b2b3..31a3d06b 100644--- a/harness/player/main.m+++ b/harness/player/main.m@@ -27,6 +27,7 @@#import <AVFoundation/AVFoundation.h>#import <UIKit/UIKit.h>#import <objc/runtime.h>+#import <objc/message.h>#import "Shared/Lyrics/Lyrics.h"#import "Redesigned/Player/Player.h"#import "Redesigned/Kit/SGRBridges.h"@@ -215,6 +216,12 @@ static NSString *colorName(UIImage *image) {- (void)cancelTrackingWithEvent:(UIEvent *)event {}@end+// The play button's view, which PlayerControls.x hooks for its tap.+@interface _TtC28EncoreConsumerMobile_BaseKit14PlayButtonView : UIView @end+@implementation _TtC28EncoreConsumerMobile_BaseKit14PlayButtonView+- (void)uiButtonTapped {}+@end+@interface MockEncoreButton : UIControl @end@implementation MockEncoreButton @end@@ -555,6 +562,7 @@ static void loadLyrics(void) {else if ([scenario() isEqualToString:@"cover"]) [self runCoverChecks];else if ([scenario() isEqualToString:@"immersive"]) [self runImmersiveChecks];else if ([scenario() isEqualToString:@"settings"]) [self runSettingsChecks];+ else if ([scenario() isEqualToString:@"seek"]) [self runSeekChecks];// Opened, closed and opened again, so a screenshot can be taken of each state and of the move itself.else for (NSNumber *at in @[@2, @6, @10]) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(at.doubleValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{@@ -1274,6 +1282,88 @@ static BOOL linesSeek(SGRKaraokeView *lyrics) {});}+#pragma mark - tap to seek++// The tap around the progress bar (PlayerControls.x): where it lands, what it maps to, and how it seeks. The+// slider is the mock's, given Spotify's identifier, with the two times beside it. A tap is the recognizer's+// action with its starting point set, since the harness cannot make a touch.+- (void)runSeekChecks {+ __block NSUInteger right = 0, checks = 0;+ void (^check)(NSString *, BOOL, NSString *) = ^(NSString *step, BOOL ok, NSString *detail) {+ checks++;+ if (ok) right++;+ NSLog(@"[harness] seek %@: %@ -- %@", step, detail, ok ? @"ok" : @"WRONG");+ };+ UIViewController *durationUnit = _units[1];+ UIView *unit = durationUnit.view;+ CGFloat W = unit.bounds.size.width;+ UISlider *slider = [[_TtCO17NowPlaying_ECMKit11ProgressBar6Slider alloc] initWithFrame:CGRectMake(24, 2, W - 48, 14)];+ slider.accessibilityIdentifier = @"SPTNowPlayingSliderV2";+ slider.value = 0.22;+ [unit addSubview:slider];+ UILabel *taken = [[UILabel alloc] initWithFrame:CGRectMake(24, 20, 44, 16)];+ taken.accessibilityIdentifier = @"now-playing-time-take-label";+ UILabel *remaining = [[UILabel alloc] initWithFrame:CGRectMake(W - 24 - 44, 20, 44, 16)];+ remaining.accessibilityIdentifier = @"now-playing-time-remaning-label";+ [unit addSubview:taken];+ [unit addSubview:remaining];+ [durationUnit viewDidLayoutSubviews];+ [SGPlayerState() setValue:@200 forKey:@"duration"];++ UIGestureRecognizer *tap = nil;+ for (UIGestureRecognizer *recognizer in unit.gestureRecognizers) {+ if ([NSStringFromClass(recognizer.class) isEqualToString:@"SGRSeekTap"]) tap = recognizer;+ }+ CGRect bar = slider.frame;+ CGRect thumb = [unit convertRect:[slider thumbRectForBounds:slider.bounds trackRect:[slider trackRectForBounds:slider.bounds] value:slider.value] fromView:slider];+ check(@"on the duration unit", tap != nil, tap ? @"a recognizer there" : @"none");+ check(@"under the bar", SGRSeekTapLands(unit, slider, CGPointMake(W / 2, CGRectGetMaxY(bar) + 12)), @"12pt below the bar");+ check(@"past the ends", SGRSeekTapLands(unit, slider, CGPointMake(CGRectGetMinX(bar) - 10, 8)) && SGRSeekTapLands(unit, slider, CGPointMake(CGRectGetMaxX(bar) + 10, 8))+ && !SGRSeekTapLands(unit, slider, CGPointMake(CGRectGetMinX(bar) - 20, 8)), @"10pt past either end, not 20");+ check(@"not on the thumb", !SGRSeekTapLands(unit, slider, CGPointMake(CGRectGetMidX(thumb), CGRectGetMidY(thumb))), NSStringFromCGRect(thumb));+ check(@"not on the times", !SGRSeekTapLands(unit, slider, taken.center) && !SGRSeekTapLands(unit, slider, remaining.center), @"either time");+ CGFloat end = SGRSeekShareAt(slider, CGPointMake(slider.bounds.size.width - 1, 7)), start = SGRSeekShareAt(slider, CGPointMake(1, 7));+ CGFloat middle = SGRSeekShareAt(slider, CGPointMake(slider.bounds.size.width / 2, 7));+ check(@"the ends are the song's", end == 1 && start == 0 && fabs(middle - 0.5) < 0.001,+ [NSString stringWithFormat:@"start %.3f, middle %.3f, end %.3f", start, middle, end]);++ // A tap at three quarters of the thumb's travel, played to the slider as a drag.+ NSMutableArray<NSNumber *> *events = [NSMutableArray array];+ __block BOOL answers = NO;+ for (NSNumber *event in @[@(UIControlEventTouchDown), @(UIControlEventValueChanged), @(UIControlEventTouchUpInside)]) {+ [slider addAction:[UIAction actionWithHandler:^(UIAction *action) {+ [events addObject:event];+ if (answers && event.unsignedIntegerValue == UIControlEventTouchUpInside) SGKaraokeSeek((NSInteger)(slider.value * 200000));+ }] forControlEvents:event.unsignedIntegerValue];+ }+ CGFloat from = CGRectGetMidX([slider thumbRectForBounds:slider.bounds trackRect:[slider trackRectForBounds:slider.bounds] value:0]);+ CGFloat to = CGRectGetMidX([slider thumbRectForBounds:slider.bounds trackRect:[slider trackRectForBounds:slider.bounds] value:1]);+ void (^tapAt)(CGFloat) = ^(CGFloat share) {+ CGPoint point = [unit convertPoint:CGPointMake(from + share * (to - from), 7) fromView:slider];+ [tap setValue:[NSValue valueWithCGPoint:point] forKey:@"start"];+ ((void (*)(id, SEL))objc_msgSend)(tap, NSSelectorFromString(@"sgr_seek"));+ };+ tapAt(0.75);+ NSArray *drag = @[@(UIControlEventTouchDown), @(UIControlEventValueChanged), @(UIControlEventTouchUpInside)];+ check(@"played as a drag", [events isEqualToArray:drag] && fabs(slider.value - 0.75) < 0.001 && slider.isTracking,+ [NSString stringWithFormat:@"%lu events, value %.3f, tracking %d", (unsigned long)events.count, slider.value, slider.isTracking]);+ // Nothing seeked for it: after a second the tap seeks directly, and the slider lets go.+ after(1.3, ^{+ NSInteger at = SGKaraokePositionMs();+ check(@"a drag that did not seek is seeked", labs(at - 150000) < 1000 && !slider.isTracking,+ [NSString stringWithFormat:@"at %ld ms, tracking %d", (long)at, slider.isTracking]);+ // Spotify seeking for the drag: the slider lets go as the position arrives.+ answers = YES;+ tapAt(0.3);+ NSInteger now = SGKaraokePositionMs();+ check(@"the slider lets go once the position is there", labs(now - 60000) < 1000 && !slider.isTracking,+ [NSString stringWithFormat:@"at %ld ms, tracking %d", (long)now, slider.isTracking]);+ });+ after(2.6, ^{+ NSLog(@"[harness] seek checks: %lu of %lu right -- %@", (unsigned long)right, (unsigned long)checks, right == checks ? @"PASS" : @"FAIL");+ });+}+// One track, then another album's at 8 s, its picture on the screens 0.4 s later.- (void)runLook {UIImage *second = secondArtwork();diff --git a/harness/player/stubs.m b/harness/player/stubs.mindex 1e15f5b0..12fbeb88 100644--- a/harness/player/stubs.m+++ b/harness/player/stubs.m@@ -78,14 +78,6 @@ void SGRHarnessSetTrack(NSString *uri, NSString *imageURI, BOOL paused) {for (id<SGPlayerStateObserver> observer in sg_observers.allObjects) [observer playerStateDidChange:state];}-#pragma mark - Redesigned/Player/PlayerControls.x--void SGRPlayerVanish(UIView *view) {- view.alpha = 0;- view.userInteractionEnabled = NO;- view.accessibilityElementsHidden = YES;-}-#pragma mark - Shared/Hapticsvoid SGPlayFeedback(NSInteger feedback) {}diff --git a/tweak/Sources/Redesigned/Player/Player.h b/tweak/Sources/Redesigned/Player/Player.hindex 38198d64..212938d2 100644--- a/tweak/Sources/Redesigned/Player/Player.h+++ b/tweak/Sources/Redesigned/Player/Player.h@@ -112,6 +112,12 @@ UIViewController *SGRPlayerSettingsPage(NSArray *more);// which SGRSuppress cannot keep (PlayerControls.x).void SGRPlayerVanish(UIView *view);+// The tap to seek around the progress bar (PlayerControls.x), apart for the harness: whether a tap at `point`+// in the duration unit's view seeks, and the share of the song a point on the slider stands for, measured+// over the thumb's travel (NAN while the slider has no width).+BOOL SGRSeekTapLands(UIView *unit, UISlider *slider, CGPoint point);+CGFloat SGRSeekShareAt(UISlider *slider, CGPoint point);+// The more button's menu opens as the system menu with the player's own items (PlayerMenu.m). Called from// PlayerHeader.x with Spotify's more button on every pass; watching it again changes nothing.void SGRPlayerMenuWatch(UIView *button);diff --git a/tweak/Sources/Redesigned/Player/PlayerControls.x b/tweak/Sources/Redesigned/Player/PlayerControls.xindex e019824d..42f6dca5 100644--- a/tweak/Sources/Redesigned/Player/PlayerControls.x+++ b/tweak/Sources/Redesigned/Player/PlayerControls.x@@ -16,8 +16,10 @@// id=now-playing-time-take-label and id=now-playing-time-remaning-label, SPTEncoreLabels whose UILabel// is id=...-internal (:227). The sticky header has a play button of its own (:492), which a search// inside the unit's view never reaches.+#import <UIKit/UIGestureRecognizerSubclass.h>#import "Core/SGCore.h"#import "Redesigned/Kit/SGRKit.h"+#import "Shared/Haptics/Haptics.h"#import "Shared/Lyrics/Lyrics.h"#import "Player.h"@@ -231,33 +233,158 @@ static UILabel *monospaced(UIView *host, NSString *identifier, const void *findK#pragma mark - tap to seek-static char kSliderKey, kSeekTapKey;+// Spotify's slider only drags. A tap in the band around it seeks: anywhere across the duration unit's height,+// up to kSeekReach past either end of the bar, moving at most kSeekSlop and lifting within kSeekLongest. A tap+// on the thumb is the thumb's, and a tap on a time is Spotify's, which turns the remaining time into the length.+static const CGFloat kSeekReach = 12, kSeekSlop = 10, kThumbSlop = 6;+static const NSTimeInterval kSeekLongest = 0.5;+// The tap is played to the slider as a short drag, so Spotify's unit seeks the way it does at the end of one.+// Meanwhile the slider says it is still tracking, until the player's position is this near the place tapped or+// for this long at most, so the unit does not put the thumb back to the old place first. A drag that did not+// seek by kSeekCheck is seeked directly.+static const NSInteger kSeekNear = 1500;+static const NSTimeInterval kSeekHold = 2, kSeekCheck = 1;++static char kSliderKey, kSeekTapKey, kTakeTimeKey, kRemainingTimeKey;+static __weak UISlider *sg_seekSlider;+static NSInteger sg_seekTarget;+static CFTimeInterval sg_seekHeldUntil;++static CGFloat thumbCentre(UISlider *slider, float value) {+ CGRect bounds = slider.bounds;+ return CGRectGetMidX([slider thumbRectForBounds:bounds trackRect:[slider trackRectForBounds:bounds] value:value]);+}++// Where along the song a point on the slider is, over the thumb's travel rather than the slider's width, so+// either end of the bar is the song's start or end exactly.+CGFloat SGRSeekShareAt(UISlider *slider, CGPoint point) {+ CGFloat start = thumbCentre(slider, slider.minimumValue), end = thumbCentre(slider, slider.maximumValue);+ if (end - start < 1) return NAN;+ return MIN(1, MAX(0, (point.x - start) / (end - start)));+}++// Whether a tap at `point` in the duration unit's view seeks.+BOOL SGRSeekTapLands(UIView *unit, UISlider *slider, CGPoint point) {+ if (!slider.window || !slider.enabled || ![slider isDescendantOfView:unit] || !CGRectContainsPoint(unit.bounds, point)) return NO;+ CGRect bar = [unit convertRect:slider.bounds fromView:slider];+ if (point.x < CGRectGetMinX(bar) - kSeekReach || point.x > CGRectGetMaxX(bar) + kSeekReach) return NO;+ CGRect bounds = slider.bounds;+ CGRect thumb = [slider thumbRectForBounds:bounds trackRect:[slider trackRectForBounds:bounds] value:slider.value];+ if (CGRectContainsPoint(CGRectInset([unit convertRect:thumb fromView:slider], -kThumbSlop, -kThumbSlop), point)) return NO;+ for (UIView *time in @[SGRFindByIdentifier(unit, @"now-playing-time-take-label", &kTakeTimeKey) ?: NSNull.null,+ SGRFindByIdentifier(unit, @"now-playing-time-remaning-label", &kRemainingTimeKey) ?: NSNull.null]) {+ if ([time isKindOfClass:UIView.class] && !time.hidden && CGRectContainsPoint([unit convertRect:time.bounds fromView:time], point)) return NO;+ }+ return YES;+}++static BOOL seekHeld(void) {+ if (!sg_seekHeldUntil) return NO;+ NSInteger at = SGKaraokePositionMs();+ if (CACurrentMediaTime() < sg_seekHeldUntil && (at < 0 || labs(at - sg_seekTarget) >= kSeekNear)) return YES;+ sg_seekHeldUntil = 0;+ return NO;+}++// Whether Spotify listens to the slider's control events, which is how a played drag reaches it.+static BOOL listensToSlider(UISlider *slider) {+ __block BOOL listens = NO;+ [slider enumerateEventHandlers:^(UIAction *action, id target, SEL selector, UIControlEvents events, BOOL *stop) {+ if (events & (UIControlEventTouchUpInside | UIControlEventValueChanged)) listens = *stop = YES;+ }];+ return listens;+}++static void seekTo(UISlider *slider, CGFloat share) {+ SPTPlayerState *state = SGPlayerState();+ if (!state || state.duration <= 0 || isnan(share)) return;+ NSInteger ms = (NSInteger)(share * state.duration * 1000);+ SGPlayFeedback(SGFeedbackSkip);+ BOOL played = listensToSlider(slider);+ static NSUInteger logged;+ if (logged++ < 3) SGLog(@"redesign player: a tap seeks to %.3f of the song, %@", share, played ? @"played to the slider as a drag" : @"directly (nothing listens to the slider)");+ if (!played) {+ SGKaraokeSeek(ms);+ return;+ }+ [slider setValue:slider.minimumValue + share * (slider.maximumValue - slider.minimumValue) animated:NO];+ sg_seekSlider = slider;+ sg_seekTarget = ms;+ sg_seekHeldUntil = CACurrentMediaTime() + kSeekHold;+ [slider sendActionsForControlEvents:UIControlEventTouchDown];+ [slider sendActionsForControlEvents:UIControlEventValueChanged];+ [slider sendActionsForControlEvents:UIControlEventTouchUpInside];+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kSeekCheck * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ NSInteger at = SGKaraokePositionMs();+ if (sg_seekTarget != ms || at < 0 || labs(at - ms) < kSeekNear) return;+ SGLog(@"redesign player: the played drag did not seek (at %ld ms, not %ld), seeking directly", (long)at, (long)ms);+ sg_seekHeldUntil = 0;+ SGKaraokeSeek(ms);+ });+}-// Spotify's slider only drags. A tap on it seeks to where it landed; the tap cancels the slider's own-// tracking, so the two never both seek.-@interface SGRSeekTap : UITapGestureRecognizer+// A recognizer of its own rather than a tap: UIKit lets a slider keep a single tap from a tap recognizer+// over it, and the band reaches past the slider.+@interface SGRSeekTap : UIGestureRecognizer+@property (nonatomic, weak) UISlider *slider;@end-@implementation SGRSeekTap+@implementation SGRSeekTap {+ CGPoint _start;+ CFTimeInterval _began;+}++- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {+ UIView *unit = self.view;+ UITouch *touch = touches.anyObject;+ CGPoint point = [touch locationInView:unit];+ if (touches.count > 1 || [event touchesForGestureRecognizer:self].count > 1 || !SGRSeekTapLands(unit, self.slider, point)) {+ self.state = UIGestureRecognizerStateFailed;+ return;+ }+ _start = point;+ _began = CACurrentMediaTime();+}++- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {+ CGPoint point = [touches.anyObject locationInView:self.view];+ if (hypot(point.x - _start.x, point.y - _start.y) > kSeekSlop) self.state = UIGestureRecognizerStateFailed;+}++- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {+ BOOL tap = CACurrentMediaTime() - _began <= kSeekLongest;+ self.state = tap ? UIGestureRecognizerStateEnded : UIGestureRecognizerStateFailed;+}++- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {+ self.state = UIGestureRecognizerStateFailed;+}+- (void)sgr_seek {- UIView *slider = self.view;- SPTPlayerState *state = SGPlayerState();- CGFloat width = slider.bounds.size.width;- if (!state || width < 1 || state.duration <= 0) return;- CGFloat share = MIN(1, MAX(0, [self locationInView:slider].x / width));- SGKaraokeSeek((NSInteger)(share * state.duration * 1000));+ UISlider *slider = self.slider;+ if (slider) seekTo(slider, SGRSeekShareAt(slider, [self.view convertPoint:_start toView:slider]));}@endstatic void watchSlider(UIView *host) {- UIView *slider = SGRFindByIdentifier(host, @"SPTNowPlayingSliderV2", &kSliderKey);- if (!slider || objc_getAssociatedObject(slider, &kSeekTapKey)) return;- SGRSeekTap *tap = [[SGRSeekTap alloc] init];- [tap addTarget:tap action:@selector(sgr_seek)];- [slider addGestureRecognizer:tap];- objc_setAssociatedObject(slider, &kSeekTapKey, tap, OBJC_ASSOCIATION_ASSIGN);- SGLog(@"redesign player: a tap on the progress bar seeks");+ UISlider *slider = (UISlider *)SGRFindByIdentifier(host, @"SPTNowPlayingSliderV2", &kSliderKey);+ if (![slider isKindOfClass:UISlider.class]) return;+ SGRSeekTap *tap = objc_getAssociatedObject(host, &kSeekTapKey);+ if (!tap) {+ tap = [[SGRSeekTap alloc] initWithTarget:nil action:nil];+ [tap addTarget:tap action:@selector(sgr_seek)];+ [host addGestureRecognizer:tap];+ objc_setAssociatedObject(host, &kSeekTapKey, tap, OBJC_ASSOCIATION_ASSIGN);+ SGLog(@"redesign player: a tap around the progress bar seeks");+ }+ tap.slider = slider;+}++%hook _TtCO17NowPlaying_ECMKit11ProgressBar6Slider+- (BOOL)isTracking {+ return ((UISlider *)self == sg_seekSlider && seekHeld()) || %orig;}+%end%hook _TtC20NowPlaying_ModesImpl19DurationElementUnit- (void)viewDidLayoutSubviews {@@ -284,5 +411,6 @@ static void watchSlider(UIView *host) {@"_TtC20NowPlaying_ModesImpl19DurationElementUnit",@"_TtC28EncoreConsumerMobile_BaseKit14PlayButtonView",@"SPTEncoreIconView",+ @"_TtCO17NowPlaying_ECMKit11ProgressBar6Slider",]);}