SPB Git

Repository Public MIT

ORIXCalcApp.swift 3.7 KB 92 lines swift

1// ORIX Calc2// File: App/Sources/ORIXCalcApp.swift3// Description: App entry point — creates the shared session, wires launch options, keyboard commands and document opening.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import ORIXUI9import ORIXFeatureCalculator1011/// Options read from launch arguments. They exist for UI tests and for capturing real screenshots; they never change calculations.12struct LaunchOptions {13    var mode: CalcMode?14    var section: String?15    var demo = false16    var inMemory = false17    var companion = false18    var appearance: AppearancePreference?19    var expression: String?2021    init(arguments: [String] = ProcessInfo.processInfo.arguments) {22        func value(_ flag: String) -> String? {23            guard let i = arguments.firstIndex(of: flag), i + 1 < arguments.count else { return nil }24            return arguments[i + 1]25        }26        mode = value("-ORIXMode").flatMap(CalcMode.init(rawValue:))27        section = value("-ORIXSection")28        demo = value("-ORIXDemo") == "1"29        inMemory = value("-ORIXInMemory") == "1"30        companion = value("-ORIXCompanion") == "1"31        appearance = value("-ORIXAppearance").flatMap(AppearancePreference.init(rawValue:))32        expression = value("-ORIXExpression")33    }34}3536@main37struct ORIXCalcApp: App {38    @State private var app: AppModel39    @State private var calculator: CalculatorModel40    private let options: LaunchOptions4142    init() {43        let options = LaunchOptions()44        self.options = options45        let settings: AppSettings? = options.inMemory ? AppSettings(defaults: UserDefaults(suiteName: "orix.calc.ephemeral.\(UUID().uuidString)") ?? .standard) : nil46        let model = AppModel(settings: settings, inMemory: options.inMemory)47        if let appearance = options.appearance { model.settings.data.appearance = appearance }48        if let mode = options.mode { model.mode = mode }49        FeatureRegistry.registerNatives(model)50        _app = State(initialValue: model)51        let calc = CalculatorModel(app: model)52        if let e = options.expression { _ = calc.setExpression(e); calc.evaluate() }53        _calculator = State(initialValue: calc)54    }5556    var body: some Scene {57        WindowGroup {58            RootView(calculator: calculator, options: options)59                .environment(app)60                .preferredColorScheme(app.settings.data.appearance.colorScheme)61                .onOpenURL { url in openDocument(url) }62        }63        .commands {64            CommandMenu("Environment") {65                ForEach(CalcMode.allCases) { mode in66                    Button(mode.title) { app.mode = mode }67                        .keyboardShortcut(KeyEquivalent(Character("\(mode.shortcutNumber)")), modifiers: .command)68                }69            }70            CommandGroup(after: .sidebar) {71                Button("History") { NotificationCenter.default.post(name: .orixShowHistory, object: nil) }72                    .keyboardShortcut("y", modifiers: .command)73            }74        }75    }7677    private func openDocument(_ url: URL) {78        let scoped = url.startAccessingSecurityScopedResource()79        defer { if scoped { url.stopAccessingSecurityScopedResource() } }80        guard let data = try? Data(contentsOf: url) else {81            NotificationCenter.default.post(name: .orixNotice, object: "The file could not be read.")82            return83        }84        if let notice = app.openWorkspaceFile(data) { NotificationCenter.default.post(name: .orixNotice, object: notice) }85    }86}8788extension Notification.Name {89    static let orixShowHistory = Notification.Name("orix.showHistory")90    static let orixNotice = Notification.Name("orix.notice")91}92
Files main
Menu

Theme