Branchwell

Jobley / app · jobley.ai / Apps / Mac / Snapshot.swift

1#if DEBUG
2import AppKit
3
4/// Debug-only proof capture that works while the Mac is locked: draws the real desk window (or the
5/// sheet in front of it) into a PNG. `JOBLEY_SNAPSHOT=/path/shot.png` captures once the window has
6/// loaded, then quits. The glass sidebar is drawn by the window server, so it needs a live capture.
7@MainActor
8enum Snapshot {
9 static func runIfRequested() {
10 guard let path = ProcessInfo.processInfo.environment["JOBLEY_SNAPSHOT"] else { return }
11 let delay = Double(ProcessInfo.processInfo.environment["JOBLEY_SNAPSHOT_DELAY"] ?? "") ?? 5
12 DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
13 let main = NSApp.windows.first { $0.isVisible && $0.contentView != nil && $0.sheetParent == nil }
14 let window = main?.sheets.first ?? main
15 guard let window, let view = window.contentView?.superview ?? window.contentView,
16 let rep = view.bitmapImageRepForCachingDisplay(in: view.bounds) else { exit(2) }
17 view.cacheDisplay(in: view.bounds, to: rep)
18 try? rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
19 exit(0)
20 }
21 }
22}
23#endif