Cotton code
91 lines · 3.6 KB · SwiftRaw
// A several-choice column's words as chips: each a small cream paper with one// typed word on it; a chosen one is picked up — straw, lifted — and ringed in// red pencil, so the choice reads as a thing marked on the row rather than a// toggle (straw on cream alone is a shade a glance misses). Wrapped to the// width, over a list (`ChipRow`, the Mood screen's feelings) and on a page's// editor alike. Drawn once for every table.import SwiftUIstruct ChoiceChips: View {    @Environment(\.theme) private var theme    let words: [String]    let chosen: Set<String>    /// The accessibility identifier's stem: "<stem>" for the chips, and    /// "<stem>-<word>" for each chip.    let identifier: String    let onTap: (String) -> Void    /// Wrapped to the width, every word in sight — over a list too: one    /// row that scrolled sideways cut its last chip at the edge, read as the    /// row's end (290 walk: the mood chips, "no wrap, no scroll cue").    var body: some View {        ChipWrap(spacing: ChoiceChipsKnobs.faceChoiceChipWrapGap) { chips }            .padding(.vertical, ChoiceChipsKnobs.faceChoiceChipWrapVerticalPadding)            .accessibilityIdentifier(identifier)    }    private var chips: some View {        ForEach(words, id: \.self) { word in            ChoiceChip(word: word, chosen: chosen.contains(word)) { onTap(word) }                .accessibilityIdentifier("\(identifier)-\(word)")        }    }}/// One word on a small cream paper.struct ChoiceChip: View {    @Environment(\.theme) private var theme    let word: String    let chosen: Bool    let action: () -> Void    @Environment(\.cottonHaptics) private var haptics    var body: some View {        Button {            haptics.select()            action()        } label: {            Paper(                dye: chosen ? .straw : .cream,                edge: .cut,                seed: "choice-\(word)",                shadow: chosen ? .lifted : .resting,                inset: 0            ) {                Text(word.replacingOccurrences(of: "_", with: " "))                    .paperBody(theme.scale.bodySize, ink: chosen ? .ink : .pencil)                    .lineLimit(1)                    .fixedSize()                    .padding(.horizontal, ChoiceChipsKnobs.faceChoiceChipSelectedHorizontalPadding)                    .padding(.vertical, ChoiceChipsKnobs.faceChoiceChipSelectedVerticalPadding)            }            .overlay {                if chosen {                    PencilCircle(seed: "chosen-\(word)", theme: theme)                        .stroke(theme.print(.red), lineWidth: ChoiceChipsKnobs.faceChoiceChipRingWidth)                        .padding(ChoiceChipsKnobs.faceChoiceChipOverlap)                }            }            .offset(y: chosen ? ChoiceChipsKnobs.faceChoiceChipLift : 0)        }        .buttonStyle(MotionButtonStyle())        .accessibilityLabel(word.replacingOccurrences(of: "_", with: " "))        .accessibilityAddTraits(chosen ? [.isButton, .isSelected] : .isButton)    }}/// The sizes, counts and timings ChoiceChips draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ChoiceChipsKnobs {    static let faceChoiceChipLift: CGFloat = -2    static let faceChoiceChipOverlap: CGFloat = -4    static let faceChoiceChipRingWidth: CGFloat = 1.3    static let faceChoiceChipSelectedHorizontalPadding: CGFloat = 12    static let faceChoiceChipSelectedVerticalPadding: CGFloat = 8    static let faceChoiceChipWrapGap: CGFloat = 10    static let faceChoiceChipWrapVerticalPadding: CGFloat = 4}