Skip to content

Commit 5204298

Browse files
committed
Fix closing chat panel
1 parent 25a48ea commit 5204298

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

Core/Sources/Service/GUI/ChatProvider+Service.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ extension ChatProvider {
5252
onClose = {
5353
Task {
5454
await service.stopReceivingMessage()
55-
PresentInWindowSuggestionPresenter().closeChatRoom(fileURL: fileURL)
5655
onCloseChat()
5756
}
5857
}

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,38 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
7171
func chatForFile(at url: URL) async -> ChatProvider? {
7272
let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat)
7373
let buildChatProvider = { (service: ChatService) in
74-
return ChatProvider(
74+
ChatProvider(
7575
service: service,
7676
fileURL: url,
7777
onCloseChat: { [weak self] in
78-
self?.globalChat = nil
78+
if UserDefaults.shared.value(for: \.useGlobalChat) {
79+
self?.globalChat = nil
80+
} else {
81+
self?.chats[url] = nil
82+
}
83+
let presenter = PresentInWindowSuggestionPresenter()
84+
presenter.closeChatRoom(fileURL: url)
7985
},
8086
onSwitchContext: { [weak self] in
87+
let useGlobalChat = UserDefaults.shared.value(for: \.useGlobalChat)
8188
UserDefaults.shared.set(!useGlobalChat, for: \.useGlobalChat)
8289
self?.createChatIfNeeded(for: url)
8390
let presenter = PresentInWindowSuggestionPresenter()
8491
presenter.presentChatRoom(fileURL: url)
8592
}
8693
)
8794
}
88-
89-
if useGlobalChat, let globalChat {
90-
return buildChatProvider(globalChat)
91-
}
9295

93-
if let service = chats[url] {
94-
return buildChatProvider(service)
96+
if useGlobalChat {
97+
if let globalChat {
98+
return buildChatProvider(globalChat)
99+
}
100+
} else {
101+
if let service = chats[url] {
102+
return buildChatProvider(service)
103+
}
95104
}
96-
105+
97106
return nil
98107
}
99108
}

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public final class SuggestionWidgetController {
9797
private var colorScheme: ColorScheme = .light
9898

9999
public var dataSource: SuggestionWidgetDataSource?
100-
100+
101101
public nonisolated init() {
102102
#warning(
103103
"TODO: A test is initializing this class for unknown reasons, try a better way to avoid this."
@@ -117,6 +117,7 @@ public final class SuggestionWidgetController {
117117
windowChangeObservationTask = nil
118118
self.observeXcodeWindowChangeIfNeeded(app)
119119
}
120+
await self.updateContentForActiveEditor()
120121
self.updateWindowLocation()
121122
} else {
122123
if ActiveApplicationMonitor.activeApplication?.bundleIdentifier != Bundle
@@ -248,7 +249,7 @@ public extension SuggestionWidgetController {
248249
Task {
249250
if let chat = await dataSource?.chatForFile(at: fileURL) {
250251
suggestionPanelViewModel.chat = chat
251-
252+
252253
Task { @MainActor in
253254
// looks like we need a delay.
254255
try await Task.sleep(nanoseconds: 150_000_000)
@@ -276,6 +277,7 @@ extension SuggestionWidgetController {
276277
let notifications = AXNotificationStream(
277278
app: app,
278279
notificationNames:
280+
kAXApplicationActivatedNotification,
279281
kAXMovedNotification,
280282
kAXResizedNotification,
281283
kAXMainWindowChangedNotification,
@@ -287,12 +289,11 @@ extension SuggestionWidgetController {
287289
for await notification in notifications {
288290
guard let self else { return }
289291
try Task.checkCancellation()
290-
self.updateWindowLocation(animated: false)
291-
panelWindow.orderFront(nil)
292-
widgetWindow.orderFront(nil)
293-
tabWindow.orderFront(nil)
294292
295-
if notification.name == kAXFocusedUIElementChangedNotification {
293+
if [
294+
kAXFocusedUIElementChangedNotification,
295+
kAXApplicationActivatedNotification,
296+
].contains(notification.name) {
296297
sourceEditorMonitorTask?.cancel()
297298
sourceEditorMonitorTask = nil
298299
observeEditorChangeIfNeeded(app)
@@ -309,6 +310,8 @@ extension SuggestionWidgetController {
309310
currentFileURL = fileURL
310311
await updateContentForActiveEditor(fileURL: fileURL)
311312
}
313+
314+
self.updateWindowLocation(animated: false)
312315
}
313316
}
314317
}
@@ -428,17 +431,17 @@ extension SuggestionWidgetController {
428431
return
429432
}
430433

431-
if let suggestion = await dataSource?.suggestionForFile(at: fileURL) {
432-
suggestionPanelViewModel.content = .suggestion(suggestion)
433-
} else {
434-
suggestionPanelViewModel.content = nil
435-
}
436-
437434
if let chat = await dataSource?.chatForFile(at: fileURL) {
438435
suggestionPanelViewModel.chat = chat
439436
} else {
440437
suggestionPanelViewModel.chat = nil
441438
}
439+
440+
if let suggestion = await dataSource?.suggestionForFile(at: fileURL) {
441+
suggestionPanelViewModel.content = .suggestion(suggestion)
442+
} else {
443+
suggestionPanelViewModel.content = nil
444+
}
442445
}
443446
}
444447

0 commit comments

Comments
 (0)