Cotton code
240 lines · 9.1 KB · SwiftRaw
// COTTON CONTACTS' LOOK (PEOPLE-DESIGN.md d.2 and f; the family's grammar of Comms and Secrets): a big title, rows of// a round mark, a bold name, a quiet second line and a chevron, quiet section headings, sentence case. Its colours are// assets/design/contacts/DECISIONS.md's (Android `ContactsApp.kt` `Ink`); the screens follow assets/design/contacts/SCREENS.md.#if os(iOS)import SwiftUIimport UIKit/// Contacts' colours, light and dark by the phone's appearance.enum ContactsInk {    private static func tone(_ light: UInt32, _ dark: UInt32) -> Color {        func ui(_ v: UInt32) -> UIColor {            UIColor(red: CGFloat((v >> 16) & 0xFF) / 255, green: CGFloat((v >> 8) & 0xFF) / 255, blue: CGFloat(v & 0xFF) / 255, alpha: 1)        }        return Color(uiColor: UIColor { $0.userInterfaceStyle == .dark ? ui(dark) : ui(light) })    }    static let ground = tone(0xF4F2EE, 0x141311)    static let ink = tone(0x1D1B18, 0xEDEAE4)    static let quiet = tone(0x5F5A53, 0xA9A398)    static let faint = tone(0x9A948B, 0x6E685F)    static let rule = tone(0xDCD7CF, 0x2C2925)    /// The one accent: "act here" (Contacts' blue, assets/design/contacts/DECISIONS.md).    static let accent = tone(0x2F5D8A, 0x8DB4DC)    static let onAccent = tone(0xFFFFFF, 0x10202E)    /// A round mark's ground.    static let mark = tone(0xE6E1D8, 0x26231F)    /// A destroying act in a system sheet and the settings parts' danger ink: Contacts draws red acts in ink.    static let red = tone(0x1D1B18, 0xEDEAE4)    static let side: CGFloat = 20}/// Contacts' type: the system face, every size following the phone's text size.enum ContactsType {    static let title = Font.system(size: 30, weight: .bold).leading(.tight)    static let rowName = Font.system(.callout).weight(.medium)    static let body = Font.system(.callout)    static let small = Font.system(.footnote)    static let section = Font.system(.footnote).weight(.semibold)    static let tab = Font.system(.subheadline)}/// Contacts' words that are the shell's own (tabs, reading the address book); every screen's words come from the core.enum ContactsWords {    static func line(_ key: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("contacts", key, fills) }}/// A press that dims, as the family's rows do.struct ContactsPress: ButtonStyle {    func makeBody(configuration: Configuration) -> some View {        configuration.label.opacity(configuration.isPressed ? 0.55 : 1)    }}/// The screen's big title.struct ContactsTitle: View {    let text: String    var body: some View {        Text(verbatim: text)            .font(ContactsType.title)            .foregroundStyle(ContactsInk.ink)            .fixedSize(horizontal: false, vertical: true)            .accessibilityAddTraits(.isHeader)    }}/// A section's heading, quiet, with its count when it has one.struct ContactsHeading: View {    let text: String    var count: Int?    var body: some View {        Text(verbatim: count.map { "\(text) · \($0.formatted())" } ?? text)            .font(ContactsType.section).foregroundStyle(ContactsInk.quiet)        .frame(maxWidth: .infinity, alignment: .leading)        .padding(.top, 22)        .padding(.bottom, 4)        .accessibilityElement(children: .combine)        .accessibilityAddTraits(.isHeader)    }}/// A 1 px rule.struct ContactsRule: View {    var body: some View { Rectangle().fill(ContactsInk.rule).frame(height: 1) }}/// A person's initials in a ring; `#` (a number with no name) shows a plain person.struct ContactsRing: View {    let initials: String    var size: CGFloat = 40    @ScaledMetric(relativeTo: .body) private var grow: CGFloat = 1    var body: some View {        let size = self.size * min(grow, 1.5)        Group {            if initials.first?.isLetter == true {                Text(verbatim: String(initials.prefix(2))).font(.system(size: size * 0.38, weight: .bold))            } else {                Image(systemName: "person").font(.system(size: size * 0.42))            }        }        .foregroundStyle(ContactsInk.ink)        .frame(width: size, height: size)        .background(Circle().fill(ContactsInk.mark))        .accessibilityHidden(true)    }}/// A row: an optional ring, the name over its second line, something at the right, a rule under it unless last.struct ContactsRowView<End: View>: View {    let title: String    var detail: String?    var initials: String?    var last = false    var red = false    @ViewBuilder var end: () -> End    var body: some View {        VStack(spacing: 0) {            HStack(spacing: 12) {                if let initials { ContactsRing(initials: initials) }                VStack(alignment: .leading, spacing: 2) {                    Text(verbatim: title)                        .font(ContactsType.rowName)                        .foregroundStyle(red ? ContactsInk.red : ContactsInk.ink)                        .multilineTextAlignment(.leading)                    if let detail, !detail.isEmpty {                        Text(verbatim: detail).font(ContactsType.small).foregroundStyle(ContactsInk.quiet).multilineTextAlignment(.leading)                    }                }                .frame(maxWidth: .infinity, alignment: .leading)                end()            }            .padding(.vertical, 10)            .frame(minHeight: 44)            if !last { ContactsRule() }        }        .contentShape(Rectangle())    }}extension ContactsRowView where End == ContactsChevron {    init(title: String, detail: String? = nil, initials: String? = nil, last: Bool = false, red: Bool = false) {        self.init(title: title, detail: detail, initials: initials, last: last, red: red) { ContactsChevron() }    }}struct ContactsChevron: View {    var body: some View {        Image(systemName: "chevron.right").font(.system(size: 13, weight: .semibold)).foregroundStyle(ContactsInk.faint).accessibilityHidden(true)    }}/// The list's search field.struct ContactsSearch: View {    let prompt: String    @Binding var text: String    var body: some View {        HStack(spacing: 8) {            Image(systemName: "magnifyingglass").foregroundStyle(ContactsInk.faint).accessibilityHidden(true)            TextField("", text: $text, prompt: Text(verbatim: prompt).foregroundStyle(ContactsInk.faint))                .font(ContactsType.body)                .foregroundStyle(ContactsInk.ink)                .tint(ContactsInk.accent)                .autocorrectionDisabled()                .textInputAutocapitalization(.never)                .submitLabel(.search)                .accessibilityIdentifier("contacts-search")        }        .padding(.horizontal, 12)        .padding(.vertical, 10)        .frame(minHeight: 44)        .overlay(RoundedRectangle(cornerRadius: 10, style: .continuous).strokeBorder(ContactsInk.rule, lineWidth: 1))    }}/// A kind to narrow the list by.struct ContactsChip: View {    let label: String    let on: Bool    var id: String    let action: () -> Void    var body: some View {        Button(action: action) {            Text(verbatim: label)                .font(.system(.subheadline).weight(on ? .bold : .regular))                .foregroundStyle(on ? ContactsInk.ground : ContactsInk.ink)                .lineLimit(1)                .fixedSize()                .padding(.horizontal, 12)                .padding(.vertical, 6)                .frame(minHeight: 36)                .background(Capsule().fill(on ? ContactsInk.ink : Color.clear))                .overlay(Capsule().strokeBorder(on ? ContactsInk.ink : ContactsInk.rule, lineWidth: 1))                .contentShape(Capsule())        }        .buttonStyle(ContactsPress())        .accessibilityAddTraits(on ? .isSelected : [])        .accessibilityIdentifier(id)    }}/// A button that names its act: filled for the main one, outlined otherwise.struct ContactsButton: View {    let label: String    var filled = true    var red = false    var id: String    let action: () -> Void    var body: some View {        Button(action: action) {            Text(verbatim: label)                .font(.system(.body).weight(.semibold))                .foregroundStyle(filled ? ContactsInk.onAccent : red ? ContactsInk.red : ContactsInk.accent)                .multilineTextAlignment(.center)                .padding(.horizontal, 16)                .padding(.vertical, 10)                .frame(maxWidth: .infinity, minHeight: 46)                .background(RoundedRectangle(cornerRadius: 12, style: .continuous).fill(filled ? ContactsInk.accent : Color.clear))                .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).strokeBorder(filled ? Color.clear : (red ? ContactsInk.red : ContactsInk.accent), lineWidth: 1.5))                .contentShape(Rectangle())        }        .buttonStyle(ContactsPress())        .accessibilityIdentifier(id)    }}/// A quiet sentence.struct ContactsNote: View {    let text: String    var body: some View {        Text(verbatim: text).font(ContactsType.small).foregroundStyle(ContactsInk.quiet)            .fixedSize(horizontal: false, vertical: true)            .frame(maxWidth: .infinity, alignment: .leading)    }}#endif