feat: add independent CLI launches and preserve shared video bookmarks
This commit is contained in:
@@ -4,6 +4,10 @@ import MediaPlayer
|
||||
@_cdecl("terebi_app_main")
|
||||
@MainActor
|
||||
public func checkAppStartup() {
|
||||
if let configuration = Bundle.main.url(forResource: "cli-check", withExtension: "json") {
|
||||
CLIInstanceCheck.run(configuration)
|
||||
return
|
||||
}
|
||||
AppStartupCheck.run()
|
||||
}
|
||||
|
||||
@@ -57,3 +61,47 @@ private enum AppStartupCheck {
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private enum CLIInstanceCheck {
|
||||
static var timer: Timer?
|
||||
|
||||
static func run(_ configuration: URL) {
|
||||
let settings = try! JSONSerialization.jsonObject(with: Data(contentsOf: configuration)) as! [String: String]
|
||||
let directory = URL(fileURLWithPath: settings["directory"]!, isDirectory: true)
|
||||
setenv("PATH", settings["path"]!, 1)
|
||||
_ = NSApplication.shared
|
||||
PlayerModel.shared.setVolume(0)
|
||||
// Exercise the real SwiftUI scene and delegate without displaying test windows.
|
||||
_ = NotificationCenter.default.addObserver(forName: NSWindow.didUpdateNotification, object: nil, queue: .main) { _ in
|
||||
MainActor.assumeIsolated { for window in NSApp.windows { window.alphaValue = 0 } }
|
||||
}
|
||||
timer = Timer.scheduledTimer(withTimeInterval: 0.05, repeats: true) { _ in
|
||||
MainActor.assumeIsolated {
|
||||
let model = PlayerModel.shared
|
||||
let pid = ProcessInfo.processInfo.processIdentifier
|
||||
let windows = NSApp.windows.filter { !($0 is NSPanel) && $0.contentView != nil }
|
||||
for window in windows { window.alphaValue = 0 }
|
||||
let applications = NSWorkspace.shared.runningApplications.filter {
|
||||
!$0.isTerminated && $0.bundleURL == Bundle.main.bundleURL
|
||||
}
|
||||
let snapshot: [String: Any] = [
|
||||
"pid": pid,
|
||||
"pids": applications.map(\.processIdentifier).sorted(),
|
||||
"media": model.fileName ?? "",
|
||||
"status": model.state.status.rawValue,
|
||||
"position": model.state.position,
|
||||
"windows": windows.count,
|
||||
"hasAudio": model.state.has_audio,
|
||||
"hasVideo": model.state.has_video,
|
||||
"error": model.errorText ?? "",
|
||||
]
|
||||
do {
|
||||
let data = try JSONSerialization.data(withJSONObject: snapshot)
|
||||
try data.write(to: directory.appendingPathComponent("state-\(pid).json"), options: .atomic)
|
||||
} catch { fatalError("Could not publish CLI instance check: \(error)") }
|
||||
}
|
||||
}
|
||||
TerebiApp.main()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user