Repository Public
1import AppKit2import SwiftUI3import SheetCore4import SheetData5import SheetUI6import FeatureData78// Development harness for the data screens of ORIX Sheet.9// data-preview [--pane data|sql|import|insert|refused|empty|missing|sidebar|transform|transform-sql|pivot|pivot-create]10// [--shot out.png] [--dark] [--size WxH] [--lang en|fr-CA] [--rows N] [--bench]11// It builds a demo workbook, generates clearly synthetic sample files in a temporary directory12// (a streamed orders CSV of --rows rows, default 1,000,000; a small Parquet written by the engine)13// and sets up a meaningful state for the chosen pane. With --shot the window content is rendered14// to a PNG by the app itself and the process exits. With --bench it measures grid paging latency.1516let arguments = CommandLine.arguments17func option(_ flag: String) -> String? { arguments.firstIndex(of: flag).flatMap { $0 + 1 < arguments.count ? arguments[$0 + 1] : nil } }18// the harness is not a localized app bundle: the package honours this variable instead (must be set before the first string is read)19if let language = option("--lang") { setenv("ORIX_DATA_LANGUAGE", language, 1) }2021@MainActor final class AppDelegate: NSObject, NSApplicationDelegate {22 var window: NSWindow!23 var session: WorkbookSession!24 let pane = option("--pane") ?? "data"25 let rows = option("--rows").flatMap { Int($0) } ?? 1_000_0002627 func applicationDidFinishLaunching(_ notification: Notification) {28 var size = NSSize(width: 1440, height: 900)29 if let s = option("--size")?.split(separator: "x").compactMap({ Double($0) }), s.count == 2 { size = NSSize(width: s[0], height: s[1]) }30 if arguments.contains("--dark") { NSApp.appearance = NSAppearance(named: .darkAqua) }31 DataFeature.register()32 session = WorkbookSession(workbook: Workbook(sheetNames: ["Sales", "Notes"]), locale: Locale(identifier: "en_US"))33 session.showSidebar = false34 session.inspectorPane = nil35 window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: [.titled, .closable, .resizable, .miniaturizable], backing: .buffered, defer: false)36 window.title = "ORIX Sheet — data preview (\(pane))"37 let root: AnyView = pane == "sidebar" ? AnyView(Scenes.sidebar(session).frame(width: size.width, height: size.height)) : AnyView(WorkbookView(session: session))38 window.contentView = NSHostingView(rootView: root)39 window.center()40 window.makeKeyAndOrderFront(nil)41 NSApp.activate(ignoringOtherApps: false)42 Task {43 do {44 try await Scenes.prepare(pane, session: session, rows: rows)45 } catch {46 FileHandle.standardError.write(Data("preview setup failed: \(error)\n".utf8))47 if arguments.contains("--shot") || arguments.contains("--bench") { exit(3) }48 }49 if arguments.contains("--bench") {50 await Bench.run(session: session, rows: rows)51 if option("--shot") == nil { exit(0) }52 }53 if let out = option("--shot") { await shoot(out) }54 }55 }5657 /// Waits until nothing is loading, lets the views settle, then renders the window content to a PNG.58 func shoot(_ path: String) async {59 let start = Date()60 while !DataAutomation.isSettled(session), Date().timeIntervalSince(start) < 60 { try? await Task.sleep(for: .milliseconds(50)) }61 try? await Task.sleep(for: .milliseconds(900))62 guard let view = window.contentView, let rep = view.bitmapImageRepForCachingDisplay(in: view.bounds) else { exit(2) }63 view.cacheDisplay(in: view.bounds, to: rep)64 try? FileManager.default.createDirectory(at: URL(fileURLWithPath: path).deletingLastPathComponent(), withIntermediateDirectories: true)65 try? rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))66 print("wrote \(path)")67 exit(0)68 }6970 func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true }71}7273let app = NSApplication.shared74let delegate = AppDelegate()75app.delegate = delegate76app.setActivationPolicy(arguments.contains("--shot") || arguments.contains("--bench") ? .accessory : .regular)77app.run()78