Cotton code
243 lines · 10.9 KB · SwiftRaw
// COTTON CHAT'S "WARM" LOOK (assets/design/chat/DECISIONS.md "Look", warm.html's// `:root`): a warm off-white ground, charcoal ink, one terracotta accent, the// person's own words in a pale warm tint; at night a near-black warm ground// with an amber accent, following the phone's setting. One humanist grotesque// throughout, Hanken Grotesk, with Noto Sans for the Indian scripts matched in// weight (`ChatFonts`, carried by Chat alone). The same colours draw every// Chat screen (`ChatLook.warm`, read by role as the core's themes once were)// and the shell's cards, sheets and toasts inside Chat (`ChatLookShell`).#if os(iOS)import CoreTextimport SwiftUIimport UIKit/// warm.html's colours, by name, for one appearance.struct WarmInks: Equatable {    var ground: String    var ink: String    var quiet: String    var faint: String    var mine: String    var mineInk: String    var accent: String    var onAccent: String    var chip: String    var line: String    /// The writing box's own fill, a step off the ground.    var field: String    /// Initials on a soft tint.    var tint: String    var danger: String    /// A group's quiet name colours, one per person.    var names: [String]    static let day = WarmInks(        ground: "#FAF7F2", ink: "#221F1B", quiet: "#8A847B", faint: "#B9B3A9", mine: "#F6E3D3", mineInk: "#2A2018",        accent: "#C8552D", onAccent: "#FFFFFF", chip: "#F0EBE3", line: "#E6E0D6", field: "#FFFFFF", tint: "#EFD9CB",        danger: "#B3361F",        names: ["#C8552D", "#3F7A5C", "#4A66B0", "#8A5A9E", "#A0702A", "#2F7C86", "#B0475E", "#5E7A2E"])    static let night = WarmInks(        ground: "#171411", ink: "#F1EBE3", quiet: "#9C9387", faint: "#6B645B", mine: "#3A2B20", mineInk: "#F6E8DA",        accent: "#E2A040", onAccent: "#1A140E", chip: "#25201B", line: "#2F2923", field: "#201B17", tint: "#3A2C22",        danger: "#E2705A",        names: ["#E2A040", "#7DBF98", "#8DA4E6", "#C29BD6", "#D9B26A", "#6FBEC8", "#E08A9C", "#A6C26E"])    static func of(dark: Bool) -> WarmInks { dark ? night : day }    /// These colours with Look's laid over them (the core's `look {app: messages}` tokens for the half standing).    func looked(_ t: [String: String]?) -> WarmInks {        guard let t else { return self }        var w = self        w.ground = t["ground"] ?? ground        w.ink = t["ink"] ?? ink        w.quiet = t["quiet"] ?? quiet        w.faint = t["faint"] ?? faint        w.mine = t["mine"] ?? mine        w.mineInk = t["mine_ink"] ?? mineInk        w.accent = t["accent"] ?? accent        w.onAccent = t["on_accent"] ?? onAccent        w.chip = t["chip"] ?? chip        w.line = t["line"] ?? line        w.field = t["raised"] ?? field        w.tint = t["initials"] ?? tint        w.danger = t["danger"] ?? danger        let named = (0..<6).compactMap { t["name.\($0)"] }        if named.count == 6 { w.names = named }        return w    }    /// The tokens Chat's screens read by role (`ChatColours`).    var tokens: [String: String] {        var t: [String: String] = [            "ground": ground, "header": chip, "text": ink, "secondary": quiet, "meta": faint, "rule": line,            "accent": accent, "badge": accent, "badge_ink": onAccent, "bubble_out": mine, "ink_out": mineInk,            "meta_out": quiet, "bubble_in": ground, "ink_in": ink, "meta_in": quiet, "tick": faint, "tick_read": accent,            "link": accent, "pill": chip, "pill_ink": quiet, "reaction": chip, "reaction_ink": ink, "danger": danger,            "pattern": line, "quote_bar": accent, "field": field, "faint": faint,        ]        for n in 0..<8 {            t["tint.\(n)"] = tint            t["name.\(n)"] = names[n % names.count]        }        return t    }}/// Chat's faces, registered once from the app's own `ChatFonts`.enum WarmFonts {    /// The variable axis CoreText names weight by ('wght').    private static let weightAxis = 0x7767_6874    static let scripts = ["NotoSansDevanagari", "NotoSansBengali", "NotoSansTamil", "NotoSansTelugu", "NotoSansKannada",                          "NotoSansMalayalam", "NotoSansGujarati", "NotoSansGurmukhi", "NotoSansOriya"]    private static let registered: (sans: String?, scripts: [String]) = {        func register(_ file: String) -> String? {            guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "ChatFonts") 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("HankenGrotesk"), scripts.compactMap(register))    }()    static func register() { _ = registered }    /// Whether Hanken Grotesk is in this build (Chat's own).    static var hasSans: Bool { registered.sans != nil }    /// Hanken Grotesk at `size` points and 'wght' `weight`, with each Indian    /// script's Noto face at the same weight behind it, scaled by the phone's    /// text size (the largest accessibility sizes included). Without the    /// files (another app's build, a test) the system sans stands in.    static func ui(_ size: CGFloat, weight: Double = 400, italic: Bool = false, scaled: Bool = true) -> UIFont {        let base: UIFont        if let sans = registered.sans {            let variation = UIFontDescriptor.AttributeName(rawValue: kCTFontVariationAttribute as String)            let cascade = registered.scripts.map { UIFontDescriptor(name: $0, size: size).addingAttributes([variation: [weightAxis: weight]]) }            var descriptor = UIFontDescriptor(name: sans, size: size)                .addingAttributes([variation: [weightAxis: weight], .cascadeList: cascade])            if italic { descriptor = descriptor.withMatrix(CGAffineTransform(a: 1, b: 0, c: 0.2, d: 1, tx: 0, ty: 0)) }            base = UIFont(descriptor: descriptor, size: size)        } else {            base = UIFont.systemFont(ofSize: size, weight: systemWeight(weight))        }        return scaled ? UIFontMetrics(forTextStyle: .body).scaledFont(for: base) : base    }    static func font(_ size: CGFloat, weight: Double = 400, italic: Bool = false) -> Font {        Font(ui(size, weight: weight, italic: italic))    }    /// The 'wght' a named weight stands for.    static func wght(_ weight: UIFont.Weight) -> Double {        switch weight {        case .ultraLight: 200        case .thin: 250        case .light: 300        case .regular: 400        case .medium: 500        case .semibold: 600        case .bold: 700        case .heavy: 800        case .black: 900        default: 400 + Double(weight.rawValue) * 500        }    }    private static func systemWeight(_ wght: Double) -> UIFont.Weight {        switch wght {        case ..<350: .light        case ..<450: .regular        case ..<550: .medium        case ..<650: .semibold        default: .bold        }    }}extension ChatLook {    /// Chat's Warm look for this appearance, at the person's text size multiple.    /// As the person's Look stands (shared/DECISIONS.md "Look"): the chosen paper's half for the phone's appearance, the    /// ink, the face and the size, from the core's `look` (`LookModel`); before an answer, Warm itself.    @MainActor static func warm(dark: Bool, scale: Double = 1) -> ChatLook {        let look = LookModel.standing("messages")        let inks = WarmInks.of(dark: dark).looked(look.tokens)        return ChatLook(colours: ChatColours(inks.tokens), ground: "plain", scale: look.answer == nil ? scale : Double(look.scale), face: look.face)    }}/// Chat's app look for the shell's pieces inside it (the sealed-key and/// recovery cards, the sign-in ask and sheet, the unlock request, the gate,/// the toasts), as Notes', Story's and Days' (`VaultLook.standingApp`).enum ChatLookShell {    private static func inks(_ w: WarmInks) -> AppLookInks {        AppLookInks(ground: w.ground, field: w.chip, rule: w.line, text: w.ink, secondary: w.quiet, accent: w.accent,                    onAccent: w.onAccent, danger: w.danger, safe: w.accent, scrim: w.ink + "33")    }    static let appLook = VaultLook.app(light: inks(.day), dark: inks(.night), text: .hanken, display: .hanken, mono: .hanken, card: 22)}/// The shell's passing line (an approval, a sync failure, a sealed key, a report sent) in Comms' own toast/// (`AppToast`): a word said once, above the foot and clear of the floating pill, in the ground's colour on the ink,/// rounded, as Android's `Said`; Undo beside it when the line carries one.struct ChatToast: View {    let toast: CottonToast    let onUndo: () -> Void    let onDismiss: () -> Void    @Environment(\.colorScheme) private var scheme    var body: some View {        let look = ChatLook.warm(dark: scheme == .dark)        HStack(spacing: 12) {            Text(verbatim: toast.message)                .font(look.font(15))                .foregroundStyle(look.colours.ground)                .multilineTextAlignment(toast.undo == nil ? .center : .leading)                .lineLimit(AppToastView.lineLimit(toast.tone))                .fixedSize(horizontal: false, vertical: true)            if toast.undo != nil {                Button(action: onUndo) {                    Text(CottonStrings.undo).font(look.font(15, .semibold)).foregroundStyle(look.colours.ground)                        .frame(minWidth: 44, minHeight: 44).contentShape(Rectangle())                }                .buttonStyle(.plain)            }        }        .padding(.horizontal, 18)        .padding(.vertical, toast.undo == nil ? 12 : 0)        .background(RoundedRectangle(cornerRadius: 18, style: .continuous).fill(look.colours.text))        .padding(.horizontal, 24)        .onTapGesture(perform: onDismiss)        .padding(.bottom, Warm.toastAboveFoot)        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)    }}/// warm.html's measures.enum Warm {    /// A word said once stands this far above the foot of the safe area, clear of the pill and the writing box.    static let toastAboveFoot: CGFloat = 96    /// The page's side gutter.    static let side: CGFloat = 18    /// A conversation row's picture.    static let rowPicture: CGFloat = 54    /// A run's picture beside their words.    static let runPicture: CGFloat = 34    /// The round buttons (compose, back, call, plus, microphone).    static let round: CGFloat = 40    /// Between the floating pill and the foot of the screen's safe area.    static let pillGap: CGFloat = 6    /// Below a place's last row, above the pill, once scrolled to the end.    static let footRoom: CGFloat = 16}#endif#if os(iOS)extension ChatLook {    /// A Warm panel: the ground, rounded, over a soft scrim.    var panel: LookPanelStyle { LookPanelStyle(scrim: colours.text.opacity(0.28), ground: colours.ground, corner: 26) }}#endif