@@ -11,6 +11,7 @@ struct CompactSliderSystemDemo: View {
1111
1212 @State private var playerTime : TimeInterval = 10
1313 @State private var isPlaying = false
14+ @State private var wasPlaying = false
1415 @State private var timer = Timer . publish ( every: 1 , on: . main, in: . common) . autoconnect ( )
1516
1617 var body : some View {
@@ -59,6 +60,18 @@ struct CompactSliderSystemDemo: View {
5960 . systemSliderStyle ( handleStyle: . hidden( ) )
6061 . compactSliderOptionsByAdding ( . expandOnFocus( minScale: 0.5 ) )
6162 . compactSliderAnimation ( . bouncy, when: . dragging, . hovering)
63+ . compactSliderAnimation ( isPlaying ? . linear( duration: 1 ) : nil , when: . progressDidChange)
64+ . compactSliderOnChange { configuration in
65+ if configuration. focusState. isDragging {
66+ Task {
67+ isPlaying = false
68+ }
69+ } else if wasPlaying {
70+ Task {
71+ isPlaying = true
72+ }
73+ }
74+ }
6275
6376 if #available( macOS 13 . 0 , iOS 16 . 0 , * ) {
6477 HStack ( alignment: . top) {
@@ -67,8 +80,11 @@ struct CompactSliderSystemDemo: View {
6780 . foregroundStyle ( . secondary)
6881 Spacer ( )
6982
70- Button ( action: { isPlaying. toggle ( ) } ) {
71- Image ( systemName: isPlaying ? " pause.fill " : " play.fill " )
83+ Button ( action: {
84+ isPlaying. toggle ( )
85+ wasPlaying = isPlaying
86+ } ) {
87+ Image ( systemName: wasPlaying ? " pause.fill " : " play.fill " )
7288 }
7389 . buttonStyle ( BorderlessButtonStyle ( ) )
7490 . font ( . title)
@@ -82,20 +98,20 @@ struct CompactSliderSystemDemo: View {
8298 . font ( . footnote)
8399 . monospacedDigit ( )
84100 . padding ( . horizontal, 6 )
101+ . animation ( . default, value: playerTime)
85102 }
86103 }
87104 . onReceive ( timer) { _ in
88105 guard isPlaying else { return }
89106
90- withAnimation {
91- if playerTime >= 59 {
92- isPlaying = false
93- playerTime = 60
94- return
95- }
96-
97- playerTime += 1
107+ if playerTime >= 59 {
108+ isPlaying = false
109+ wasPlaying = isPlaying
110+ playerTime = 60
111+ return
98112 }
113+
114+ playerTime += 1
99115 }
100116 }
101117}
0 commit comments