// 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. import CoreText import CottonKit import SwiftUI /// One type role: which face, its size in points, weight, line height and tracking. struct VaultTypeRole: Decodable, Equatable, Sendable { let font: String let size: CGFloat let weight: Int let line: CGFloat let tracking: CGFloat } /// One bundled family (`sans`, `mono`). struct VaultFontFamily: Decodable, Equatable, Sendable { let family: String let licence: String let weights: [Int] } /// The whole look, as `vault.look` answers it. struct VaultLook: 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`). static var 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. static var 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). static var saysItsName: Bool { FlavourManifest.current?.flavour == "health" } /// The line set the app's own name is said in. static var 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. static var 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 { guard let spec = type[role] else { return .body } // An app's own faces (Notes, Story, Days: `AppLookFace`), carried by that app alone. #if os(iOS) if let face = AppLookFace(rawValue: spec.font) { return face.font(spec.size, weight: spec.weight, at: size) } #endif let 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. static func textStyle(_ size: CGFloat) -> Font.TextStyle { switch size { case 30...: return .largeTitle case 21..<30: return .title2 case 17..<21: return .body case 15..<17: return .subheadline default: return .footnote } } } extension Color { /// `#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 { return nil } let r, g, b, a: Double if 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. enum VaultFonts { /// The files, by face and weight. static let 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. private static let registered: [String: String] = { var names: [String: String] = [:] for file in files.values.flatMap(\.values) { guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "VaultFonts") else { continue } _ = CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) if let 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 }() static func register() { _ = registered } /// The PostScript name for a face at a weight: the nearest weight the /// face ships. static func postScriptName(family: String, weight: Int) -> String { let faces = files[family] ?? files["sans"] ?? [:] let nearest = faces.keys.min(by: { abs($0 - weight) < abs($1 - weight) }) ?? 400 let file = faces[nearest] ?? "Inter-Regular" return registered[file] ?? file } } extension EnvironmentValues { /// 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. struct VaultText: ViewModifier { let look: VaultLook let role: String let colour: String @Environment(\.colorScheme) private var scheme @Environment(\.dynamicTypeSize) private var size func body(content: Content) -> some View { 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)) } } extension View { /// Draw text in one of the look's type roles and colour roles. func vault(_ look: VaultLook, _ role: String, _ colour: String = "text") -> some View { modifier(VaultText(look: look, role: role, colour: colour)) } } /// What the app's root holds for Cotton Vault: its model on the phone, /// nothing elsewhere. @MainActor final class VaultAppHolder { #if os(iOS) let model: VaultModel init(model: VaultModel) { self.model = model } var look: VaultLook? { model.look } var scheme: ColorScheme? { model.scheme } #else var look: VaultLook? { nil } var scheme: ColorScheme? { nil } #endif }