Skip to content

Commit 060e08c

Browse files
committed
Make WidgetWindowsController an actor
1 parent d1d118e commit 060e08c

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public final class SuggestionWidgetController: NSObject {
3737
dependency.windowsController = windowsController
3838

3939
store.send(.startup)
40-
windowsController.start()
40+
Task {
41+
await windowsController.start()
42+
}
4143
}
4244
}
4345

Core/Sources/SuggestionWidget/WidgetWindowsController.swift

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Foundation
88
import SwiftUI
99
import XcodeInspector
1010

11-
final class WidgetWindowsController: NSObject {
11+
actor WidgetWindowsController: NSObject {
1212
let userDefaultsObservers = WidgetUserDefaultsObservers()
1313
var xcodeInspector: XcodeInspector { .shared }
1414

@@ -90,11 +90,9 @@ final class WidgetWindowsController: NSObject {
9090

9191
let isChatPanelDetached = state.chatPanelState.chatPanelInASeparateWindow
9292
let hasChat = !state.chatPanelState.chatTabGroup.tabInfo.isEmpty
93-
let shouldDebounce = await MainActor.run {
94-
defer { lastUpdateWindowOpacityTime = Date() }
95-
return !immediately &&
96-
!(Date().timeIntervalSince(lastUpdateWindowOpacityTime) > 5)
97-
}
93+
let shouldDebounce = !immediately &&
94+
!(Date().timeIntervalSince(lastUpdateWindowOpacityTime) > 5)
95+
lastUpdateWindowOpacityTime = Date()
9896
let activeApp = xcodeInspector.activeApplication
9997

10098
updateWindowOpacityTask?.cancel()
@@ -169,7 +167,7 @@ final class WidgetWindowsController: NSObject {
169167
func update() async {
170168
let state = store.withState { $0 }
171169
let isChatPanelDetached = state.chatPanelState.chatPanelInASeparateWindow
172-
guard let widgetLocation = generateWidgetLocation() else { return }
170+
guard let widgetLocation = await generateWidgetLocation() else { return }
173171
await updatePanelState(widgetLocation)
174172

175173
windows.widgetWindow.setFrame(
@@ -208,10 +206,8 @@ final class WidgetWindowsController: NSObject {
208206
}
209207

210208
let now = Date()
211-
let shouldThrottle = await MainActor.run {
212-
!immediately &&
213-
!(now.timeIntervalSince(lastUpdateWindowLocationTime) > 5)
214-
}
209+
let shouldThrottle = !immediately &&
210+
!(now.timeIntervalSince(lastUpdateWindowLocationTime) > 5)
215211

216212
updateWindowLocationTask?.cancel()
217213
let interval: TimeInterval = 0.1
@@ -222,14 +218,11 @@ final class WidgetWindowsController: NSObject {
222218
interval - now.timeIntervalSince(lastUpdateWindowLocationTime)
223219
)
224220

225-
let task = Task {
221+
updateWindowLocationTask = Task {
226222
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
227223
try Task.checkCancellation()
228224
await update()
229225
}
230-
await MainActor.run {
231-
updateWindowLocationTask = task
232-
}
233226
} else {
234227
Task {
235228
await update()
@@ -272,13 +265,13 @@ extension WidgetWindowsController: NSWindowDelegate {
272265
}
273266

274267
private extension WidgetWindowsController {
275-
func activate(_ app: AppInstanceInspector) async {
268+
func activate(_ app: AppInstanceInspector) {
276269
guard currentApplicationProcessIdentifier != app.processIdentifier else { return }
277270
currentApplicationProcessIdentifier = app.processIdentifier
278-
await observe(to: app)
271+
observe(to: app)
279272
}
280273

281-
func observe(to app: AppInstanceInspector) async {
274+
func observe(to app: AppInstanceInspector) {
282275
Task {
283276
await updateWindowLocation(animated: false, immediately: true)
284277
await updateWindowOpacity(immediately: true)
@@ -287,7 +280,8 @@ private extension WidgetWindowsController {
287280
return
288281
}
289282
let notifications = app.axNotifications
290-
let task = Task {
283+
observeToAppTask?.cancel()
284+
observeToAppTask = Task {
291285
await windows.orderFront()
292286

293287
let documentURL = await MainActor.run { store.withState { $0.focusingDocumentURL } }
@@ -335,13 +329,11 @@ private extension WidgetWindowsController {
335329
}
336330
}
337331
}
338-
339-
observeToAppTask?.cancel()
340-
observeToAppTask = task
341332
}
342333

343-
func observe(to editor: SourceEditor) async {
344-
let task = Task {
334+
func observe(to editor: SourceEditor) {
335+
observeToFocusedEditorTask?.cancel()
336+
observeToFocusedEditorTask = Task {
345337
let selectionRangeChange = editor.axNotifications
346338
.filter { $0.kind == .selectedTextChanged }
347339
let scroll = editor.axNotifications
@@ -378,9 +370,6 @@ private extension WidgetWindowsController {
378370
}
379371
}
380372
}
381-
382-
observeToFocusedEditorTask?.cancel()
383-
observeToFocusedEditorTask = task
384373
}
385374
}
386375

0 commit comments

Comments
 (0)