// 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) import SwiftUI import 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. enum WarmTimes { static let 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. static let edge: CGFloat = 8 } /// One item of a thread, in the Warm look. struct WarmItemView: View { let item: ChatThreadItem let group: Bool /// The person's picture beside their runs in a one-to-one chat. var picture: ChatAvatar? = nil let flash: Bool let taps: ChatBubbleTaps @Environment(\.chatLook) private var look var body: some View { 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 { if let groupCall { ChatGroupCallJoinLine(text: text, line: groupCall) { taps.join(groupCall) } } else if let call { WarmCallLine(text: text, call: call) { taps.call(call) } } else if 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"). struct WarmDayView: View { let label: String let numeral: String? let words: String? @Environment(\.chatLook) private var look var body: some View { HStack(alignment: .firstTextBaseline, spacing: 10) { if let 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. struct WarmSafetyLine: View { let text: String let tap: () -> Void @Environment(\.chatLook) private var look var body: some View { 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. struct WarmCallLine: View { let text: String let call: ChatThreadCall let tap: () -> Void @Environment(\.chatLook) private var look var body: some View { 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). private struct WarmTimeColumn: 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) private var look var body: some View { 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. enum WarmMeta { #if COTTON_DEV static var on: Bool { UserDefaults.standard.bool(forKey: "chat-warm-meta") } #else static let on = false #endif } private struct WarmMetaProbe: View { let id: String let state: CGRect let time: CGRect var body: some View { 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. struct WarmTheirsRow: View { let bubble: ChatBubble let group: Bool var picture: ChatAvatar? = nil var flash = false let taps: ChatBubbleTaps @Environment(\.chatLook) private var look var body: some View { 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)) } else if 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: "), with Retry when the core offers it first. if let 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. struct WarmMineRow: View { let bubble: ChatBubble var flash = false let taps: ChatBubbleTaps @Environment(\.chatLook) private var look /// The core's quiet moment (`quietUntil`) has passed: a message still going says "Sending" from now. @State private var quietOver = false /// The state line's and the time's frames, for Chat Dev's probe (`WarmMeta`). @State private var stateFrame = CGRect.null @State private var timeFrame = CGRect.null private var state: WarmState? { WarmState.of(bubble, quietOver: quietOver) } var body: some View { 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) } if let state { WarmStateLine(bubble: bubble, state: state, taps: taps) .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { if WarmMeta.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) { if WarmMeta.on, state != nil { WarmMetaProbe(id: "chat-meta-\(bubble.message)", state: stateFrame, time: timeFrame) } } .task(id: bubble.quietUntil) { guard let until = bubble.quietUntil else { return } let wait = until / 1_000 - Date().timeIntervalSince1970 if wait > 0 { try? await Task.sleep(for: .seconds(wait)) } if !Task.isCancelled { quietOver = true } } } } extension WarmMineRow { /// 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. static func 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. struct WarmBlockShape: ViewModifier { let tail: Bool let voice: Bool @ViewBuilder func body(content: Content) -> some View { 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. struct WarmRowSpoken: ViewModifier { let bubble: ChatBubble let said: String static func 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. static func votes(_ bubble: ChatBubble) -> Bool { bubble.poll?.votable == true } @ViewBuilder func body(content: Content) -> some View { if Self.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. struct WarmMineShape: Shape { let tail: Bool func 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. enum WarmState: Equatable { case read, sending, failed case 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. static func of(_ bubble: ChatBubble, quietOver: Bool = false) -> WarmState? { if let words = bubble.state, !words.isEmpty { return .said(words, retry: bubble.actions.first == "retry") } return switch bubble.tick { case .failed: .failed case .sending: bubble.tail && (quietOver || bubble.quietUntil.map { $0 <= Date().timeIntervalSince1970 * 1_000 } ?? true) ? .sending : nil case .read: bubble.tail ? .read : nil default: nil } } } struct WarmStateLine: View { let bubble: ChatBubble let state: WarmState let taps: ChatBubbleTaps @Environment(\.chatLook) private var look var body: some View { 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 == .failed Button { 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. struct WarmBody: View { let bubble: ChatBubble let taps: ChatBubbleTaps @Environment(\.chatLook) private var look private var ink: Color { bubble.mine ? look.colours.bubbleOutText : look.colours.text } private var dim: Color { bubble.mine ? look.colours.bubbleOutDim : look.colours.dim } var body: some View { VStack(alignment: .leading, spacing: 6) { if let forwarded = bubble.forwarded { Label { Text(verbatim: forwarded) } icon: { Image(systemName: "arrowshape.turn.up.right") } .font(look.font(12.5, .regular, italic: true)) .foregroundStyle(dim) } if let quote = bubble.reply { WarmQuote(quote: quote, mine: bubble.mine) .onTapGesture { if let id = quote.message { taps.quote(id) } } } content if let edited = bubble.edited, bubble.kind != .deleted { Text(verbatim: edited).font(look.font(12)).foregroundStyle(dim) } } } @ViewBuilder private var content: some View { switch bubble.kind { case .photo, .video, .album: ChatAlbumView(bubble: bubble, taps: taps) .frame(maxWidth: 240, alignment: .leading) .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous)) if let 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: if let 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: if let link = bubble.link { ChatLinkView(link: link, mine: bubble.mine) .frame(maxWidth: 260) .onTapGesture { if let url = URL(string: link.url) { taps.link(url) } } } words(bubble.text ?? "") } } private func words(_ text: String) -> some View { 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). struct WarmQuote: View { let quote: ChatQuote let mine: Bool @Environment(\.chatLook) private var look var body: some View { HStack(spacing: 8) { if let 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. @MainActor enum WarmSpoken { static func of(_ bubble: ChatBubble, group: Bool, sending: Bool = false) -> String { var parts: [String] = [] if bubble.mine { parts.append(ShellLines.line("chat", "app.thread.you")) } else if let agent = bubble.agent { parts += [bubble.sender?.name ?? agent.name, agent.line] } else if let sender = bubble.sender { parts.append(sender.name) } // Who wrote what it quotes, then the words, as the quote shows them. if let quote = bubble.reply { let quoted = [quote.sender, quote.line].filter { !$0.isEmpty }.joined(separator: ", ") parts.append(ShellLines.line("chat", "replying", ["words": quoted])) } if let 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 { if let 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. if let words = first.transcript, !words.isEmpty { parts.append(words) } } // A poll is read whole, in the core's one line. if let poll = bubble.poll { parts.append(poll.spoken) } if let text = bubble.text, !text.isEmpty { parts.append(text) } // Its link's card, as it stands under the words: the site and the title. if let link = bubble.link { parts += [link.site, link.title].compactMap { $0 }.filter { !$0.isEmpty } } if let 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) if let 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