Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Cosmos9// File : App/Sources/RootView.swift10// Description : The application's shared state object: one AppModel11// (CosmosUI) for every window, created at launch. On the12// first launch the welcome screen is shown; afterwards the13// last session is resumed.14// Module : App15// Repository : https://git.spboucher.ai/orix-cosmos16// Project Website : https://www.orixlab.dev17// Created : 2026-11-2418// Last Modified : 2026-11-2419//20// =============================================================================21// AUTHOR & PROFESSIONAL AFFILIATION22// =============================================================================23//24// Author : Simon-Pierre Boucher25// Position : Professor of Finance26// Institution : Université du Québec en Outaouais (UQO)27// Department : Département des sciences administratives28// Organization : SP Boucher29//30// Website : https://spboucher.ai31// Git : https://git.spboucher.ai32//33// =============================================================================34// OFFICIAL CONTACT DIRECTORY35// =============================================================================36//37// General : info@spboucher.ai38// Professional : contact@spboucher.ai39// Direct : simon@spboucher.ai40// Research : research@spboucher.ai41// Development : dev@spboucher.ai42// Support : support@spboucher.ai43// Security/Admin : admin@spboucher.ai44// Notifications : notifications@spboucher.ai45//46// Preferred Contact: support@spboucher.ai47//48// =============================================================================49// COPYRIGHT & INTELLECTUAL PROPERTY50// =============================================================================51//52// Copyright (c) 202653// Simon-Pierre Boucher54//55// All rights reserved where applicable.56//57// This file is governed by the applicable project license,58// copyright notices, institutional agreements and relevant law.59//60// Unless expressly authorized by the applicable license or61// rights holder, copying, redistribution, sublicensing,62// publication and commercial exploitation are not permitted.63//64// Existing third-party rights and notices remain unaffected.65//66// =============================================================================67// CONFIDENTIALITY & SECURITY68// =============================================================================69//70// Classification : Public71// Distribution : Public repository; use is governed by the project license72// Security Contact : admin@spboucher.ai73//74// Access to this source does not independently grant permission75// to redistribute or disclose confidential materials.76//77// Do not embed credentials, private keys, access tokens,78// certificates or confidential personal information.79//80// =============================================================================81// LICENSE & MAINTENANCE82// =============================================================================83//84// License : None specified (no license file in this repository)85// Maintainer : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import AppKit93import CosmosUI94import SwiftUI9596@MainActor97enum AppState {98 /// The model shared by the main window, the detached panels, the preferences and the menus.99 static let model: AppModel = {100 let model = AppModel()101 if model.phase == .ready {102 if model.sessionStore.welcomeSeen, model.hasSavedSession {103 model.resumeLastSession()104 } else {105 model.showWelcome = true106 }107 }108 if let path = menuDumpPath {109 // Verification aid: write the menu bar as built by SwiftUI (titles and key equivalents), then quit.110 NSApplication.shared.setActivationPolicy(.accessory)111 DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) { dumpMenus(to: path) }112 }113 return model114 }()115116 static var menuDumpPath: String? {117 let arguments = CommandLine.arguments118 guard let i = arguments.firstIndex(of: "--dump-menus"), i + 1 < arguments.count else { return nil }119 return arguments[i + 1]120 }121122 private static func describe(_ menu: NSMenu) -> [[String: Any]] {123 menu.items.map { item in124 if item.isSeparatorItem { return ["separator": true] }125 var entry: [String: Any] = ["title": item.title, "enabled": item.isEnabled]126 if !item.keyEquivalent.isEmpty {127 let flags = item.keyEquivalentModifierMask128 entry["shortcut"] = (flags.contains(.control) ? "⌃" : "") + (flags.contains(.option) ? "⌥" : "")129 + (flags.contains(.shift) ? "⇧" : "") + (flags.contains(.command) ? "⌘" : "") + item.keyEquivalent130 }131 if item.state == .on { entry["checked"] = true }132 if let submenu = item.submenu { entry["items"] = describe(submenu) }133 return entry134 }135 }136137 private static func dumpMenus(to path: String) {138 let menu = NSApplication.shared.mainMenu139 // Menus are populated lazily: ask each one to update as it would when opened.140 func update(_ m: NSMenu) { m.update(); for item in m.items { if let s = item.submenu { update(s) } } }141 if let menu { update(menu) }142 let tree: [String: Any] = ["menus": menu.map(describe) ?? [], "windows": NSApplication.shared.windows.map(\.title)]143 let data = (try? JSONSerialization.data(withJSONObject: tree, options: [.prettyPrinted, .sortedKeys])) ?? Data()144 try? data.write(to: URL(fileURLWithPath: (path as NSString).expandingTildeInPath))145 exit(0)146 }147}148