Sources/Orgstar/DevelopmentHooks.swift
58 lines · 4016 bytes
1#if !APPSTORE
2import AppKit
3import OrgEditorAppKit
4
5/// Switches for checking the app from scripts, read from the environment:
6/// - `ORGSTAR_DATA_DIR`: where the index and roots live (see `WorkspaceModel.defaultDirectory`).
7/// - `ORGSTAR_OPEN`: a file to select at launch.
8/// - `ORGSTAR_ROOT`: a folder to add at launch.
9/// - `ORGSTAR_WINDOW`: a window to open at launch (`agenda`, `settings`); the snapshot renders it.
10/// - `ORGSTAR_COMMAND`: commands to run at launch, separated by commas (`app.import-emacs`); a
11/// sheet or prompt they open is what the snapshot renders.
12/// - `ORGSTAR_ANSWERS`: answers to the prompts those commands ask, in order, separated by commas.
13/// - `ORGSTAR_URL`: an `org-protocol://` link to handle at launch, as if opened from a browser.
14/// - `ORGSTAR_PASTEBOARD`: the name of a pasteboard to paste images and files from, instead of
15/// the clipboard.
16/// - `ORGSTAR_APPEARANCE`: `dark` or `light`, instead of the system's.
17/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
18/// window to this PNG path and quit. Rendering from inside the app needs no screen-recording
19/// permission.
20enum DevelopmentHooks {
21 static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
22 static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
23 static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
24 static var commandToRun: String? { ProcessInfo.processInfo.environment["ORGSTAR_COMMAND"] }
25 static var urlToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_URL"] }
26 static var answers: [String] { ProcessInfo.processInfo.environment["ORGSTAR_ANSWERS"]?.split(separator: ",").map(String.init) ?? [] }
27
28 @MainActor
29 static func scheduleSnapshot() {
30 let environment = ProcessInfo.processInfo.environment
31 if let name = environment["ORGSTAR_PASTEBOARD"] { OrgEditor.pasteboard = NSPasteboard(name: NSPasteboard.Name(name)) }
32 if let appearance = environment["ORGSTAR_APPEARANCE"] {
33 NSApp.appearance = NSAppearance(named: appearance == "dark" ? .darkAqua : .aqua)
34 }
35 guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
36 let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
37 DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
38 let visible = NSApp.windows.filter(\.isVisible)
39 let chosen = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
40 let window = chosen?.attachedSheet ?? chosen
41 // The window server's image of the app's own window, which needs no permission and,
42 // unlike cacheDisplay, draws the sidebar. Swift can't call it directly since macOS 15.
43 typealias Capture = @convention(c) (CGRect, CGWindowListOption, CGWindowID, CGWindowImageOption) -> Unmanaged<CGImage>?
44 if let window, let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CGWindowListCreateImage"),
45 let image = unsafeBitCast(symbol, to: Capture.self)(.null, .optionIncludingWindow, CGWindowID(window.windowNumber),
46 [.boundsIgnoreFraming, .bestResolution])?.takeRetainedValue() {
47 try? NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
48 } else if let view = window?.contentView?.superview,
49 let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
50 view.cacheDisplay(in: view.bounds, to: bitmap)
51 try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
52 }
53 if let chosen, let sheet = chosen.attachedSheet { chosen.endSheet(sheet) }
54 NSApp.terminate(nil)
55 }
56 }
57}
58#endif