| 1 | #if os(macOS) |
| 2 | import Foundation |
| 3 | import Observation |
| 4 | import RastrilloApple |
| 5 | import SwiftUI |
| 6 | |
| 7 | /// Scene values are opaque IDs, never credentials. Reserve before opening a scene. |
| 8 | @MainActor @Observable final class SheetsWindows { |
| 9 | struct Entry: Identifiable { |
| 10 | let id: UUID |
| 11 | let account: String |
| 12 | let document: UInt64 |
| 13 | let client: NativeHTTP |
| 14 | var title: String |
| 15 | var isOpen = false |
| 16 | } |
| 17 | var entries: [Entry] = [] |
| 18 | var activeAccount: String? |
| 19 | var createRequested = false |
| 20 | var accountsRequested = false |
| 21 | func reserve(account: String, document: UInt64, title: String, client: NativeHTTP) -> UUID { |
| 22 | if let index = entries.firstIndex(where: { $0.account == account && $0.client.origin == client.origin && $0.document == document }) { |
| 23 | var entry = entries.remove(at: index) |
| 24 | entry.title = title |
| 25 | entry.isOpen = true |
| 26 | entries.insert(entry, at: 0) |
| 27 | return entry.id |
| 28 | } |
| 29 | let entry = Entry(id: UUID(), account: account, document: document, client: client, title: title, isOpen: true) |
| 30 | entries.insert(entry, at: 0) |
| 31 | return entry.id |
| 32 | } |
| 33 | func updateRecents(_ documents: [(UInt64, String)], account: String, client: NativeHTTP) { |
| 34 | for (document, title) in documents { |
| 35 | if let index = entries.firstIndex(where: { $0.account == account && $0.client.origin == client.origin && $0.document == document }) { |
| 36 | entries[index].title = title |
| 37 | } else { |
| 38 | entries.append(Entry(id: UUID(), account: account, document: document, client: client, title: title)) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | func setOpen(_ id: UUID, _ value: Bool) { |
| 43 | if let index = entries.firstIndex(where: { $0.id == id }) { entries[index].isOpen = value } |
| 44 | } |
| 45 | func canSignOut(_ account: String?) -> Bool { !entries.contains { $0.account == account && $0.isOpen } } |
| 46 | } |
| 47 | extension FocusedValues { @Entry var sheetsWindowAccount: String? } |
| 48 | #endif |
| 49 | |