// A CONVERSATION in the Warm look (warm.html "A conversation", "A group"):// their words are plain text beside their photo, shown once per run, with the// name above the run in a group, each person a quiet colour of their own; the// person's own words sit in one warm block at the right. The day between days// is a large numeral with its words. Times are hidden until the conversation// is pulled sideways (`WarmTimes`). Replies quote,// photos carry their caption, voice messages, files, reactions, edited and// deleted states, and a failed message says so and retries on a tap.#if os(iOS)importSwiftUIimport UIKit/// The times' column: the page is this much wider than the screen, the/// times standing in the extra width at its right, and a pull to the left/// slides the page over to show them.enumWarmTimes {staticlet width: CGFloat = 56/// Between the time and the page's right edge; with the row's own gap,/// a message's right edge stands `Warm.side` in from the screen's.staticlet edge: CGFloat = 8}/// One item of a thread, in the Warm look.structWarmItemView: View {let item: ChatThreadItemlet group: Bool/// The person's picture beside their runs in a one-to-one chat.var picture: ChatAvatar? = nillet flash: Boollet taps: ChatBubbleTaps @Environment(\.chatLook) privatevar lookvar body: someView {switch item {case .day(let label, let numeral, let words):WarmDayView(label: label, numeral: numeral, words: words)case .unread(let label): HStack(spacing: 10) {Rectangle().fill(look.colours.accent.opacity(0.45)).frame(height: 1)Text(verbatim: label) .font(look.font(13, .semibold)) .foregroundStyle(look.colours.accent) .layoutPriority(1)Rectangle().fill(look.colours.accent.opacity(0.45)).frame(height: 1) } .padding(.leading, Warm.side) .padding(.trailing, Warm.side + WarmTimes.width) .padding(.vertical, 10) .accessibilityElement(children: .combine) .accessibilityIdentifier("chat-unread-divider")case .event(_, let text, let call, let opens, let groupCall):Group {iflet groupCall {ChatGroupCallJoinLine(text: text, line: groupCall) { taps.join(groupCall) } } elseiflet call {WarmCallLine(text: text, call: call) { taps.call(call) } } elseif opens == "safety" {WarmSafetyLine(text: text, tap: taps.safety) } else {Text(verbatim: text) .font(look.font(13.5)) .foregroundStyle(look.colours.dim) .multilineTextAlignment(.center) .frame(maxWidth: .infinity) } } .padding(.leading, Warm.side + 12) .padding(.trailing, Warm.side + 12 + WarmTimes.width) .padding(.vertical, 8)case .bubble(let bubble):if bubble.mine {WarmMineRow(bubble: bubble, flash: flash, taps: taps) } else {WarmTheirsRow(bubble: bubble, group: group, picture: picture, flash: flash, taps: taps) } } }}/// The day as a large numeral with its words beside it ("20 Friday · November").structWarmDayView: View {let label: Stringlet numeral: String?let words: String? @Environment(\.chatLook) privatevar lookvar body: someView { HStack(alignment: .firstTextBaseline, spacing: 10) {iflet numeral {Text(verbatim: numeral) .font(look.font(30, .bold)) .foregroundStyle(look.colours.text) .kerning(-0.6)Text(verbatim: words ?? label) .font(look.font(13)) .foregroundStyle(look.colours.dim) } else {Text(verbatim: label) .font(look.font(15, .semibold)) .foregroundStyle(look.colours.dim) }Spacer(minLength: 0) } .padding(.horizontal, Warm.side) .padding(.top, 18) .padding(.bottom, 8) .accessibilityElement(children: .ignore) .accessibilityLabel(Text(verbatim: label)) .accessibilityAddTraits(.isHeader) }}/// A safety number's line (CONTRACT.md "Safety number"): drawn as any event line, quiet; a tap opens the safety screen.structWarmSafetyLine: View {let text: Stringlet tap: () -> Void @Environment(\.chatLook) privatevar lookvar body: someView {Button(action: tap) {Text(verbatim: text) .font(look.font(13.5)) .foregroundStyle(look.colours.dim) .multilineTextAlignment(.center) .frame(maxWidth: .infinity) .frame(minHeight: 32) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityHint(Text(verbatim: ChatWords.line("safety.title"))) .accessibilityIdentifier("chat-safety-line") }}/// A call's line in the thread: a quiet centred line, missed in the danger ink; a tap calls back.structWarmCallLine: View {let text: Stringlet call: ChatThreadCalllet tap: () -> Void @Environment(\.chatLook) privatevar lookvar body: someView {Button(action: tap) { HStack(spacing: 6) {Image(systemName: call.video ? "video" : "phone").font(.system(size: 12 * look.scale, weight: .medium))Text(verbatim: text).font(look.font(13.5, .medium))Text(verbatim: call.time).font(look.font(12.5)).foregroundStyle(look.colours.faint) } .foregroundStyle(call.missed ? look.colours.danger : look.colours.dim) .frame(maxWidth: .infinity) .frame(minHeight: 32) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityElement(children: .combine) .accessibilityHint(Text(verbatim: call.back)) .accessibilityIdentifier("chat-call-line") }}/// The time at the right edge, slid in by the sideways pull. It stays in/// its column, past the screen's edge until pulled: at a large text size it/// shrinks, to half, and wraps onto a second line ("5:30" over "PM") rather/// than spill over the screen's edge onto a message's Read or Sending (owner,/// Comms build 19, the largest text).privatestructWarmTimeColumn: View {let time: String/// Where the time is drawn, in the window (Chat Dev's meta probe, `WarmMeta`).var measured: ((CGRect) -> Void)? = nil @Environment(\.chatLook) privatevar lookvar body: someView {Text(verbatim: time) .font(look.font(11.5)) .monospacedDigit() .foregroundStyle(look.colours.faint) .multilineTextAlignment(.trailing) .lineLimit(2) .minimumScaleFactor(0.5) .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { measured?($0) } .padding(.leading, WarmTimes.edge) .frame(width: WarmTimes.width, alignment: .trailing) .accessibilityHidden(true) }}/// Chat Dev's probe of a message's foot (`-chat-warm-meta YES`): where its/// state line and its time are drawn, in the window, as the value of an/// element a UI test can read beside the message's own (which combines its/// children): `state x y w h|time x y w h`. Off, nothing is added.enumWarmMeta { #if COTTON_DEVstaticvar on: Bool { UserDefaults.standard.bool(forKey: "chat-warm-meta") } #elsestaticlet on = false #endif}privatestructWarmMetaProbe: View {let id: Stringlet state: CGRectlet time: CGRectvar body: someView {Color.clear .frame(width: 4, height: 4) .accessibilityElement() .accessibilityLabel(Text(verbatim: id)) .accessibilityValue(Text(verbatim: [("state", state), ("time", time)].map { name, f in"\(name) \(f.minX) \(f.minY) \(f.width) \(f.height)" }.joined(separator: "|"))) .accessibilityIdentifier(id) }}/// Their words: plain text, their photo beside the first line of their words/// (once, however long they paused: the core's `face`), the name above it in a group.structWarmTheirsRow: View {let bubble: ChatBubblelet group: Boolvar picture: ChatAvatar? = nilvar flash = falselet taps: ChatBubbleTaps @Environment(\.chatLook) privatevar lookvar body: someView { HStack(alignment: .top, spacing: 10) {Group {if bubble.face, let agent = bubble.agent {ChatAgentMark(name: agent.name, side: Warm.runPicture * min(look.scale, 1.3)) } elseif bubble.face, let face = bubble.sender?.picture ?? picture {ChatAvatarView(avatar: face, side: Warm.runPicture * min(look.scale, 1.3)) } else {Color.clear.frame(width: Warm.runPicture * min(look.scale, 1.3), height: 1) } } .accessibilityHidden(true) VStack(alignment: .leading, spacing: 4) {if group, bubble.face, let sender = bubble.sender {Text(verbatim: sender.name) .font(look.font(13, .semibold)) .foregroundStyle(look.colours.name(sender.tint)) .lineLimit(1) .accessibilityHidden(WarmRowSpoken.voice(bubble)) }// Whose agent wrote it ("Bimal's agent", "Your agent"), under its name.if bubble.face, let agent = bubble.agent {Text(verbatim: agent.line) .font(look.font(12)).foregroundStyle(look.colours.dim) .lineLimit(1) .accessibilityHidden(true) }WarmBody(bubble: bubble, taps: taps)if !bubble.reactions.isEmpty { ChatReactionsView(bubble: bubble, taps: taps) }// His reaction on their message on its way through the Mac: the core's words for where it stands// ("Reaction waiting to send", "Reaction not sent: <why>"), with Retry when the core offers it first.iflet words = bubble.state, !words.isEmpty {WarmStateLine(bubble: bubble, state: .said(words, retry: bubble.actions.first == "retry"), taps: taps) } } .frame(maxWidth: .infinity, alignment: .leading) .background {if flash { RoundedRectangle(cornerRadius: 10).fill(look.colours.accent.opacity(0.14)).padding(-6) } }WarmTimeColumn(time: bubble.time) } .padding(.leading, Warm.side)// The time stands past the screen's edge, in the page's extra width, until the page is pulled left. .padding(.trailing, WarmTimes.edge) .padding(.top, bubble.head ? 10 : 2) .padding(.bottom, bubble.tail ? 4 : 0) .modifier(WarmRowSpoken(bubble: bubble, said: WarmSpoken.of(bubble, group: group))) }}/// The person's own words: one warm block at the right.structWarmMineRow: View {let bubble: ChatBubblevar flash = falselet taps: ChatBubbleTaps @Environment(\.chatLook) privatevar look/// The core's quiet moment (`quietUntil`) has passed: a message still going says "Sending" from now. @State privatevar quietOver = false/// The state line's and the time's frames, for Chat Dev's probe (`WarmMeta`). @State privatevar stateFrame = CGRect.null @State privatevar timeFrame = CGRect.nullprivatevar state: WarmState? { WarmState.of(bubble, quietOver: quietOver) }var body: someView { HStack(alignment: .bottom, spacing: 10) {Spacer(minLength: 64) VStack(alignment: .trailing, spacing: 4) {WarmBody(bubble: bubble, taps: taps) .padding(Self.inset(bubble.kind)) .background(WarmMineShape(tail: bubble.tail).fill(flash ? look.colours.accent.opacity(0.3) : look.colours.bubbleOut))// A voice message's own controls keep their own frames for VoiceOver: the block is their ground, not// their shape, and nothing in it needs clipping (a picture does). .modifier(WarmBlockShape(tail: bubble.tail, voice: WarmRowSpoken.voice(bubble)))if !bubble.reactions.isEmpty { ChatReactionsView(bubble: bubble, taps: taps) }iflet state {WarmStateLine(bubble: bubble, state: state, taps: taps) .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { ifWarmMeta.on { stateFrame = $0 } } } }WarmTimeColumn(time: bubble.time, measured: WarmMeta.on ? { timeFrame = $0 } : nil) } .padding(.leading, Warm.side)// The time stands past the screen's edge, in the page's extra width, until the page is pulled left. .padding(.trailing, WarmTimes.edge) .padding(.top, bubble.head ? 10 : 2) .padding(.bottom, bubble.tail ? 4 : 0) .modifier(WarmRowSpoken(bubble: bubble, said: WarmSpoken.of(bubble, group: false, sending: state == .sending))) .overlay(alignment: .topLeading) {ifWarmMeta.on, state != nil {WarmMetaProbe(id: "chat-meta-\(bubble.message)", state: stateFrame, time: timeFrame) } } .task(id: bubble.quietUntil) {guardlet until = bubble.quietUntil else { return }let wait = until / 1_000 - Date().timeIntervalSince1970if wait > 0 { try? awaitTask.sleep(for: .seconds(wait)) }if !Task.isCancelled { quietOver = true } } }}extensionWarmMineRow {/// Inside the warm block: a picture's thin edge, a voice message's player (10 top, 12 right, 8 bottom, 10 left:/// voice-player.html), words' 14 by 10.staticfunc inset(_ kind: ChatBubbleKind) -> EdgeInsets {switch kind {case .photo, .album, .video: EdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4)case .voice: EdgeInsets(top: 10, leading: 10, bottom: 8, trailing: 12)default: EdgeInsets(top: 10, leading: 14, bottom: 10, trailing: 14) } }}/// The warm block's clip and touch shape. A voice message's block has neither: a content shape on the block hands/// its whole frame to the first control inside it (VoiceOver's ring, and its double tap, on the round would cover/// the block), nothing in the player needs clipping, and the long press is the table's own.structWarmBlockShape: ViewModifier {let tail: Boollet voice: Bool @ViewBuilder func body(content: Content) -> someView {if voice { content } else { content.clipShape(WarmMineShape(tail: tail)).contentShape(WarmMineShape(tail: tail)) } }}/// A message's row for a screen reader: one element of the bubble's words (`WarmSpoken`), as every message; a/// voice message's player stands beside it as its own controls (the round, the place, the speed), so VoiceOver/// reaches them and hears what state the note is in.structWarmRowSpoken: ViewModifier {let bubble: ChatBubblelet said: Stringstaticfunc voice(_ bubble: ChatBubble) -> Bool { bubble.kind == .voice && bubble.media.first?.kind == .voice }/// A poll he can vote on: its options are controls of their own, beside the one line the poll is read as.staticfunc votes(_ bubble: ChatBubble) -> Bool { bubble.poll?.votable == true } @ViewBuilder func body(content: Content) -> someView {ifSelf.voice(bubble) || Self.votes(bubble) { content .accessibilityElement(children: .contain) .background(alignment: .topLeading) {Color.clear .accessibilityElement() .accessibilityLabel(Text(verbatim: said)) .accessibilityIdentifier("chat-bubble-\(bubble.message)") .accessibilitySortPriority(1) } } else { content .accessibilityElement(children: .combine) .accessibilityLabel(Text(verbatim: said)) .accessibilityIdentifier("chat-bubble-\(bubble.message)") } }}/// The warm block: 20 round, the corner at the bottom right tighter on the last of a run.structWarmMineShape: Shape {let tail: Boolfunc path(in rect: CGRect) -> Path {UnevenRoundedRectangle(topLeadingRadius: 20, bottomLeadingRadius: 20, bottomTrailingRadius: tail ? 6 : 20, topTrailingRadius: 20, style: .continuous).path(in: rect) }}/// What stands under the last of the person's own messages: Read, Not sent; or the core's own words for where/// it stands (`state`), with Retry when the core offers it first.enumWarmState: Equatable {case read, sending, failedcase said(String, retry: Bool)/// "Sending" only once the core's quiet moment has passed (`quietUntil`): a fast send shows no state at all./// The core's `state` wins whenever a bubble carries it.staticfunc of(_ bubble: ChatBubble, quietOver: Bool = false) -> WarmState? {iflet words = bubble.state, !words.isEmpty { return .said(words, retry: bubble.actions.first == "retry") }returnswitch bubble.tick {case .failed: .failedcase .sending: bubble.tail && (quietOver || bubble.quietUntil.map { $0 <= Date().timeIntervalSince1970 * 1_000 } ?? true) ? .sending : nilcase .read: bubble.tail ? .read : nildefault: nil } }}structWarmStateLine: View {let bubble: ChatBubblelet state: WarmStatelet taps: ChatBubbleTaps @Environment(\.chatLook) privatevar lookvar body: someView {switch state {case .failed:Button { taps.retry(bubble) } label: { HStack(spacing: 5) {Image(systemName: "exclamationmark.circle").font(.system(size: 12 * look.scale, weight: .semibold))Text(verbatim: [ShellLines.line("chat", "state.failed"), ChatWords.line("thread.retry")].joined(separator: " · ")) } .font(look.font(12.5, .semibold)) .foregroundStyle(look.colours.danger) .frame(minHeight: 28) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(Text(verbatim: [ShellLines.line("chat", "state.failed"), ChatWords.line("thread.retry")].joined(separator: ", "))) .accessibilityIdentifier("chat-retry-\(bubble.message)")case .said(let words, let retry) where retry:// The core's words, then Retry: a send that failed, or one going far longer than a send takes.let failed = bubble.tick == .failedButton { taps.retry(bubble) } label: { HStack(spacing: 5) {if failed { Image(systemName: "exclamationmark.circle").font(.system(size: 12 * look.scale, weight: .semibold)) }Text(verbatim: [words, ChatWords.line("thread.retry")].joined(separator: " · ")) .multilineTextAlignment(.trailing)// A long reason wraps, so Retry is never cut ("Not sent: Your Mac could not open this file. · Tr…"). .fixedSize(horizontal: false, vertical: true) } .font(look.font(12.5, failed ? .semibold : .regular)) .foregroundStyle(failed ? look.colours.danger : look.colours.faint) .frame(minHeight: 28) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(Text(verbatim: [words, ChatWords.line("thread.retry")].joined(separator: ", "))) .accessibilityIdentifier("chat-retry-\(bubble.message)")case .said(let words, _):Text(verbatim: words) .font(look.font(12)) .foregroundStyle(bubble.tick == .failed ? look.colours.danger : look.colours.faint) .multilineTextAlignment(.trailing) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("chat-state-\(bubble.message)")case .read:Text(verbatim: ShellLines.line("chat", "state.read")).font(look.font(12)).foregroundStyle(look.colours.faint)case .sending:Text(verbatim: ShellLines.line("chat", "state.sending")).font(look.font(12)).foregroundStyle(look.colours.faint) } }}/// Inside a message, theirs or the person's own: forwarded, the quote, the/// photos with their caption, a voice message, a file, a link, the words,/// edited, deleted.structWarmBody: View {let bubble: ChatBubblelet taps: ChatBubbleTaps @Environment(\.chatLook) privatevar lookprivatevar ink: Color { bubble.mine ? look.colours.bubbleOutText : look.colours.text }privatevar dim: Color { bubble.mine ? look.colours.bubbleOutDim : look.colours.dim }var body: someView { VStack(alignment: .leading, spacing: 6) {iflet forwarded = bubble.forwarded {Label { Text(verbatim: forwarded) } icon: { Image(systemName: "arrowshape.turn.up.right") } .font(look.font(12.5, .regular, italic: true)) .foregroundStyle(dim) }iflet quote = bubble.reply {WarmQuote(quote: quote, mine: bubble.mine) .onTapGesture { iflet id = quote.message { taps.quote(id) } } } contentiflet edited = bubble.edited, bubble.kind != .deleted {Text(verbatim: edited).font(look.font(12)).foregroundStyle(dim) } } } @ViewBuilder privatevar content: someView {switch bubble.kind {case .photo, .video, .album:ChatAlbumView(bubble: bubble, taps: taps) .frame(maxWidth: 240, alignment: .leading) .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))iflet text = bubble.text, !text.isEmpty { words(text).padding(.horizontal, bubble.mine ? 10 : 0).padding(.bottom, bubble.mine ? 6 : 0) }case .voice, .audio:ChatVoiceView(bubble: bubble, ink: ink, dim: dim, played: taps.voicePlayed)case .file:// A document opens in the reader (Comms 28 fixes 2: the Warm chat drew it and took no tap).ChatFileView(bubble: bubble, ink: ink, dim: dim).frame(maxWidth: 260) .contentShape(Rectangle()) .onTapGesture { taps.file(bubble, bubble.media.first?.index ?? 0) } .accessibilityAddTraits(.isButton)case .poll where bubble.poll != nil:iflet poll = bubble.poll {ChatPollView(poll: poll, message: bubble.message, mine: bubble.mine, vote: poll.votable == true ? { taps.vote(bubble, $0) } : nil) { taps.poll(bubble) } }case .deleted:// The mark beside the first line, at the words' own size (it sat small and mid-height at the largest text). HStack(alignment: .firstTextBaseline, spacing: 6) {Image(systemName: "nosign").font(look.font(14))Text(verbatim: bubble.text ?? "").font(look.font(16, .regular, italic: true)) } .foregroundStyle(dim)default:iflet link = bubble.link {ChatLinkView(link: link, mine: bubble.mine) .frame(maxWidth: 260) .onTapGesture { iflet url = URL(string: link.url) { taps.link(url) } } } words(bubble.text ?? "") } }privatefunc words(_ text: String) -> someView {Text(ChatText.attributed(text)) .font(look.font(17)) .lineSpacing(2) .foregroundStyle(ink) .fixedSize(horizontal: false, vertical: true) .tint(look.colours.link) .environment(\.openURL, OpenURLAction { url in taps.link(url)return .handled }) }}/// A reply's quote: a thin accent rule, who and what (a picture's mark).structWarmQuote: View {let quote: ChatQuotelet mine: Bool @Environment(\.chatLook) privatevar lookvar body: someView { HStack(spacing: 8) {iflet mark = quote.mark {Image(systemName: ChatMarks.symbol(mark)).font(.system(size: 14 * look.scale)).foregroundStyle(look.colours.dim) } VStack(alignment: .leading, spacing: 1) {Text(verbatim: quote.sender).font(look.font(13, .semibold)).foregroundStyle(look.colours.accent).lineLimit(1)Text(verbatim: quote.line).font(look.font(13.5)).foregroundStyle(look.colours.dim).lineLimit(2) } } .padding(.leading, 9) .overlay(alignment: .leading) { Rectangle().fill(look.colours.accent).frame(width: 2.5) } .contentShape(Rectangle()) }}/// What a screen reader says for a message: who, what, when, its state.@MainActorenumWarmSpoken {staticfunc of(_ bubble: ChatBubble, group: Bool, sending: Bool = false) -> String {var parts: [String] = []if bubble.mine { parts.append(ShellLines.line("chat", "app.thread.you")) }elseiflet agent = bubble.agent { parts += [bubble.sender?.name ?? agent.name, agent.line] }elseiflet sender = bubble.sender { parts.append(sender.name) }// Who wrote what it quotes, then the words, as the quote shows them.iflet quote = bubble.reply {let quoted = [quote.sender, quote.line].filter { !$0.isEmpty }.joined(separator: ", ") parts.append(ShellLines.line("chat", "replying", ["words": quoted])) }iflet first = bubble.media.first {let gone = first.gone == true || ChatPictureTries.shared.gone.contains(ChatPictureTries.key(first.opens(in: bubble), first.index))if first.kind == .voice { parts.append(gone ? ShellLines.line("chat", "app.voice.gone") : ChatMarks.spoken(first.kind)) } else { parts.append(gone ? ChatWords.line("thread.picture_gone") : ChatMarks.spoken(first.kind)) }// A file: its own name and size, as its chip shows them, so which file it is is heard too.if first.kind == .file, !gone { parts += [first.name, first.size].compactMap { $0 }.filter { !$0.isEmpty } }// A voice message: its length, and "not played" for theirs not played yet.if first.kind == .voice, !gone {iflet length = first.length { parts.append(length) }if !bubble.mine, first.played == false, !ChatVoicePlayer.shared.playedHere.contains(bubble.message) { parts.append(ShellLines.line("chat", "app.voice.not_played")) } }// A voice message's words, heard on its sender's phone, are said with it, as they stand under the player.iflet words = first.transcript, !words.isEmpty { parts.append(words) } }// A poll is read whole, in the core's one line.iflet poll = bubble.poll { parts.append(poll.spoken) }iflet text = bubble.text, !text.isEmpty { parts.append(text) }// Its link's card, as it stands under the words: the site and the title.iflet link = bubble.link { parts += [link.site, link.title].compactMap { $0 }.filter { !$0.isEmpty } }iflet edited = bubble.edited { parts.append(edited) }// Its reactions, as their capsules under it say them (the row is one element, so they are said with it). parts += bubble.reactions.map { "\($0.emoji) \($0.count)" } parts.append(bubble.time)iflet words = bubble.state, !words.isEmpty {// The core's words for where it stands win over the marks. parts.append(words) } else {if bubble.tick == .failed { parts.append(ShellLines.line("chat", "state.failed")) }if sending { parts.append(ShellLines.line("chat", "state.sending")) } }return parts.joined(separator: ", ") }}#endif