// A WhatsApp poll in Comms, drawn as a poll (FEEDBACK-3 "polls shown";// CONTRACT.md "Polls shown", the core's bubble.poll, 9dea975162; Android// e69a6d5152). In the bubble: the question in the body's weight, how many may// be chosen, each option with its count at the end and a thin bar of its part// of the people who voted (the accent on theirs, his own ink on his), his own// choice ticked, then the total. The whole card is read as one line (the// core's `spoken`) and a tap opens Votes: each option, its count and who chose// it. A vote arriving changes the row and the open chat is answered again, so// the bars move, and the Votes panel, read from the page as it stands, follows.// Where he can vote (`poll.votable`, CONTRACT.md "A vote"), each option is its// own control, a round box beside it ticked on his choice: a tap sends// `chat.act vote` and the core works out his choice; the total opens Votes.#if canImport(UIKit)importSwiftUIstructChatPollView: View {let poll: ChatPolllet message: Stringlet mine: Bool/// Where he can vote (`poll.votable`): a tap on option n votes (`chat.act vote`), and the total opens Votes./// Without it, a tap anywhere on the card opens Votes.var vote: ((Int) -> Void)? = nilvar votes: () -> Void = {} @Environment(\.chatLook) privatevar look/// At the accessibility sizes an option's count stands under its words, which wrap whole, never cut. @Environment(\.dynamicTypeSize) privatevar sizeprivatevar large: Bool { size.isAccessibilitySize }/// The round box he votes with, growing with the text (Android's 20 dp at the default size). @ScaledMetric(relativeTo: .body) privatevar box: CGFloat = 20privatevar ink: Color { mine ? look.colours.bubbleOutText : look.colours.text }privatevar quiet: Color { mine ? look.colours.bubbleOutDim : look.colours.dim }privatevar track: Color { mine ? look.colours.bubbleOutText.opacity(0.16) : look.colours.rule }privatevar bar: Color { mine ? look.colours.bubbleOutText.opacity(0.7) : look.colours.accent }var body: someView {if vote != nil { card } else { card .contentShape(Rectangle()) .onTapGesture(perform: votes) .accessibilityAddTraits(.isButton) } }privatevar card: someView { VStack(alignment: .leading, spacing: 10) { VStack(alignment: .leading, spacing: 2) {Text(verbatim: poll.question) .font(look.font(17, .semibold)) .foregroundStyle(ink) .fixedSize(horizontal: false, vertical: true)iflet choose = poll.choose {Text(verbatim: choose).font(look.font(12.5)).foregroundStyle(quiet) } }ForEach(Array(poll.options.enumerated()), id: \.offset) { index, option iniflet vote {// Where he votes, each option is its own control: a round box beside it, filled with a tick on his// own choice (Android's PollView).Button { vote(index) } label: { row(option, picking: true) } .buttonStyle(.plain) .accessibilityElement(children: .ignore) .accessibilityLabel(Text(verbatim: "\(option.text), \(option.said)")) .accessibilityAddTraits(option.mine ? [.isButton, .isSelected] : .isButton) .accessibilityIdentifier("chat-poll-\(message)-option-\(index)") } else { row(option, picking: false) } }if vote != nil {Button(action: votes) { total } .buttonStyle(.plain) .accessibilityLabel(Text(verbatim: poll.said)) .accessibilityIdentifier("chat-poll-\(message)-said") } else { total } } .frame(minWidth: 220, maxWidth: large ? .infinity : 280, alignment: .leading) .padding(.top, 2) .padding(.bottom, 4) }privatevar total: someView {Text(verbatim: poll.said).font(look.font(12.5)).foregroundStyle(quiet) .frame(maxWidth: .infinity, minHeight: vote != nil ? 28 : nil, alignment: .leading) .contentShape(Rectangle()) }privatefunc row(_ option: ChatPollOption, picking: Bool) -> someView { VStack(alignment: .leading, spacing: 5) { HStack(alignment: picking ? .center : .firstTextBaseline, spacing: picking ? 8 : 6) {if picking { ZStack {Circle().fill(option.mine ? bar : track)if option.mine {Image(systemName: "checkmark") .font(.system(size: min(box, 36) * 0.55 * look.scale, weight: .bold)) .foregroundStyle(mine ? look.colours.bubbleOut : look.colours.onAccent) } } .frame(width: min(box, 36) * look.scale, height: min(box, 36) * look.scale) } elseif option.mine {Image(systemName: "checkmark") .font(.system(size: 13 * look.scale, weight: .bold)) .foregroundStyle(ink) }Text(verbatim: option.text) .font(look.font(15)) .foregroundStyle(ink) .lineLimit(large ? nil : 3) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading)if !large { count(option) } }if large { count(option) }ChatPollBar(percent: option.percent, track: track, bar: bar) } .contentShape(Rectangle()) }}extensionChatPollView {fileprivatefunc count(_ option: ChatPollOption) -> someView {Text(verbatim: option.said) .font(look.font(12.5)) .foregroundStyle(quiet) .lineLimit(1) .layoutPriority(1) }}/// An option's part of the people who voted: a thin track, filled from the start.structChatPollBar: View {let percent: Intlet track: Colorlet bar: Colorvar body: someView {Capsule() .fill(track) .frame(height: 4) .overlay(alignment: .leading) {GeometryReader { space inif percent > 0 {Capsule().fill(bar).frame(width: space.size.width * CGFloat(min(percent, 100)) / 100) } } } .accessibilityHidden(true) }}/// The poll a tap opened, by its message: the Votes panel reads the poll from the page as it stands now.structChatPollOpen: Identifiable, Equatable {let message: Stringvar id: String { message }}/// A poll's Votes: each option, how many chose it, and who, as the chat names them.structChatPollVotesSheet: View {let poll: ChatPoll @Environment(\.chatLook) privatevar lookvar body: someView {// Whole when it fits; scrolled inside the panel when it does not (the largest text, a long poll).ViewThatFits(in: .vertical) { votesScrollView { votes } } .accessibilityElement(children: .contain) .accessibilityIdentifier("chat-poll-votes") }privatevar votes: someView { VStack(alignment: .leading, spacing: 4) {Text(verbatim: poll.votesTitle) .font(look.font(20, .bold)).foregroundStyle(look.colours.text) .accessibilityAddTraits(.isHeader)Text(verbatim: poll.question) .font(look.font(16, .semibold)).foregroundStyle(look.colours.text) .fixedSize(horizontal: false, vertical: true) .padding(.bottom, 6)ForEach(Array(poll.options.enumerated()), id: \.offset) { index, option in VStack(alignment: .leading, spacing: 4) { HStack(alignment: .firstTextBaseline) {Text(verbatim: option.text).font(look.font(16, .medium)).foregroundStyle(look.colours.text) .fixedSize(horizontal: false, vertical: true)Spacer(minLength: 12)Text(verbatim: option.said).font(look.font(13)).foregroundStyle(look.colours.dim) }ForEach(option.voters, id: \.self) { who inText(verbatim: who).font(look.font(15)).foregroundStyle(look.colours.dim) } } .padding(.vertical, 8) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityElement(children: .combine) .accessibilityIdentifier("chat-poll-votes-\(index)") } } .padding(.horizontal, Warm.side) .padding(.vertical, 18) }}#endif