Cotton code
174 lines · 6.7 KB · SwiftRaw
// The settings grammar — the grouped cards Home itself is built from.//// A card sits on the RECESSED ground (`groundMuted`) at the settings rail// (16pt), which is the one deliberate exception to the 29pt content margin.//// THE DIVIDER DIVIDES THE WORDS: it starts at the label's left edge, past the// icon column, and stops at the card's inner padding. A full-bleed rule would// cut the icons off from their own labels.import SwiftUIpublic struct SettingsCard<Content: View>: View {    private let label: String?    private let isPadded: Bool    private let content: Content    @Environment(\.theme) private var theme    public init(label: String? = nil, padded: Bool = false, @ViewBuilder content: () -> Content) {        self.label = label        self.isPadded = padded        self.content = content()    }    public var body: some View {        VStack(alignment: .leading, spacing: 8) {            if let label {                // Flush with the card's left edge — no extra indent.                Text(label).cottonType(.sectionLabel)            }            VStack(spacing: 0) { content }                .padding(isPadded ? ChromeKnobs.chromeSettingsMargin : 0)                .frame(maxWidth: .infinity, alignment: .leading)                .background(CottonColors.groundElevated)                .clipShape(RoundedRectangle(cornerRadius: SettingsCardKnobs.chromeCardRadius, style: .continuous))        }    }}/// The icon column's width — the glyph plus its gap, so every label in a card/// shares one left edge whether its row has an icon or not.private let iconSlot: CGFloat = 36public struct SettingsRow: View {    private let symbol: String?    private let label: String    private let subtitle: String?    private let value: String?    /// The value's ink. Secondary — a figure — unless the caller says    /// otherwise; Home's Account row sets the owner's name in tertiary.    private let valueInk: TextInk    private let showsChevron: Bool    private let isDestructive: Bool    private let isDisabled: Bool    private let isLast: Bool    private let identifier: String?    private let action: (() -> Void)?    @Environment(\.theme) private var theme    public init(        symbol: String? = nil,        label: String,        subtitle: String? = nil,        value: String? = nil,        valueInk: TextInk = .secondary,        chevron: Bool = false,        destructive: Bool = false,        disabled: Bool = false,        isLast: Bool = false,        identifier: String? = nil,        action: (() -> Void)? = nil    ) {        self.symbol = symbol        self.label = label        self.subtitle = subtitle        self.value = value        self.valueInk = valueInk        self.showsChevron = chevron        self.isDestructive = destructive        self.isDisabled = disabled        self.isLast = isLast        self.identifier = identifier        self.action = action    }    public var body: some View {        VStack(spacing: 0) {            // The row's own name is GIVEN rather than applied from outside, so            // the VALUE can carry `<id>-value`. Applied only when there is one:            // setting an empty identifier would fight with a caller that still            // names the row from outside, and every existing caller does.            if let identifier {                button.accessibilityIdentifier(identifier)            } else {                button            }            if !isLast {                Rectangle()                    .fill(CottonColors.hairline)                    .frame(height: ComposerCardKnobs.chromeHairline)                    .padding(.leading, ChromeKnobs.chromeSettingsMargin + (symbol == nil ? 0 : iconSlot))                    .padding(.trailing, ChromeKnobs.chromeSettingsMargin)            }        }    }    private var button: some View {        // `TravellingPress`, not a bare `Button`: a row is the full width of        // the card, so a finger dragged ACROSS one never leaves its bounds and        // a plain button released as a tap and opened the row. The rule and        // why it takes nothing from the scroller are in `Chrome/Pressable.swift`.        TravellingPress(style: OpacityButtonStyle(), action: action) {            content        }        // Inert, and NOT a button, without a handler.        .disabled(action == nil || isDisabled)        .accessibilityLabel(value.map { "\(label), \($0)" } ?? label)    }    private var content: some View {        HStack(spacing: 0) {            if let symbol {                Image(systemName: symbol)                    .font(.system(size: 20))                    // The icon wears the ACCENT — the one interactive colour —                    // or the error tone. Never a per-row hue.                    .foregroundStyle(isDestructive ? CottonColors.statusError : CottonColors.accent)                    .frame(width: iconSlot, alignment: .leading)            }            VStack(alignment: .leading, spacing: 0) {                Text(label)                    .cottonType(.rowTitle, ink: isDestructive ? .error : .ink, weight: .regular)                    .lineLimit(1)                if let subtitle {                    Text(subtitle).cottonType(.caption).lineLimit(1)                }            }            Spacer(minLength: 8)            if let value {                Text(value)                    .cottonType(.rowTitle, ink: valueInk, weight: .regular)                    .lineLimit(1)                    // `<id>-value`, so a test can read what a row STATES rather                    // than only that the row is there — the assertion                    // `settings-database-connected-ais-value` makes.                    .accessibilityIdentifier(identifier.map { "\($0)-value" } ?? "")            }            if showsChevron {                Image(systemName: "chevron.right")                    .font(.system(size: 18))                    .foregroundStyle(CottonColors.inkTertiary)                    .padding(.leading, 6)            }        }        .padding(.horizontal, ChromeKnobs.chromeSettingsMargin)        .padding(.vertical, 10)        .frame(minHeight: 52)        .opacity(isDisabled ? DisabledOpacity.control : 1)        .contentShape(Rectangle())    }}/// The sizes, counts and timings SettingsCard draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum SettingsCardKnobs {    static let chromeCardRadius: CGFloat = 24}/// The side margin every settings-style screen and row keeps: Swift's own,/// beside its first reader — a theme sets only its tokens.enum ChromeKnobs {    static let chromeSettingsMargin: CGFloat = 16}