Skip to content

Commit e1e972e

Browse files
committed
Fix that suggestion from another file may be presented
1 parent 64958b0 commit e1e972e

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public final class SuggestionWidgetController {
7070
private var windowChangeObservationTask: Task<Void, Error>?
7171
private var activeApplicationMonitorTask: Task<Void, Error>?
7272
private var suggestionForFiles: [URL: Suggestion] = [:]
73+
private var currentFileURL: URL?
7374

7475
public var onAcceptButtonTapped: (() -> Void)? {
7576
get { suggestionPanelViewModel.onAcceptButtonTapped }
@@ -147,11 +148,13 @@ public final class SuggestionWidgetController {
147148
suggestionCount: Int
148149
) {
149150
withAnimation(.easeInOut(duration: 0.2)) {
150-
suggestionPanelViewModel.suggestion = highlighted(code: code, language: language)
151-
suggestionPanelViewModel.startLineIndex = startLineIndex
152-
suggestionPanelViewModel.isPanelDisplayed = true
153-
suggestionPanelViewModel.currentSuggestionIndex = currentSuggestionIndex
154-
suggestionPanelViewModel.suggestionCount = suggestionCount
151+
if fileURL == currentFileURL || currentFileURL == nil {
152+
suggestionPanelViewModel.suggestion = highlighted(code: code, language: language)
153+
suggestionPanelViewModel.startLineIndex = startLineIndex
154+
suggestionPanelViewModel.isPanelDisplayed = true
155+
suggestionPanelViewModel.currentSuggestionIndex = currentSuggestionIndex
156+
suggestionPanelViewModel.suggestionCount = suggestionCount
157+
}
155158
suggestionForFiles[fileURL] = .code(
156159
code,
157160
language: language,
@@ -166,11 +169,13 @@ public final class SuggestionWidgetController {
166169
public func discardSuggestion(fileURL: URL) {
167170
withAnimation(.easeInOut(duration: 0.2)) {
168171
suggestionForFiles[fileURL] = nil
169-
suggestionPanelViewModel.suggestion = []
170-
suggestionPanelViewModel.startLineIndex = 0
171-
suggestionPanelViewModel.currentSuggestionIndex = 0
172-
suggestionPanelViewModel.suggestionCount = 0
173-
suggestionPanelViewModel.isPanelDisplayed = false
172+
if fileURL == currentFileURL || currentFileURL == nil {
173+
suggestionPanelViewModel.suggestion = []
174+
suggestionPanelViewModel.startLineIndex = 0
175+
suggestionPanelViewModel.currentSuggestionIndex = 0
176+
suggestionPanelViewModel.suggestionCount = 0
177+
suggestionPanelViewModel.isPanelDisplayed = false
178+
}
174179
widgetViewModel.isProcessing = false
175180
}
176181
}
@@ -195,10 +200,14 @@ public final class SuggestionWidgetController {
195200
guard let self else { return }
196201
try Task.checkCancellation()
197202
self.updateWindowLocation(animated: false)
198-
203+
199204
if notification.name == kAXFocusedUIElementChangedNotification {
200-
guard let fileURL = try? await Environment.fetchCurrentFileURL(),
201-
let suggestion = suggestionForFiles[fileURL]
205+
guard let fileURL = try? await Environment.fetchCurrentFileURL() else {
206+
suggestionPanelViewModel.suggestion = []
207+
continue
208+
}
209+
currentFileURL = fileURL
210+
guard let suggestion = suggestionForFiles[fileURL]
202211
else {
203212
suggestionPanelViewModel.suggestion = []
204213
continue

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,25 @@ struct WidgetView: View {
6868
}
6969
}
7070
}
71-
.onChange(of: viewModel.isProcessing) { isProcessing in
72-
Task {
73-
await Task.yield()
74-
if isProcessing {
75-
processingProgress = 1 - processingProgress
76-
} else {
77-
processingProgress = panelViewModel.suggestion.isEmpty ? 0 : 1
78-
}
79-
}
80-
}
71+
.onChange(of: viewModel.isProcessing) { _ in refreshRing() }
72+
.onChange(of: panelViewModel.suggestion.isEmpty) { _ in refreshRing() }
8173
.onHover { yes in
8274
withAnimation(.easeInOut(duration: 0.2)) {
8375
isHovering = yes
8476
}
8577
}
8678
}
79+
80+
func refreshRing() {
81+
Task {
82+
await Task.yield()
83+
if viewModel.isProcessing {
84+
processingProgress = 1 - processingProgress
85+
} else {
86+
processingProgress = panelViewModel.suggestion.isEmpty ? 0 : 1
87+
}
88+
}
89+
}
8790
}
8891

8992
struct WidgetView_Preview: PreviewProvider {

0 commit comments

Comments
 (0)