oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3/// Help → Colophon: every open-source project and typeface the app ships,
4/// what it is, what Base uses it for, who made it, its licence and the
5/// licence's text (Resources/Colophon.json, scripts/colophon.py, with
6/// estilo's catalog's words where it has the project).
7public enum BaseColophon {
8 public struct Entry: Decodable, Hashable, Identifiable, Sendable {
9 public struct Licence: Decodable, Hashable, Sendable { public let spdx: String; public let name: String }
10 public let id: String
11 public let name: String
12 /// Catalog keys: what it is, and what Base uses it for, are copy.
13 public let blurbKey: String
14 public let usageKey: String
15 public var blurb: String { BaseText.text(blurbKey) }
16 public var usage: String { BaseText.text(usageKey) }
17 public let author: String
18 public let licence: Licence
19 public let home: String
20 public let source: String
21 public let licenceText: String
22 public let platforms: [String]?
23 }
24 public struct Section: Decodable, Hashable, Identifiable, Sendable {
25 public let id: String
26 public let entries: [Entry]
27 }
28 private struct File: Decodable { let sections: [Section] }
29
30 static func heading(_ section: String) -> String {
31 switch section {
32 case "typefaces": BaseText.text("native.colophon.section.typefaces")
33 default: BaseText.text("native.colophon.section.swift")
34 }
35 }
36
37 #if os(macOS)
38 static let platform = "mac"
39 #else
40 static let platform = "ios"
41 #endif
42
43 /// This platform's sections; nil when the file is missing (a broken build).
44 public static let sections: [Section] = {
45 guard let url = Bundle.module.url(forResource: "Colophon", withExtension: "json"),
46 let data = try? Data(contentsOf: url), let file = try? JSONDecoder().decode(File.self, from: data) else { return [] }
47 return file.sections.map { s in
48 Section(id: s.id, entries: s.entries.filter { $0.platforms?.contains(platform) ?? true })
49 }.filter { !$0.entries.isEmpty }
50 }()
51}
52
53/// The Colophon's window: the list, and an entry's details beside it.
54public struct ColophonView: View {
55 @State private var chosen: BaseColophon.Entry?
56 public init() {}
57 public static var title: String { BaseText.text("native.colophon") }
58
59 public var body: some View {
60 NavigationSplitView {
61 List(selection: $chosen) {
62 Section { Text(BaseText.text("native.colophon.intro")).fixedSize(horizontal: false, vertical: true) }
63 ForEach(BaseColophon.sections) { section in
64 Section(BaseColophon.heading(section.id)) {
65 ForEach(section.entries) { entry in
66 VStack(alignment: .leading, spacing: 2) {
67 Text(verbatim: entry.name)
68 Text(verbatim: entry.licence.name).font(.subheadline).foregroundStyle(.secondary)
69 }
70 .tag(entry)
71 .accessibilityElement(children: .combine)
72 }
73 }
74 }
75 }
76 .navigationSplitViewColumnWidth(min: 220, ideal: 260)
77 } detail: {
78 if let entry = chosen {
79 ScrollView {
80 VStack(alignment: .leading, spacing: 12) {
81 Text(verbatim: entry.name).font(.title2.weight(.semibold))
82 Text(verbatim: entry.blurb)
83 Text(verbatim: entry.usage).foregroundStyle(.secondary)
84 LabeledContent(BaseText.text("native.colophon.by"), value: entry.author)
85 LabeledContent(BaseText.text("native.colophon.licence"), value: entry.licence.name)
86 if let home = URL(string: entry.home) { Link(BaseText.text("native.colophon.home"), destination: home) }
87 if let source = URL(string: entry.source), entry.source != entry.home { Link(BaseText.text("native.colophon.source"), destination: source) }
88 Text(verbatim: entry.licenceText).font(.callout.monospaced()).textSelection(.enabled)
89 }
90 .padding(20)
91 .frame(maxWidth: .infinity, alignment: .leading)
92 }
93 } else {
94 Text(verbatim: "").frame(maxWidth: .infinity, maxHeight: .infinity)
95 }
96 }
97 .frame(minWidth: 680, minHeight: 460)
98 }
99}
100