// A settings row whose value is a SWITCH. // // ONE component, used by the settings sheet's Connected AIs and notification // panels and the OAuth approve sheet. Four of 4h's screen families each wrote their own while they were // being built in parallel (two lanes both declaring the same public name is a // duplicate symbol, so each said so in its header and left the fold to the // integrator); this is that fold, and it is the only one. // // It lives beside `SettingsCard`/`SettingsRow` because it is a SETTINGS-GRAMMAR // primitive, not any one screen's: same geometry as `SettingsRow` — accent // glyph in a fixed 36pt icon column, label on the shared left edge, an inset // hairline that starts under the LABEL and stops at the card's inner padding — // with the trailing value replaced by the control. Bear-quiet: the accent // appears on the switch because a switch IS the thing the person set, and on // nothing else. // // A row can carry a `caption` under its label, for the one line of honesty a // toggle sometimes needs. It stays out of the label so the row still scans as // one word. // // **THE CALLBACK IS THE BINDING'S SETTER, never `.onChange`.** Seeding a // `@State` from a read IS a change, so an `.onChange` here would write the // person's setting back the first time the screen drew — a switch that "saved" // something nobody touched. A binding's setter runs on the interaction alone. import SwiftUI public struct SettingsSwitchRow: View { /// Matches `SettingsRow`'s icon column so the two share a left edge. private static let iconSlot: CGFloat = 36 private let symbol: String? private let hasIcon: Bool private let label: String private let caption: String? private let captionLines: Int private let isOn: Bool private let isDisabled: Bool private let isLast: Bool private let identifier: String? private let onChange: (Bool) -> Void @Environment(\.theme) private var theme /// - Parameters: /// - lucideIcon: the kebab-case lucide name, translated by `Glyph`. An /// unmapped name draws NO glyph rather than a plausible neighbour — but /// the row still reserves the icon column, so a card whose other rows /// have glyphs keeps one left edge. /// - captionLines: how many lines the caption may run to before it is /// clipped. Two suits a short line of honesty; a row whose caption /// states a RULE needs the whole sentence, because half a rule reads as /// a different rule. /// - identifier: the row's `testID`. The divider takes it with /// `-divider`, exactly as the phone's does. public init( lucideIcon: String? = nil, label: String, caption: String? = nil, captionLines: Int = 2, isOn: Bool, disabled: Bool = false, isLast: Bool = false, identifier: String? = nil, onChange: @escaping (Bool) -> Void ) { self.symbol = lucideIcon.flatMap(Glyph.symbol) self.hasIcon = lucideIcon != nil self.label = label self.caption = caption self.captionLines = captionLines self.isOn = isOn self.isDisabled = disabled self.isLast = isLast self.identifier = identifier self.onChange = onChange } public var body: some View { VStack(spacing: 0) { row if !isLast { Rectangle() .fill(CottonColors.hairline) .frame(height: ComposerCardKnobs.chromeHairline) .padding(.leading, ChromeKnobs.chromeSettingsMargin + (hasIcon ? Self.iconSlot : 0)) .padding(.trailing, ChromeKnobs.chromeSettingsMargin) .accessibilityIdentifier(identifier.map { "\($0)-divider" } ?? "") } } } private var row: some View { HStack(spacing: 0) { if hasIcon { Group { if let symbol { Image(systemName: symbol) .font(.system(size: 20)) .foregroundStyle(CottonColors.accent) } } .frame(width: Self.iconSlot, alignment: .leading) } VStack(alignment: .leading, spacing: 0) { // **Two lines, not one — a ceiling, not a target.** A label // that fits still draws on one line; only a label that does not // wraps instead of losing its end. `/settings#photos` read // "Keep my photos in Cott…" — a switch whose own name is cut // off, which is the one word on the row a person needs whole. // The "scans as one word" grammar this row was built for is // unchanged: nothing wraps that did not have to. Text(label).cottonType(.rowTitle, weight: .regular).lineLimit(2) if let caption { Text(caption).cottonType(.preview).lineLimit(captionLines).padding(.top, 2) } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.trailing, 12) Toggle( label, isOn: Binding( get: { isOn }, // The interaction, and only the interaction. set: { next in guard next != isOn else { return } onChange(next) } ) ) .labelsHidden() .tint(CottonColors.accent) .disabled(isDisabled) .accessibilityLabel(label) .accessibilityIdentifier(identifier ?? "") } .padding(.horizontal, ChromeKnobs.chromeSettingsMargin) .padding(.vertical, 10) .frame(minHeight: 52) .opacity(isDisabled ? DisabledOpacity.control : 1) } }