mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
new version
This commit is contained in:
parent
cc09f17074
commit
c8a6c2b1fa
10 changed files with 1531 additions and 187 deletions
|
|
@ -33289,6 +33289,9 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Replaces the artwork tile with the live canvas when the current app provides one, and reuses that moving canvas for the surrounding lighting effect." : {
|
||||
|
||||
},
|
||||
"Request Access" : {
|
||||
"localizations" : {
|
||||
|
|
@ -34267,7 +34270,7 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. The swap is temporary and leaves no trace in System Settings." : {
|
||||
"Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. If a canvas is available, Atoll can also keep the same album art + player layout on top of the live canvas." : {
|
||||
|
||||
},
|
||||
"Ring" : {
|
||||
|
|
@ -37328,6 +37331,9 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Show live canvas in Dynamic Island" : {
|
||||
|
||||
},
|
||||
"Show location label" : {
|
||||
"localizations" : {
|
||||
|
|
@ -43392,6 +43398,9 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Use album art layout over fullscreen canvas" : {
|
||||
|
||||
},
|
||||
"Use circular battery indicator" : {
|
||||
"localizations" : {
|
||||
|
|
|
|||
|
|
@ -166,9 +166,12 @@ class SpotifyController: MediaControllerProtocol {
|
|||
state.artwork = existingArtwork
|
||||
}
|
||||
|
||||
if trackURI == currentCanvasTrackURI {
|
||||
let isSameCanvasTrack = trackURI == currentCanvasTrackURI
|
||||
|
||||
if isSameCanvasTrack {
|
||||
state.liveArtworkURL = playbackState.liveArtworkURL
|
||||
} else {
|
||||
state.liveArtworkURL = nil
|
||||
currentCanvasTrackURI = trackURI.isEmpty ? nil : trackURI
|
||||
canvasFetchTask?.cancel()
|
||||
canvasFetchTask = nil
|
||||
|
|
@ -185,6 +188,7 @@ class SpotifyController: MediaControllerProtocol {
|
|||
artworkFetchTask?.cancel()
|
||||
|
||||
let currentState = state
|
||||
let expectedTrackURI = trackURI
|
||||
|
||||
artworkFetchTask = Task {
|
||||
do {
|
||||
|
|
@ -192,6 +196,10 @@ class SpotifyController: MediaControllerProtocol {
|
|||
|
||||
await MainActor.run { [weak self] in
|
||||
guard let self = self else { return }
|
||||
guard expectedTrackURI.isEmpty || self.currentCanvasTrackURI == expectedTrackURI else {
|
||||
self.artworkFetchTask = nil
|
||||
return
|
||||
}
|
||||
var updatedState = currentState
|
||||
updatedState.artwork = data
|
||||
updatedState.liveArtworkURL = self.playbackState.liveArtworkURL
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ struct LockScreenMusicPanel: View {
|
|||
|
||||
@ObservedObject var musicManager = MusicManager.shared
|
||||
@ObservedObject private var routeManager = AudioRouteManager.shared
|
||||
@ObservedObject private var fullscreenArtworkManager = FullScreenArtworkWindowManager.shared
|
||||
@StateObject private var volumeModel = MediaOutputVolumeViewModel()
|
||||
@ObservedObject private var airPlayManager = AppleMusicAirPlayManager.shared
|
||||
@ObservedObject private var animator: LockScreenPanelAnimator
|
||||
|
|
@ -123,6 +124,22 @@ struct LockScreenMusicPanel: View {
|
|||
usesCustomLiquidGlass && useEnhancedLiquidBorder
|
||||
}
|
||||
|
||||
private var hidesInlineArtworkForSpotifyCanvasFallback: Bool {
|
||||
fullscreenArtworkManager.isShowingSpotifyCanvasFallback
|
||||
}
|
||||
|
||||
private var usesSpotifyCanvasFallbackContentPresentation: Bool {
|
||||
fullscreenArtworkManager.isShowingSpotifyCanvasFallback
|
||||
}
|
||||
|
||||
private var showsDetachedFullscreenLyrics: Bool {
|
||||
usesSpotifyCanvasFallbackContentPresentation && enableLyrics
|
||||
}
|
||||
|
||||
private var shouldShowInlineLyrics: Bool {
|
||||
enableLyrics && !showsDetachedFullscreenLyrics
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if isActive && musicManager.hasActiveSession {
|
||||
panelContent
|
||||
|
|
@ -140,12 +157,17 @@ struct LockScreenMusicPanel: View {
|
|||
.frame(width: currentSize.width, height: currentSize.height, alignment: .topLeading)
|
||||
.clipShape(RoundedRectangle(cornerRadius: panelCornerRadius, style: .continuous))
|
||||
.overlay {
|
||||
if showPanelBorder {
|
||||
if showPanelBorder && !usesSpotifyCanvasFallbackContentPresentation {
|
||||
panelBorderOverlay
|
||||
}
|
||||
}
|
||||
.shadow(color: Color.black.opacity(0.3), radius: 20, x: 0, y: 10)
|
||||
.contentShape(RoundedRectangle(cornerRadius: panelCornerRadius, style: .continuous))
|
||||
.shadow(
|
||||
color: usesSpotifyCanvasFallbackContentPresentation ? .clear : Color.black.opacity(0.3),
|
||||
radius: usesSpotifyCanvasFallbackContentPresentation ? 0 : 20,
|
||||
x: 0,
|
||||
y: usesSpotifyCanvasFallbackContentPresentation ? 0 : 10
|
||||
)
|
||||
.contentShape(Rectangle())
|
||||
.animation(.easeInOut(duration: 0.28), value: isExpanded)
|
||||
.animation(.easeInOut(duration: 0.24), value: shouldShowVolumeSlider)
|
||||
.onAppear {
|
||||
|
|
@ -231,9 +253,13 @@ struct LockScreenMusicPanel: View {
|
|||
collapsedLayout
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, isExpanded ? 24 : 20)
|
||||
.padding(.vertical, isExpanded ? 22 : 16)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
.padding(.horizontal, usesSpotifyCanvasFallbackContentPresentation ? 8 : (isExpanded ? 24 : 20))
|
||||
.padding(.vertical, usesSpotifyCanvasFallbackContentPresentation ? 0 : (isExpanded ? 22 : 16))
|
||||
.frame(
|
||||
maxWidth: .infinity,
|
||||
maxHeight: .infinity,
|
||||
alignment: usesSpotifyCanvasFallbackContentPresentation ? .center : .topLeading
|
||||
)
|
||||
}
|
||||
|
||||
private var collapsedLayout: some View {
|
||||
|
|
@ -248,25 +274,40 @@ struct LockScreenMusicPanel: View {
|
|||
}
|
||||
|
||||
private var expandedLayout: some View {
|
||||
HStack(alignment: .center, spacing: expandedContentSpacing) {
|
||||
albumArtButton(size: 230, cornerRadius: expandedAlbumArtCornerRadius)
|
||||
.frame(width: 230, height: 230)
|
||||
Group {
|
||||
if hidesInlineArtworkForSpotifyCanvasFallback {
|
||||
VStack(alignment: .leading, spacing: 20) {
|
||||
expandedHeader
|
||||
progressBar
|
||||
.padding(.top, 10)
|
||||
.frame(maxWidth: .infinity)
|
||||
playbackControls(alignment: .leading)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
} else {
|
||||
HStack(alignment: .center, spacing: expandedContentSpacing) {
|
||||
albumArtButton(size: 230, cornerRadius: expandedAlbumArtCornerRadius)
|
||||
.frame(width: 230, height: 230)
|
||||
|
||||
VStack(alignment: .leading, spacing: 20) {
|
||||
expandedHeader
|
||||
progressBar
|
||||
.padding(.top, 10)
|
||||
.frame(maxWidth: .infinity)
|
||||
playbackControls(alignment: .leading)
|
||||
VStack(alignment: .leading, spacing: 20) {
|
||||
expandedHeader
|
||||
progressBar
|
||||
.padding(.top, 10)
|
||||
.frame(maxWidth: .infinity)
|
||||
playbackControls(alignment: .leading)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.frame(maxHeight: .infinity, alignment: .center)
|
||||
}
|
||||
|
||||
private var collapsedHeader: some View {
|
||||
HStack(alignment: .center, spacing: 16) {
|
||||
albumArtButton(size: 60, cornerRadius: collapsedAlbumArtCornerRadius)
|
||||
HStack(alignment: .center, spacing: hidesInlineArtworkForSpotifyCanvasFallback ? 10 : 16) {
|
||||
if !hidesInlineArtworkForSpotifyCanvasFallback {
|
||||
albumArtButton(size: 60, cornerRadius: collapsedAlbumArtCornerRadius)
|
||||
}
|
||||
|
||||
GeometryReader { geo in
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
|
|
@ -338,9 +379,11 @@ struct LockScreenMusicPanel: View {
|
|||
}
|
||||
|
||||
private func albumArtButton(size: CGFloat, cornerRadius: CGFloat) -> some View {
|
||||
ZStack(alignment: .bottomTrailing) {
|
||||
let artworkCornerRadius = resolvedArtworkCornerRadius(from: cornerRadius)
|
||||
|
||||
return ZStack(alignment: .bottomTrailing) {
|
||||
if isArtworkFullscreen && !keepAlbumArtVisibleDuringFullscreenArtwork {
|
||||
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
|
||||
RoundedRectangle(cornerRadius: artworkCornerRadius, style: .continuous)
|
||||
.fill(Color.white.opacity(0.06))
|
||||
.overlay(
|
||||
Image(systemName: "photo.fill")
|
||||
|
|
@ -366,8 +409,8 @@ struct LockScreenMusicPanel: View {
|
|||
suspended: isParallaxSuspended
|
||||
)
|
||||
.frame(width: size)
|
||||
.background(albumArtBackground(cornerRadius: cornerRadius))
|
||||
.clipShape(RoundedRectangle(cornerRadius: musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? appIconCornerRadius/3 : appIconCornerRadius, style: .continuous))
|
||||
.background(albumArtBackground(cornerRadius: artworkCornerRadius))
|
||||
.clipShape(RoundedRectangle(cornerRadius: artworkCornerRadius, style: .continuous))
|
||||
.opacity(musicManager.isPlaying ? 1 : 0.4)
|
||||
.scaleEffect(musicManager.isPlaying ? 1 : 0.85)
|
||||
.animation(.easeInOut(duration: 0.2), value: musicManager.isPlaying)
|
||||
|
|
@ -544,7 +587,7 @@ struct LockScreenMusicPanel: View {
|
|||
|
||||
private func playbackControls(alignment: Alignment) -> some View {
|
||||
let spacing: CGFloat = isExpanded ? 24 : 20
|
||||
let verticalSpacing: CGFloat = (shouldShowVolumeSlider || enableLyrics) ? 14 : 10
|
||||
let verticalSpacing: CGFloat = (shouldShowVolumeSlider || shouldShowInlineLyrics) ? 14 : 10
|
||||
|
||||
return VStack(spacing: verticalSpacing) {
|
||||
controlsRow(alignment: alignment, spacing: spacing)
|
||||
|
|
@ -565,7 +608,7 @@ struct LockScreenMusicPanel: View {
|
|||
}
|
||||
}
|
||||
|
||||
if enableLyrics {
|
||||
if shouldShowInlineLyrics {
|
||||
lyricsSection(alignment: alignment)
|
||||
.transition(.move(edge: .top).combined(with: .opacity))
|
||||
}
|
||||
|
|
@ -1009,7 +1052,7 @@ struct LockScreenMusicPanel: View {
|
|||
}
|
||||
|
||||
private var lyricsExtraHeight: CGFloat {
|
||||
lyricsHeight(forExpanded: isExpanded, enabled: enableLyrics)
|
||||
lyricsHeight(forExpanded: isExpanded, enabled: shouldShowInlineLyrics)
|
||||
}
|
||||
|
||||
private var accessorySectionExtraHeight: CGFloat {
|
||||
|
|
@ -1039,7 +1082,7 @@ struct LockScreenMusicPanel: View {
|
|||
private func panelAdditionalHeight(forExpanded expanded: Bool) -> CGFloat {
|
||||
sliderHeight(forExpanded: expanded, visible: shouldShowVolumeSlider) +
|
||||
accessorySectionExtraHeight +
|
||||
lyricsHeight(forExpanded: expanded, enabled: enableLyrics)
|
||||
lyricsHeight(forExpanded: expanded, enabled: shouldShowInlineLyrics)
|
||||
}
|
||||
|
||||
private func updatePanelSize(animated: Bool = true) {
|
||||
|
|
@ -1094,7 +1137,11 @@ struct LockScreenMusicPanel: View {
|
|||
|
||||
@ViewBuilder
|
||||
private var panelBackgroundLayer: some View {
|
||||
if usesCustomLiquidGlass {
|
||||
if usesSpotifyCanvasFallbackContentPresentation {
|
||||
Color.clear
|
||||
.allowsHitTesting(false)
|
||||
.accessibilityHidden(true)
|
||||
} else if usesCustomLiquidGlass {
|
||||
customLiquidPanelBackdrop
|
||||
} else if usesStandardLiquidGlass {
|
||||
standardLiquidPanelBackdrop
|
||||
|
|
@ -1192,7 +1239,8 @@ struct LockScreenMusicPanel: View {
|
|||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.clipShape(RoundedRectangle(cornerRadius: musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? cornerRadius/3 : cornerRadius))
|
||||
.compositingGroup()
|
||||
.clipShape(RoundedRectangle(cornerRadius: resolvedArtworkCornerRadius(from: cornerRadius), style: .continuous))
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
|
|
@ -1230,6 +1278,13 @@ struct LockScreenMusicPanel: View {
|
|||
isExpanded ? 18 : 12
|
||||
}
|
||||
|
||||
private func resolvedArtworkCornerRadius(from baseCornerRadius: CGFloat) -> CGFloat {
|
||||
let aspectRatio = musicManager.albumArt.size.height > 0
|
||||
? musicManager.albumArt.size.width / musicManager.albumArt.size.height
|
||||
: 1
|
||||
return aspectRatio > 1.0 ? baseCornerRadius / 3 : baseCornerRadius
|
||||
}
|
||||
|
||||
@available(macOS 26.0, *)
|
||||
private func clearLiquidGlassSurface(cornerRadius: CGFloat) -> some View {
|
||||
RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
|
||||
|
|
@ -1480,9 +1535,48 @@ private extension View {
|
|||
|
||||
final class RightClickNSView: NSView {
|
||||
var onRightClick: (() -> Void)?
|
||||
private var eventMonitor: Any?
|
||||
|
||||
override func rightMouseDown(with event: NSEvent) {
|
||||
onRightClick?()
|
||||
deinit {
|
||||
removeEventMonitor()
|
||||
}
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
|
||||
if window == nil {
|
||||
removeEventMonitor()
|
||||
} else {
|
||||
installEventMonitorIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
override func hitTest(_ point: NSPoint) -> NSView? {
|
||||
nil
|
||||
}
|
||||
|
||||
private func installEventMonitorIfNeeded() {
|
||||
guard eventMonitor == nil else { return }
|
||||
|
||||
eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.rightMouseDown]) { [weak self] event in
|
||||
guard let self, let window = self.window, event.window === window else {
|
||||
return event
|
||||
}
|
||||
|
||||
let pointInView = self.convert(event.locationInWindow, from: nil)
|
||||
guard self.bounds.contains(pointInView) else {
|
||||
return event
|
||||
}
|
||||
|
||||
self.onRightClick?()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func removeEventMonitor() {
|
||||
guard let eventMonitor else { return }
|
||||
NSEvent.removeMonitor(eventMonitor)
|
||||
self.eventMonitor = nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1113,8 +1113,17 @@ private struct MinimalisticReminderDetailsView: View {
|
|||
struct MinimalisticAlbumArtView: View {
|
||||
@ObservedObject var musicManager = MusicManager.shared
|
||||
@ObservedObject var vm: DynamicIslandViewModel
|
||||
@Default(.showLiveCanvasInDynamicIsland) private var showLiveCanvasInDynamicIsland
|
||||
let albumArtNamespace: Namespace.ID
|
||||
|
||||
private var usesLiveCanvasArtwork: Bool {
|
||||
showLiveCanvasInDynamicIsland && musicManager.videoArtworkURL != nil
|
||||
}
|
||||
|
||||
private var albumArtCornerRadius: CGFloat {
|
||||
musicManager.albumArt.size.width / musicManager.albumArt.size.height > 1.0 ? 4 : 12
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack(alignment: .bottomTrailing) {
|
||||
if Defaults[.lightingEffect] {
|
||||
|
|
@ -1128,16 +1137,27 @@ struct MinimalisticAlbumArtView: View {
|
|||
Color.clear
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.background(
|
||||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
DynamicIslandArtworkSourceView(
|
||||
cornerRadius: albumArtCornerRadius,
|
||||
contentMode: .fill
|
||||
)
|
||||
)
|
||||
.clipped()
|
||||
.clipShape(RoundedRectangle(cornerRadius: 12))
|
||||
.clipShape(RoundedRectangle(cornerRadius: albumArtCornerRadius))
|
||||
.scaleEffect(x: 1.3, y: 1.4)
|
||||
.rotationEffect(.degrees(92))
|
||||
.blur(radius: 35)
|
||||
.opacity(min(0.6, 1 - max(musicManager.albumArt.getBrightness(), 0.3)))
|
||||
.opacity(
|
||||
usesLiveCanvasArtwork
|
||||
? (musicManager.isPlaying ? 0.68 : 0.22)
|
||||
: min(0.6, 1 - max(musicManager.albumArt.getBrightness(), 0.3))
|
||||
)
|
||||
.shadow(
|
||||
color: Color(nsColor: musicManager.avgColor).opacity(usesLiveCanvasArtwork ? 0.28 : 0.16),
|
||||
radius: usesLiveCanvasArtwork ? 18 : 12,
|
||||
x: 0,
|
||||
y: 0
|
||||
)
|
||||
}
|
||||
|
||||
private var albumArtButton: some View {
|
||||
|
|
@ -1147,13 +1167,10 @@ struct MinimalisticAlbumArtView: View {
|
|||
Color.clear
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.background(
|
||||
|
||||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.clipShape(RoundedRectangle(cornerRadius: musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? 4 : 12))
|
||||
|
||||
|
||||
DynamicIslandArtworkSourceView(
|
||||
cornerRadius: albumArtCornerRadius,
|
||||
contentMode: .fit
|
||||
)
|
||||
)
|
||||
.clipped()
|
||||
.matchedGeometryEffect(id: "albumArt", in: albumArtNamespace)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,112 @@
|
|||
import Combine
|
||||
import Defaults
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
import AVFoundation
|
||||
|
||||
private final class DynamicIslandArtworkLoopController {
|
||||
let player: AVQueuePlayer
|
||||
private var looper: AVPlayerLooper?
|
||||
|
||||
init(url: URL) {
|
||||
let item = AVPlayerItem(url: url)
|
||||
player = AVQueuePlayer()
|
||||
player.isMuted = true
|
||||
player.actionAtItemEnd = .none
|
||||
looper = AVPlayerLooper(player: player, templateItem: item)
|
||||
player.play()
|
||||
}
|
||||
|
||||
deinit {
|
||||
player.pause()
|
||||
looper = nil
|
||||
}
|
||||
}
|
||||
|
||||
private final class DynamicIslandArtworkVideoContainerView: NSView {
|
||||
let playerLayer = AVPlayerLayer()
|
||||
|
||||
override init(frame frameRect: NSRect) {
|
||||
super.init(frame: frameRect)
|
||||
wantsLayer = true
|
||||
layer?.backgroundColor = NSColor.clear.cgColor
|
||||
playerLayer.videoGravity = .resizeAspectFill
|
||||
playerLayer.backgroundColor = NSColor.clear.cgColor
|
||||
layer?.addSublayer(playerLayer)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
nil
|
||||
}
|
||||
|
||||
override func layout() {
|
||||
super.layout()
|
||||
playerLayer.frame = bounds
|
||||
}
|
||||
}
|
||||
|
||||
private struct DynamicIslandArtworkVideoView: NSViewRepresentable {
|
||||
let url: URL
|
||||
let videoGravity: AVLayerVideoGravity
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator()
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> DynamicIslandArtworkVideoContainerView {
|
||||
let view = DynamicIslandArtworkVideoContainerView(frame: .zero)
|
||||
context.coordinator.attach(layer: view.playerLayer, url: url, gravity: videoGravity)
|
||||
return view
|
||||
}
|
||||
|
||||
func updateNSView(_ nsView: DynamicIslandArtworkVideoContainerView, context: Context) {
|
||||
context.coordinator.attach(layer: nsView.playerLayer, url: url, gravity: videoGravity)
|
||||
}
|
||||
|
||||
final class Coordinator {
|
||||
private var controller: DynamicIslandArtworkLoopController?
|
||||
private var currentURL: URL?
|
||||
|
||||
func attach(layer: AVPlayerLayer, url: URL, gravity: AVLayerVideoGravity) {
|
||||
layer.videoGravity = gravity
|
||||
|
||||
if currentURL != url || controller == nil {
|
||||
currentURL = url
|
||||
controller = DynamicIslandArtworkLoopController(url: url)
|
||||
}
|
||||
|
||||
if layer.player !== controller?.player {
|
||||
layer.player = controller?.player
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DynamicIslandArtworkSourceView: View {
|
||||
@ObservedObject private var musicManager = MusicManager.shared
|
||||
@Default(.showLiveCanvasInDynamicIsland) private var showLiveCanvasInDynamicIsland
|
||||
|
||||
let cornerRadius: CGFloat
|
||||
let contentMode: ContentMode
|
||||
|
||||
private var liveCanvasURL: URL? {
|
||||
guard showLiveCanvasInDynamicIsland else { return nil }
|
||||
return musicManager.videoArtworkURL
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if let liveCanvasURL {
|
||||
DynamicIslandArtworkVideoView(url: liveCanvasURL, videoGravity: .resizeAspectFill)
|
||||
} else {
|
||||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: contentMode)
|
||||
}
|
||||
}
|
||||
.clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Music Player Components
|
||||
|
||||
|
|
@ -43,8 +149,23 @@ struct MusicPlayerView: View {
|
|||
struct AlbumArtView: View {
|
||||
@ObservedObject var musicManager = MusicManager.shared
|
||||
@ObservedObject var vm: DynamicIslandViewModel
|
||||
@Default(.showLiveCanvasInDynamicIsland) private var showLiveCanvasInDynamicIsland
|
||||
let albumArtNamespace: Namespace.ID
|
||||
|
||||
private var usesLiveCanvasArtwork: Bool {
|
||||
showLiveCanvasInDynamicIsland && musicManager.videoArtworkURL != nil
|
||||
}
|
||||
|
||||
private var albumArtCornerRadius: CGFloat {
|
||||
Defaults[.cornerRadiusScaling]
|
||||
? musicManager.albumArt.size.width / musicManager.albumArt.size.height > 1.0
|
||||
? MusicPlayerImageSizes.cornerRadiusInset.opened / 3
|
||||
: MusicPlayerImageSizes.cornerRadiusInset.opened
|
||||
: musicManager.albumArt.size.width / musicManager.albumArt.size.height > 1.0
|
||||
? MusicPlayerImageSizes.cornerRadiusInset.closed / 3
|
||||
: MusicPlayerImageSizes.cornerRadiusInset.closed
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack(alignment: .bottomTrailing) {
|
||||
if Defaults[.lightingEffect] {
|
||||
|
|
@ -58,24 +179,26 @@ struct AlbumArtView: View {
|
|||
Color.clear
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.background(
|
||||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.clipShape(
|
||||
RoundedRectangle(
|
||||
cornerRadius: Defaults[.cornerRadiusScaling]
|
||||
? musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? MusicPlayerImageSizes.cornerRadiusInset.opened/3 : MusicPlayerImageSizes.cornerRadiusInset.opened
|
||||
: musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? MusicPlayerImageSizes.cornerRadiusInset.closed/3 : MusicPlayerImageSizes.cornerRadiusInset.closed
|
||||
|
||||
|
||||
)
|
||||
)
|
||||
DynamicIslandArtworkSourceView(
|
||||
cornerRadius: albumArtCornerRadius,
|
||||
contentMode: .fill
|
||||
)
|
||||
)
|
||||
.clipped()
|
||||
.scaleEffect(x: 1.3, y: 1.4)
|
||||
.rotationEffect(.degrees(92))
|
||||
.blur(radius: 40)
|
||||
.opacity(musicManager.isPlaying ? 0.5 : 0)
|
||||
.opacity(
|
||||
usesLiveCanvasArtwork
|
||||
? (musicManager.isPlaying ? 0.62 : 0.18)
|
||||
: (musicManager.isPlaying ? 0.5 : 0)
|
||||
)
|
||||
.shadow(
|
||||
color: Color(nsColor: musicManager.avgColor).opacity(usesLiveCanvasArtwork ? 0.24 : 0.16),
|
||||
radius: usesLiveCanvasArtwork ? 22 : 14,
|
||||
x: 0,
|
||||
y: 0
|
||||
)
|
||||
}
|
||||
|
||||
private var albumArtButton: some View {
|
||||
|
|
@ -111,18 +234,10 @@ struct AlbumArtView: View {
|
|||
Color.clear
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.overlay {
|
||||
Image(nsImage: musicManager.albumArt)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.clipShape(
|
||||
RoundedRectangle(
|
||||
cornerRadius: Defaults[.cornerRadiusScaling]
|
||||
? musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? MusicPlayerImageSizes.cornerRadiusInset.opened/3 : MusicPlayerImageSizes.cornerRadiusInset.opened
|
||||
: musicManager.albumArt.size.width/musicManager.albumArt.size.height > 1.0 ? MusicPlayerImageSizes.cornerRadiusInset.closed/3 : MusicPlayerImageSizes.cornerRadiusInset.closed
|
||||
|
||||
|
||||
)
|
||||
) // ensures that even the rectagular album cover is rounded just like the MacOS media player
|
||||
DynamicIslandArtworkSourceView(
|
||||
cornerRadius: albumArtCornerRadius,
|
||||
contentMode: .fit
|
||||
)
|
||||
}
|
||||
.matchedGeometryEffect(id: "albumArt", in: albumArtNamespace)
|
||||
.clipped()
|
||||
|
|
|
|||
|
|
@ -763,6 +763,7 @@ struct SettingsView: View {
|
|||
SettingsSearchEntry(tab: .media, title: "Skip buttons", keywords: ["skip", "controls", "±10"], highlightID: SettingsTab.media.highlightID(for: "Skip buttons")),
|
||||
SettingsSearchEntry(tab: .media, title: "Sneak Peek Style", keywords: ["sneak peek", "preview"], highlightID: SettingsTab.media.highlightID(for: "Sneak Peek Style")),
|
||||
SettingsSearchEntry(tab: .media, title: "Enable lyrics", keywords: ["lyrics", "song text"], highlightID: SettingsTab.media.highlightID(for: "Enable lyrics")),
|
||||
SettingsSearchEntry(tab: .media, title: "Show live canvas in Dynamic Island", keywords: ["canvas", "live canvas", "album art", "dynamic island", "spotify canvas"], highlightID: SettingsTab.media.highlightID(for: "Show live canvas in Dynamic Island")),
|
||||
SettingsSearchEntry(tab: .media, title: "Auto-hide inactive notch media player", keywords: ["auto hide", "inactive", "placeholder", "notch media"], highlightID: SettingsTab.media.highlightID(for: "Auto-hide inactive notch media player")),
|
||||
SettingsSearchEntry(tab: .media, title: "Show Change Media Output control", keywords: ["airplay", "route picker", "media output"], highlightID: SettingsTab.media.highlightID(for: "Show Change Media Output control")),
|
||||
SettingsSearchEntry(tab: .media, title: "Enable album art parallax", keywords: ["parallax", "lock screen", "album art"], highlightID: SettingsTab.media.highlightID(for: "Enable album art parallax")),
|
||||
|
|
@ -2609,6 +2610,11 @@ struct Media: View {
|
|||
Text("Enable lyrics")
|
||||
}
|
||||
.settingsHighlight(id: highlightID("Enable lyrics"))
|
||||
Defaults.Toggle(key: .showLiveCanvasInDynamicIsland) {
|
||||
Text("Show live canvas in Dynamic Island")
|
||||
}
|
||||
.settingsHighlight(id: highlightID("Show live canvas in Dynamic Island"))
|
||||
.help("Replaces the artwork tile with the live canvas when the current app provides one, and reuses that moving canvas for the surrounding lighting effect.")
|
||||
|
||||
//Parallax Effect Intensity to control how much parallax is wanted
|
||||
Slider(value: $parallaxEffectIntensity, in: 0...12, step: 1.0) {
|
||||
|
|
@ -2703,12 +2709,17 @@ struct Media: View {
|
|||
}
|
||||
.disabled(!enableLockScreenMediaWidget)
|
||||
.settingsHighlight(id: highlightID("Fullscreen artwork on right-click"))
|
||||
Defaults.Toggle(key: .lockScreenUseArtworkLayoutOverFullscreenCanvas) {
|
||||
Text("Use album art layout over fullscreen canvas")
|
||||
}
|
||||
.disabled(!enableLockScreenMediaWidget || !lockScreenMusicFullscreenArtworkEnabled)
|
||||
.settingsHighlight(id: highlightID("Use album art layout over fullscreen canvas"))
|
||||
Defaults.Toggle(key: .lockScreenKeepAlbumArtVisibleDuringFullscreenArtwork) {
|
||||
Text("Keep album art visible during fullscreen artwork")
|
||||
}
|
||||
.disabled(!enableLockScreenMediaWidget || !lockScreenMusicFullscreenArtworkEnabled)
|
||||
.settingsHighlight(id: highlightID("Keep album art visible during fullscreen artwork"))
|
||||
Text("Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. The swap is temporary and leaves no trace in System Settings.")
|
||||
Text("Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. If a canvas is available, Atoll can also keep the same album art + player layout on top of the live canvas.")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
|
|
@ -4833,12 +4844,17 @@ struct LockScreenSettings: View {
|
|||
}
|
||||
.disabled(!enableLockScreenMediaWidget)
|
||||
.settingsHighlight(id: highlightID("Fullscreen artwork on right-click"))
|
||||
Defaults.Toggle(key: .lockScreenUseArtworkLayoutOverFullscreenCanvas) {
|
||||
Text("Use album art layout over fullscreen canvas")
|
||||
}
|
||||
.disabled(!enableLockScreenMediaWidget || !lockScreenMusicFullscreenArtworkEnabled)
|
||||
.settingsHighlight(id: highlightID("Use album art layout over fullscreen canvas"))
|
||||
Defaults.Toggle(key: .lockScreenKeepAlbumArtVisibleDuringFullscreenArtwork) {
|
||||
Text("Keep album art visible during fullscreen artwork")
|
||||
}
|
||||
.disabled(!enableLockScreenMediaWidget || !lockScreenMusicFullscreenArtworkEnabled)
|
||||
.settingsHighlight(id: highlightID("Keep album art visible during fullscreen artwork"))
|
||||
Text("Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. The swap is temporary and leaves no trace in System Settings.")
|
||||
Text("Right-click the album art on the lock screen to set it as the wallpaper. Right-click again or click the background to restore the original wallpaper. If a canvas is available, Atoll can also keep the same album art + player layout on top of the live canvas.")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -58,13 +58,30 @@ class LockScreenPanelManager {
|
|||
return formatter.string(from: Date())
|
||||
}
|
||||
|
||||
private func publishPanelFrame(_ frame: NSRect?) {
|
||||
latestFrame = frame
|
||||
|
||||
var userInfo: [AnyHashable: Any] = [:]
|
||||
if let frame {
|
||||
userInfo["frame"] = NSValue(rect: frame)
|
||||
}
|
||||
|
||||
NotificationCenter.default.post(
|
||||
name: .atollLockScreenPanelFrameDidChange,
|
||||
object: self,
|
||||
userInfo: userInfo
|
||||
)
|
||||
}
|
||||
|
||||
private func registerScreenChangeObservers() {
|
||||
screenChangeObserver = NotificationCenter.default.addObserver(
|
||||
forName: NSApplication.didChangeScreenParametersNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
self?.handleScreenGeometryChange(reason: "screen-parameters")
|
||||
Task { @MainActor [weak self] in
|
||||
self?.handleScreenGeometryChange(reason: "screen-parameters")
|
||||
}
|
||||
}
|
||||
|
||||
let workspaceCenter = NSWorkspace.shared.notificationCenter
|
||||
|
|
@ -73,7 +90,9 @@ class LockScreenPanelManager {
|
|||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
self?.handleScreenGeometryChange(reason: "screens-did-wake")
|
||||
Task { @MainActor [weak self] in
|
||||
self?.handleScreenGeometryChange(reason: "screens-did-wake")
|
||||
}
|
||||
}
|
||||
|
||||
workspaceObservers = [wakeObserver]
|
||||
|
|
@ -94,8 +113,9 @@ class LockScreenPanelManager {
|
|||
}
|
||||
|
||||
let screenFrame = screen.frame
|
||||
let targetFrame = collapsedFrame(for: screenFrame)
|
||||
collapsedFrame = targetFrame
|
||||
let defaultCollapsedFrame = collapsedFrame(for: screenFrame)
|
||||
let targetFrame = targetFrame(for: screenFrame, panelSize: LockScreenMusicPanel.collapsedSize)
|
||||
collapsedFrame = defaultCollapsedFrame
|
||||
isPanelExpanded = false
|
||||
currentAdditionalHeight = 0
|
||||
|
||||
|
|
@ -127,14 +147,14 @@ class LockScreenPanelManager {
|
|||
}
|
||||
|
||||
window.setFrame(targetFrame, display: true)
|
||||
latestFrame = targetFrame
|
||||
publishPanelFrame(targetFrame)
|
||||
hideTask?.cancel()
|
||||
panelAnimator.isPresented = false
|
||||
LockScreenTimerWidgetManager.shared.notifyMusicPanelFrameChanged(animated: false)
|
||||
|
||||
let hosting = NSHostingView(rootView: LockScreenMusicPanel(animator: panelAnimator))
|
||||
hosting.frame = NSRect(origin: .zero, size: targetFrame.size)
|
||||
hosting.autoresizingMask = [.width, .height]
|
||||
let hosting = NSHostingView(rootView: LockScreenMusicPanel(animator: panelAnimator))
|
||||
hosting.frame = NSRect(origin: .zero, size: targetFrame.size)
|
||||
hosting.autoresizingMask = [.width, .height]
|
||||
window.contentView = hosting
|
||||
|
||||
// Ensure the underlying window content is clipped to rounded corners
|
||||
|
|
@ -162,18 +182,17 @@ class LockScreenPanelManager {
|
|||
}
|
||||
|
||||
func updatePanelSize(expanded: Bool, additionalHeight: CGFloat = 0, animated: Bool = true) {
|
||||
guard let window = panelWindow, let baseFrame = collapsedFrame else {
|
||||
guard let window = panelWindow, let screen = currentScreen() else {
|
||||
return
|
||||
}
|
||||
|
||||
let resizeDuration: CFTimeInterval = 0.28
|
||||
|
||||
let baseSize = expanded ? LockScreenMusicPanel.expandedSize : LockScreenMusicPanel.collapsedSize
|
||||
let targetWidth = baseSize.width
|
||||
let targetHeight = baseSize.height + additionalHeight
|
||||
let originX = baseFrame.midX - (targetWidth / 2)
|
||||
let originY = baseFrame.origin.y
|
||||
let targetFrame = NSRect(x: originX, y: originY, width: targetWidth, height: targetHeight)
|
||||
let targetFrame = targetFrame(
|
||||
for: screen.frame,
|
||||
panelSize: CGSize(width: baseSize.width, height: baseSize.height + additionalHeight)
|
||||
)
|
||||
|
||||
if animated {
|
||||
NSAnimationContext.runAnimationGroup { context in
|
||||
|
|
@ -185,7 +204,7 @@ class LockScreenPanelManager {
|
|||
window.setFrame(targetFrame, display: true)
|
||||
}
|
||||
|
||||
latestFrame = targetFrame
|
||||
publishPanelFrame(targetFrame)
|
||||
|
||||
LockScreenTimerWidgetManager.shared.notifyMusicPanelFrameChanged(animated: animated)
|
||||
|
||||
|
|
@ -229,7 +248,7 @@ class LockScreenPanelManager {
|
|||
|
||||
guard let window = panelWindow else {
|
||||
print("LockScreenPanelManager: no panel to hide")
|
||||
latestFrame = nil
|
||||
publishPanelFrame(nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -239,7 +258,7 @@ class LockScreenPanelManager {
|
|||
await MainActor.run {
|
||||
window?.orderOut(nil)
|
||||
window?.contentView = nil
|
||||
self.latestFrame = nil
|
||||
self.publishPanelFrame(nil)
|
||||
print("[\(self.timestamp())] LockScreenPanelManager: panel hidden")
|
||||
}
|
||||
}
|
||||
|
|
@ -267,6 +286,60 @@ class LockScreenPanelManager {
|
|||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private func targetFrame(for screenFrame: NSRect, panelSize: CGSize) -> NSRect {
|
||||
let defaultCollapsedFrame = collapsedFrame(for: screenFrame)
|
||||
|
||||
if FullScreenArtworkWindowManager.shared.isShowingSpotifyCanvasFallback {
|
||||
return spotifyCanvasFallbackPanelFrame(
|
||||
screenFrame: screenFrame,
|
||||
defaultCollapsedFrame: defaultCollapsedFrame,
|
||||
panelSize: panelSize
|
||||
)
|
||||
}
|
||||
|
||||
let originX = defaultCollapsedFrame.midX - (panelSize.width / 2)
|
||||
return NSRect(
|
||||
x: originX,
|
||||
y: defaultCollapsedFrame.origin.y,
|
||||
width: panelSize.width,
|
||||
height: panelSize.height
|
||||
)
|
||||
}
|
||||
|
||||
private func spotifyCanvasFallbackPanelFrame(
|
||||
screenFrame: NSRect,
|
||||
defaultCollapsedFrame: NSRect,
|
||||
panelSize: CGSize
|
||||
) -> NSRect {
|
||||
let fullscreenArtworkManager = FullScreenArtworkWindowManager.shared
|
||||
let spacing = fullscreenArtworkManager.spotifyCanvasFallbackInterItemSpacing(screenFrame: screenFrame)
|
||||
let artworkSide = fullscreenArtworkManager.spotifyCanvasFallbackArtworkSideLength(
|
||||
screenFrame: screenFrame,
|
||||
panelSize: panelSize
|
||||
)
|
||||
let totalWidth = artworkSide + spacing + panelSize.width
|
||||
let horizontalMargin: CGFloat = 48
|
||||
let minGroupOriginX = screenFrame.minX + horizontalMargin
|
||||
let maxGroupOriginX = max(screenFrame.maxX - horizontalMargin - totalWidth, minGroupOriginX)
|
||||
let groupOriginX = min(
|
||||
max(screenFrame.midX - (totalWidth / 2), minGroupOriginX),
|
||||
maxGroupOriginX
|
||||
)
|
||||
|
||||
let baseCenterY = screenFrame.midY
|
||||
let largestHalfHeight = max(artworkSide, panelSize.height) / 2
|
||||
let minCenterY = screenFrame.minY + largestHalfHeight + 56
|
||||
let maxCenterY = screenFrame.maxY - largestHalfHeight - 84
|
||||
let centerY = min(max(baseCenterY, minCenterY), maxCenterY)
|
||||
|
||||
return NSRect(
|
||||
x: groupOriginX + artworkSide + spacing,
|
||||
y: centerY - (panelSize.height / 2),
|
||||
width: panelSize.width,
|
||||
height: panelSize.height
|
||||
)
|
||||
}
|
||||
|
||||
private func collapsedFrame(for screenFrame: NSRect) -> NSRect {
|
||||
let collapsedSize = LockScreenMusicPanel.collapsedSize
|
||||
let originX = screenFrame.midX - (collapsedSize.width / 2)
|
||||
|
|
@ -289,3 +362,7 @@ class LockScreenPanelManager {
|
|||
LockScreenDisplayContextProvider.shared.contextSnapshot()?.screen ?? NSScreen.main
|
||||
}
|
||||
}
|
||||
|
||||
extension Notification.Name {
|
||||
static let atollLockScreenPanelFrameDidChange = Notification.Name("atollLockScreenPanelFrameDidChange")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,16 @@ struct LyricLine: Identifiable, Codable {
|
|||
}
|
||||
}
|
||||
|
||||
private struct LyricsLookupKey: Hashable {
|
||||
let title: String
|
||||
let artist: String
|
||||
let album: String
|
||||
|
||||
var isValid: Bool {
|
||||
!title.isEmpty && !artist.isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
let defaultImage: NSImage = .init(
|
||||
systemSymbolName: "heart.fill",
|
||||
accessibilityDescription: "Album Art"
|
||||
|
|
@ -475,6 +485,10 @@ class MusicManager: ObservableObject {
|
|||
|
||||
// Task used to periodically sync displayed lyric with playback position
|
||||
private var lyricSyncTask: Task<Void, Never>?
|
||||
private var lyricsFetchTask: Task<Void, Never>?
|
||||
private var lyricsFetchKey: LyricsLookupKey?
|
||||
private var activeLyricsKey: LyricsLookupKey?
|
||||
private var lyricsCache: [LyricsLookupKey: [LyricLine]] = [:]
|
||||
private var explicitLookupTask: Task<Void, Never>?
|
||||
private var explicitLookupKey: String?
|
||||
|
||||
|
|
@ -514,6 +528,13 @@ class MusicManager: ObservableObject {
|
|||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
Defaults.publisher(.enableLyrics)
|
||||
.receive(on: RunLoop.main)
|
||||
.sink { [weak self] change in
|
||||
self?.handleLyricsPreferenceChange(isEnabled: change.newValue)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Observe Pear Desktop launch/terminate for auto-detection
|
||||
setupPearDesktopAutoDetection()
|
||||
|
||||
|
|
@ -582,6 +603,8 @@ class MusicManager: ObservableObject {
|
|||
|
||||
public func destroy() {
|
||||
debounceIdleTask?.cancel()
|
||||
lyricsFetchTask?.cancel()
|
||||
lyricSyncTask?.cancel()
|
||||
explicitLookupTask?.cancel()
|
||||
cancellables.removeAll()
|
||||
controllerCancellables.removeAll()
|
||||
|
|
@ -743,7 +766,7 @@ class MusicManager: ObservableObject {
|
|||
self.lastArtworkContentIdentifier = state.contentIdentifier
|
||||
self.lastArtworkContentURL = state.contentURL
|
||||
|
||||
self.fetchLyrics()
|
||||
self.prepareLyricsForCurrentTrack()
|
||||
if let liveArtworkURL = state.liveArtworkURL {
|
||||
self.videoArtworkURL = liveArtworkURL
|
||||
} else {
|
||||
|
|
@ -1252,53 +1275,108 @@ class MusicManager: ObservableObject {
|
|||
|
||||
// MARK: - Lyrics Methods
|
||||
func fetchLyrics() {
|
||||
guard Defaults[.enableLyrics] else { return }
|
||||
// If the lyrics panel is visible already, provide immediate feedback
|
||||
if showLyrics {
|
||||
Task { @MainActor in
|
||||
self.currentLyrics = "Loading lyrics..."
|
||||
self.syncedLyrics = []
|
||||
self.currentLyricIndex = -1
|
||||
}
|
||||
prepareLyricsForCurrentTrack(forceFetch: true, prioritizeVisibleResult: Defaults[.enableLyrics])
|
||||
}
|
||||
|
||||
private func handleLyricsPreferenceChange(isEnabled: Bool) {
|
||||
showLyrics = isEnabled
|
||||
|
||||
if isEnabled {
|
||||
prepareLyricsForCurrentTrack(prioritizeVisibleResult: true)
|
||||
} else {
|
||||
stopLyricSync()
|
||||
}
|
||||
}
|
||||
|
||||
private func prepareLyricsForCurrentTrack(forceFetch: Bool = false, prioritizeVisibleResult: Bool = false) {
|
||||
guard let lookup = currentLyricsLookupContext() else {
|
||||
activeLyricsKey = nil
|
||||
lyricsFetchKey = nil
|
||||
lyricsFetchTask?.cancel()
|
||||
lyricsFetchTask = nil
|
||||
syncedLyrics = []
|
||||
currentLyrics = ""
|
||||
currentLyricIndex = -1
|
||||
stopLyricSync()
|
||||
return
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
let lyrics = try await fetchLyricsFromAPI(artist: artistName, title: songTitle)
|
||||
await MainActor.run {
|
||||
self.syncedLyrics = lyrics
|
||||
self.currentLyricIndex = -1
|
||||
if !lyrics.isEmpty {
|
||||
self.currentLyrics = lyrics[0].text
|
||||
} else {
|
||||
self.currentLyrics = ""
|
||||
}
|
||||
let key = lookup.key
|
||||
let lyricsEnabled = Defaults[.enableLyrics]
|
||||
let shouldShowLoading = lyricsEnabled && prioritizeVisibleResult
|
||||
let trackChanged = activeLyricsKey != key
|
||||
activeLyricsKey = key
|
||||
|
||||
// If lyrics are enabled, start syncing them to playback position
|
||||
if Defaults[.enableLyrics] && !self.syncedLyrics.isEmpty {
|
||||
self.startLyricSync()
|
||||
} else if self.syncedLyrics.isEmpty {
|
||||
self.stopLyricSync()
|
||||
}
|
||||
if trackChanged {
|
||||
syncedLyrics = []
|
||||
currentLyricIndex = -1
|
||||
currentLyrics = shouldShowLoading ? "Loading lyrics..." : ""
|
||||
stopLyricSync()
|
||||
}
|
||||
|
||||
if !forceFetch, let cachedLyrics = lyricsCache[key] {
|
||||
applyLyricsToDisplay(cachedLyrics)
|
||||
return
|
||||
}
|
||||
|
||||
if lyricsFetchKey == key {
|
||||
if shouldShowLoading && syncedLyrics.isEmpty {
|
||||
currentLyrics = "Loading lyrics..."
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
lyricsFetchTask?.cancel()
|
||||
lyricsFetchKey = key
|
||||
|
||||
if shouldShowLoading || (lyricsEnabled && syncedLyrics.isEmpty) {
|
||||
currentLyrics = "Loading lyrics..."
|
||||
}
|
||||
|
||||
let requestArtist = lookup.requestArtist
|
||||
let requestTitle = lookup.requestTitle
|
||||
let requestAlbum = lookup.requestAlbum
|
||||
|
||||
lyricsFetchTask = Task { [weak self] in
|
||||
guard let self else { return }
|
||||
|
||||
do {
|
||||
let lyrics = try await self.fetchLyricsFromAPI(
|
||||
artist: requestArtist,
|
||||
title: requestTitle,
|
||||
album: requestAlbum
|
||||
)
|
||||
guard !Task.isCancelled else { return }
|
||||
|
||||
await MainActor.run {
|
||||
guard self.activeLyricsKey == key else { return }
|
||||
self.lyricsCache[key] = lyrics
|
||||
self.lyricsFetchKey = nil
|
||||
self.lyricsFetchTask = nil
|
||||
self.applyLyricsToDisplay(lyrics)
|
||||
}
|
||||
} catch {
|
||||
print("Failed to fetch lyrics: \(error)")
|
||||
await MainActor.run {
|
||||
guard self.activeLyricsKey == key else { return }
|
||||
self.lyricsFetchKey = nil
|
||||
self.lyricsFetchTask = nil
|
||||
self.syncedLyrics = []
|
||||
self.currentLyrics = ""
|
||||
self.currentLyricIndex = -1
|
||||
self.currentLyrics = lyricsEnabled ? "No lyrics found" : ""
|
||||
self.stopLyricSync()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func fetchLyricsFromAPI(artist: String, title: String) async throws -> [LyricLine] {
|
||||
private func fetchLyricsFromAPI(artist: String, title: String, album: String) async throws -> [LyricLine] {
|
||||
guard !artist.isEmpty, !title.isEmpty else { return [] }
|
||||
|
||||
// Normalize input and percent-encode
|
||||
let cleanArtist = artist.folding(options: .diacriticInsensitive, locale: .current)
|
||||
let cleanTitle = title.folding(options: .diacriticInsensitive, locale: .current)
|
||||
let cleanAlbum = album.folding(options: .diacriticInsensitive, locale: .current)
|
||||
guard let encodedArtist = cleanArtist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
|
||||
let encodedTitle = cleanTitle.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
||||
return []
|
||||
|
|
@ -1312,7 +1390,8 @@ class MusicManager: ObservableObject {
|
|||
if let http = response as? HTTPURLResponse, http.statusCode == 200 {
|
||||
// Try parse as array JSON (preferred)
|
||||
if let jsonArray = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]],
|
||||
let first = jsonArray.first {
|
||||
let bestMatch = bestLyricsMatch(in: jsonArray, artist: cleanArtist, title: cleanTitle, album: cleanAlbum) {
|
||||
let first = bestMatch
|
||||
let plain = (first["plainLyrics"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
let synced = (first["syncedLyrics"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||
|
||||
|
|
@ -1354,6 +1433,107 @@ class MusicManager: ObservableObject {
|
|||
}
|
||||
}
|
||||
|
||||
private func currentLyricsLookupContext() -> (key: LyricsLookupKey, requestArtist: String, requestTitle: String, requestAlbum: String)? {
|
||||
let requestArtist = normalizedLyricsRequestComponent(artistName)
|
||||
let requestTitle = normalizedLyricsTitle(songTitle)
|
||||
let requestAlbum = normalizedLyricsRequestComponent(album)
|
||||
|
||||
let key = LyricsLookupKey(
|
||||
title: requestTitle.lowercased(),
|
||||
artist: requestArtist.lowercased(),
|
||||
album: requestAlbum.lowercased()
|
||||
)
|
||||
|
||||
return key.isValid ? (key, requestArtist, requestTitle, requestAlbum) : nil
|
||||
}
|
||||
|
||||
private func normalizedLyricsRequestComponent(_ value: String) -> String {
|
||||
value
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
|
||||
}
|
||||
|
||||
private func normalizedLyricsTitle(_ value: String) -> String {
|
||||
var normalized = normalizedLyricsRequestComponent(value)
|
||||
let cleanupPatterns = [
|
||||
"\\s*\\((feat\\.?|ft\\.?|featuring)[^\\)]*\\)",
|
||||
"\\s*\\[(feat\\.?|ft\\.?|featuring)[^\\]]*\\]",
|
||||
"\\s*-\\s*(feat\\.?|ft\\.?|featuring)\\s+.*$",
|
||||
"\\s*\\((remaster(ed)?|live|mono|stereo)[^\\)]*\\)$",
|
||||
"\\s*\\[(remaster(ed)?|live|mono|stereo)[^\\]]*\\]$"
|
||||
]
|
||||
|
||||
for pattern in cleanupPatterns {
|
||||
normalized = normalized.replacingOccurrences(
|
||||
of: pattern,
|
||||
with: "",
|
||||
options: [.regularExpression, .caseInsensitive]
|
||||
)
|
||||
}
|
||||
|
||||
return normalizedLyricsRequestComponent(normalized)
|
||||
}
|
||||
|
||||
private func bestLyricsMatch(in results: [[String: Any]], artist: String, title: String, album: String) -> [String: Any]? {
|
||||
let normalizedArtist = artist.lowercased()
|
||||
let normalizedTitle = title.lowercased()
|
||||
let normalizedAlbum = album.lowercased()
|
||||
|
||||
return results.max { lhs, rhs in
|
||||
lyricsMatchScore(for: lhs, artist: normalizedArtist, title: normalizedTitle, album: normalizedAlbum)
|
||||
< lyricsMatchScore(for: rhs, artist: normalizedArtist, title: normalizedTitle, album: normalizedAlbum)
|
||||
}
|
||||
}
|
||||
|
||||
private func lyricsMatchScore(for result: [String: Any], artist: String, title: String, album: String) -> Int {
|
||||
let resultArtist = ((result["artistName"] as? String) ?? "").lowercased()
|
||||
let resultTitle = ((result["trackName"] as? String) ?? "").lowercased()
|
||||
let resultAlbum = ((result["albumName"] as? String) ?? "").lowercased()
|
||||
|
||||
var score = 0
|
||||
|
||||
if resultTitle == title { score += 8 }
|
||||
else if resultTitle.contains(title) || title.contains(resultTitle) { score += 4 }
|
||||
|
||||
if resultArtist == artist { score += 8 }
|
||||
else if resultArtist.contains(artist) || artist.contains(resultArtist) { score += 4 }
|
||||
|
||||
if !album.isEmpty {
|
||||
if resultAlbum == album { score += 4 }
|
||||
else if resultAlbum.contains(album) || album.contains(resultAlbum) { score += 2 }
|
||||
}
|
||||
|
||||
if !(result["syncedLyrics"] as? String ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
score += 3
|
||||
}
|
||||
|
||||
return score
|
||||
}
|
||||
|
||||
private func applyLyricsToDisplay(_ lyrics: [LyricLine]) {
|
||||
syncedLyrics = lyrics
|
||||
currentLyricIndex = -1
|
||||
|
||||
guard !lyrics.isEmpty else {
|
||||
currentLyrics = Defaults[.enableLyrics] ? "No lyrics found" : ""
|
||||
stopLyricSync()
|
||||
return
|
||||
}
|
||||
|
||||
let playbackPosition = max(estimatedPlaybackPosition(), elapsedTime)
|
||||
updateCurrentLyric(for: playbackPosition)
|
||||
|
||||
if currentLyricIndex == -1, let firstLine = lyrics.first?.text {
|
||||
currentLyrics = firstLine
|
||||
}
|
||||
|
||||
if Defaults[.enableLyrics] {
|
||||
startLyricSync()
|
||||
} else {
|
||||
stopLyricSync()
|
||||
}
|
||||
}
|
||||
|
||||
private func parseLRC(_ lrc: String) -> [LyricLine] {
|
||||
let lines = lrc.components(separatedBy: .newlines)
|
||||
var lyrics: [LyricLine] = []
|
||||
|
|
|
|||
|
|
@ -913,6 +913,7 @@ extension Defaults.Keys {
|
|||
static let lockScreenMusicFullscreenArtworkEnabled = Key<Bool>("lockScreenMusicFullscreenArtworkEnabled", default: true)
|
||||
static let lockScreenKeepAlbumArtVisibleDuringFullscreenArtwork = Key<Bool>("lockScreenKeepAlbumArtVisibleDuringFullscreenArtwork", default: false)
|
||||
static let lockScreenMusicFullscreenVideoArtwork = Key<Bool>("lockScreenMusicFullscreenVideoArtwork", default: true)
|
||||
static let lockScreenUseArtworkLayoutOverFullscreenCanvas = Key<Bool>("lockScreenShowCenteredAlbumArtOverFullscreenCanvas", default: true)
|
||||
static let lockScreenTimerWidgetUsesBlur = Key<Bool>("lockScreenTimerWidgetUsesBlur", default: false)
|
||||
static let lockScreenReminderChipStyle = Key<LockScreenReminderChipStyle>("lockScreenReminderChipStyle", default: .eventColor)
|
||||
static let lockScreenReminderWidgetHorizontalAlignment = Key<String>("lockScreenReminderWidgetHorizontalAlignment", default: "center")
|
||||
|
|
@ -1197,6 +1198,7 @@ extension Defaults.Keys {
|
|||
|
||||
// MARK: Lyrics Feature
|
||||
static let enableLyrics = Key<Bool>("enableLyrics", default: false)
|
||||
static let showLiveCanvasInDynamicIsland = Key<Bool>("showLiveCanvasInDynamicIsland", default: false)
|
||||
|
||||
// MARK: Notes Feature
|
||||
static let enableNotes = Key<Bool>("enableNotes", default: false)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue