@@ -12,14 +12,8 @@ import QuartzCore
1212import Workspace
1313import XcodeInspector
1414
15- @WorkspaceActor
1615public class RealtimeSuggestionController {
17- var eventObserver : CGEventObserverType = CGEventObserver ( eventsOfInterest: [
18- . keyUp,
19- . keyDown,
20- . rightMouseDown,
21- . leftMouseDown,
22- ] )
16+ let eventObserver : CGEventObserverType = CGEventObserver ( eventsOfInterest: [ . keyDown] )
2317 private var task : Task < Void , Error > ?
2418 private var inflightPrefetchTask : Task < Void , Error > ?
2519 private var windowChangeObservationTask : Task < Void , Error > ?
@@ -28,34 +22,34 @@ public class RealtimeSuggestionController {
2822 private var focusedUIElement : AXUIElement ?
2923 private var sourceEditor : SourceEditor ?
3024
31- init ( ) {
25+ init ( ) { }
26+
27+ func start( ) {
3228 Task { [ weak self] in
33- if let app = ActiveApplicationMonitor . activeXcode {
29+ if let app = ActiveApplicationMonitor . shared . activeXcode {
3430 self ? . handleXcodeChanged ( app)
35- self ? . startHIDObservation ( by : 1 )
31+ self ? . startHIDObservation ( )
3632 }
37- var previousApp = ActiveApplicationMonitor . activeXcode
38- for await app in ActiveApplicationMonitor . createStream ( ) {
33+ var previousApp = ActiveApplicationMonitor . shared . activeXcode
34+ for await app in ActiveApplicationMonitor . shared . createStream ( ) {
3935 guard let self else { return }
4036 try Task . checkCancellation ( )
4137 defer { previousApp = app }
4238
43- if let app = ActiveApplicationMonitor . activeXcode, app != previousApp {
39+ if let app = ActiveApplicationMonitor . shared . activeXcode, app != previousApp {
4440 self . handleXcodeChanged ( app)
4541 }
4642
47- if ActiveApplicationMonitor . activeXcode != nil {
48- startHIDObservation ( by : 1 )
43+ if ActiveApplicationMonitor . shared . activeXcode != nil {
44+ startHIDObservation ( )
4945 } else {
50- stopHIDObservation ( by : 1 )
46+ stopHIDObservation ( )
5147 }
5248 }
5349 }
5450 }
5551
56- private func startHIDObservation( by listener: AnyHashable ) {
57- Logger . service. info ( " Add auto trigger listener: \( listener) . " )
58-
52+ private func startHIDObservation( ) {
5953 if task == nil {
6054 task = Task { [ weak self, eventObserver] in
6155 for await event in eventObserver. createStream ( ) {
@@ -67,8 +61,7 @@ public class RealtimeSuggestionController {
6761 eventObserver. activateIfPossible ( )
6862 }
6963
70- private func stopHIDObservation( by listener: AnyHashable ) {
71- Logger . service. info ( " Remove auto trigger listener: \( listener) . " )
64+ private func stopHIDObservation( ) {
7265 task? . cancel ( )
7366 task = nil
7467 eventObserver. deactivate ( )
@@ -98,7 +91,7 @@ public class RealtimeSuggestionController {
9891 }
9992
10093 private func handleFocusElementChange( ) {
101- guard let activeXcode = ActiveApplicationMonitor . activeXcode else { return }
94+ guard let activeXcode = ActiveApplicationMonitor . shared . activeXcode else { return }
10295 let application = AXUIElementCreateApplication ( activeXcode. processIdentifier)
10396 guard let focusElement = application. focusedElement else { return }
10497 let focusElementType = focusElement. description
@@ -233,15 +226,15 @@ public class RealtimeSuggestionController {
233226 /// Looks like the Xcode will keep the panel around until content is changed,
234227 /// not sure how to observe that it's hidden.
235228 func isCompletionPanelPresenting( ) -> Bool {
236- guard let activeXcode = ActiveApplicationMonitor . activeXcode else { return false }
229+ guard let activeXcode = ActiveApplicationMonitor . shared . activeXcode else { return false }
237230 let application = AXUIElementCreateApplication ( activeXcode. processIdentifier)
238231 return application. focusedWindow? . child ( identifier: " _XC_COMPLETION_TABLE_ " ) != nil
239232 }
240233
241234 func notifyEditingFileChange( editor: AXUIElement ) async {
242235 guard let fileURL = try ? await Environment . fetchCurrentFileURL ( ) ,
243236 let ( workspace, filespace) = try ? await Service . shared. workspacePool
244- . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
237+ . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
245238 else { return }
246239 workspace. suggestionPlugin? . notifyUpdateFile ( filespace: filespace, content: editor. value)
247240 }
0 commit comments