Cotton code
168 lines · 8.8 KB · SwiftRaw
// SECRETS' PLUM LOOK (assets/design/keys/DECISIONS.md "The look", secrets.html; the owner chose Plum on 25 Nov 2026):// a lilac-grey ground, a deep plum-black ink, and one plum accent, only for the main button on each screen, the dot on// an ask, "Answer" and "Change it", and "Still known to…". Red Hat Text for every word, Red Hat Mono for every secret// value, token, link and generated password (whatever face Look chooses, so 0 and O never blur). Thin rules and space;// no cards, shadows, gradients or coloured blocks. Every Secrets screen draws from this file, through the core's one// `look {app: keys}` (papers Lilac, Paper, Night, Coal; inks Plum, Jade, Garnet, Indigo, Graphite), as Android's.#if os(iOS)import CoreTextimport CottonWritingimport SwiftUIimport UIKit/// secrets.html's `:root`, as the person's Look stands: the chosen paper's half for the phone's appearance, in the/// chosen ink, from the core's `look`; before the core has ever answered on this phone, the Plum default for the/// phone's appearance. Read inside a body, a change in Look draws the screen again.@MainActorenum Plum {    static var look: LookModel { LookModel.standing("keys") }    private static func tone(_ token: String, _ light: UInt32, _ dark: UInt32) -> Color {        if let hex = look.tokens?[token] { return Color(uiColor: LookAnswer.ui(hex)) }        let value = LookAppearance.shared.systemDark ? dark : light        return Color(uiColor: UIColor(red: CGFloat((value >> 16) & 0xFF) / 255, green: CGFloat((value >> 8) & 0xFF) / 255,                                      blue: CGFloat(value & 0xFF) / 255, alpha: 1))    }    /// `--ground`: every Secrets screen stands on this.    static var ground: Color { tone("ground", 0xF1EEF3, 0x18131D) }    static var ink: Color { tone("ink", 0x261D30, 0xECE5F1) }    static var quiet: Color { tone("quiet", 0x62566B, 0xA69BB0) }    static var faint: Color { tone("faint", 0x978CA0, 0x6D6377) }    static var rule: Color { tone("rule", 0xD5CCDB, 0x2F2737) }    /// `--acc`: the one accent. "Act here", never "danger".    static var accent: Color { tone("accent", 0x7A3C9E, 0xC39BE3) }    static var onAccent: Color { tone("on_accent", 0xFFFFFF, 0x1A1020) }    /// Whether the standing ground is light (bars, keyboards and the status bar follow it).    static var isLight: Bool { look.answer == nil ? !LookAppearance.shared.systemDark : look.groundIsLight }    /// The screen's side margin (`.ph` padding 22).    static let side: CGFloat = 22    /// A 1 px rule.    static let hair: CGFloat = 1    /// A button's and a chosen pill's outline (`1.5px`).    static let stroke: CGFloat = 1.5}/// Red Hat Text (words) and Red Hat Mono (values), both variable on their 'wght' axis, SIL OFL, in the bundle's `look`/// folder (`assets/fonts/look`, the same files Android ships: `kits/look/look.json`'s `red_hat` and `red_hat_mono`).enum SecretsFonts {    private static let weightAxis = 0x7767_6874    private static let registered: (text: String?, italic: String?, mono: String?) = {        func register(_ file: String) -> String? {            guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "look") else { return nil }            CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)            let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? []            return descriptors.first.flatMap { CTFontDescriptorCopyAttribute($0, kCTFontNameAttribute) as? String }        }        return (register("RedHatText"), register("RedHatText-Italic"), register("RedHatMono"))    }()    static func register() { _ = registered }    /// The face `name` at `size` and 'wght' `weight`, or the system's when the file is not in this build.    static func ui(mono: Bool, _ size: CGFloat, weight: Double, italic: Bool = false) -> UIFont {        let name = mono ? registered.mono : italic ? (registered.italic ?? registered.text) : registered.text        guard let name else {            let w: UIFont.Weight = weight >= 650 ? .bold : weight >= 550 ? .semibold : weight >= 450 ? .medium : .regular            return mono ? .monospacedSystemFont(ofSize: size, weight: w) : .systemFont(ofSize: size, weight: w)        }        let variation = UIFontDescriptor.AttributeName(rawValue: kCTFontVariationAttribute as String)        return UIFont(descriptor: UIFontDescriptor(name: name, size: size).addingAttributes([variation: [weightAxis: weight]]), size: size)    }}/// secrets.html's type. Every size follows Look's size step and the phone's own text size (`relativeTo`), so larger/// text grows the screen rather than clipping it.@MainActorenum SecretsType {    enum Weight: Double { case regular = 400, medium = 500, semibold = 600, bold = 700 }    /// Words: Red Hat Text, or the face Look chose.    static func text(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body, italic: Bool = false) -> Font {        Font(ui(size, weight, relativeTo: style, italic: italic))    }    static func ui(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body, italic: Bool = false) -> UIFont {        let look = Plum.look        let size = size * look.scale        let base: UIFont        if look.face != LookFaces.own {            let w: UIFont.Weight = weight == .bold ? .bold : weight == .semibold ? .semibold : weight == .medium ? .medium : .regular            base = WritingFonts.font(LookFaces.drawn(look.face, own: "figtree"), size: size, weight: w)        } else {            base = SecretsFonts.ui(mono: false, size, weight: weight.rawValue, italic: italic)        }        return UIFontMetrics(forTextStyle: style.ui).scaledFont(for: base)    }    /// A secret value, a token, a link, a generated password: always Red Hat Mono.    static func mono(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body) -> Font {        Font(UIFontMetrics(forTextStyle: style.ui).scaledFont(for: SecretsFonts.ui(mono: true, size * Plum.look.scale, weight: weight.rawValue)))    }    /// The face as Look's screen draws a choice of it, at `size`.    static func face(_ id: String, size: CGFloat) -> Font {        id == LookFaces.own ? Font(SecretsFonts.ui(mono: false, size, weight: 400))            : Font(WritingFonts.font(LookFaces.drawn(id, own: "figtree"), size: size))    }    // secrets.html's sizes, by role.    /// `.ph h3`: 31 bold.    static var title: Font { text(31, .bold, relativeTo: .largeTitle) }    /// `.r .t`: a row's name, 17 semibold.    static var rowName: Font { text(17, .semibold) }    /// Body: 16 to 17.    static var body: Font { text(17) }    /// `.r .s`, `.note`: second lines and notes, 14.    static var small: Font { text(14, relativeTo: .footnote) }    /// `.ph h4`: a section's heading, 14 semibold quiet.    static var section: Font { text(14, .semibold, relativeTo: .footnote) }    /// `.f label`: a field's label, 13.    static var label: Font { text(13, relativeTo: .caption) }    /// `.btn`: a button's words, 17 bold.    static var button: Font { text(17, .bold) }    /// `.val`: a value in the mono face, 17.    static var value: Font { mono(17) }}private extension Font.TextStyle {    var ui: UIFont.TextStyle {        switch self {        case .largeTitle: .largeTitle        case .title: .title1        case .title2: .title2        case .title3: .title3        case .headline: .headline        case .subheadline: .subheadline        case .callout: .callout        case .footnote: .footnote        case .caption: .caption1        case .caption2: .caption2        default: .body        }    }}/// The shell's pieces inside Secrets (the unlock card, the sign-in ask, the gate, the toasts) in the standing Plum/// look, as Comms' and Days' (`VaultLook.standingApp`).@MainActorenum SecretsLookShell {    private static func inks(dark: Bool) -> AppLookInks {        let fallback: [String: String] = dark            ? ["ground": "#18131D", "ink": "#ECE5F1", "quiet": "#A69BB0", "rule": "#2F2737", "accent": "#C39BE3", "on_accent": "#1A1020"]            : ["ground": "#F1EEF3", "ink": "#261D30", "quiet": "#62566B", "rule": "#D5CCDB", "accent": "#7A3C9E", "on_accent": "#FFFFFF"]        let stood = dark ? Plum.look.answer?.colours.dark : Plum.look.answer?.colours.light        func t(_ key: String) -> String { stood?[key] ?? fallback[key] ?? "#808080" }        return AppLookInks(ground: t("ground"), field: t("ground"), rule: t("rule"), text: t("ink"), secondary: t("quiet"),                           accent: t("accent"), onAccent: t("on_accent"), danger: t("ink"), safe: t("accent"), scrim: t("ink") + "33")    }    static var appLook: VaultLook {        VaultLook.app(light: inks(dark: false), dark: inks(dark: true), text: .redHat, display: .redHat, mono: .redHatMono, card: 18)    }}#endif