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 Live Activity carries the cover's color, progress and translated lyrics

My-Name-Is-Jeffc60198ec755a
diff --git a/extension/LiveActivity/LiveActivityWidget.swift b/extension/LiveActivity/LiveActivityWidget.swift
index 3b1ea3ae..01a23fc4 100644
--- a/extension/LiveActivity/LiveActivityWidget.swift
+++ b/extension/LiveActivity/LiveActivityWidget.swift
@@ -22,6 +22,13 @@ struct SGLiveActivityBundle: WidgetBundle {
}
}
+// The cover's colour, RRGGBB, as the card's background; the old translucent black without one.
+private func tint(_ state: State) -> Color {
+ guard let hex = state.tint, hex.count == 6, let value = Int(hex, radix: 16) else { return Color.black.opacity(0.75) }
+ return Color(red: Double((value >> 16) & 0xFF) / 255, green: Double((value >> 8) & 0xFF) / 255,
+ blue: Double(value & 0xFF) / 255)
+}
+
extension SGLyricsAttributes.Tab {
var symbol: String {
switch self {
@@ -43,12 +50,9 @@ extension SGLyricsAttributes.Tab {
struct SGLyricsLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: SGLyricsAttributes.self) { context in
- ContentView(state: context.state, upNext: 4)
- .frame(maxWidth: .infinity, alignment: .leading)
- .padding(.horizontal, context.state.view == .panel ? 12 : 16)
- .padding(.vertical, 12)
+ FamilyView(state: context.state)
.foregroundStyle(.white)
- .activityBackgroundTint(Color.black.opacity(0.75))
+ .activityBackgroundTint(tint(context.state))
.activitySystemActionForegroundColor(.white)
.widgetURL(URL(string: "spotify:"))
} dynamicIsland: { context in
@@ -74,8 +78,7 @@ struct SGLyricsLiveActivity: Widget {
.foregroundStyle(green)
.frame(maxWidth: 44)
} else {
- Image(systemName: context.state.paused ? "pause.fill" : "waveform")
- .foregroundStyle(green)
+ ProgressRing(state: context.state)
}
} minimal: {
Image(systemName: "music.note")
@@ -86,6 +89,64 @@ struct SGLyricsLiveActivity: Widget {
}
}
+// The lock screen's card, and StandBy's: what the view shows, and the bar under it.
+private struct FamilyView: View {
+ let state: State
+
+ var body: some View {
+ VStack(alignment: .leading, spacing: 10) {
+ ContentView(state: state, upNext: 4)
+ ProgressBar(state: state)
+ }
+ .frame(maxWidth: .infinity, alignment: .leading)
+ .padding(.horizontal, state.view == .panel ? 12 : 16)
+ .padding(.vertical, 12)
+ }
+}
+
+// The track's progress as a ring, for the compact Dynamic Island, which Apple Watch and CarPlay show too.
+private struct ProgressRing: View {
+ let state: State
+
+ var body: some View {
+ if let start = state.trackStart, let end = state.trackEnd, end > start {
+ Group {
+ if state.paused, let at = state.pausedAt {
+ ProgressView(value: at)
+ } else {
+ ProgressView(timerInterval: start...end, countsDown: false) { EmptyView() } currentValueLabel: { EmptyView() }
+ }
+ }
+ .progressViewStyle(.circular)
+ .tint(green)
+ .frame(width: 18, height: 18)
+ } else {
+ Image(systemName: state.paused ? "pause.fill" : "waveform")
+ .foregroundStyle(green)
+ }
+ }
+}
+
+// The track's progress, running by itself between two dates while it plays and held while it is paused.
+private struct ProgressBar: View {
+ let state: State
+
+ var body: some View {
+ if let start = state.trackStart, let end = state.trackEnd, end > start {
+ Group {
+ if state.paused, let at = state.pausedAt {
+ ProgressView(value: at)
+ } else {
+ ProgressView(timerInterval: start...end, countsDown: false) { EmptyView() } currentValueLabel: { EmptyView() }
+ }
+ }
+ .progressViewStyle(.linear)
+ .tint(.white)
+ .frame(height: 4)
+ }
+ }
+}
+
private struct ContentView: View {
let state: State
let upNext: Int
@@ -105,12 +166,23 @@ private struct LyricsView: View {
let state: State
var body: some View {
- VStack(alignment: .leading, spacing: 4) {
+ VStack(alignment: .center, spacing: 4) {
Text(state.line)
.font(.title3.weight(.bold))
.lineLimit(2)
- .minimumScaleFactor(0.8)
+ .minimumScaleFactor(0.7)
+ .multilineTextAlignment(.center)
+ .frame(maxWidth: .infinity)
.direction(of: state.line)
+ if let translation = state.translation, !translation.isEmpty {
+ Text(translation)
+ .font(.subheadline.weight(.medium))
+ .foregroundStyle(.white.opacity(0.7))
+ .lineLimit(2)
+ .multilineTextAlignment(.center)
+ .frame(maxWidth: .infinity)
+ .direction(of: translation)
+ }
if !state.nextLine.isEmpty {
Text(state.nextLine)
.font(.subheadline.weight(.semibold))
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
index 4e1eb6d8..6b330f2d 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
@@ -5,6 +5,7 @@
// The activity is started only while the app is in front, the one place ActivityKit allows it.
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
+#import <MediaPlayer/MediaPlayer.h>
#import "Core/SGCore.h"
#import "Headers/SPTPlayer.h"
#import "Shared/Lyrics/Lyrics.h"
@@ -16,7 +17,9 @@ API_AVAILABLE(ios(17.0))
+ (void)showWithView:(NSInteger)view paused:(BOOL)paused line:(NSString *)line nextLine:(NSString *)nextLine
titles:(NSArray<NSString *> *)titles artists:(NSArray<NSString *> *)artists uris:(NSArray<NSString *> *)uris
tab:(NSInteger)tab title:(NSString *)title artist:(NSString *)artist shuffle:(BOOL)shuffle repeatMode:(NSInteger)repeatMode
- timerEnd:(NSDate *)timerEnd timerEndOfTrack:(BOOL)timerEndOfTrack;
+ timerEnd:(NSDate *)timerEnd timerEndOfTrack:(BOOL)timerEndOfTrack
+ tint:(NSString *)tint trackStart:(NSDate *)trackStart trackEnd:(NSDate *)trackEnd pausedAt:(NSNumber *)pausedAt
+ translation:(NSString *)translation;
+ (void)end;
@end
@@ -103,7 +106,7 @@ static NSInteger repeatModeOf(SPTPlayerOptions *options) {
// The line being sung and the one after it; before the first line, between lines and without lyrics
// at all, a note holds the place.
-static NSString *lyricsLine(NSString *trackID, NSString **next) {
+static NSString *lyricsLine(NSString *trackID, NSString **next, NSString **translation) {
NSArray<SGKaraokeLine *> *lines = SGKaraokeLinesForTrack(trackID);
if (!lines) {
SGKaraokeRequestLyrics(trackID);
@@ -120,10 +123,34 @@ static NSString *lyricsLine(NSString *trackID, NSString **next) {
*next = index + 1 < (NSInteger)lines.count ? SGKaraokeLineText(lines[index + 1]) : @"";
if (index < 0) return @"♪";
SGKaraokeLine *current = lines[index];
+ *translation = current.translation;
BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > kBreakMs;
return position > current.end + kBreakMs && nextFarOff ? @"♪" : SGKaraokeLineText(current);
}
+// The cover's colour for the card: Spotify's now playing artwork drawn into one pixel, darkened so white
+// text keeps its contrast. Read once a track.
+static NSString *sg_tintTrack, *sg_tint;
+
+static NSString *tintFor(NSString *trackID) {
+ if (!trackID || [trackID isEqualToString:sg_tintTrack]) return sg_tint;
+ MPMediaItemArtwork *artwork = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo[MPMediaItemPropertyArtwork];
+ UIImage *image = [artwork isKindOfClass:MPMediaItemArtwork.class] ? [artwork imageWithSize:CGSizeMake(32, 32)] : nil;
+ if (!image.CGImage) return sg_tint;
+ unsigned char pixel[4] = {0};
+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(space);
+ if (!context) return sg_tint;
+ CGContextSetInterpolationQuality(context, kCGInterpolationMedium);
+ CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage);
+ CGContextRelease(context);
+ const double keep = 0.55; // of the colour, the rest black
+ sg_tintTrack = trackID;
+ sg_tint = [NSString stringWithFormat:@"%02X%02X%02X", (int)(pixel[0] * keep), (int)(pixel[1] * keep), (int)(pixel[2] * keep)];
+ return sg_tint;
+}
+
static void clearSleepTimer(void) {
sg_sleepEnd = nil;
sg_sleepTrack = nil;
@@ -159,8 +186,23 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {
BOOL paused = state.isPaused;
NSTimeInterval every = paused ? kPausedTick : kTick;
if (sg_timer && sg_tickEvery != every) startTimer(every);
- NSString *line = @"", *next = @"";
- if (view == SGLiveActivityLyrics) line = lyricsLine(trackID, &next);
+ NSString *line = @"", *next = @"", *translation = nil;
+ if (view == SGLiveActivityLyrics) line = lyricsLine(trackID, &next, &translation);
+ // The redesign's switch for translations (SGRKeyLyricsTranslation, Redesigned/Lyrics/LyricsText.h).
+ if (!SGFlag(@"spotifyglass.redesign.lyricsTranslation", NO)) translation = nil;
+ NSString *tint = tintFor(trackID);
+ // The bar runs by itself from where the track started; paused, it holds at the share played.
+ double duration = [state respondsToSelector:@selector(duration)] ? state.duration : 0;
+ NSInteger position = SGKaraokePositionMs();
+ NSDate *trackStart = nil, *trackEnd = nil;
+ NSNumber *pausedAt = nil;
+ if (duration > 0 && position >= 0) {
+ double played = position / 1000.0;
+ trackStart = [NSDate dateWithTimeIntervalSinceNow:-played];
+ trackStart = [NSDate dateWithTimeIntervalSince1970:round(trackStart.timeIntervalSince1970)];
+ trackEnd = [trackStart dateByAddingTimeInterval:duration];
+ if (paused) pausedAt = @(MIN(1, played / duration));
+ }
NSMutableArray<NSString *> *titles = [NSMutableArray array], *artists = [NSMutableArray array], *uris = [NSMutableArray array];
if (view != SGLiveActivityLyrics) {
@@ -183,12 +225,14 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {
NSMutableArray<NSString *> *parts = [NSMutableArray arrayWithObjects:@(view).stringValue, paused ? @"1" : @"0", line, next,
@(tab).stringValue, title, artist, shuffle ? @"1" : @"0", @(repeatMode).stringValue,
- @((long long)sleepEnd.timeIntervalSince1970).stringValue, endOfTrack ? @"1" : @"0", nil];
+ @((long long)sleepEnd.timeIntervalSince1970).stringValue, endOfTrack ? @"1" : @"0", tint ?: @"", translation ?: @"",
+ @((long long)trackStart.timeIntervalSince1970).stringValue, paused ? @(round(pausedAt.doubleValue * 100)).stringValue : @"", nil];
for (NSUInteger i = 0; i < titles.count; i++) [parts addObject:[NSString stringWithFormat:@"%@\t%@\t%@", titles[i], artists[i], uris[i]]];
send([parts componentsJoinedByString:@"\n"], ^{
[SGLiveActivityBridge showWithView:view paused:paused line:line nextLine:next titles:titles artists:artists uris:uris
tab:tab title:title artist:artist shuffle:shuffle repeatMode:repeatMode
- timerEnd:sleepEnd timerEndOfTrack:endOfTrack];
+ timerEnd:sleepEnd timerEndOfTrack:endOfTrack
+ tint:tint trackStart:trackStart trackEnd:trackEnd pausedAt:pausedAt translation:translation];
});
}
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
index 3ed7ac0c..2625875c 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
@@ -67,7 +67,8 @@ public final class SGLiveActivityBridge: NSObject {
@objc public static func show(view: Int, paused: Bool, line: String, nextLine: String,
titles: [String], artists: [String], uris: [String],
tab: Int, title: String, artist: String, shuffle: Bool, repeatMode: Int,
- timerEnd: Date?, timerEndOfTrack: Bool) {
+ timerEnd: Date?, timerEndOfTrack: Bool,
+ tint: String?, trackStart: Date?, trackEnd: Date?, pausedAt: NSNumber?, translation: String?) {
let tracks = titles.indices.map {
SGLyricsAttributes.Track(title: titles[$0], artist: artists[$0], uri: uris[$0])
}
@@ -75,7 +76,9 @@ public final class SGLiveActivityBridge: NSObject {
view: SGLyricsAttributes.View(rawValue: view) ?? .lyrics, paused: paused,
line: line, nextLine: nextLine, tracks: tracks,
tab: SGLyricsAttributes.Tab(rawValue: tab) ?? .controls, title: title, artist: artist,
- shuffle: shuffle, repeatMode: repeatMode, timerEnd: timerEnd, timerEndOfTrack: timerEndOfTrack)
+ shuffle: shuffle, repeatMode: repeatMode, timerEnd: timerEnd, timerEndOfTrack: timerEndOfTrack,
+ tint: tint, trackStart: trackStart, trackEnd: trackEnd, pausedAt: pausedAt?.doubleValue,
+ translation: translation)
let content = ActivityContent(state: state, staleDate: nil)
if let activity = current {
queue(.update(activity.id, content))
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
index 94548ff6..9ea49aac 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
@@ -39,6 +39,15 @@ struct SGLyricsAttributes: ActivityAttributes {
var repeatMode: Int // 0 off, 1 the playlist or album, 2 the track
var timerEnd: Date? // the sleep timer's end, nil when none is set
var timerEndOfTrack: Bool
+ // The cover's colour as RRGGBB, darkened for white text; nil before it is read.
+ var tint: String?
+ // When the track started and ends at the speed it plays, for a progress bar that runs by itself;
+ // nil when the length is not known. Paused, the bar holds at `pausedAt`.
+ var trackStart: Date?
+ var trackEnd: Date?
+ var pausedAt: Double?
+ // The line's translation, when the lyrics have one and translations are shown.
+ var translation: String?
}
}