Skip to content

Commit 68ed1eb

Browse files
committed
fix: compile native entry point and AX sheet detection on macOS
1 parent 36f642f commit 68ed1eb

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import AppKit
2+
3+
@main
4+
struct ApplicationMain {
5+
@MainActor static func main() {
6+
if CommandLine.arguments.contains("--script-worker") { ScriptRunner.worker() }
7+
if CommandLine.arguments.contains("--mock-app-server") { SelfTest.mockServer() }
8+
if CommandLine.arguments.contains("--self-test") { SelfTest.run() }
9+
let app = NSApplication.shared
10+
app.setActivationPolicy(.accessory)
11+
let controller = Controller()
12+
app.delegate = controller
13+
withExtendedLifetime(controller) { app.run() }
14+
}
15+
}

‎macos/Sources/CodexUsageBar/HTTPFixture.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ enum HTTPFixture {
2020
}
2121
listener.start(queue: queue)
2222
defer { listener.cancel() }
23-
let started = await Task.detached { ready.wait(timeout: .now() + 5) == .success }.value
23+
let started = await withCheckedContinuation { continuation in
24+
DispatchQueue.global(qos: .utility).async {
25+
continuation.resume(returning: ready.wait(timeout: .now() + 5) == .success)
26+
}
27+
}
2428
guard started, let port = listener.port else { throw UsageError.message("Loopback listener failed") }
2529
let base = "http://127.0.0.1:\(port.rawValue)"
2630
let data = try await UsageHTTP().fetch(["url": base + "/ok", "headers": ["Authorization": "Bearer synthetic"]], timeout: 2)

‎macos/Sources/CodexUsageBar/ScriptRunner.swift‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ enum ScriptRunner {
2828

2929
static func evaluate(_ script: String) async throws -> Any {
3030
guard script.utf8.count <= limit else { throw UsageError.message("Usage script exceeds 2 MiB") }
31-
return try await Task.detached(priority: .utility) {
31+
return try await withCheckedThrowingContinuation { continuation in
32+
DispatchQueue.global(qos: .utility).async {
33+
do { continuation.resume(returning: try evaluateBlocking(script)) }
34+
catch { continuation.resume(throwing: error) }
35+
}
36+
}
37+
}
38+
39+
private static func evaluateBlocking(_ script: String) throws -> Any {
3240
let child = Process(), input = Pipe(), output = Pipe()
3341
child.executableURL = Bundle.main.executableURL ?? URL(fileURLWithPath: CommandLine.arguments[0])
3442
child.arguments = ["--script-worker"]
@@ -57,7 +65,6 @@ enum ScriptRunner {
5765
child.waitUntilExit()
5866
guard child.terminationStatus == 0, let data = result.data else { throw UsageError.message("Usage script failed") }
5967
return try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
60-
}.value
6168
}
6269

6370
static func code(_ provider: Provider) -> String {

‎macos/Sources/CodexUsageBar/WindowFollower.swift‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import ApplicationServices
4444
AXUIElementSetMessagingTimeout(axApp, 0.2)
4545
let windows = (value(axApp, kAXWindowsAttribute) as? [AXUIElement] ?? []).filter { standard($0) }
4646
if let main = value(axApp, kAXMainWindowAttribute), CFGetTypeID(main) == AXUIElementGetTypeID(), standard(main as! AXUIElement) {
47-
target = main as! AXUIElement
47+
target = (main as! AXUIElement)
4848
} else if let previous = target, windows.contains(where: { CFEqual($0, previous) }) { target = previous }
4949
else { target = windows.first }
5050
if observedPID != app.processIdentifier || observer == nil {
@@ -109,7 +109,8 @@ import ApplicationServices
109109
// Do not lift above Codex's file picker or sheet while it has focus.
110110
let axApp = AXUIElementCreateApplication(app.processIdentifier)
111111
let focused = value(axApp, kAXFocusedWindowAttribute)
112-
let sheets = value(target, kAXSheetsAttribute) as? [AXUIElement] ?? []
112+
let children = (value(target, kAXChildrenAttribute) as? [AXUIElement]) ?? []
113+
let sheets = children.filter { value($0, kAXRoleAttribute) as? String == kAXSheetRole }
113114
let mainFocused = focused.map { CFEqual($0, target) } == true && sheets.isEmpty
114115
if mainFocused { panel.orderFrontRegardless() }
115116
else { orderAboveTarget(app, rect: rect) }

‎macos/Sources/CodexUsageBar/main.swift‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)