Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/App/WindowModel.swift3// Description: Per-window navigation state (what the window shows, search text, palette) and its restoration encoding.4import SwiftUI56enum LibrarySection: String, CaseIterable, Codable, Identifiable {7 case projects, prompts, presets, files, memory, models, usage, compare, research, images, constellation8 var id: String { rawValue }9 var title: String {10 switch self {11 case .projects: "Projets"; case .prompts: "Prompts"; case .presets: "Profils"; case .files: "Fichiers"; case .memory: "Mémoire"12 case .models: "Explorateur de modèles"; case .usage: "Utilisation"; case .compare: "Arena"; case .research: "Recherche approfondie"; case .images: "Images"13 case .constellation: "Constellation"14 }15 }16 var systemImage: String {17 switch self {18 case .projects: "folder"; case .prompts: "text.quote"; case .presets: "slider.horizontal.3"; case .files: "doc.on.doc"; case .memory: "brain.head.profile"19 case .models: "cpu"; case .usage: "chart.bar.xaxis"; case .compare: "rectangle.split.2x1"; case .research: "doc.text.magnifyingglass"; case .images: "photo.on.rectangle.angled"20 case .constellation: "point.3.filled.connected.trianglepath.dotted"21 }22 }23}2425enum Destination: Hashable, Codable {26 /// `nil` = a new, not yet created conversation.27 case chat(String?)28 case library(LibrarySection)2930 /// Compact form for `@SceneStorage` (window restoration).31 var storageValue: String { switch self { case .chat(let id): "chat:" + (id ?? ""); case .library(let s): "lib:" + s.rawValue } }32 init(storageValue: String) {33 if storageValue.hasPrefix("lib:"), let s = LibrarySection(rawValue: String(storageValue.dropFirst(4))) { self = .library(s) }34 else if storageValue.hasPrefix("chat:"), storageValue.count > 5 { self = .chat(String(storageValue.dropFirst(5))) }35 else { self = .chat(nil) }36 }37}3839/// Contextual panel on the right of a conversation: one panel at a time, switching cross-fades the content.40enum PanelTab: String, CaseIterable, Identifiable, Codable {41 case inspector, timeline, lens, branches42 var id: String { rawValue }43 var title: String { switch self { case .inspector: "Contexte"; case .timeline: "Chronologie"; case .lens: "Lens"; case .branches: "Branches" } }44 var systemImage: String { switch self { case .inspector: "gauge.with.dots.needle.33percent"; case .timeline: "clock.arrow.circlepath"; case .lens: "camera.aperture"; case .branches: "arrow.triangle.branch" } }45}4647@Observable final class WindowModel {48 var destination: Destination = .chat(nil)49 /// Identifies the unsaved conversation of this window; renewed by « Nouvelle conversation ».50 var draftKey = UUID().uuidString51 var searchText = ""52 var showPalette = false53 var showExportMenu = false54 /// Incremented to move the keyboard focus (composer, sidebar search).55 var composerFocus = 056 var searchFocus = 057 /// Tab of the contextual panel (the panel's visibility is `Preferences.inspectorVisible`).58 var panelTab: PanelTab = .inspector59 /// Answer the Response Lens panel works on.60 var lensMessageId: String?61 /// Explorer: model whose detail sheet is requested from elsewhere (picker, palette, deep navigation).62 var explorerModelId: String?63 /// Arena: models pre-selected when the screen is opened from the palette or the Explorer compare tray.64 var arenaModelIds: [String] = []65 /// Arena: stored comparison to re-display when the screen opens (history, palette).66 var arenaComparisonId: String?67 /// Incremented to open the model picker / the composer's web toggle from a menu command.68 var modelPickerToken = 06970 func showPanel(_ tab: PanelTab, preferences: Preferences) { panelTab = tab; preferences.inspectorVisible = true }71 func openLens(messageId: String, preferences: Preferences) { lensMessageId = messageId; showPanel(.lens, preferences: preferences) }7273 func newConversation() { draftKey = UUID().uuidString; destination = .chat(nil); composerFocus += 1 }74 func open(_ conversationId: String) { destination = .chat(conversationId) }75 var currentConversationId: String? { if case .chat(let id) = destination { return id }; return nil }76}7778struct WindowModelKey: FocusedValueKey { typealias Value = WindowModel }79struct ActiveChatKey: FocusedValueKey { typealias Value = ChatViewModel }80extension FocusedValues {81 var windowModel: WindowModel? { get { self[WindowModelKey.self] } set { self[WindowModelKey.self] = newValue } }82 var activeChat: ChatViewModel? { get { self[ActiveChatKey.self] } set { self[ActiveChatKey.self] = newValue } }83}84