Skip to content

Commit fa794b7

Browse files
committed
Update Xcode inspector observation
1 parent 0557b54 commit fa794b7

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Core/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public final class XcodeInspector: ObservableObject {
1010

1111
private var cancellable = Set<AnyCancellable>()
1212
private var activeXcodeObservations = Set<Task<Void, Error>>()
13+
private var activeXcodeCancellable = Set<AnyCancellable>()
1314

1415
@Published public internal(set) var activeApplication: AppInstanceInspector?
1516
@Published public internal(set) var activeXcode: XcodeAppInstanceInspector?
@@ -20,6 +21,7 @@ public final class XcodeInspector: ObservableObject {
2021
@Published public internal(set) var focusedWindow: XcodeWindowInspector?
2122
@Published public internal(set) var focusedEditor: SourceEditor?
2223
@Published public internal(set) var focusedElement: AXUIElement?
24+
@Published public internal(set) var completionPanel: AXUIElement?
2325

2426
init() {
2527
let runningApplications = NSWorkspace.shared.runningApplications
@@ -31,10 +33,6 @@ public final class XcodeInspector: ObservableObject {
3133
.first(where: \.isActive)
3234
.map(AppInstanceInspector.init(runningApplication:))
3335

34-
for xcode in xcodes {
35-
observeXcode(xcode)
36-
}
37-
3836
if let activeXcode {
3937
setActiveXcode(activeXcode)
4038
}
@@ -56,7 +54,6 @@ public final class XcodeInspector: ObservableObject {
5654
let new = XcodeAppInstanceInspector(runningApplication: app)
5755
xcodes.append(new)
5856
setActiveXcode(new)
59-
observeXcode(new)
6057
}
6158
} else {
6259
activeApplication = AppInstanceInspector(runningApplication: app)
@@ -90,24 +87,19 @@ public final class XcodeInspector: ObservableObject {
9087
}
9188
}
9289

93-
func observeXcode(_ xcode: XcodeAppInstanceInspector) {
94-
activeDocumentURL = xcode.documentURL
95-
activeProjectURL = xcode.projectURL
96-
focusedWindow = xcode.focusedWindow
97-
98-
xcode.$documentURL.filter { _ in xcode.isActive }.assign(to: &$activeDocumentURL)
99-
xcode.$projectURL.filter { _ in xcode.isActive }.assign(to: &$activeProjectURL)
100-
xcode.$focusedWindow.filter { _ in xcode.isActive }.assign(to: &$focusedWindow)
101-
}
102-
10390
func setActiveXcode(_ xcode: XcodeAppInstanceInspector) {
10491
for task in activeXcodeObservations { task.cancel() }
92+
for cancellable in activeXcodeCancellable { cancellable.cancel() }
10593
activeXcodeObservations.removeAll()
94+
activeXcodeCancellable.removeAll()
10695

10796
activeXcode = xcode
10897
latestActiveXcode = xcode
10998
activeDocumentURL = xcode.documentURL
11099
focusedWindow = xcode.focusedWindow
100+
completionPanel = xcode.completionPanel
101+
activeProjectURL = xcode.projectURL
102+
focusedWindow = xcode.focusedWindow
111103

112104
let setFocusedElement = { [weak self] in
113105
guard let self else { return }
@@ -135,6 +127,22 @@ public final class XcodeInspector: ObservableObject {
135127
}
136128

137129
activeXcodeObservations.insert(focusedElementChanged)
130+
131+
xcode.$completionPanel.sink { [weak self] element in
132+
self?.completionPanel = element
133+
}.store(in: &activeXcodeCancellable)
134+
135+
xcode.$documentURL.sink { [weak self] url in
136+
self?.activeDocumentURL = url
137+
}.store(in: &activeXcodeCancellable)
138+
139+
xcode.$projectURL.sink { [weak self] url in
140+
self?.activeProjectURL = url
141+
}.store(in: &activeXcodeCancellable)
142+
143+
xcode.$focusedWindow.sink { [weak self] window in
144+
self?.focusedWindow = window
145+
}.store(in: &activeXcodeCancellable)
138146
}
139147
}
140148

@@ -168,7 +176,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
168176
@Published public var projectURL: URL = .init(fileURLWithPath: "/")
169177
@Published public var workspaces = [WorkspaceIdentifier: WorkspaceInfo]()
170178
@Published public private(set) var completionPanel: AXUIElement?
171-
179+
172180
var _version: String?
173181
public var version: String? {
174182
if let _version { return _version }
@@ -234,7 +242,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
234242
}
235243

236244
longRunningTasks.insert(updateTabsTask)
237-
245+
238246
completionPanel = appElement.firstChild { element in
239247
element.identifier == "_XC_COMPLETION_TABLE_"
240248
}?.parent
@@ -245,7 +253,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
245253
element: appElement,
246254
notificationNames: kAXCreatedNotification, kAXUIElementDestroyedNotification
247255
)
248-
256+
249257
for await event in stream {
250258
let isCompletionPanel = {
251259
event.element.firstChild { element in
@@ -263,11 +271,11 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
263271
}
264272
default: break
265273
}
266-
274+
267275
try Task.checkCancellation()
268276
}
269277
}
270-
278+
271279
longRunningTasks.insert(completionPanelTask)
272280
}
273281

0 commit comments

Comments
 (0)