Sources/Orgstar/DevelopmentHooks.swift

49 lines · 3144 bytes

 1import AppKit
 2import OrgEditorAppKit
 3
 4/// Switches for checking the app from scripts, read from the environment:
 5/// - `ORGSTAR_DATA_DIR`: where the index and roots live (see `WorkspaceModel.defaultDirectory`).
 6/// - `ORGSTAR_OPEN`: a file to select at launch.
 7/// - `ORGSTAR_ROOT`: a folder to add at launch.
 8/// - `ORGSTAR_WINDOW`: a window to open at launch (`agenda`, `settings`); the snapshot renders it.
 9/// - `ORGSTAR_COMMAND`: commands to run at launch, separated by commas (`app.import-emacs`); a
10///   sheet or prompt they open is what the snapshot renders.
11/// - `ORGSTAR_ANSWERS`: answers to the prompts those commands ask, in order, separated by commas.
12/// - `ORGSTAR_URL`: an `org-protocol://` link to handle at launch, as if opened from a browser.
13/// - `ORGSTAR_PASTEBOARD`: the name of a pasteboard to paste images and files from, instead of
14///   the clipboard.
15/// - `ORGSTAR_APPEARANCE`: `dark` or `light`, instead of the system's.
16/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
17///   window to this PNG path and quit. Rendering from inside the app needs no screen-recording
18///   permission.
19enum DevelopmentHooks {
20    static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
21    static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
22    static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
23    static var commandToRun: String? { ProcessInfo.processInfo.environment["ORGSTAR_COMMAND"] }
24    static var urlToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_URL"] }
25    static var answers: [String] { ProcessInfo.processInfo.environment["ORGSTAR_ANSWERS"]?.split(separator: ",").map(String.init) ?? [] }
26
27    @MainActor
28    static func scheduleSnapshot() {
29        let environment = ProcessInfo.processInfo.environment
30        if let name = environment["ORGSTAR_PASTEBOARD"] { OrgEditor.pasteboard = NSPasteboard(name: NSPasteboard.Name(name)) }
31        if let appearance = environment["ORGSTAR_APPEARANCE"] {
32            NSApp.appearance = NSAppearance(named: appearance == "dark" ? .darkAqua : .aqua)
33        }
34        guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
35        let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
36        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
37            let visible = NSApp.windows.filter(\.isVisible)
38            let chosen = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
39            let window = chosen?.attachedSheet ?? chosen
40            if let view = window?.contentView?.superview,
41               let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
42                view.cacheDisplay(in: view.bounds, to: bitmap)
43                try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
44            }
45            if let chosen, let sheet = chosen.attachedSheet { chosen.endSheet(sheet) }
46            NSApp.terminate(nil)
47        }
48    }
49}