Cotton code
110 lines · 4.5 KB · SwiftRaw
// Cotton Chat's look: its colours read by role, and the type Chat writes in// (Hanken Grotesk with Noto for the Indian scripts, `WarmFonts`, scaled by the// phone's text size and the person's own multiple). The Warm look's colours// are `ChatWarm.swift`'s; the core's old themes are no longer drawn.#if os(iOS)import CottonWritingimport SwiftUIimport UIKit/// The colours of one appearance of one theme, by role.struct ChatColours: Equatable {    var tokens: [String: String]    init(_ tokens: [String: String]) { self.tokens = tokens }    /// A token's colour (`ground`, `bubble_out`, `tint.3` …, read as    /// `chat.<role>`). A token the theme lacks draws in the theme's text ink,    /// so a gap shows rather than vanishes.    func ui(_ role: String) -> UIColor {        if let hex = tokens["chat.\(role)"] ?? tokens[role], writingRGBA(hex) != nil { return WritingTheme.ui(hex) }        if role != "text" { return ui("text") }        return WritingTheme.ui(tokens["chat.text"] ?? "")    }    func color(_ role: String) -> Color { Color(uiColor: ui(role)) }    // Chat's roles, read from the core's tokens (`messages::app::themes::TOKENS`).    var ground: Color { color("ground") }    var bar: Color { color("header") }    var text: Color { color("text") }    var dim: Color { color("secondary") }    var faint: Color { color("meta") }    var rule: Color { color("rule") }    var accent: Color { color("accent") }    /// Ink on the accent (the round buttons, a badge's count).    var onAccent: Color { color("badge_ink") }    var bubbleOut: Color { color("bubble_out") }    var bubbleOutText: Color { color("ink_out") }    var bubbleOutDim: Color { color("meta_out") }    var bubbleIn: Color { color("bubble_in") }    var bubbleInText: Color { color("ink_in") }    var bubbleInDim: Color { color("meta_in") }    var badge: Color { color("badge") }    var badgeText: Color { color("badge_ink") }    var badgeMuted: Color { color("secondary") }    var tick: Color { color("tick") }    var tickRead: Color { color("tick_read") }    var link: Color { color("link") }    var pill: Color { color("pill") }    var pillText: Color { color("pill_ink") }    var reaction: Color { color("reaction") }    var reactionText: Color { color("reaction_ink") }    var danger: Color { color("danger") }    var pattern: Color { color("pattern") }    var quoteBar: Color { color("quote_bar") }    /// What dims the thread under a lifted bubble, and a picture's time.    var scrim: Color { text.opacity(0.32) }    /// A menu's or the reactions row's ground.    var sheet: Color { color("ground") }    /// A typed-initials picture's tint, 0..<8.    func tint(_ n: Int) -> Color { color("tint.\(n)") }    /// A sender's name ink in a group, 0..<8.    func name(_ n: Int) -> Color { color("name.\(n)") }}/// The look a Chat screen draws with: the appearance's colours, the/// thread's ground and the text size.struct ChatLook: Equatable {    var colours: ChatColours    var ground: String    var scale: Double    /// Look's face (shared/DECISIONS.md "Look"): `own` is Hanken Grotesk.    var face: String = "own"    static let empty = ChatLook(colours: ChatColours([:]), ground: "plain", scale: 1)    static func from(_ themes: ChatThemesAnswer?, dark: Bool) -> ChatLook {        guard let themes, let theme = themes.theme(dark: dark) else { return .empty }        let ground = themes.ground.chosen == "theme" ? theme.ground : themes.ground.chosen        let scale = themes.text.options.first { $0.id == themes.text.chosen }?.scale ?? 1        return ChatLook(colours: ChatColours(dark ? theme.dark : theme.light), ground: ground, scale: scale)    }    /// Chat's sans at `size` points, scaled by the phone's text size (the    /// largest accessibility sizes included) and the person's multiple.    func font(_ size: CGFloat, _ weight: UIFont.Weight = .regular, italic: Bool = false) -> Font {        Font(uiFont(size, weight, italic: italic))    }    func uiFont(_ size: CGFloat, _ weight: UIFont.Weight = .regular, italic: Bool = false) -> UIFont {        if face != "own" {            return WritingFonts.font(LookFaces.drawn(face, own: "sans"), size: size * scale, weight: weight, italic: italic)        }        return WarmFonts.ui(size * scale, weight: WarmFonts.wght(weight), italic: italic)    }}private struct ChatLookKey: EnvironmentKey {    static let defaultValue = ChatLook.empty}extension EnvironmentValues {    var chatLook: ChatLook {        get { self[ChatLookKey.self] }        set { self[ChatLookKey.self] = newValue }    }}#endif