krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Sources/Orgstar/DevelopmentHooks.swift

85c633a83a4744bef9dde8b52462f9c2a1c037e4
orgstar/Sources/Orgstar/DevelopmentHooks.swift history · blame · raw

26 lines · 1307 bytes

 1import AppKit
 2
 3/// Switches for checking the app from scripts, read from the environment:
 4/// - `ORGSTAR_DATA_DIR`: where the index and roots live (see `WorkspaceModel.defaultDirectory`).
 5/// - `ORGSTAR_OPEN`: a file to select at launch.
 6/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
 7///   window to this PNG path and quit. Rendering from inside the app needs no screen-recording
 8///   permission.
 9enum DevelopmentHooks {
10    static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
11
12    @MainActor
13    static func scheduleSnapshot() {
14        let environment = ProcessInfo.processInfo.environment
15        guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
16        let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
17        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
18            if let view = NSApp.windows.first(where: \.isVisible)?.contentView?.superview,
19               let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
20                view.cacheDisplay(in: view.bounds, to: bitmap)
21                try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
22            }
23            NSApp.terminate(nil)
24        }
25    }
26}