SPB Git

Repository Public

main.swift 7.8 KB 164 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetUI/Sources/sheet-preview/main.swift10// Module           : sheet-preview11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import AppKit88import SwiftUI89import SheetCore90import SheetUI9192// Development harness: opens a workbook window with demo content.93//   sheet-preview [--shot out.png] [--dark] [--size WxH]94// With --shot the window content is rendered to a PNG by the app itself and the process exits.9596@MainActor func demoWorkbook() -> WorkbookSession {97    let wb = Workbook(sheetNames: ["Budget", "Data"])98    let session = WorkbookSession(workbook: wb, locale: Locale(identifier: "en_US"))99    let rows: [[String]] = [100        ["Household budget", "", "", "", ""],101        ["Category", "Planned", "Actual", "Difference", "Share"],102        ["Housing", "1850", "1850", "=C3-B3", "=C3/$C$9"],103        ["Groceries", "720", "784.35", "=C4-B4", "=C4/$C$9"],104        ["Transport", "310", "268.9", "=C5-B5", "=C5/$C$9"],105        ["Utilities", "240", "251.12", "=C6-B6", "=C6/$C$9"],106        ["Leisure", "200", "342", "=C7-B7", "=C7/$C$9"],107        ["Savings", "600", "600", "=C8-B8", "=C8/$C$9"],108        ["Total", "=SUM(B3:B8)", "=SUM(C3:C8)", "=C9-B9", "=SUM(E3:E8)"],109    ]110    session.perform("Demo") { e in111        for (r, row) in rows.enumerated() { for (c, text) in row.enumerated() where !text.isEmpty {112            try? e.setInput(sheet: 0, row: r, col: c, text, parser: session.parser)113        } }114        e.setStyle(sheet: 0, range: RangeRef(a1: "A1")!) { $0.bold = true; $0.fontSize = 18 }115        e.setStyle(sheet: 0, range: RangeRef(a1: "A2:E2")!) { $0.bold = true; $0.fill = RGBA(hex: 0x0891B2); $0.textColor = .white; $0.hAlign = .center }116        e.setStyle(sheet: 0, range: RangeRef(a1: "A9:E9")!) { $0.bold = true; $0.borders.top = BorderEdge(style: .medium, color: RGBA(hex: 0x0891B2)) }117        e.setStyle(sheet: 0, range: RangeRef(a1: "B3:D9")!) { $0.numberFormat = "#,##0.00" }118        e.setStyle(sheet: 0, range: RangeRef(a1: "E3:E9")!) { $0.numberFormat = "0.0%" }119        e.editMeta(sheet: 0) { m in m.colWidths[0] = 150; m.rowHeights[0] = 32; m.frozenRows = 2; m.comments[CellRef(6, 2)] = CellComment(author: "Demo", text: "Concert tickets") }120        e.setFormula(sheet: 0, row: 11, col: 0, "=SEQUENCE(3,2)")121        e.set(sheet: 0, row: 11, col: 3, .value(.text("A long text that overflows into the empty neighbour cells")))122    }123    session.select(RangeRef(a1: "C4:D6")!, active: CellRef(a1: "C4")!)124    return session125}126127final class AppDelegate: NSObject, NSApplicationDelegate {128    var window: NSWindow!129    var session: WorkbookSession!130131    func applicationDidFinishLaunching(_ notification: Notification) {132        let args = CommandLine.arguments133        func value(_ flag: String) -> String? { args.firstIndex(of: flag).flatMap { $0 + 1 < args.count ? args[$0 + 1] : nil } }134        var size = NSSize(width: 1280, height: 800)135        if let s = value("--size")?.split(separator: "x").compactMap({ Double($0) }), s.count == 2 { size = NSSize(width: s[0], height: s[1]) }136        if args.contains("--dark") { NSApp.appearance = NSAppearance(named: .darkAqua) }137        session = demoWorkbook()138        window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: [.titled, .closable, .resizable, .miniaturizable], backing: .buffered, defer: false)139        window.title = "ORIX Sheet — preview"140        window.contentView = NSHostingView(rootView: WorkbookView(session: session))141        window.center()142        window.makeKeyAndOrderFront(nil)143        NSApp.activate(ignoringOtherApps: false)144        if let out = value("--shot") {145            DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) {146                MainActor.assumeIsolated {147                    guard let view = self.window.contentView, let rep = view.bitmapImageRepForCachingDisplay(in: view.bounds) else { exit(2) }148                    view.cacheDisplay(in: view.bounds, to: rep)149                    try? rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: out))150                    if let g = self.session.gridView { print(String(format: "grid draw: %.2f ms (%d draws)", g.lastDrawDuration * 1000, g.drawCount)) }151                    exit(0)152                }153            }154        }155    }156    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true }157}158159let app = NSApplication.shared160let delegate = AppDelegate()161app.delegate = delegate162app.setActivationPolicy(CommandLine.arguments.contains("--shot") ? .accessory : .regular)163app.run()164
Files main
Menu

Theme