11import ActiveApplicationMonitor
22import AppKit
3+ import AXNotificationStream
34import DisplayLink
45import SwiftUI
56
@@ -25,25 +26,47 @@ final class SuggestionPanelController {
2526 return it
2627 } ( )
2728
28- private var displayLinkTask : Task < Void , Never > ?
29- private var activeApplicationMonitorTask : Task < Void , Never > ?
3029 let viewModel = SuggestionPanelViewModel ( )
30+
31+ private var windowChangeObservationTask : Task < Void , Error > ?
32+ private var activeApplicationMonitorTask : Task < Void , Error > ?
3133 private var activeApplication : NSRunningApplication ? {
3234 ActiveApplicationMonitor . activeApplication
3335 }
3436
3537 nonisolated init ( ) {
3638 Task { @MainActor in
37- displayLinkTask = Task {
38- for await _ in DisplayLink . createStream ( ) {
39+ activeApplicationMonitorTask = Task { [ weak self] in
40+ guard let self else { return }
41+ var previousApp : NSRunningApplication ?
42+ for await app in ActiveApplicationMonitor . createStream ( ) {
43+ try Task . checkCancellation ( )
44+ defer { previousApp = app }
45+ if let app, app. bundleIdentifier == " com.apple.dt.Xcode " {
46+ if app != previousApp {
47+ windowChangeObservationTask? . cancel ( )
48+ windowChangeObservationTask = nil
49+ self . observeXcodeWindowChangeIfNeeded ( )
50+ }
51+ }
52+
3953 self . updateWindowLocation ( )
4054 }
4155 }
56+ }
57+ }
4258
43- activeApplicationMonitorTask = Task {
44- for await _ in ActiveApplicationMonitor . createStream ( ) {
45- self . updateWindowLocation ( )
46- }
59+ private func observeXcodeWindowChangeIfNeeded( ) {
60+ guard windowChangeObservationTask == nil else { return }
61+ windowChangeObservationTask = Task { [ weak self] in
62+ guard let self else { return }
63+ let notifications = AXNotificationStream (
64+ app: activeApplication!,
65+ notificationNames: kAXMovedNotification
66+ )
67+ for await _ in notifications {
68+ try Task . checkCancellation ( )
69+ self . updateWindowLocation ( )
4770 }
4871 }
4972 }
@@ -91,6 +114,8 @@ final class SuggestionPanelController {
91114 }
92115}
93116
117+ #warning("MUSTDO: Update when editing file is changed.")
118+
94119@MainActor
95120final class SuggestionPanelViewModel : ObservableObject {
96121 @Published var startLineIndex : Int = 0
0 commit comments