| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | import RastrilloSnapshots |
| 4 | import SwiftUI |
| 5 | import XCTest |
| 6 | @testable import DocsKit |
| 7 | |
| 8 | /// What the window around the page shows. |
| 9 | struct WindowChrome: Sendable { |
| 10 | static let headerHeight: CGFloat = 52 |
| 11 | var inspector: Bool |
| 12 | var reader = false |
| 13 | var unavailable = false |
| 14 | /// A writer's callout (today's errorKey), shown beside the inspector. |
| 15 | var errorKey: String? = nil |
| 16 | /// The interface language, nil for English. |
| 17 | var language: String? = nil |
| 18 | var rightToLeft: Bool { language == "ar" } |
| 19 | /// The side the inspector covers when it shows (writers only). |
| 20 | var inspectorSide: DocsDeskLayout.Side? { inspector && !reader && !unavailable ? (rightToLeft ? .left : .right) : nil } |
| 21 | } |
| 22 | |
| 23 | #if os(macOS) |
| 24 | |
| 25 | /// The S2a document window (spec §1, §6, §11): the header Docs draws in the |
| 26 | /// toolbar (the title area with its subtitle, undo/redo, Format, Share…), |
| 27 | /// the floating Format inspector over the desk and the page placed by the |
| 28 | /// centring rule, for a writer with the inspector open and closed, a |
| 29 | /// reader, a document no longer available, in Arabic and Russian; and every |
| 30 | /// subtitle row. |
| 31 | /// |
| 32 | /// The kit draws a borderless window, which has no toolbar, so the header |
| 33 | /// here is the same views laid out as the toolbar lays them out: the title |
| 34 | /// area at the leading edge after the window buttons' space, the controls |
| 35 | /// at the trailing edge, 8pt apart. The page, the desk and the inspector |
| 36 | /// are the real ones. |
| 37 | final class DocsWindowSnapshots: XCTestCase { |
| 38 | @MainActor func testWindowScreens() async throws { |
| 39 | try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil, |
| 40 | "Run with native/scripts/snapshots.sh") |
| 41 | DocsFonts.register() |
| 42 | let settings = SnapshotSettings(tint: OficinaPalette.accent(.docs)) |
| 43 | await assertSnapshots(of: SnapshotCatalog(app: "docs-window", variants: [.init(.light, .standard), .init(.dark, .standard)], |
| 44 | settings: settings, screens: Self.screens)) |
| 45 | await EditorScreen.stopAll() |
| 46 | } |
| 47 | |
| 48 | /// 1280 wide: the inspector open puts the page at 188pt, clear of it. |
| 49 | static let size = SnapshotSize.fixed(width: 1280, height: 760) |
| 50 | /// 1000 wide: the page runs under the inspector (the cost of option B). |
| 51 | static let narrow = SnapshotSize.fixed(width: 1000, height: 640) |
| 52 | |
| 53 | @MainActor static var screens: [SnapshotScreen] { |
| 54 | [ |
| 55 | window("window-format-open", .init(inspector: true)), |
| 56 | window("window-format-closed", .init(inspector: false)), |
| 57 | window("window-format-open-narrow", .init(inspector: true), size: narrow), |
| 58 | window("window-reader", .init(inspector: true, reader: true)), |
| 59 | window("window-unavailable", .init(inspector: true, unavailable: true)), |
| 60 | // A callout beside the inspector at 1000pt: it keeps clear of it. |
| 61 | window("window-callout-narrow", .init(inspector: true, errorKey: "docs.editor.status.retrying"), size: narrow), |
| 62 | window("window-format-open-ar", .init(inspector: true, language: "ar"), fixture: .arabic), |
| 63 | window("window-format-open-ru", .init(inspector: true, language: "ru"), fixture: .russian), |
| 64 | // S2b: the Share popover (spec §1), its panel as the popover draws it. |
| 65 | SnapshotScreen("share-popover", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "en", once: false) }, |
| 66 | SnapshotScreen("share-popover-once", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "en", once: true) }, |
| 67 | SnapshotScreen("share-popover-ar", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "ar", once: true) }, |
| 68 | SnapshotScreen("share-popover-ru", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "ru", once: true) }, |
| 69 | SnapshotScreen("subtitles", mac: .width(420), iphone: nil) { SubtitleRows(language: "en") }, |
| 70 | SnapshotScreen("subtitles-ar", mac: .width(420), iphone: nil) { SubtitleRows(language: "ar") }, |
| 71 | SnapshotScreen("subtitles-ru", mac: .width(420), iphone: nil) { SubtitleRows(language: "ru") }, |
| 72 | ] |
| 73 | } |
| 74 | |
| 75 | @MainActor static func window(_ name: String, _ chrome: WindowChrome, size: SnapshotSize = size, fixture: EditorFixture = .millbrook) -> SnapshotScreen { |
| 76 | var frame = CGSize.zero |
| 77 | if case .fixed(let width, let height) = size { frame = CGSize(width: width, height: height) } |
| 78 | return SnapshotScreen(name, mac: size, iphone: nil) { [frame] in |
| 79 | var screen = EditorScreen(fixture, window: frame, paged: true) |
| 80 | screen.chrome = chrome |
| 81 | return screen |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// The header strip and the desk, the way `DocsWorkspace.editor` puts them |
| 87 | /// in a window: the title area, the controls, the page, the floating |
| 88 | /// inspector and the callout. |
| 89 | @MainActor struct WindowChromeView<Page: View>: View { |
| 90 | static var headerHeight: CGFloat { WindowChrome.headerHeight } |
| 91 | let chrome: WindowChrome |
| 92 | let model: DocsModel |
| 93 | @ViewBuilder let page: Page |
| 94 | @State private var renaming = false |
| 95 | @State private var open = true |
| 96 | |
| 97 | var body: some View { |
| 98 | VStack(spacing: 0) { |
| 99 | HStack(spacing: 8) { |
| 100 | // Where the window's close, minimise and zoom buttons sit. |
| 101 | Color.clear.frame(width: 78) |
| 102 | DocsTitleArea(model: model, team: "Tito", renaming: $renaming, beginRename: {}, finishRename: { true }) |
| 103 | Spacer(minLength: 24) |
| 104 | DocsHeaderControls(model: model, inspectorOpen: $open, part: .history, share: {}) |
| 105 | DocsHeaderControls(model: model, inspectorOpen: $open, part: .format, share: {}) |
| 106 | DocsHeaderControls(model: model, inspectorOpen: $open, part: .share, share: {}) |
| 107 | } |
| 108 | .padding(.trailing, 16) |
| 109 | .frame(height: Self.headerHeight) |
| 110 | .background(Color(nsColor: .windowBackgroundColor)) |
| 111 | .overlay(alignment: .bottom) { Rectangle().fill(OficinaPalette.line).frame(height: 1) } |
| 112 | page |
| 113 | .overlay(alignment: .topTrailing) { |
| 114 | if chrome.inspectorSide != nil, model.showsDocument { |
| 115 | DocsFormatInspector(model: model) |
| 116 | .padding(.top, DocsDeskLayout.inspectorInset) |
| 117 | .padding(.trailing, DocsDeskLayout.inspectorInset) |
| 118 | } |
| 119 | } |
| 120 | .overlay(alignment: .top) { DocsDocumentCallouts(model: model, discard: {}, close: {}, inspectorShown: chrome.inspectorSide != nil) } |
| 121 | } |
| 122 | .onAppear { open = chrome.inspector } |
| 123 | .environment(\.locale, Locale(identifier: chrome.language ?? "en")) |
| 124 | .environment(\.layoutDirection, chrome.rightToLeft ? .rightToLeft : .leftToRight) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// The Share panel in a popover's frame: two people, the team rung on, and |
| 129 | /// the link published, with the once-only URL showing (`once`) or the line |
| 130 | /// that it was shown, with Make a new link. |
| 131 | @MainActor struct SharePopoverScreen: View { |
| 132 | let language: String |
| 133 | let once: Bool |
| 134 | var body: some View { |
| 135 | let information = DocsShareInformation(sqid: "doc", title: "A History of the Millbrook Waterworks", |
| 136 | grants: [DocsShareGrant(address: "bob@example.com", role: "writer"), |
| 137 | DocsShareGrant(address: "carol.long-address@example.org", role: "reader")], |
| 138 | published: true, public_url: "", visibility: "team", error: "") |
| 139 | let model = DocsSharePanelModel(sqid: "doc", title: information.title, information: information, |
| 140 | onceURL: once ? URL(string: "https://docs.oficina.works/p/Hx7kQ2mVb9tLr4sN") : nil) |
| 141 | DocsSharePanel(model: model, me: "alice@example.com", focusOnAppear: false, close: {}) |
| 142 | .background(RoundedRectangle(cornerRadius: 12).fill(Color(nsColor: .windowBackgroundColor))) |
| 143 | .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(OficinaPalette.line, lineWidth: 1)) |
| 144 | .padding(16) |
| 145 | .environment(\.locale, Locale(identifier: language)) |
| 146 | .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Every §6 subtitle row under one title, in one language. |
| 151 | @MainActor struct SubtitleRows: View { |
| 152 | let language: String |
| 153 | static let rows: [DocsSubtitleRow] = [.unavailable, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .needsSignIn(pending: 3), .signedOut, |
| 154 | .offline(pending: 2), .retrying(pending: 1), .saving, .viewOnly, .saved, .loading] |
| 155 | var body: some View { |
| 156 | let strings = DocsStrings(locale: Locale(identifier: language)) |
| 157 | VStack(alignment: .leading, spacing: 10) { |
| 158 | ForEach(Array(Self.rows.enumerated()), id: \.offset) { index, row in |
| 159 | VStack(alignment: .leading, spacing: 1) { |
| 160 | Text(verbatim: "A History of the Millbrook Waterworks") |
| 161 | .font(.system(size: 15, weight: .semibold)).foregroundStyle(OficinaPalette.text).lineLimit(1) |
| 162 | DocsSubtitleLine(row: row, team: "Tito", strings: strings) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | .padding(16) |
| 167 | .frame(maxWidth: .infinity, alignment: .leading) |
| 168 | .environment(\.locale, Locale(identifier: language)) |
| 169 | .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) |
| 170 | } |
| 171 | } |
| 172 | #endif |
| 173 | |