// 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)importCottonWritingimportSwiftUIimport UIKit/// The colours of one appearance of one theme, by role.structChatColours: 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 {iflet hex = tokens["chat.\(role)"] ?? tokens[role], writingRGBA(hex) != nil { returnWritingTheme.ui(hex) }if role != "text" { return ui("text") }returnWritingTheme.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.structChatLook: Equatable {var colours: ChatColoursvar ground: Stringvar scale: Double/// Look's face (shared/DECISIONS.md "Look"): `own` is Hanken Grotesk.var face: String = "own"staticlet empty = ChatLook(colours: ChatColours([:]), ground: "plain", scale: 1)staticfunc from(_ themes: ChatThemesAnswer?, dark: Bool) -> ChatLook {guardlet themes, let theme = themes.theme(dark: dark) else { return .empty }let ground = themes.ground.chosen == "theme" ? theme.ground : themes.ground.chosenlet scale = themes.text.options.first { $0.id == themes.text.chosen }?.scale ?? 1returnChatLook(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" {returnWritingFonts.font(LookFaces.drawn(face, own: "sans"), size: size * scale, weight: weight, italic: italic) }returnWarmFonts.ui(size * scale, weight: WarmFonts.wght(weight), italic: italic) }}privatestructChatLookKey: EnvironmentKey {staticlet defaultValue = ChatLook.empty}extensionEnvironmentValues {var chatLook: ChatLook { get { self[ChatLookKey.self] } set { self[ChatLookKey.self] = newValue } }}#endif