// 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.importSwiftUIstructChoiceChips: View { @Environment(\.theme) privatevar themelet words: [String]let chosen: Set<String>/// The accessibility identifier's stem: "<stem>" for the chips, and/// "<stem>-<word>" for each chip.let identifier: Stringlet 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: someView {ChipWrap(spacing: ChoiceChipsKnobs.faceChoiceChipWrapGap) { chips } .padding(.vertical, ChoiceChipsKnobs.faceChoiceChipWrapVerticalPadding) .accessibilityIdentifier(identifier) }privatevar chips: someView {ForEach(words, id: \.self) { word inChoiceChip(word: word, chosen: chosen.contains(word)) { onTap(word) } .accessibilityIdentifier("\(identifier)-\(word)") } }}/// One word on a small cream paper.structChoiceChip: View { @Environment(\.theme) privatevar themelet word: Stringlet chosen: Boollet action: () -> Void @Environment(\.cottonHaptics) privatevar hapticsvar body: someView {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`).enumChoiceChipsKnobs {staticlet faceChoiceChipLift: CGFloat = -2staticlet faceChoiceChipOverlap: CGFloat = -4staticlet faceChoiceChipRingWidth: CGFloat = 1.3staticlet faceChoiceChipSelectedHorizontalPadding: CGFloat = 12staticlet faceChoiceChipSelectedVerticalPadding: CGFloat = 8staticlet faceChoiceChipWrapGap: CGFloat = 10staticlet faceChoiceChipWrapVerticalPadding: CGFloat = 4}