// Cotton Vault's look, as the core answers it (`vault.look`, the kit's// `kits/keys/look.json`): colour roles for light and dark, type roles, spaces// and corners. Nothing inside Vault's screens names a colour, a size, a gap or// a corner of its own: every one is read here by its role. Android reads the// same file, so the two phones come out the same design.//// Gated on the app: only Cotton Keys (shown as Cotton Vault) reads it// (`VaultLook.applies`). Every other app keeps the paper it has.importCoreTextimportCottonKitimportSwiftUI/// One type role: which face, its size in points, weight, line height and tracking.structVaultTypeRole: Decodable, Equatable, Sendable {let font: Stringlet size: CGFloatlet weight: Intlet line: CGFloatlet tracking: CGFloat}/// One bundled family (`sans`, `mono`).structVaultFontFamily: Decodable, Equatable, Sendable {let family: Stringlet licence: Stringlet weights: [Int]}/// The whole look, as `vault.look` answers it.structVaultLook: Decodable, Equatable, Sendable {let fonts: [String: VaultFontFamily]let colours: [String: [String: String]]let type: [String: VaultTypeRole]let space: [String: CGFloat]let corners: [String: CGFloat]let roles: [String: String]/// Whether this app draws Vault: Cotton Keys alone. The registry slug,/// bundle and application ids stay `keys`; the name people read is the/// core's (`Cotton Vault`).staticvar applies: Bool { FlavourManifest.current?.flavour == "keys" }/// Whether this app's screens stand in a look of this shape rather than/// on paper: Cotton Vault, and Cotton Health (`health.look`). The shell's/// pieces inside either (the unlock card, the sign-in ask) stand above/// the screen in its look and cover nothing.staticvar drawsALook: Bool {let flavour = FlavourManifest.current?.flavour ?? ""return ["keys", "health", "story", "days"].contains(flavour) || flavour.hasPrefix("notes") }/// Whether the app's waiting frame and gate say its name (Health) or stand on the bare ground,/// as its launch screen does (Notes, Story, Days, Secrets: flat, no word mark).staticvar saysItsName: Bool { FlavourManifest.current?.flavour == "health" }/// The line set the app's own name is said in.staticvar nameSet: String {let flavour = FlavourManifest.current?.flavour ?? ""return flavour == "health" ? "health" : flavour.hasPrefix("notes") ? "notes" : "vault" }/// The line of that set that says the app's name.staticvar namePath: String { nameSet == "notes" ? "app.name" : "app_name" }/// A colour role in the mode drawn. A role the look does not name is a/// defect of the kit, which its own guard refuses; drawn clear here.func colour(_ role: String, _ scheme: ColorScheme) -> Color {let mode = scheme == .dark ? "dark" : "light"let name = colours[mode]?[role] ?? colours[mode]?[roles[role] ?? ""]return name.flatMap(Color.init(vaultHex:)) ?? .clear }/// A space by name (`s`, `m`, `gutter`, `row_height`, …).func gap(_ name: String) -> CGFloat { space[name] ?? 0 }/// A corner by name (`row`, `card`, `field`, `button`, `sheet`, `chip`, `picture`).func corner(_ name: String) -> CGFloat { corners[name] ?? 0 }/// The font a type role draws with: the bundled face at the role's weight,/// scaled with the person's text size.////// `at`: the text size of the view it is drawn in (`\.dynamicTypeSize`)./// A face drawn through UIKit (Notes', Chat's) is scaled to it, not to the/// phone's, so a piece held at a smaller size (the shell's cards,/// `ShellCardsKnobs.largestText`) is held in every app's faces alike.func font(_ role: String, at size: DynamicTypeSize? = nil) -> Font {guardlet spec = type[role] else { return .body }// An app's own faces (Notes, Story, Days: `AppLookFace`), carried by that app alone. #if os(iOS)iflet face = AppLookFace(rawValue: spec.font) { return face.font(spec.size, weight: spec.weight, at: size) } #endiflet name = VaultFonts.postScriptName(family: spec.font, weight: spec.weight)return .custom(name, size: spec.size, relativeTo: VaultLook.textStyle(spec.size)) }/// The UIKit font, for a measured line.func role(_ role: String) -> VaultTypeRole? { type[role] }/// The Dynamic Type style a size scales with.staticfunc textStyle(_ size: CGFloat) -> Font.TextStyle {switch size {case30...: return .largeTitlecase21..<30: return .title2case17..<21: return .bodycase15..<17: return .subheadlinedefault: return .footnote } }}extensionColor {/// `#RRGGBB` or `#RRGGBBAA`, as the look writes them.init?(vaultHex text: String) {var hex = text.trimmingCharacters(in: .whitespaces)if hex.hasPrefix("#") { hex.removeFirst() }guard hex.count == 6 || hex.count == 8, let value = UInt64(hex, radix: 16) else { returnnil }let r, g, b, a: Doubleif hex.count == 8 { r = Double((value >> 24) & 0xFF) / 255 g = Double((value >> 16) & 0xFF) / 255 b = Double((value >> 8) & 0xFF) / 255 a = Double(value & 0xFF) / 255 } else { r = Double((value >> 16) & 0xFF) / 255 g = Double((value >> 8) & 0xFF) / 255 b = Double(value & 0xFF) / 255 a = 1 }self.init(.sRGB, red: r, green: g, blue: b, opacity: a) }}/// Inter and JetBrains Mono, both SIL OFL 1.1, shipped by Cotton Vault alone/// (`App/Flavours/keys/VaultFonts`, beside their licences, copied into the/// app's bundle by the spec gate) and registered for this process once.enumVaultFonts {/// The files, by face and weight.staticlet files: [String: [Int: String]] = ["sans": [400: "Inter-Regular", 500: "Inter-Medium", 600: "Inter-SemiBold", 700: "Inter-Bold"],"mono": [400: "JetBrainsMono-Regular", 600: "JetBrainsMono-SemiBold"], ]/// Registered once; answers each file's PostScript name as CoreText read it.privatestaticlet registered: [String: String] = {var names: [String: String] = [:]for file in files.values.flatMap(\.values) {guardlet url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "VaultFonts") else { continue } _ = CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)iflet descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor],let first = descriptors.first,let name = CTFontDescriptorCopyAttribute(first, kCTFontNameAttribute) as? String { names[file] = name } else { names[file] = file } }return names }()staticfunc register() { _ = registered }/// The PostScript name for a face at a weight: the nearest weight the/// face ships.staticfunc postScriptName(family: String, weight: Int) -> String {let faces = files[family] ?? files["sans"] ?? [:]let nearest = faces.keys.min(by: { abs($0 - weight) < abs($1 - weight) }) ?? 400let file = faces[nearest] ?? "Inter-Regular"return registered[file] ?? file }}extensionEnvironmentValues {/// Vault's look, set at the root of Cotton Keys alone. Shared pieces (the/// toast, the unlock card, the sign-in approval) draw Vault's look when it/// is set and their own otherwise. @Entry var vaultLook: VaultLook? = nil}/// A type role's face, line and tracking, in a colour role.structVaultText: ViewModifier {let look: VaultLooklet role: Stringlet colour: String @Environment(\.colorScheme) privatevar scheme @Environment(\.dynamicTypeSize) privatevar sizefunc body(content: Content) -> someView {let spec = look.role(role) content .font(look.font(role, at: size)) .tracking(spec?.tracking ?? 0) .lineSpacing(max(0, (spec?.line ?? 0) - (spec?.size ?? 0) * 1.21)) .foregroundStyle(look.colour(colour, scheme)) }}extensionView {/// Draw text in one of the look's type roles and colour roles.func vault(_ look: VaultLook, _ role: String, _ colour: String = "text") -> someView { modifier(VaultText(look: look, role: role, colour: colour)) }}/// What the app's root holds for Cotton Vault: its model on the phone,/// nothing elsewhere.@MainActorfinalclassVaultAppHolder { #if os(iOS)let model: VaultModelinit(model: VaultModel) { self.model = model }var look: VaultLook? { model.look }var scheme: ColorScheme? { model.scheme } #elsevar look: VaultLook? { nil }var scheme: ColorScheme? { nil } #endif}