Sources/Orgstar/DevelopmentHooks.swift
32 lines · 1817 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_ROOT`: a folder to add at launch.
7/// - `ORGSTAR_WINDOW`: a window to open at launch (`agenda`, `settings`); the snapshot renders it.
8/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
9/// window to this PNG path and quit. Rendering from inside the app needs no screen-recording
10/// permission.
11enum DevelopmentHooks {
12 static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
13 static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
14 static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
15
16 @MainActor
17 static func scheduleSnapshot() {
18 let environment = ProcessInfo.processInfo.environment
19 guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
20 let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
21 DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
22 let visible = NSApp.windows.filter(\.isVisible)
23 let window = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
24 if let view = window?.contentView?.superview,
25 let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
26 view.cacheDisplay(in: view.bounds, to: bitmap)
27 try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
28 }
29 NSApp.terminate(nil)
30 }
31 }
32}