Skip to content

Commit 09e679b

Browse files
committed
Fix that paths were not updated
1 parent 290610b commit 09e679b

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

Core/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public final class XcodeInspector: ObservableObject {
9191
}
9292

9393
func observeXcode(_ xcode: XcodeAppInstanceInspector) {
94+
activeDocumentURL = xcode.documentURL
95+
activeProjectURL = xcode.projectURL
96+
focusedWindow = xcode.focusedWindow
97+
9498
xcode.$documentURL.filter { _ in xcode.isActive }.assign(to: &$activeDocumentURL)
9599
xcode.$projectURL.filter { _ in xcode.isActive }.assign(to: &$activeProjectURL)
96100
xcode.$focusedWindow.filter { _ in xcode.isActive }.assign(to: &$focusedWindow)
@@ -123,6 +127,8 @@ public final class XcodeInspector: ObservableObject {
123127
}
124128
}
125129
}
130+
131+
activeXcodeObservations.insert(focusedElementChanged)
126132
}
127133
}
128134

@@ -143,6 +149,7 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
143149
@Published var projectURL: URL = .init(fileURLWithPath: "/")
144150
@Published var tabs: Set<String> = []
145151
private var longRunningTasks = Set<Task<Void, Error>>()
152+
private var focusedWindowObservations = Set<AnyCancellable>()
146153

147154
deinit {
148155
for task in longRunningTasks { task.cancel() }
@@ -195,11 +202,31 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
195202

196203
func observeFocusedWindow() {
197204
if let window = appElement.focusedWindow {
198-
let window = XcodeWindowInspector(uiElement: window)
199-
focusedWindow = window
200-
if let workspaceWindow = window as? WorkspaceXcodeWindowInspector {
201-
workspaceWindow.$documentURL.assign(to: &$documentURL)
202-
workspaceWindow.$projectURL.assign(to: &$projectURL)
205+
if window.identifier == "Xcode.WorkspaceWindow" {
206+
let window = WorkspaceXcodeWindowInspector(
207+
app: runningApplication,
208+
uiElement: window
209+
)
210+
focusedWindow = window
211+
focusedWindowObservations.forEach { $0.cancel() }
212+
focusedWindowObservations.removeAll()
213+
214+
documentURL = window.documentURL
215+
projectURL = window.projectURL
216+
217+
window.$documentURL
218+
.filter { $0 != .init(fileURLWithPath: "/") }
219+
.sink { [weak self] url in
220+
self?.documentURL = url
221+
}.store(in: &focusedWindowObservations)
222+
window.$projectURL
223+
.filter { $0 != .init(fileURLWithPath: "/") }
224+
.sink { [weak self] url in
225+
self?.projectURL = url
226+
}.store(in: &focusedWindowObservations)
227+
} else {
228+
let window = XcodeWindowInspector(uiElement: window)
229+
focusedWindow = window
203230
}
204231
} else {
205232
focusedWindow = nil

Core/Sources/XcodeInspector/XcodeWindowInspector.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
2424
focusedElementChangedTask?.cancel()
2525
}
2626

27-
init(app: NSRunningApplication, uiElement: AXUIElement) {
27+
public init(app: NSRunningApplication, uiElement: AXUIElement) {
2828
self.app = app
2929
super.init(uiElement: uiElement)
3030

3131
focusedElementChangedTask = Task { @MainActor in
3232
let update = {
33-
let documentURL = Self.extractDocumentURL(app, windowElement: uiElement)
33+
let documentURL = Self.extractDocumentURL(windowElement: uiElement)
3434
if let documentURL {
3535
self.documentURL = documentURL
3636
}
3737
let projectURL = Self.extractProjectURL(
38-
app,
3938
windowElement: uiElement,
4039
fileURL: documentURL
4140
)
@@ -47,7 +46,6 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
4746
update()
4847
let notifications = AXNotificationStream(
4948
app: app,
50-
element: uiElement,
5149
notificationNames: kAXFocusedUIElementChangedNotification
5250
)
5351

@@ -59,12 +57,10 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
5957
}
6058

6159
static func extractDocumentURL(
62-
_ app: NSRunningApplication,
6360
windowElement: AXUIElement
6461
) -> URL? {
65-
// fetch file path of the frontmost window of Xcode through Accessability API.
66-
let application = AXUIElementCreateApplication(app.processIdentifier)
67-
var path = windowElement.document
62+
// fetch file path of the frontmost window of Xcode through Accessibility API.
63+
let path = windowElement.document
6864
if let path = path?.removingPercentEncoding {
6965
let url = URL(
7066
fileURLWithPath: path
@@ -76,13 +72,10 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
7672
}
7773

7874
static func extractProjectURL(
79-
_ app: NSRunningApplication,
8075
windowElement: AXUIElement,
8176
fileURL: URL?
8277
) -> URL? {
83-
let application = AXUIElementCreateApplication(app.processIdentifier)
84-
let focusedWindow = application.focusedWindow
85-
for child in focusedWindow?.children ?? [] {
78+
for child in windowElement.children {
8679
if child.description.starts(with: "/"), child.description.count > 1 {
8780
let path = child.description
8881
let trimmedNewLine = path.trimmingCharacters(in: .newlines)

0 commit comments

Comments
 (0)