1+ import ActiveApplicationMonitor
12import AppKit
23import DisplayLink
34import SwiftUI
@@ -25,7 +26,11 @@ final class SuggestionPanelController {
2526 } ( )
2627
2728 private var displayLinkTask : Task < Void , Never > ?
29+ private var activeApplicationMonitorTask : Task < Void , Never > ?
2830 private let viewModel = SuggestionPanelViewModel ( )
31+ private var activeApplication : NSRunningApplication ? {
32+ ActiveApplicationMonitor . activeApplication
33+ }
2934
3035 nonisolated init ( ) {
3136 Task { @MainActor in
@@ -34,23 +39,29 @@ final class SuggestionPanelController {
3439 self . updateWindowLocation ( )
3540 }
3641 }
42+
43+ activeApplicationMonitorTask = Task {
44+ for await _ in ActiveApplicationMonitor . createStream ( ) {
45+ self . updateWindowLocation ( )
46+ }
47+ }
3748 }
3849 }
3950
4051 /// Update the window location
4152 ///
4253 /// - note:
4354 private func updateWindowLocation( ) {
44- if let activeXcode = NSRunningApplication
45- . runningApplications ( withBundleIdentifier: " com.apple.dt.Xcode " )
46- . first ( where: \. isActive)
55+ if let activeXcode = activeApplication,
56+ activeXcode. bundleIdentifier == " com.apple.dt.Xcode "
4757 {
4858 let application = AXUIElementCreateApplication ( activeXcode. processIdentifier)
4959 if let focusElement: AXUIElement = try ? application
5060 . copyValue ( key: kAXFocusedUIElementAttribute) ,
51- let focusElementType: String = try ? focusElement. copyValue ( key: kAXDescriptionAttribute) ,
52- focusElementType == " Source Editor " ,
53- let parent: AXUIElement = try ? focusElement. copyValue ( key: kAXParentAttribute) ,
61+ let focusElementType: String = try ? focusElement
62+ . copyValue ( key: kAXDescriptionAttribute) ,
63+ focusElementType == " Source Editor " ,
64+ let parent: AXUIElement = try ? focusElement. copyValue ( key: kAXParentAttribute) ,
5465 let positionValue: AXValue = try ? parent
5566 . copyValue ( key: kAXPositionAttribute) ,
5667 let sizeValue: AXValue = try ? parent
0 commit comments