Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Next/WorkspaceMapping.swift3// Description: Window layouts ↔ the shared `workspace` object (pure mapping, no UI: also compiled by the SwiftPM target VQuantAppLogic so it is unit-tested with `swift test`).4import Foundation5import VQuantCore6import VQuantKit78/// Window layouts ↔ the shared `workspace` object (CONTRACT §1.1). A saved layout is a set of windows; the shared model is a tree of9/// panels. A layout maps when every window shows something the shared model has a panel type for; the Mac-specific10/// part (window frames, the section) travels in `panel.config.mac`, which other clients preserve untouched.11nonisolated enum WorkspaceMapping {12 static func panelType(section: String, kind: String) -> VQuantKit.VQPanel.PanelType? {13 if kind == "chart" { return .chart }14 switch section {15 case "charting": return .chart16 case "watchlists": return .watchlist17 case "research": return .chat18 case "quant": return .quant19 case "portfolio": return .portfolio20 case "risk": return .risk21 case "statements": return .statements22 case "data": return .table23 case "markets", "overview": return .macro24 case "studio": return .note25 default: return nil // agent, jobs, documents: no panel type describes them26 }27 }2829 /// Equal shares of a split as the web writes them: percentages that sum to 100.30 static func equalSizes(_ n: Int) -> [Double] {31 guard n > 0 else { return [] }32 let share = (10_000 / Double(n)).rounded(.down) / 10033 var sizes = Array(repeating: share, count: n)34 sizes[n - 1] = ((100 - share * Double(n - 1)) * 100).rounded() / 10035 return sizes36 }3738 /// The shared workspace of a layout, or nil when a window has no panel type. With `existing` (the layout is saved39 /// again under the same object) the stored value is edited, not rebuilt: every key this app does not know — at40 /// the top (`template`, `activeSymbol`, …), in a panel and in its `config` — is kept as it is.41 static func workspace(from layout: SavedWorkspace, existing: VQWorkspace? = nil) -> VQWorkspace? {42 guard !layout.windows.isEmpty else { return nil }43 let old = existing?.panels ?? [:]44 var panels: [String: VQuantKit.VQPanel] = [:]45 var order: [String] = []46 for (i, w) in layout.windows.enumerated() {47 guard let type = panelType(section: w.section, kind: w.kind) else { return nil }48 let id = "w\(i + 1)"49 var panel = old[id] ?? VQuantKit.VQPanel(type: type)50 var config = panel.config51 if let s = w.symbol { config["symbol"] = .string(s) } else { config["symbol"] = nil }52 var mac: VQJSONObject = config["mac"]?.objectValue ?? [:]53 mac["section"] = .string(w.section); mac["kind"] = .string(w.kind)54 if let f = w.frame { mac["frame"] = .array(f.map { .number($0) }) } else { mac["frame"] = nil }55 config["mac"] = .object(mac)56 panel.type = .known(type); panel.title = w.symbol ?? w.section.capitalized; panel.config = config57 panels[id] = panel58 order.append(id)59 }60 let layoutNode: VQLayoutNode = order.count == 1 ? .panel(order[0]) : .split(.row, sizes: equalSizes(order.count), children: order.map { .panel($0) })61 guard var ws = existing else { return VQWorkspace(name: layout.name, icon: "macwindow.on.rectangle", layout: layoutNode, panels: panels) }62 ws.name = layout.name; ws.layout = layoutNode; ws.panels = panels63 return ws64 }6566 /// True when every panel was written by this app (it carries `config.mac`): such a workspace is a window layout67 /// of the Mac and may be overwritten or deleted from the layout menus; any other belongs to the client that made it.68 static func isMacLayout(_ w: VQWorkspace) -> Bool {69 let ids = w.layout.panelIds, panels = w.panels70 return !ids.isEmpty && ids.allSatisfy { panels[$0]?.config["mac"]?.objectValue?["section"]?.stringValue != nil }71 }7273 /// The windows of a synced workspace. Panels written by this app restore exactly (section, frame); panels written74 /// by another client open as the nearest section of this app, without a frame.75 static func windows(of w: VQWorkspace) -> [SavedWorkspace.Window] {76 let panels = w.panels77 // web convention: a panel without its own symbol follows the workspace's `activeSymbol` unless `linked` is false78 let active = w.raw["activeSymbol"]?.stringValue.flatMap { $0.isEmpty ? nil : $0 }79 return w.layout.panelIds.compactMap { id -> SavedWorkspace.Window? in80 guard let p = panels[id] else { return nil }81 let own = p.config["symbol"]?.stringValue.flatMap { $0.isEmpty ? nil : $0 }82 let symbol = own ?? (p.config["linked"]?.boolValue == false ? nil : active)83 if let mac = p.config["mac"]?.objectValue, let section = mac["section"]?.stringValue {84 let frame = mac["frame"]?.arrayValue?.compactMap(\.doubleValue)85 return SavedWorkspace.Window(section: section, symbol: symbol, frame: frame?.count == 4 ? frame : nil, kind: mac["kind"]?.stringValue ?? "main")86 }87 let section: String88 switch p.type.known {89 case .chart?: section = "charting"; case .watchlist?: section = "watchlists"; case .chat?, .notebook?: section = "research"90 case .quant?, .options?: section = "quant"; case .portfolio?: section = "portfolio"; case .risk?: section = "risk"91 case .statements?: section = "statements"; case .table?: section = "data"; case .macro?, .quote?, .news?: section = "overview"92 case .note?: section = "studio"; case nil: section = "overview"93 }94 return SavedWorkspace.Window(section: section, symbol: symbol, frame: nil, kind: "main")95 }96 }97}98