Animate mini player playback control

This commit is contained in:
lucast committed 2026-10-16 03:26:39 +02:00
1 parent 77c00cc47e
commit 84675dbfc6
1 file changed
+20 -2
+20 -2
View File
@@ -10,6 +10,8 @@ struct MiniPlayerView: View {
let onNext: () -> Void let onNext: () -> Void
let onOpen: () -> Void let onOpen: () -> Void
@State private var displayedIsPlaying: Bool @State private var displayedIsPlaying: Bool
@State private var playIconScale: CGFloat = 1
@State private var pendingIsPlaying: Bool?
init( init(
track: Track?, track: Track?,
@@ -71,12 +73,13 @@ struct MiniPlayerView: View {
.onTapGesture(perform: onOpen) .onTapGesture(perform: onOpen)
Button { Button {
displayedIsPlaying.toggle() animatePlayIcon(to: !(pendingIsPlaying ?? displayedIsPlaying))
onTogglePlayback() onTogglePlayback()
} label: { } label: {
Image(systemName: displayedIsPlaying ? "pause.fill" : "play.fill") Image(systemName: displayedIsPlaying ? "pause.fill" : "play.fill")
.font(.system(size: usesCompactLayout ? 18 : 21, weight: .medium)) .font(.system(size: usesCompactLayout ? 18 : 21, weight: .medium))
.frame(width: usesCompactLayout ? 34 : 42, height: 32) .frame(width: usesCompactLayout ? 34 : 42, height: 32)
.scaleEffect(playIconScale)
.contentShape(Rectangle()) .contentShape(Rectangle())
} }
.buttonStyle(.plain) .buttonStyle(.plain)
@@ -99,11 +102,26 @@ struct MiniPlayerView: View {
.frame(height: 52) .frame(height: 52)
.foregroundStyle(.primary) .foregroundStyle(.primary)
.onChange(of: isPlaying) { _, newValue in .onChange(of: isPlaying) { _, newValue in
displayedIsPlaying = newValue animatePlayIcon(to: newValue)
} }
.accessibilityElement(children: .contain) .accessibilityElement(children: .contain)
} }
private func animatePlayIcon(to newValue: Bool) {
guard (pendingIsPlaying ?? displayedIsPlaying) != newValue else { return }
pendingIsPlaying = newValue
withAnimation(.easeOut(duration: 0.12)) {
playIconScale = 0
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.12) {
displayedIsPlaying = newValue
pendingIsPlaying = nil
withAnimation(.easeOut(duration: 0.12)) {
playIconScale = 1
}
}
}
@ViewBuilder @ViewBuilder
private var artwork: some View { private var artwork: some View {
if let artworkURL { if let artworkURL {