| 1 | #if os(macOS) && canImport(RastrilloMacUpdates) |
| 2 | import RastrilloMacUpdates |
| 3 | #endif |
| 4 | import DocsKit |
| 5 | import RastrilloApple |
| 6 | import SwiftUI |
| 7 | @main struct DocsApp: App { |
| 8 | #if os(macOS) && canImport(RastrilloMacUpdates) |
| 9 | // Quiet, as Calls and Calendar: a scheduled check never opens a window; |
| 10 | // an update downloads in the background and installs on quit. Needs |
| 11 | // "install": "automatic" in mac-updates.json too (rastrillo/native |
| 12 | // docs/mac-updates.md). |
| 13 | @State private var updater = NativeUpdater(policy: .quiet) |
| 14 | #endif |
| 15 | #if os(macOS) |
| 16 | @State private var windows = DocsWindows() |
| 17 | @NSApplicationDelegateAdaptor(DocsApplicationDelegate.self) private var appDelegate |
| 18 | #endif |
| 19 | /// One Oficina ID model for the whole app (slice A: one identity), read |
| 20 | /// from the keychain once at launch, whichever window asks first. |
| 21 | @StateObject private var oficina = DocsOficinaSession.live() |
| 22 | /// Every bundled face is registered here, synchronously, before the |
| 23 | /// first window exists, so no text view ever lays out with a face |
| 24 | /// missing (polish-audit/fonts/NO-FOUT.md §7). |
| 25 | init() { |
| 26 | #if os(macOS) |
| 27 | // The launch gate: run the signed executable with this argument. |
| 28 | if CommandLine.arguments.contains(DocsOficinaLaunchCheck.argument) { |
| 29 | let result = DocsOficinaLaunchCheck.run() |
| 30 | result.lines.forEach { print($0) } |
| 31 | exit(result.passed ? 0 : 1) |
| 32 | } |
| 33 | #endif |
| 34 | DocsFonts.register() |
| 35 | } |
| 36 | var body: some Scene { |
| 37 | #if os(macOS) |
| 38 | Window(docsMacString("Home"), id: "home") { |
| 39 | DocsAccountWindow(oficina: oficina, windows: windows) |
| 40 | #if canImport(RastrilloMacUpdates) |
| 41 | // The quiet updater's one line, only while an update waits: where an |
| 42 | // update that needs the person (automatic downloads off) shows up. |
| 43 | .safeAreaInset(edge: .bottom) { |
| 44 | if updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) } |
| 45 | } |
| 46 | #endif |
| 47 | } |
| 48 | .defaultSize(width: 1000, height: 760) |
| 49 | .commands { |
| 50 | // First: About, Hide, Quit say "Docs", not the Finder name, and the |
| 51 | // groups after it (Check for Updates, the Colophon) survive |
| 52 | // (rastrillo/native docs/mac-updates.md). |
| 53 | PlainAppNameCommands() |
| 54 | DocsWindowCommands(windows: windows) |
| 55 | DocsEditorCommands() |
| 56 | #if canImport(RastrilloMacUpdates) |
| 57 | NativeUpdateCommands(updater: updater) |
| 58 | #endif |
| 59 | } |
| 60 | WindowGroup(docsMacString("Docs"), id: "document", for: UUID.self) { $id in |
| 61 | DocsDocumentWindow(windows: windows, oficina: oficina, id: id) |
| 62 | } |
| 63 | .defaultSize(width: 1120, height: 820) |
| 64 | .restorationBehavior(.disabled) |
| 65 | // The Colophon, from the Help menu: everything the app is made with, |
| 66 | // the typefaces' licences among it. |
| 67 | Window(DocsColophonView.title, id: "colophon") { DocsColophonView() } |
| 68 | .defaultSize(width: 680, height: 760) |
| 69 | Settings { DocsSettingsWindow(windows: windows) } |
| 70 | #else |
| 71 | WindowGroup { DocsAccountWindow(oficina: oficina) } |
| 72 | #endif |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #if os(macOS) |
| 77 | /// Docs ▸ Settings… (⌘,): the home window's account and the version. Its |
| 78 | /// buttons ask the home window, which holds the guards against stranding |
| 79 | /// unsent changes. |
| 80 | struct DocsSettingsWindow: View { |
| 81 | let windows: DocsWindows |
| 82 | @Environment(\.openWindow) private var openWindow |
| 83 | @State private var block: DocsLeaveBlock? |
| 84 | var body: some View { |
| 85 | DocsSettingsView(account: windows.settingsAccount, block: block, manage: { |
| 86 | windows.accountsRequested = true; openWindow(id: "home") |
| 87 | }, signOut: { |
| 88 | windows.signOutRequested = true; openWindow(id: "home") |
| 89 | }, signIn: { openWindow(id: "home") }) |
| 90 | // The same guard as the home window's, kept current while Settings is |
| 91 | // open: open document windows change, outboxes drain. |
| 92 | .task(id: "\(windows.settingsIdentity ?? "")|\(windows.activeAccount ?? "")|\(windows.entries.filter(\.isOpen).count)") { |
| 93 | while !Task.isCancelled { |
| 94 | block = await windows.leaveBlock() |
| 95 | try? await Task.sleep(for: .seconds(2)) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |