forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.swift
More file actions
213 lines (183 loc) · 6.57 KB
/
Status.swift
File metadata and controls
213 lines (183 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import AppKit
import Foundation
public enum ExtensionPermissionStatus {
case unknown
case succeeded
case failed
}
@objc public enum ObservedAXStatus: Int {
case unknown = -1
case granted = 1
case notGranted = 0
}
public struct CLSStatus: Equatable {
public enum Status {
case unknown
case normal
case inProgress
case error
case warning
case inactive
}
public let status: Status
public let message: String
public var isInactiveStatus: Bool {
status == .inactive && !message.isEmpty
}
public var isErrorStatus: Bool {
(status == .warning || status == .error) && !message.isEmpty
}
}
public struct AuthStatus: Equatable {
public enum Status {
case unknown
case loggedIn
case notLoggedIn
}
public let status: Status
public let username: String?
public let message: String?
}
public extension Notification.Name {
static let authStatusDidChange = Notification.Name("com.github.CopilotForXcode.authStatusDidChange")
static let serviceStatusDidChange = Notification.Name("com.github.CopilotForXcode.serviceStatusDidChange")
}
public struct StatusResponse {
public struct Icon {
public let name: String
public init(name: String) {
self.name = name
}
public var nsImage: NSImage? {
NSImage(named: name)
}
}
public let icon: Icon
public let inProgress: Bool
public let message: String?
public let url: String?
public let authMessage: String
}
public final actor Status {
public static let shared = Status()
private var extensionStatus: ExtensionPermissionStatus = .unknown
private var axStatus: ObservedAXStatus = .unknown
private var clsStatus = CLSStatus(status: .unknown, message: "")
private var authStatus = AuthStatus(status: .unknown, username: nil, message: nil)
private let okIcon = StatusResponse.Icon(name: "MenuBarIcon")
private let errorIcon = StatusResponse.Icon(name: "MenuBarWarningIcon")
private let inactiveIcon = StatusResponse.Icon(name: "MenuBarInactiveIcon")
private init() {}
public func updateExtensionStatus(_ status: ExtensionPermissionStatus) {
guard status != extensionStatus else { return }
extensionStatus = status
broadcast()
}
public func updateAXStatus(_ status: ObservedAXStatus) {
guard status != axStatus else { return }
axStatus = status
broadcast()
}
public func updateCLSStatus(_ status: CLSStatus.Status, message: String) {
let newStatus = CLSStatus(status: status, message: message)
guard newStatus != clsStatus else { return }
clsStatus = newStatus
broadcast()
}
public func updateAuthStatus(_ status: AuthStatus.Status, username: String? = nil, message: String? = nil) {
let newStatus = AuthStatus(status: status, username: username, message: message)
guard newStatus != authStatus else { return }
authStatus = newStatus
broadcast()
}
public func getAXStatus() -> ObservedAXStatus {
// if Xcode is running, return the observed status
if isXcodeRunning() {
return axStatus
} else if AXIsProcessTrusted() {
// if Xcode is not running but AXIsProcessTrusted() is true, return granted
return .granted
} else {
// otherwise, return the last observed status, which may be unknown
return axStatus
}
}
private func isXcodeRunning() -> Bool {
let xcode = NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dt.Xcode")
return !xcode.isEmpty
}
public func getAuthStatus() -> AuthStatus.Status {
return authStatus.status
}
public func getStatus() -> StatusResponse {
let (authIcon, authMessage) = getAuthStatusInfo()
let (icon, message, url) = getExtensionStatusInfo()
return .init(
icon: authIcon ?? icon ?? okIcon,
inProgress: clsStatus.status == .inProgress,
message: message,
url: url,
authMessage: authMessage
)
}
private func getAuthStatusInfo() -> (authIcon: StatusResponse.Icon?, authMessage: String) {
switch authStatus.status {
case .unknown,
.loggedIn:
(authIcon: nil, authMessage: "Logged in as \(authStatus.username ?? "")")
case .notLoggedIn:
(authIcon: errorIcon, authMessage: authStatus.message ?? "Not logged in")
}
}
private func getExtensionStatusInfo() -> (icon: StatusResponse.Icon?, message: String?, url: String?) {
if clsStatus.isInactiveStatus {
return (icon: inactiveIcon, message: clsStatus.message, url: nil)
} else if clsStatus.isErrorStatus {
return (icon: errorIcon, message: clsStatus.message, url: nil)
}
if extensionStatus == .failed {
// TODO differentiate between the permission not being granted and the
// extension just getting disabled by Xcode.
return (
icon: errorIcon,
message: """
Extension is not enabled. Enable GitHub Copilot under Xcode
and then restart Xcode.
""",
url: "x-apple.systempreferences:com.apple.ExtensionsPreferences"
)
}
switch getAXStatus() {
case .granted:
return (icon: nil, message: nil, url: nil)
case .notGranted:
return (
icon: errorIcon,
message: """
Accessibility permission not granted. \
Click to open System Preferences.
""",
url: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
)
case .unknown:
return (
icon: errorIcon,
message: """
Accessibility permission not granted or Copilot restart needed.
""",
url: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
)
}
}
private func broadcast() {
NotificationCenter.default.post(
name: .serviceStatusDidChange,
object: nil
)
// Can remove DistributedNotificationCenter if the settings UI moves in-process
DistributedNotificationCenter.default().post(
name: .serviceStatusDidChange,
object: nil
)
}
}