Skip to content

Commit cb94ad2

Browse files
committed
Fix that activate app brings all windows to front
1 parent bcf63e0 commit cb94ad2

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

Tool/Sources/AppActivator/AppActivator.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public extension NSWorkspace {
1616
if activated { return }
1717

1818
// Fallback solution
19-
20-
let axApplication = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier)
21-
axApplication.isFrontmost = true
19+
20+
let axApplication = AXUIElementCreateApplication(
21+
ProcessInfo.processInfo.processIdentifier
22+
)
23+
activateAppElement(axApplication)
2224
//
2325
// let appleScript = """
2426
// tell application "System Events"
@@ -35,19 +37,28 @@ public extension NSWorkspace {
3537
guard let app = await XcodeInspector.shared.safe.previousActiveApplication
3638
else { return }
3739
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
38-
app.appElement.isFrontmost = true
39-
// _ = app.activate()
40+
activateApp(app)
4041
}
4142
}
4243

4344
static func activatePreviousActiveXcode(delay: TimeInterval = 0.2) {
4445
Task { @MainActor in
4546
guard let app = await XcodeInspector.shared.safe.latestActiveXcode else { return }
4647
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
47-
app.appElement.isFrontmost = true
48-
// _ = app.activate()
48+
activateApp(app)
49+
}
50+
}
51+
52+
static func activateApp(_ app: AppInstanceInspector) {
53+
// we prefer `.activate()` because it only brings the active window to the front
54+
if !app.activate() {
55+
activateAppElement(app.appElement)
4956
}
5057
}
58+
59+
static func activateAppElement(_ appElement: AXUIElement) {
60+
appElement.isFrontmost = true
61+
}
5162
}
5263

5364
struct ActivateThisAppDependencyKey: DependencyKey {

Tool/Sources/XcodeInspector/XcodeInspector+TriggerCommand.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ public extension AppInstanceInspector {
3030
guard path.count >= 2 else { throw cantRunCommand("Path too short.") }
3131

3232
if activateApp {
33-
appElement.isFrontmost = true
34-
// if !runningApplication.activate() {
35-
// Logger.service.error("""
36-
// Trigger menu item \(sourcePath): \
37-
// Xcode not activated.
38-
// """)
39-
// }
33+
if !runningApplication.isActive {
34+
// we prefer `.activate()` because it only brings the active window to the front
35+
if !activate() {
36+
appElement.isFrontmost = true
37+
}
38+
}
4039
} else {
4140
if !runningApplication.isActive {
4241
Logger.service.error("""

0 commit comments

Comments
 (0)