| 1 | import Foundation |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// The apps' Colophon: every open-source project they are made with, what |
| 5 | /// it is, what the apps use it for, who made it, its licence and where it |
| 6 | /// lives, with every package's full licence text. It replaces the font |
| 7 | /// licences screen; the typefaces are its first section. |
| 8 | /// |
| 9 | /// Nothing here is a list. Resources/Colophon.json is generated by the |
| 10 | /// server repository's internal/shipped from what the apps ship (the |
| 11 | /// Rust in the document core and the interpreter that runs it, the Swift |
| 12 | /// packages, the fonts, the icons, the HTML entities table), with every |
| 13 | /// description from estilo's catalog, the same words the web's Help → |
| 14 | /// Colophon uses. TestColophonsMatchWhatShips fails when it is stale. |
| 15 | enum DocsColophon { |
| 16 | struct Notice: Decodable, Hashable { |
| 17 | let name: String |
| 18 | let text: String |
| 19 | enum CodingKeys: String, CodingKey { case name = "Name", text = "Text" } |
| 20 | } |
| 21 | struct Package: Decodable, Hashable { |
| 22 | let ecosystem: String |
| 23 | let name: String |
| 24 | let version: String? |
| 25 | let tool: Bool? |
| 26 | /// The platforms the package ships on; nil is all of them. |
| 27 | let platforms: [String]? |
| 28 | let notices: [Notice] |
| 29 | } |
| 30 | struct Licence: Decodable, Hashable { |
| 31 | let spdx: String |
| 32 | let name: String |
| 33 | } |
| 34 | struct Entry: Decodable, Hashable, Identifiable { |
| 35 | let id: String |
| 36 | let name: String |
| 37 | let blurb: String |
| 38 | let usage: String |
| 39 | let author: String |
| 40 | let licence: Licence |
| 41 | let home: String |
| 42 | let source: String |
| 43 | let packages: [Package] |
| 44 | var homeURL: URL? { URL(string: home) } |
| 45 | var sourceURL: URL? { URL(string: source) } |
| 46 | /// The entry's version, or each package's when they differ. |
| 47 | var versionText: String { |
| 48 | let versions = Set(packages.map { $0.version ?? "" }) |
| 49 | if versions.count == 1 { return versions.first ?? "" } |
| 50 | return packages.compactMap { p in p.version.map { "\(p.name) \($0)" } }.joined(separator: ", ") |
| 51 | } |
| 52 | } |
| 53 | struct Section: Decodable, Hashable, Identifiable { |
| 54 | let id: String |
| 55 | let entries: [Entry] |
| 56 | } |
| 57 | private struct File: Decodable { let sections: [Section] } |
| 58 | |
| 59 | static let sectionOrder: Set<String> = ["typefaces", "go", "javascript", "rust", "swift", "icons", "data", "tools", "services"] |
| 60 | |
| 61 | #if os(macOS) |
| 62 | static let platform = "mac" |
| 63 | #else |
| 64 | static let platform = "ios" |
| 65 | #endif |
| 66 | |
| 67 | /// The Colophon for this platform: a package limited to other |
| 68 | /// platforms is left out, and so is an entry left with none. |
| 69 | static let sections: [Section] = { |
| 70 | guard let url = DocsResources.bundle.url(forResource: "Colophon", withExtension: "json"), |
| 71 | let data = try? Data(contentsOf: url), |
| 72 | let file = try? JSONDecoder().decode(File.self, from: data) else { |
| 73 | fatalError("DocsColophon: Resources/Colophon.json is missing from the DocsKit bundle or unreadable") |
| 74 | } |
| 75 | return file.sections.compactMap { section in |
| 76 | let entries = section.entries.compactMap { entry -> Entry? in |
| 77 | let packages = entry.packages.filter { $0.platforms?.contains(platform) ?? true } |
| 78 | if packages.isEmpty { return nil } |
| 79 | 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) |
| 80 | } |
| 81 | return entries.isEmpty ? nil : Section(id: section.id, entries: entries) |
| 82 | } |
| 83 | }() |
| 84 | |
| 85 | // NSLocalizedString, not String(localized:): an interpolated |
| 86 | // LocalizationValue looks up a format key ("…section.%@"), not this one. |
| 87 | static func heading(_ section: String) -> String { |
| 88 | NSLocalizedString("native.colophon.section." + section, bundle: DocsResources.bundle, comment: "") |
| 89 | } |
| 90 | |
| 91 | static func label(_ key: String) -> String { |
| 92 | NSLocalizedString("native.colophon." + key, bundle: DocsResources.bundle, comment: "") |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// The Colophon's list, by section. Text styles throughout, so it follows |
| 97 | /// Dynamic Type; every row is plain text to VoiceOver. |
| 98 | public struct DocsColophonView: View { |
| 99 | private let sections = DocsColophon.sections |
| 100 | private let close: (() -> Void)? |
| 101 | /// close adds a Close button, for the sheet that presents this on iOS. |
| 102 | public init(close: (() -> Void)? = nil) { self.close = close } |
| 103 | /// "Colophon", for the menu item and window that open this. |
| 104 | public static var title: String { String(localized: "native.colophon", bundle: DocsResources.bundle) } |
| 105 | public var body: some View { |
| 106 | NavigationStack { |
| 107 | List { |
| 108 | Section { |
| 109 | Text(DocsColophon.label("intro")).fixedSize(horizontal: false, vertical: true) |
| 110 | } |
| 111 | ForEach(sections) { section in |
| 112 | Section(DocsColophon.heading(section.id)) { |
| 113 | ForEach(section.entries) { entry in |
| 114 | NavigationLink(value: entry) { |
| 115 | VStack(alignment: .leading, spacing: 2) { |
| 116 | Text(entry.name).font(.body) |
| 117 | Text(entry.licence.name).font(.subheadline).foregroundStyle(.secondary) |
| 118 | } |
| 119 | .accessibilityElement(children: .combine) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | .navigationTitle(Text("native.colophon", bundle: DocsResources.bundle)) |
| 126 | .navigationDestination(for: DocsColophon.Entry.self) { DocsColophonDetail(entry: $0) } |
| 127 | .toolbar { |
| 128 | if let close { |
| 129 | ToolbarItem(placement: .confirmationAction) { |
| 130 | Button(action: close) { Text("native.close", bundle: DocsResources.bundle) } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | .accessibilityIdentifier("docs.colophon") |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// One entry: what it is and what the apps use it for, who made it, its |
| 140 | /// licence, links to its home and source, and every package's full |
| 141 | /// licence text. |
| 142 | struct DocsColophonDetail: View { |
| 143 | let entry: DocsColophon.Entry |
| 144 | var body: some View { |
| 145 | ScrollView { DocsColophonDetailContent(entry: entry) } |
| 146 | .navigationTitle(entry.name) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// The page's text, in text styles so it grows with Dynamic Type and wraps |
| 151 | /// rather than truncating at accessibility sizes. |
| 152 | struct DocsColophonDetailContent: View { |
| 153 | let entry: DocsColophon.Entry |
| 154 | var body: some View { |
| 155 | VStack(alignment: .leading, spacing: 12) { |
| 156 | Text(entry.name).font(.title2.bold()).accessibilityAddTraits(.isHeader) |
| 157 | Text(entry.blurb + " " + entry.usage).font(.body) |
| 158 | LabeledContent(DocsColophon.label("by"), value: entry.author) |
| 159 | LabeledContent(DocsColophon.label("licence"), value: entry.licence.name) |
| 160 | if !entry.versionText.isEmpty { LabeledContent(DocsColophon.label("version"), value: entry.versionText) } |
| 161 | if entry.home == entry.source, let url = entry.homeURL { |
| 162 | Link(DocsColophon.label("homeandsource"), destination: url) |
| 163 | } else { |
| 164 | if let url = entry.homeURL { Link(DocsColophon.label("home"), destination: url) } |
| 165 | if let url = entry.sourceURL { Link(DocsColophon.label("source"), destination: url) } |
| 166 | } |
| 167 | ForEach(entry.packages, id: \.self) { package in |
| 168 | Divider() |
| 169 | Text([package.name, package.version].compactMap { $0 }.joined(separator: " ")).font(.headline).accessibilityAddTraits(.isHeader) |
| 170 | if package.notices.isEmpty { |
| 171 | Text(DocsColophon.label(package.tool == true ? "notincluded" : "missing")).font(.footnote) |
| 172 | } |
| 173 | ForEach(package.notices, id: \.self) { notice in |
| 174 | Text(notice.name).font(.subheadline.bold()) |
| 175 | // A long notice in one Text is laid out at once; the |
| 176 | // Rust standard library's runs to thousands of lines, so |
| 177 | // it is split into paragraphs that lay out as they scroll. |
| 178 | LazyVStack(alignment: .leading, spacing: 0) { |
| 179 | ForEach(Array(paragraphs(notice.text).enumerated()), id: \.offset) { _, paragraph in |
| 180 | Text(paragraph).font(.footnote).textSelection(.enabled) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | .fixedSize(horizontal: false, vertical: true) |
| 187 | .frame(maxWidth: .infinity, alignment: .leading) |
| 188 | .padding() |
| 189 | } |
| 190 | |
| 191 | private func paragraphs(_ text: String) -> [String] { |
| 192 | var out: [String] = [] |
| 193 | var current: [Substring] = [] |
| 194 | for line in text.split(separator: "\n", omittingEmptySubsequences: false) { |
| 195 | current.append(line) |
| 196 | if current.count >= 40 && line.trimmingCharacters(in: .whitespaces).isEmpty { |
| 197 | out.append(current.joined(separator: "\n")) |
| 198 | current = [] |
| 199 | } |
| 200 | } |
| 201 | if !current.isEmpty { out.append(current.joined(separator: "\n")) } |
| 202 | return out |
| 203 | } |
| 204 | } |
| 205 | |