Skip to content

Commit c3063bd

Browse files
committed
Update animation
1 parent da14062 commit c3063bd

1 file changed

Lines changed: 53 additions & 39 deletions

File tree

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,59 @@ struct WidgetView: View {
3232
}
3333
}
3434
.overlay {
35-
let lineWidth: Double = 4
36-
37-
Circle()
38-
.stroke(
39-
panelViewModel.suggestion.isEmpty
40-
? Color(nsColor: .darkGray)
41-
: Color.accentColor,
42-
style: .init(lineWidth: lineWidth)
43-
)
44-
.padding(lineWidth / 2)
35+
let minimumLineWidth: Double = 4
36+
let lineWidth = (1 - processingProgress) * 28 + minimumLineWidth
37+
let scale = max(processingProgress * 1, 0.0001)
38+
39+
ZStack {
40+
Circle()
41+
.stroke(
42+
Color(nsColor: .darkGray),
43+
style: .init(lineWidth: minimumLineWidth)
44+
)
45+
.padding(2)
46+
47+
#warning("TODO: Tweak the animation")
48+
// how do I stop the repeatForever animation without removing the view?
49+
// I tried many solutions found on stackoverflow but non of them works.
50+
if viewModel.isProcessing {
51+
Circle()
52+
.stroke(
53+
Color.accentColor,
54+
style: .init(lineWidth: lineWidth)
55+
)
56+
.padding(2)
57+
.scaleEffect(x: scale, y: scale)
58+
.opacity(
59+
!panelViewModel.suggestion.isEmpty || viewModel.isProcessing ? 1 : 0
60+
)
61+
.animation(
62+
.easeInOut(duration: 1).repeatForever(autoreverses: true),
63+
value: processingProgress
64+
)
65+
} else {
66+
Circle()
67+
.stroke(
68+
Color.accentColor,
69+
style: .init(lineWidth: lineWidth)
70+
)
71+
.padding(2)
72+
.scaleEffect(x: scale, y: scale)
73+
.opacity(
74+
!panelViewModel.suggestion.isEmpty || viewModel.isProcessing ? 1 : 0
75+
)
76+
.animation(.easeInOut(duration: 1), value: processingProgress)
77+
}
78+
}
4579
}
46-
.overlay {
47-
if viewModel.isProcessing {
48-
animationgRing()
80+
.onChange(of: viewModel.isProcessing) { isProcessing in
81+
Task {
82+
await Task.yield()
83+
if isProcessing {
84+
processingProgress = 1 - processingProgress
85+
} else {
86+
processingProgress = panelViewModel.suggestion.isEmpty ? 0 : 1
87+
}
4988
}
5089
}
5190
.onHover { yes in
@@ -54,31 +93,6 @@ struct WidgetView: View {
5493
}
5594
}
5695
}
57-
58-
@ViewBuilder
59-
func animationgRing() -> some View {
60-
let lineWidth = processingProgress * 5 + 4
61-
62-
Circle()
63-
.stroke(
64-
Color.accentColor,
65-
style: .init(lineWidth: lineWidth)
66-
)
67-
.onAppear {
68-
Task {
69-
await Task.yield()
70-
withAnimation(
71-
.easeInOut(duration: 1)
72-
.repeatForever(
73-
autoreverses: true
74-
)
75-
) {
76-
processingProgress = 1
77-
}
78-
}
79-
}
80-
.padding(lineWidth / 2)
81-
}
8296
}
8397

8498
struct WidgetView_Preview: PreviewProvider {
@@ -108,7 +122,7 @@ struct WidgetView_Preview: PreviewProvider {
108122
isHovering: false
109123
)
110124
}
111-
.frame(width: 40)
125+
.frame(width: 30)
112126
.background(Color.black)
113127
}
114128
}

0 commit comments

Comments
 (0)