| 1 | #if os(macOS) |
| 2 | import Foundation |
| 3 | import DocsKit |
| 4 | import Observation |
| 5 | import RastrilloApple |
| 6 | import SwiftUI |
| 7 | |
| 8 | /// Scene values are opaque IDs, never credentials. Reserve before opening a scene. |
| 9 | @MainActor @Observable final class DocsWindows { |
| 10 | struct Entry: Identifiable { |
| 11 | let id: UUID |
| 12 | let account: String |
| 13 | var document: String |
| 14 | let client: NativeHTTP |
| 15 | var title: String |
| 16 | var isOpen = false |
| 17 | var isDraft = false |
| 18 | /// Deleted from this window (Delete…): out of File ▸ Recent, and |
| 19 | /// gone once its window closes. |
| 20 | var isDeleted = false |
| 21 | } |
| 22 | var entries: [Entry] = [] |
| 23 | var activeAccount: String? |
| 24 | var createRequested = false |
| 25 | var accountsRequested = false |
| 26 | /// Settings' "Sign out", carried out by the home window's guard. |
| 27 | var signOutRequested = false |
| 28 | /// What Settings shows: the home window's account. |
| 29 | var settingsAccount: DocsSettingsView.Account = .none |
| 30 | /// The home window's Oficina ID, for Settings' identity-wide guard. |
| 31 | var settingsIdentity: String? |
| 32 | /// Oficina ID accounts' transports, so a document window reads the |
| 33 | /// latest session of its team (re-minted every 24 hours and on a 401) |
| 34 | /// rather than the one it opened with. Server accounts have none: their |
| 35 | /// windows keep the client they were opened with, as before. |
| 36 | var transports: [String: @MainActor () -> NativeTransport?] = [:] |
| 37 | /// Each account's team name, for a document window's subtitle (spec |
| 38 | /// D7): the Oficina ID team's name, or the server account's. |
| 39 | var teamNames: [String: String] = [:] |
| 40 | func reserve(account: String, document: String, title: String, client: NativeHTTP) -> UUID { |
| 41 | if let index = entries.firstIndex(where: { $0.account == account && $0.client.origin == client.origin && $0.document == document }) { |
| 42 | var entry = entries.remove(at: index) |
| 43 | entry.title = title |
| 44 | entry.isOpen = true |
| 45 | entries.insert(entry, at: 0) |
| 46 | return entry.id |
| 47 | } |
| 48 | let entry = Entry(id: UUID(), account: account, document: document, client: client, title: title, isOpen: true) |
| 49 | entries.insert(entry, at: 0) |
| 50 | return entry.id |
| 51 | } |
| 52 | func reserveDraft(account: String, client: NativeHTTP) -> UUID { |
| 53 | let id = UUID() |
| 54 | entries.insert(Entry(id: id, account: account, document: "draft-" + id.uuidString, client: client, title: docsMacString("Untitled document"), isOpen: true, isDraft: true), at: 0) |
| 55 | return id |
| 56 | } |
| 57 | func attach(_ id: UUID, document: String, title: String) { |
| 58 | guard let index = entries.firstIndex(where: { $0.id == id }) else { return } |
| 59 | entries[index].document = document; entries[index].title = title; entries[index].isDraft = false |
| 60 | let account = entries[index].account |
| 61 | entries.removeAll { $0.id != id && !$0.isOpen && $0.account == account && $0.document == document } |
| 62 | } |
| 63 | func updateRecents(_ documents: [(String, String)], account: String, client: NativeHTTP) { |
| 64 | for (document, title) in documents { |
| 65 | if let index = entries.firstIndex(where: { $0.account == account && $0.client.origin == client.origin && $0.document == document }) { |
| 66 | entries[index].title = title |
| 67 | } else { |
| 68 | entries.append(Entry(id: UUID(), account: account, document: document, client: client, title: title)) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | func setOpen(_ id: UUID, _ value: Bool) { |
| 73 | if let index = entries.firstIndex(where: { $0.id == id }) { |
| 74 | if !value && (entries[index].isDraft || entries[index].isDeleted) { entries.remove(at: index) } else { entries[index].isOpen = value } |
| 75 | } |
| 76 | } |
| 77 | /// The document of entry `id` was deleted: File ▸ Recent forgets it (and |
| 78 | /// any other closed entry for it), and the entry goes when its window does. |
| 79 | func forget(_ id: UUID) { |
| 80 | guard let index = entries.firstIndex(where: { $0.id == id }) else { return } |
| 81 | let entry = entries[index] |
| 82 | entries[index].isDeleted = true |
| 83 | entries.removeAll { $0.id != id && !$0.isOpen && $0.account == entry.account && $0.client.origin == entry.client.origin && $0.document == entry.document } |
| 84 | } |
| 85 | func canSignOut(_ account: String?) -> Bool { !entries.contains { $0.account == account && $0.isOpen } } |
| 86 | /// Whether a document window of any of `identity`'s teams is open: |
| 87 | /// signing out of an Oficina ID ends all of its teams at once. |
| 88 | func hasOpenDocuments(identity: String) -> Bool { |
| 89 | entries.contains { DocsOficinaSession.account($0.account, belongsTo: identity) && $0.isOpen } |
| 90 | } |
| 91 | /// Settings' guard, matching the home window's: identity-wide for an |
| 92 | /// Oficina ID, the account's own windows for a server account. |
| 93 | func leaveBlock() async -> DocsLeaveBlock? { |
| 94 | if let identity = settingsIdentity { |
| 95 | return await DocsLeaveBlock.check(identity: identity, openDocuments: hasOpenDocuments(identity: identity)) |
| 96 | } |
| 97 | return canSignOut(activeAccount) ? nil : .openDocuments |
| 98 | } |
| 99 | } |
| 100 | extension FocusedValues { @Entry var docsWindowAccount: String? } |
| 101 | #endif |
| 102 | |