import Foundation import SwiftUI /// The apps' Colophon: every open-source project they are made with, what /// it is, what the apps use it for, who made it, its licence and where it /// lives, with every package's full licence text. It replaces the font /// licences screen; the typefaces are its first section. /// /// Nothing here is a list. Resources/Colophon.json is generated by the /// server repository's internal/shipped from what the apps ship (the /// Rust in the document core and the interpreter that runs it, the Swift /// packages, the fonts, the icons, the HTML entities table), with every /// description from estilo's catalog, the same words the web's Help → /// Colophon uses. TestColophonsMatchWhatShips fails when it is stale. enum DocsColophon { struct Notice: Decodable, Hashable { let name: String let text: String enum CodingKeys: String, CodingKey { case name = "Name", text = "Text" } } struct Package: Decodable, Hashable { let ecosystem: String let name: String let version: String? let tool: Bool? /// The platforms the package ships on; nil is all of them. let platforms: [String]? let notices: [Notice] } struct Licence: Decodable, Hashable { let spdx: String let name: String } struct Entry: Decodable, Hashable, Identifiable { let id: String let name: String let blurb: String let usage: String let author: String let licence: Licence let home: String let source: String let packages: [Package] var homeURL: URL? { URL(string: home) } var sourceURL: URL? { URL(string: source) } /// The entry's version, or each package's when they differ. var versionText: String { let versions = Set(packages.map { $0.version ?? "" }) if versions.count == 1 { return versions.first ?? "" } return packages.compactMap { p in p.version.map { "\(p.name) \($0)" } }.joined(separator: ", ") } } struct Section: Decodable, Hashable, Identifiable { let id: String let entries: [Entry] } private struct File: Decodable { let sections: [Section] } static let sectionOrder: Set = ["typefaces", "go", "javascript", "rust", "swift", "icons", "data", "tools", "services"] #if os(macOS) static let platform = "mac" #else static let platform = "ios" #endif /// The Colophon for this platform: a package limited to other /// platforms is left out, and so is an entry left with none. static let sections: [Section] = { guard let url = DocsResources.bundle.url(forResource: "Colophon", withExtension: "json"), let data = try? Data(contentsOf: url), let file = try? JSONDecoder().decode(File.self, from: data) else { fatalError("DocsColophon: Resources/Colophon.json is missing from the DocsKit bundle or unreadable") } return file.sections.compactMap { section in let entries = section.entries.compactMap { entry -> Entry? in let packages = entry.packages.filter { $0.platforms?.contains(platform) ?? true } if packages.isEmpty { return nil } return Entry(id: entry.id, name: entry.name, blurb: entry.blurb, usage: entry.usage, author: entry.author, licence: entry.licence, home: entry.home, source: entry.source, packages: packages) } return entries.isEmpty ? nil : Section(id: section.id, entries: entries) } }() // NSLocalizedString, not String(localized:): an interpolated // LocalizationValue looks up a format key ("…section.%@"), not this one. static func heading(_ section: String) -> String { NSLocalizedString("native.colophon.section." + section, bundle: DocsResources.bundle, comment: "") } static func label(_ key: String) -> String { NSLocalizedString("native.colophon." + key, bundle: DocsResources.bundle, comment: "") } } /// The Colophon's list, by section. Text styles throughout, so it follows /// Dynamic Type; every row is plain text to VoiceOver. public struct DocsColophonView: View { private let sections = DocsColophon.sections private let close: (() -> Void)? /// close adds a Close button, for the sheet that presents this on iOS. public init(close: (() -> Void)? = nil) { self.close = close } /// "Colophon", for the menu item and window that open this. public static var title: String { String(localized: "native.colophon", bundle: DocsResources.bundle) } public var body: some View { NavigationStack { List { Section { Text(DocsColophon.label("intro")).fixedSize(horizontal: false, vertical: true) } ForEach(sections) { section in Section(DocsColophon.heading(section.id)) { ForEach(section.entries) { entry in NavigationLink(value: entry) { VStack(alignment: .leading, spacing: 2) { Text(entry.name).font(.body) Text(entry.licence.name).font(.subheadline).foregroundStyle(.secondary) } .accessibilityElement(children: .combine) } } } } } .navigationTitle(Text("native.colophon", bundle: DocsResources.bundle)) .navigationDestination(for: DocsColophon.Entry.self) { DocsColophonDetail(entry: $0) } .toolbar { if let close { ToolbarItem(placement: .confirmationAction) { Button(action: close) { Text("native.close", bundle: DocsResources.bundle) } } } } } .accessibilityIdentifier("docs.colophon") } } /// One entry: what it is and what the apps use it for, who made it, its /// licence, links to its home and source, and every package's full /// licence text. struct DocsColophonDetail: View { let entry: DocsColophon.Entry var body: some View { ScrollView { DocsColophonDetailContent(entry: entry) } .navigationTitle(entry.name) } } /// The page's text, in text styles so it grows with Dynamic Type and wraps /// rather than truncating at accessibility sizes. struct DocsColophonDetailContent: View { let entry: DocsColophon.Entry var body: some View { VStack(alignment: .leading, spacing: 12) { Text(entry.name).font(.title2.bold()).accessibilityAddTraits(.isHeader) Text(entry.blurb + " " + entry.usage).font(.body) LabeledContent(DocsColophon.label("by"), value: entry.author) LabeledContent(DocsColophon.label("licence"), value: entry.licence.name) if !entry.versionText.isEmpty { LabeledContent(DocsColophon.label("version"), value: entry.versionText) } if entry.home == entry.source, let url = entry.homeURL { Link(DocsColophon.label("homeandsource"), destination: url) } else { if let url = entry.homeURL { Link(DocsColophon.label("home"), destination: url) } if let url = entry.sourceURL { Link(DocsColophon.label("source"), destination: url) } } ForEach(entry.packages, id: \.self) { package in Divider() Text([package.name, package.version].compactMap { $0 }.joined(separator: " ")).font(.headline).accessibilityAddTraits(.isHeader) if package.notices.isEmpty { Text(DocsColophon.label(package.tool == true ? "notincluded" : "missing")).font(.footnote) } ForEach(package.notices, id: \.self) { notice in Text(notice.name).font(.subheadline.bold()) // A long notice in one Text is laid out at once; the // Rust standard library's runs to thousands of lines, so // it is split into paragraphs that lay out as they scroll. LazyVStack(alignment: .leading, spacing: 0) { ForEach(Array(paragraphs(notice.text).enumerated()), id: \.offset) { _, paragraph in Text(paragraph).font(.footnote).textSelection(.enabled) } } } } } .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .padding() } private func paragraphs(_ text: String) -> [String] { var out: [String] = [] var current: [Substring] = [] for line in text.split(separator: "\n", omittingEmptySubsequences: false) { current.append(line) if current.count >= 40 && line.trimmingCharacters(in: .whitespaces).isEmpty { out.append(current.joined(separator: "\n")) current = [] } } if !current.isEmpty { out.append(current.joined(separator: "\n")) } return out } }