Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: a tapped lyrics line glides into place at once, and lines scrolled by hand keep their room
My-Name-Is-Jeffd66f514e943b
diff --git a/harness/lyrics/main.m b/harness/lyrics/main.mindex 2a1599a8..901a9020 100644--- a/harness/lyrics/main.m+++ b/harness/lyrics/main.m@@ -20,8 +20,9 @@// Musixmatch's community translations come in after the lines on the phone// -gemini 1 a Gemini key is set, so the menu offers Translate with Gemini (stubs.m answers it)// -geminiDelay S stubs.m's Gemini answers S seconds later instead of at once+// -seekLag S the player reports a seek S seconds after it is asked for (stubs.m)// -check 1 asserts the view's fixes (see runChecks), prints PASS and FAIL lines and quits with the-// number of failures; run it with -song duet -at 20000 -gemini 1 -geminiDelay 1+// number of failures; run it with -song duet -at 20000 -gemini 1 -geminiDelay 1 -seekLag 0.4// and the lyrics' own settings by their keys: -spotifyglass.lyricsSimulateWords 1 (sweep line timed// lines on the estimate), -spotifyglass.redesign.lyricsPronunciation 1,// -spotifyglass.redesign.lyricsTranslation 1, -spotifyglass.redesign.lyricsTextOrder '(translation, lyrics, pronunciation)'.@@ -220,6 +221,86 @@ static NSString *textsOf(NSArray<SGKaraokeLine *> *lines) {return [texts componentsJoinedByString:@" "];}+// The tops of the line views there are, by line, as laid out (the scale a line is shown at left out).+static NSArray<NSNumber *> *shownLines(SGRKaraokeView *karaoke) {+ return [[[karaoke valueForKey:@"shown"] allKeys] sortedArrayUsingSelector:@selector(compare:)];+}++static CGFloat topOf(UIView *view) {+ return view.center.y - view.bounds.size.height / 2;+}++// The smallest and the largest room between two line views one after the other.+static void gapsBetween(SGRKaraokeView *karaoke, CGFloat *least, CGFloat *most) {+ NSDictionary<NSNumber *, UIView *> *shown = [karaoke valueForKey:@"shown"];+ NSArray<NSNumber *> *lines = shownLines(karaoke);+ *least = CGFLOAT_MAX;+ *most = -CGFLOAT_MAX;+ for (NSUInteger i = 0; i + 1 < lines.count; i++) {+ if (lines[i + 1].integerValue != lines[i].integerValue + 1) continue;+ UIView *a = shown[lines[i]], *b = shown[lines[i + 1]];+ CGFloat gap = topOf(b) - topOf(a) - a.bounds.size.height;+ *least = MIN(*least, gap);+ *most = MAX(*most, gap);+ }+}++// A tap on a line three ahead, more than the song moving on: the stack is arranged around it (or around a+// line still sung over it) from the first frame, gliding there, and stays so while the player is slow to+// report the seek and after it has.+static void checkGlide(SGRKaraokeView *karaoke, dispatch_block_t then) {+ NSInteger focus = [[karaoke valueForKey:@"focus"] integerValue], target = focus + 3;+ NSArray<SGKaraokeLine *> *lines = [karaoke valueForKey:@"lines"];+ UIView *view = [karaoke valueForKey:@"shown"][@(target)];+ if (!view || target >= (NSInteger)lines.count) {+ expect(NO, @"a line three ahead of the sung one has a view to tap");+ then();+ return;+ }+ // HEAD's sources (build.sh old) seek and follow the song, as their tap did.+ if ([karaoke respondsToSelector:NSSelectorFromString(@"glideTo:")]) {+ ((void (*)(id, SEL, NSInteger))objc_msgSend)(karaoke, NSSelectorFromString(@"glideTo:"), lines[(NSUInteger)target].start);+ } else {+ SGKaraokeSeek(lines[(NSUInteger)target].start);+ ((void (*)(id, SEL))objc_msgSend)(karaoke, NSSelectorFromString(@"followSong"));+ }+ NSInteger goal = [[karaoke valueForKey:@"focus"] integerValue];+ expect(goal > focus && goal <= target && view.layer.animationKeys.count > 0,+ [NSString stringWithFormat:@"a tapped line is arranged around at once, and glides there (line %ld to %ld for %ld)", (long)focus, (long)goal, (long)target]);+ __block BOOL held = YES;+ for (int i = 1; i <= 5; i++) {+ after(i * 0.06, ^{ held = held && [[karaoke valueForKey:@"focus"] integerValue] == goal; });+ }+ after(0.8, ^{+ expect(held && [[karaoke valueForKey:@"focus"] integerValue] == goal,+ @"it stays arranged around it while the player is slow to report the seek, and after");+ then();+ });+}++// Scrolled by hand, the page's room grows (the controls go) and the scroll goes on up: the lines made for+// the view meanwhile keep the room between them that the lines already there have.+static void checkBrowse(SGRKaraokeView *karaoke, UIView *host, dispatch_block_t then) {+ UIScrollView *scroll = [karaoke valueForKey:@"scroll"];+ CGFloat gap = [[karaoke valueForKey:@"lineGap"] doubleValue];+ [(id<UIScrollViewDelegate>)karaoke scrollViewWillBeginDragging:scroll];+ CGRect frame = host.frame;+ host.frame = CGRectMake(frame.origin.x, frame.origin.y - 150, frame.size.width, frame.size.height + 300);+ [karaoke layoutIfNeeded];+ scroll.contentOffset = CGPointMake(0, -700);+ after(0.5, ^{+ CGFloat least, most;+ gapsBetween(karaoke, &least, &most);+ expect(least >= gap - 1 && least < CGFLOAT_MAX, [NSString stringWithFormat:@"scrolled by hand while the room grows, no two lines run into each other (least room %.1f, most %.1f, %lu views)",+ least, most, (unsigned long)shownLines(karaoke).count]);+ [karaoke setValue:@NO forKey:@"browsing"];+ scroll.contentOffset = CGPointZero;+ host.frame = frame;+ [karaoke layoutIfNeeded];+ after(0.6, then); // the lines around the sung one are made again, a few a frame+ });+}+static void runChecks(SGRKaraokeView *karaoke, UIView *host) {// Lines listed out of time order are estimated up to the line sung after it, and kept in time order.NSArray<SGKaraokeLine *> *estimated = SGKaraokeInTimeOrder(SGKaraokeEstimatedLines(@[@1000, @8000, @6000, @3000, @6000], @[@"a", @"b", @"c", @"d", @"e"]));@@ -295,9 +376,13 @@ static void runChecks(SGRKaraokeView *karaoke, UIView *host) {after([NSUserDefaults.standardUserDefaults doubleForKey:@"geminiDelay"] + 0.5, ^{expect(!extras.configuration.showsActivityIndicator, @"once it answers the spinner is gone");after(0.5, ^{- printf("%d failed\n", sg_failures);- fflush(stdout);- exit(sg_failures);+ checkBrowse(karaoke, host, ^{+ checkGlide(karaoke, ^{+ printf("%d failed\n", sg_failures);+ fflush(stdout);+ exit(sg_failures);+ });+ });});});}diff --git a/harness/lyrics/stubs.m b/harness/lyrics/stubs.mindex a82e97ac..af32308f 100644--- a/harness/lyrics/stubs.m+++ b/harness/lyrics/stubs.m@@ -84,4 +84,14 @@ NSInteger SGKaraokePositionMs(void) {return (NSInteger)at;}-void SGKaraokeSeek(NSInteger ms) { SGHarnessStartClock(ms, sg_rate, -1, 0); }+// -seekLag S: the player reports a seek S seconds after it is asked for, as Spotify's takes a few frames.+void SGKaraokeSeek(NSInteger ms) {+ double lag = [NSUserDefaults.standardUserDefaults doubleForKey:@"seekLag"];+ if (lag <= 0) {+ SGHarnessStartClock(ms, sg_rate, -1, 0);+ return;+ }+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(lag * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ SGHarnessStartClock(ms, sg_rate, -1, 0);+ });+}diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex d4e259f2..48b1ec86 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -48,6 +48,10 @@ static const CGFloat kGlowRadius = 9, kGlowOpacity = 0.85;static const NSTimeInterval kWholeFade = 0.35;static const double kWholeRiseMs = 900;static const double kClockSnapMs = 250, kClockPull = 0.08;+// After a tap seeks to a line, the clock holds at it until the player reports a position this near it,+// or for this long at the most: the player takes a few frames to report a seek.+static const double kSeekNearMs = 500;+static const CFTimeInterval kSeekWait = 1.5;// Lines get views this far outside the visible part, in screen heights: half a screen above it// and below, and a quarter more before a view is let go. Every view held is one more for the window// to take in and let go when the lyrics come up; a line comes into view about once in three seconds,@@ -1084,6 +1088,10 @@ typedef struct {@implementation SGRKaraokeView {UIScrollView *_scroll;BOOL _browsing;+ // The anchor and the open break as they were when the page was taken by hand, which the stack keeps to+ // until it follows the song again (topOfLine:).+ CGFloat _browseAnchor;+ NSInteger _browseBreak;CADisplayLink *_link;NSString *_track;NSArray<SGKaraokeLine *> *_lines;@@ -1126,6 +1134,8 @@ typedef struct {double _clock;NSInteger _reported;CFTimeInterval _clockTime;+ NSInteger _seekTo; // the line a tap seeked to, held by the clock until the player reports it+ CFTimeInterval _seekAt; // when, 0 while no seek is waitingBOOL _sweepsEstimates; // the Lyrics page's "Simulate word-by-word timing", read once like the creditBOOL _plain; // the song has no timing at all: every line lit, nothing follows the clockNSDictionary<NSNumber *, NSArray<SGLyricsMeaning *> *> *_meanings; // Genius's, by line@@ -1199,7 +1209,7 @@ typedef struct {if (!CGRectContainsPoint(CGRectInset(view.frame, -_margin, -_lineGap / 2), point)) continue;SGKaraokeSeek(view.line.start);SGPlayFeedback(SGFeedbackSkip);- [self followSong];+ [self glideTo:view.line.start];return;}}@@ -1243,6 +1253,10 @@ typedef struct {// While the user browses, placement stands still and every line is sharp.- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];+ if (!_browsing) {+ _browseAnchor = self.bounds.size.height * kAnchor;+ _browseBreak = _openBreak;+ }_browsing = YES;if (self.browsingBegan) self.browsingBegan();for (SGRKaraokeLineView *view in _shown.allValues) view.blur = 0;@@ -1283,6 +1297,26 @@ typedef struct {[self placeLinesAnimated:YES];}+// A tapped line glides to the anchor at once, and the stack with it: the clock is held at the line until+// the player reports the seek, which takes a few frames. Read before then, the position still on the line+// being sung put the stack back on it, and the seek's arrival then jumped it over to the tapped one.+- (void)glideTo:(NSInteger)ms {+ _seekTo = ms;+ _seekAt = CACurrentMediaTime();+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(followSong) object:nil];+ if (_browsing) {+ _browsing = NO;+ [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:0+ options:UIViewAnimationOptionAllowUserInteraction+ animations:^{ self->_scroll.contentOffset = CGPointZero; } completion:nil];+ }+ if (!_tops) return;+ NSUInteger arrangement = _arrangement;+ [self arrangeAt:[self clockMs] glide:YES];+ // The line sung tapped again, off a page scrolled by hand: nothing moved on, so the stack is put back.+ if (_arrangement == arrangement) [self placeLinesAnimated:YES];+}+- (void)didMoveToWindow {[super didMoveToWindow];if (!self.window) {@@ -1588,9 +1622,16 @@ static BOOL hasWords(SGKaraokeLine *line) {// Where a line starts on the page, for the stack as it is arranged now: an open break holds the room// of one row at the anchor, and the lines from the one after it on are moved down by it.+//+// While the page is scrolled by hand the stack stands still, and so does what places it: the room can+// change meanwhile (the player's controls go as a scroll starts, and the anchor moves with the height)+// and a break can open or close, and a line made for the view then went by the new anchor while the+// lines beside it kept the old one, the two running into each other.- (CGFloat)topOfLine:(NSInteger)index {- CGFloat top = self.bounds.size.height * kAnchor + _tops[index].doubleValue - _focusTop;- return _openBreak >= 0 && index >= _openBreak ? top + [self breakRoom] : top;+ CGFloat anchor = _browsing ? _browseAnchor : self.bounds.size.height * kAnchor;+ NSInteger openBreak = _browsing ? _browseBreak : _openBreak;+ CGFloat top = anchor + _tops[index].doubleValue - _focusTop;+ return openBreak >= 0 && index >= openBreak ? top + [self breakRoom] : top;}- (CGFloat)breakRoom {@@ -1775,7 +1816,18 @@ static BOOL hasWords(SGKaraokeLine *line) {// corrections. A seek or a new track is too far off to ease and is taken at once.- (double)clockMs {NSInteger raw = SGKaraokePositionMs();- CFTimeInterval shown = _link.targetTimestamp;+ CFTimeInterval shown = _link ? _link.targetTimestamp : CACurrentMediaTime();+ if (_seekAt) {+ // A paused player never moves, so the hold ends by the position or the time, not by motion.+ BOOL reported = raw >= 0 && labs(raw - _seekTo) <= kSeekNearMs;+ if (!reported && CACurrentMediaTime() - _seekAt < kSeekWait) {+ _clock = _seekTo;+ _reported = raw;+ _clockTime = shown;+ return _clock;+ }+ _seekAt = 0;+ }BOOL running = raw != _reported; // a paused player reports the same position every frameif (running) _stillSince = 0;else if (!_stillSince) _stillSince = shown;@@ -1845,6 +1897,14 @@ static BOOL hasWords(SGKaraokeLine *line) {}double now = [self clockMs];+ [self arrangeAt:now glide:NO];+ for (NSUInteger i = 0; i < _sungCount; i++) [_shown[@(_sung[i])] showTime:now];+ if (_dots.superview) [_dots showTime:now running:!_stillSince || _link.targetTimestamp - _stillSince < kStillFor at:_link.targetTimestamp];+}++// What is sung at `now` lit and the stack arranged around it; a jump of more than a couple of lines is+// placed at once, unless it is a tapped line gliding over.+- (void)arrangeAt:(double)now glide:(BOOL)glide {NSInteger sung[kMostSung], focus, openBreak;NSUInteger count = [self sungAt:now into:sung focus:&focus openBreak:&openBreak];if (focus != _focus || openBreak != _openBreak || count != _sungCount || memcmp(sung, _sung, count * sizeof(NSInteger))) {@@ -1864,12 +1924,10 @@ static BOOL hasWords(SGKaraokeLine *line) {_openBreak = openBreak;_arrangement++;if (openBreak < 0) [_dots removeFromSuperview];- [self placeLinesAnimated:!jump];+ [self placeLinesAnimated:glide || !jump];} else {[self showLinesInSight]; // the page may be scrolling by hand, or springing back}- for (NSUInteger i = 0; i < _sungCount; i++) [_shown[@(_sung[i])] showTime:now];- if (_dots.superview) [_dots showTime:now running:!_stillSince || _link.targetTimestamp - _stillSince < kStillFor at:_link.targetTimestamp];}// What is sung at `now`: each line from its start until it is sung out, so two voices over each