Cotton code
135 lines · 7.8 KB · SwiftRaw
// THE APP LOOK an app with a look of its own hands the shell (research.md A5,// iPhone): the shell's pieces inside it (the sealed-key and recovery cards,// the sign-in ask, the unlock request, the read-only card, the sign-in sheet,// the gate, the toasts, the remove-from-phone ask) read `\.vaultLook` and draw// Vault's standing designs in the colours and faces it carries, never paper.// Vault and Health answer theirs from the core's look; Notes, Story and Days// build theirs here from a few colours and their own faces, so it stands from// the first frame. One value through the existing environment entry: nothing// new on the stack (433ec0d3d).#if os(iOS)import CottonWritingimport SwiftUIimport UIKit/// The few colours an app look is built from, as `#RRGGBB`.struct AppLookInks {    var ground: String    /// A field's and a quiet surface's fill.    var field: String    var rule: String    var text: String    var secondary: String    var accent: String    var onAccent: String    /// What a refusal or a taking-away act is said in (Story and Days have no red: their ink).    var danger: String    var safe: String    var scrim: String}/// The faces an app look names in its type roles, each drawn by `VaultLook.font`.enum AppLookFace: String {    /// Notes: Figtree and JetBrains Mono (`WritingFonts`).    case figtree, jetbrains    /// Story: Courier Prime for titles, IBM Plex Mono for everything else (`InkType`).    case inkTitle = "ink-title", inkMono = "ink-mono"    /// Days: DM Mono Days (`PenType`).    case penMono = "pen-mono"    /// Chat: Hanken Grotesk, with Noto Sans for the Indian scripts (`WarmFonts`).    case hanken    /// Secrets: Red Hat Text for words, Red Hat Mono for values (`SecretsType`).    case redHat = "red-hat", redHatMono = "red-hat-mono"    /// The face at `size` and `weight` (100–900), scaled to the text size    /// `at` where one is given (the view's), else to the phone's.    func font(_ size: CGFloat, weight: Int, at textSize: DynamicTypeSize? = nil) -> Font {        let style = VaultLook.textStyle(size)        let traits = textSize.map { UITraitCollection(preferredContentSizeCategory: UIContentSizeCategory($0)) }        switch self {        case .figtree, .jetbrains:            let w: UIFont.Weight = switch weight {            case ..<450: .regular            case ..<550: .medium            case ..<650: .semibold            default: .bold            }            return Font(WritingFonts.font(rawValue, size: size, weight: w, traits: traits))        // Story's and Days' faces follow their Look, which lives on the main actor, where every view is drawn.        case .inkTitle:            return MainActor.assumeIsolated { InkType.title(size, relativeTo: style) }        case .inkMono:            return MainActor.assumeIsolated { InkType.mono(size, weight >= 600 ? .semibold : weight >= 500 ? .medium : .regular, relativeTo: style) }        case .penMono:            return MainActor.assumeIsolated { PenType.mono(size, weight >= 500 ? .medium : weight < 400 ? .light : .regular, relativeTo: style) }        case .hanken:            let base = WarmFonts.ui(size, weight: Double(weight), scaled: false)            return Font(UIFontMetrics(forTextStyle: .body).scaledFont(for: base, compatibleWith: traits))        case .redHat, .redHatMono:            let base = SecretsFonts.ui(mono: self == .redHatMono, size, weight: Double(weight))            return Font(UIFontMetrics(forTextStyle: .body).scaledFont(for: base, compatibleWith: traits))        }    }}extension VaultLook {    /// An app look from its inks, light and dark, in `text` for words, `display` for titles and codes, `mono` for secrets.    static func app(light: AppLookInks, dark: AppLookInks, text: AppLookFace, display: AppLookFace, mono: AppLookFace,                    card: CGFloat = 14) -> VaultLook {        func roles(_ c: AppLookInks, dark: Bool) -> [String: String] {            let wash = dark ? "33" : "1F"            return [                "background": c.ground, "surface": c.field, "raised": c.ground, "sheet": c.ground, "separator": c.rule,                "field": c.field, "text": c.text, "secondary": c.secondary, "tertiary": c.secondary, "disabled": c.rule,                "accent": c.accent, "on_accent": c.onAccent, "safe": c.safe, "safe_wash": c.field,                "attention": c.accent, "attention_wash": c.accent + wash, "danger": c.danger, "danger_wash": c.danger + wash,                "scrim": c.scrim,                "tint.0": c.field, "tint.1": c.rule, "tint.2": c.field, "tint.3": c.rule, "tint.4": c.field,                "tint.5": c.rule, "tint.6": c.field, "tint.7": c.rule, "on_tint": c.text,            ]        }        func role(_ face: AppLookFace, _ size: CGFloat, _ weight: Int, _ line: CGFloat, _ tracking: CGFloat = 0) -> VaultTypeRole {            VaultTypeRole(font: face.rawValue, size: size, weight: weight, line: line, tracking: tracking)        }        return VaultLook(            // The faces are the app's own, named by the type roles: nothing to register here.            fonts: [:],            colours: ["light": roles(light, dark: false), "dark": roles(dark, dark: true)],            type: ["large_title": role(display, 32, 700, 39, -0.3), "title": role(display, 22, 700, 28, -0.2),                   "headline": role(text, 17, 600, 22), "body": role(text, 16, 400, 22), "row_title": role(text, 16, 600, 22),                   "row_detail": role(text, 14, 400, 19), "label": role(text, 13, 500, 18, 0.1), "section": role(text, 13, 600, 18, 0.2),                   "button": role(text, 16, 600, 22), "chip": role(text, 14, 500, 18), "initials": role(text, 15, 600, 20),                   "secret": role(mono, 17, 400, 22), "secret_large": role(display, 26, 600, 32), "code": role(mono, 15, 400, 20)],            space: ["xxs": 2, "xs": 4, "s": 8, "m": 12, "l": 16, "xl": 24, "xxl": 32, "xxxl": 48, "gutter": 20, "row_height": 60,                    "picture": 40, "tap": 44],            corners: ["picture": 8, "row": 10, "card": card, "field": 11, "button": 12, "sheet": 18, "chip": 999],            roles: ["row_title": "text", "row_detail": "secondary", "section": "tertiary", "main_action": "accent",                    "on_main_action": "on_accent", "safe": "safe", "attention": "attention", "danger": "danger", "neutral": "secondary"])    }    /// The look the shell's pieces draw in for this app, when it is one with a look of its own built here.    @MainActor static var standingApp: VaultLook? {        if NotesRoot.applies { return NotesLook.appLook }        if InkStoryRoot.applies { return InkLookShell.appLook }        if DaysRoot.applies { return PenLookShell.appLook }        if ChatRoot.applies { return ChatLookShell.appLook }        if SecretsRoot.applies { return SecretsLookShell.appLook }        return nil    }}/// Story's app look, from ink.html's `:root` (as Android's `InkCardLook`): one look, light and dark alike.enum InkLookShell {    static let inks = AppLookInks(ground: "#EEEDF5", field: "#E9E8F1", rule: "#9A9DBB", text: "#161A3A", secondary: "#5D6183",                                  accent: "#1F3FE0", onAccent: "#FFFFFF", danger: "#161A3A", safe: "#1F3FE0", scrim: "#161A3A2E")    static let appLook = VaultLook.app(light: inks, dark: inks, text: .inkMono, display: .inkTitle, mono: .inkMono, card: 18)}/// Days' app look, from pen.html's `:root` (as Android's `DaysCardLook`): one look, light and dark alike.enum PenLookShell {    static let inks = AppLookInks(ground: "#F1F2F6", field: "#ECEEF2", rule: "#AAB0C6", text: "#171C33", secondary: "#6B7088",                                  accent: "#1D3FD8", onAccent: "#FFFFFF", danger: "#171C33", safe: "#1D3FD8", scrim: "#171C332E")    static let appLook = VaultLook.app(light: inks, dark: inks, text: .penMono, display: .penMono, mono: .penMono, card: 18)}#endif