import Foundation import RastrilloApple import RastrilloSnapshots import SwiftUI import XCTest @testable import DocsKit /// What the window around the page shows. struct WindowChrome: Sendable { static let headerHeight: CGFloat = 52 var inspector: Bool var reader = false var unavailable = false /// A writer's callout (today's errorKey), shown beside the inspector. var errorKey: String? = nil /// The interface language, nil for English. var language: String? = nil var rightToLeft: Bool { language == "ar" } /// The side the inspector covers when it shows (writers only). var inspectorSide: DocsDeskLayout.Side? { inspector && !reader && !unavailable ? (rightToLeft ? .left : .right) : nil } } #if os(macOS) /// The S2a document window (spec §1, §6, §11): the header Docs draws in the /// toolbar (the title area with its subtitle, undo/redo, Format, Share…), /// the floating Format inspector over the desk and the page placed by the /// centring rule, for a writer with the inspector open and closed, a /// reader, a document no longer available, in Arabic and Russian; and every /// subtitle row. /// /// The kit draws a borderless window, which has no toolbar, so the header /// here is the same views laid out as the toolbar lays them out: the title /// area at the leading edge after the window buttons' space, the controls /// at the trailing edge, 8pt apart. The page, the desk and the inspector /// are the real ones. final class DocsWindowSnapshots: XCTestCase { @MainActor func testWindowScreens() async throws { try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil, "Run with native/scripts/snapshots.sh") DocsFonts.register() let settings = SnapshotSettings(tint: OficinaPalette.accent(.docs)) await assertSnapshots(of: SnapshotCatalog(app: "docs-window", variants: [.init(.light, .standard), .init(.dark, .standard)], settings: settings, screens: Self.screens)) await EditorScreen.stopAll() } /// 1280 wide: the inspector open puts the page at 188pt, clear of it. static let size = SnapshotSize.fixed(width: 1280, height: 760) /// 1000 wide: the page runs under the inspector (the cost of option B). static let narrow = SnapshotSize.fixed(width: 1000, height: 640) @MainActor static var screens: [SnapshotScreen] { [ window("window-format-open", .init(inspector: true)), window("window-format-closed", .init(inspector: false)), window("window-format-open-narrow", .init(inspector: true), size: narrow), window("window-reader", .init(inspector: true, reader: true)), window("window-unavailable", .init(inspector: true, unavailable: true)), // A callout beside the inspector at 1000pt: it keeps clear of it. window("window-callout-narrow", .init(inspector: true, errorKey: "docs.editor.status.retrying"), size: narrow), window("window-format-open-ar", .init(inspector: true, language: "ar"), fixture: .arabic), window("window-format-open-ru", .init(inspector: true, language: "ru"), fixture: .russian), // S2b: the Share popover (spec §1), its panel as the popover draws it. SnapshotScreen("share-popover", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "en", once: false) }, SnapshotScreen("share-popover-once", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "en", once: true) }, SnapshotScreen("share-popover-ar", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "ar", once: true) }, SnapshotScreen("share-popover-ru", mac: .width(452), iphone: nil) { SharePopoverScreen(language: "ru", once: true) }, SnapshotScreen("subtitles", mac: .width(420), iphone: nil) { SubtitleRows(language: "en") }, SnapshotScreen("subtitles-ar", mac: .width(420), iphone: nil) { SubtitleRows(language: "ar") }, SnapshotScreen("subtitles-ru", mac: .width(420), iphone: nil) { SubtitleRows(language: "ru") }, ] } @MainActor static func window(_ name: String, _ chrome: WindowChrome, size: SnapshotSize = size, fixture: EditorFixture = .millbrook) -> SnapshotScreen { var frame = CGSize.zero if case .fixed(let width, let height) = size { frame = CGSize(width: width, height: height) } return SnapshotScreen(name, mac: size, iphone: nil) { [frame] in var screen = EditorScreen(fixture, window: frame, paged: true) screen.chrome = chrome return screen } } } /// The header strip and the desk, the way `DocsWorkspace.editor` puts them /// in a window: the title area, the controls, the page, the floating /// inspector and the callout. @MainActor struct WindowChromeView: View { static var headerHeight: CGFloat { WindowChrome.headerHeight } let chrome: WindowChrome let model: DocsModel @ViewBuilder let page: Page @State private var renaming = false @State private var open = true var body: some View { VStack(spacing: 0) { HStack(spacing: 8) { // Where the window's close, minimise and zoom buttons sit. Color.clear.frame(width: 78) DocsTitleArea(model: model, team: "Tito", renaming: $renaming, beginRename: {}, finishRename: { true }) Spacer(minLength: 24) DocsHeaderControls(model: model, inspectorOpen: $open, part: .history, share: {}) DocsHeaderControls(model: model, inspectorOpen: $open, part: .format, share: {}) DocsHeaderControls(model: model, inspectorOpen: $open, part: .share, share: {}) } .padding(.trailing, 16) .frame(height: Self.headerHeight) .background(Color(nsColor: .windowBackgroundColor)) .overlay(alignment: .bottom) { Rectangle().fill(OficinaPalette.line).frame(height: 1) } page .overlay(alignment: .topTrailing) { if chrome.inspectorSide != nil, model.showsDocument { DocsFormatInspector(model: model) .padding(.top, DocsDeskLayout.inspectorInset) .padding(.trailing, DocsDeskLayout.inspectorInset) } } .overlay(alignment: .top) { DocsDocumentCallouts(model: model, discard: {}, close: {}, inspectorShown: chrome.inspectorSide != nil) } } .onAppear { open = chrome.inspector } .environment(\.locale, Locale(identifier: chrome.language ?? "en")) .environment(\.layoutDirection, chrome.rightToLeft ? .rightToLeft : .leftToRight) } } /// The Share panel in a popover's frame: two people, the team rung on, and /// the link published, with the once-only URL showing (`once`) or the line /// that it was shown, with Make a new link. @MainActor struct SharePopoverScreen: View { let language: String let once: Bool var body: some View { let information = DocsShareInformation(sqid: "doc", title: "A History of the Millbrook Waterworks", grants: [DocsShareGrant(address: "bob@example.com", role: "writer"), DocsShareGrant(address: "carol.long-address@example.org", role: "reader")], published: true, public_url: "", visibility: "team", error: "") let model = DocsSharePanelModel(sqid: "doc", title: information.title, information: information, onceURL: once ? URL(string: "https://docs.oficina.works/p/Hx7kQ2mVb9tLr4sN") : nil) DocsSharePanel(model: model, me: "alice@example.com", focusOnAppear: false, close: {}) .background(RoundedRectangle(cornerRadius: 12).fill(Color(nsColor: .windowBackgroundColor))) .overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(OficinaPalette.line, lineWidth: 1)) .padding(16) .environment(\.locale, Locale(identifier: language)) .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) } } /// Every §6 subtitle row under one title, in one language. @MainActor struct SubtitleRows: View { let language: String static let rows: [DocsSubtitleRow] = [.unavailable, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .needsSignIn(pending: 3), .signedOut, .offline(pending: 2), .retrying(pending: 1), .saving, .viewOnly, .saved, .loading] var body: some View { let strings = DocsStrings(locale: Locale(identifier: language)) VStack(alignment: .leading, spacing: 10) { ForEach(Array(Self.rows.enumerated()), id: \.offset) { index, row in VStack(alignment: .leading, spacing: 1) { Text(verbatim: "A History of the Millbrook Waterworks") .font(.system(size: 15, weight: .semibold)).foregroundStyle(OficinaPalette.text).lineLimit(1) DocsSubtitleLine(row: row, team: "Tito", strings: strings) } } } .padding(16) .frame(maxWidth: .infinity, alignment: .leading) .environment(\.locale, Locale(identifier: language)) .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) } } #endif