Sources/Orgstar/DevelopmentHooks.swift

0dc8dea033dd64cad6c9b4130124d1c229454342
orgstar/Sources/Orgstar/DevelopmentHooks.swift

59 lines · 4097 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///   for Replace in Files and the renames, the two names, then the search runs.
14/// - `ORGSTAR_URL`: an `org-protocol://` link to handle at launch, as if opened from a browser.
15/// - `ORGSTAR_PASTEBOARD`: the name of a pasteboard to paste images and files from, instead of
16///   the clipboard.
17/// - `ORGSTAR_APPEARANCE`: `dark` or `light`, instead of the system's.
18/// - `ORGSTAR_SNAPSHOT`: after `ORGSTAR_SNAPSHOT_DELAY` seconds (default 3), render the main
19///   window to this PNG path and quit. Rendering from inside the app needs no screen-recording
20///   permission.
21enum DevelopmentHooks {
22    static var fileToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_OPEN"] }
23    static var rootToAdd: String? { ProcessInfo.processInfo.environment["ORGSTAR_ROOT"] }
24    static var windowToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_WINDOW"] }
25    static var commandToRun: String? { ProcessInfo.processInfo.environment["ORGSTAR_COMMAND"] }
26    static var urlToOpen: String? { ProcessInfo.processInfo.environment["ORGSTAR_URL"] }
27    static var answers: [String] { ProcessInfo.processInfo.environment["ORGSTAR_ANSWERS"]?.split(separator: ",").map(String.init) ?? [] }
28
29    @MainActor
30    static func scheduleSnapshot() {
31        let environment = ProcessInfo.processInfo.environment
32        if let name = environment["ORGSTAR_PASTEBOARD"] { OrgEditor.pasteboard = NSPasteboard(name: NSPasteboard.Name(name)) }
33        if let appearance = environment["ORGSTAR_APPEARANCE"] {
34            NSApp.appearance = NSAppearance(named: appearance == "dark" ? .darkAqua : .aqua)
35        }
36        guard let path = environment["ORGSTAR_SNAPSHOT"] else { return }
37        let delay = Double(environment["ORGSTAR_SNAPSHOT_DELAY"] ?? "") ?? 3
38        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
39            let visible = NSApp.windows.filter(\.isVisible)
40            let chosen = windowToOpen.flatMap { id in visible.first { $0.identifier?.rawValue.lowercased().contains(id) == true } } ?? visible.first
41            let window = chosen?.attachedSheet ?? chosen
42            // The window server's image of the app's own window, which needs no permission and,
43            // unlike cacheDisplay, draws the sidebar. Swift can't call it directly since macOS 15.
44            typealias Capture = @convention(c) (CGRect, CGWindowListOption, CGWindowID, CGWindowImageOption) -> Unmanaged<CGImage>?
45            if let window, let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CGWindowListCreateImage"),
46               let image = unsafeBitCast(symbol, to: Capture.self)(.null, .optionIncludingWindow, CGWindowID(window.windowNumber),
47                                                                   [.boundsIgnoreFraming, .bestResolution])?.takeRetainedValue() {
48                try? NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
49            } else if let view = window?.contentView?.superview,
50               let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) {
51                view.cacheDisplay(in: view.bounds, to: bitmap)
52                try? bitmap.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))
53            }
54            if let chosen, let sheet = chosen.attachedSheet { chosen.endSheet(sheet) }
55            NSApp.terminate(nil)
56        }
57    }
58}
59#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form