@@ -8,19 +8,27 @@ import SwiftUI
88import UserNotifications
99import XPCShared
1010
11+ let bundleIdentifierBase = Bundle . main
12+ . object ( forInfoDictionaryKey: " BUNDLE_IDENTIFIER_BASE " ) as! String
13+ let serviceIdentifier = bundleIdentifierBase + " .ExtensionService "
14+
15+ @main
1116class AppDelegate : NSObject , NSApplicationDelegate , NSWindowDelegate {
1217 let scheduledCleaner = ScheduledCleaner ( )
1318 private let userDefaultsObserver = UserDefaultsObserver ( )
1419 private var statusBarItem : NSStatusItem !
15-
20+ private var xpcListener : ( NSXPCListener , ServiceDelegate ) ?
21+
1622 func applicationDidFinishLaunching( _: Notification ) {
1723 // setup real-time suggestion controller
1824 _ = RealtimeSuggestionController . shared
19- setupRestartOnUpdate ( )
25+ setupQuitOnUpdate ( )
2026 setupQuitOnUserTerminated ( )
21-
27+ xpcListener = setupXPCListener ( )
28+ os_log ( . info, " XPC Service started. " )
2229 NSApp . setActivationPolicy ( . accessory)
2330 buildStatusBarMenu ( )
31+ AXIsProcessTrustedWithOptions ( nil )
2432 }
2533
2634 @objc private func buildStatusBarMenu( ) {
@@ -107,13 +115,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
107115 UserDefaults . shared. set ( isOn, forKey: SettingsKey . realtimeSuggestionToggle)
108116 }
109117
110- func setupRestartOnUpdate ( ) {
118+ func setupQuitOnUpdate ( ) {
111119 Task {
112120 guard let url = Bundle . main. executableURL else { return }
113121 let checker = await FileChangeChecker ( fileURL: url)
114122
115123 // If Xcode or Copilot for Xcode is made active, check if the executable of this program
116- // is changed. If changed, restart the launch agent .
124+ // is changed. If changed, quit this program .
117125
118126 let sequence = NSWorkspace . shared. notificationCenter
119127 . notifications ( named: NSWorkspace . didActivateApplicationNotification)
@@ -124,25 +132,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
124132 app. isUserOfService
125133 else { continue }
126134 guard await checker. checkIfChanged ( ) else {
127- os_log ( . info, " XPC Service is not updated, no need to restart ." )
135+ os_log ( . info, " Extension Service is not updated, no need to quit ." )
128136 continue
129137 }
130- os_log ( . info, " XPC Service will be restarted ." )
138+ os_log ( . info, " Extension Service will quit ." )
131139 #if DEBUG
132140 #else
133- let manager = LaunchAgentManager (
134- serviceIdentifier: serviceIdentifier,
135- executablePath: Bundle . main. executablePath ?? " "
136- )
137- do {
138- try await manager. restartLaunchAgent ( )
139- } catch {
140- os_log (
141- . error,
142- " XPC Service failed to restart. %{public}s " ,
143- error. localizedDescription
144- )
145- }
141+ exit ( 0 )
146142 #endif
147143 }
148144 }
@@ -170,6 +166,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
170166 }
171167 }
172168 }
169+
170+ func setupXPCListener( ) -> ( NSXPCListener , ServiceDelegate ) {
171+ let listener = NSXPCListener ( machServiceName: serviceIdentifier)
172+ let delegate = ServiceDelegate ( )
173+ listener. delegate = delegate
174+ listener. resume ( )
175+ return ( listener, delegate)
176+ }
173177}
174178
175179private class UserDefaultsObserver : NSObject {
0 commit comments