Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Design/Components/ConversationRow.swift3// Description: The conversation row of every list (avatar, unread dot, sender, date, subject, preview, attachment / thread count / label dots / star / pin),4// its avatar, the inset rounded selection background and the pointer quick actions. Reflows at accessibility Dynamic Type sizes.5import SPMailCore6import SwiftUI78/// What a row shows, independent from where the conversation comes from (folder, search, unified list).9struct ConversationRowData: Equatable, Sendable {10 var title: String11 /// The address the avatar hue derives from.12 var address: String13 var subject: String14 var preview: String15 var date: Date16 var isUnread = false17 var threadCount = 118 var isFlagged = false19 /// Keyword `$pinned`.20 var isPinned = false21 /// Keyword `$snoozed` (the message waits in « En veille »).22 var isSnoozed = false23 /// Keyword `$followup` (a reminder fired on this message).24 var needsFollowUp = false25 /// The label keywords (`lbl_…`) of the message; `labels` are resolved from them by `decorated(by:)`.26 var labelKeywords: [String] = []27 /// A subtle AI triage badge (only while the triage feature is on).28 var badge: String?29 /// A semantic (« par le sens ») search hit.30 var isSemantic = false31 var hasAttachment = false32 var isAnswered = false33 /// Palette indices of the user labels (`LabelPalette`).34 var labels: [Int] = []35 /// Unified list: the address (mailbox or alias) that received the message.36 var receivedBy: String?3738 init(row: ThreadSummary, showRecipients: Bool, receivedBy: String? = nil) {39 let e = row.email40 if showRecipients {41 let names = e.to.map(\.displayName).joined(separator: ", ")42 title = names.isEmpty ? L("(aucun destinataire)") : names43 address = e.to.first?.email ?? ""44 } else {45 title = e.sender?.displayName ?? L("(expéditeur inconnu)")46 address = e.sender?.email ?? ""47 }48 subject = e.subject.isEmpty ? L("(sans objet)") : e.subject49 preview = e.preview.split(whereSeparator: \.isWhitespace).joined(separator: " ")50 date = e.date51 isUnread = row.unreadCount > 052 threadCount = row.count53 isFlagged = row.anyFlagged54 hasAttachment = e.hasAttachment55 isAnswered = e.isAnswered56 isPinned = e.keywords.contains(NextKeyword.pinned)57 isSnoozed = e.keywords.contains(NextKeyword.snoozed)58 needsFollowUp = e.keywords.contains(NextKeyword.followUp)59 labelKeywords = e.keywords.filter(MailLabel.isLabelKeyword)60 self.receivedBy = receivedBy61 }6263 var accessibilityLabel: String {64 var parts = [isUnread ? L("Non lu") : "", title, subject, date.listLabel, preview]65 if isFlagged { parts.append(L("Avec drapeau")) }66 if isPinned { parts.append(L("Épinglé")) }67 if isSnoozed { parts.append(L("En veille")) }68 if needsFollowUp { parts.append(L("Suivi à faire")) }69 if let badge { parts.append(badge) }70 if isSemantic { parts.append(L("Trouvé par le sens")) }71 if hasAttachment { parts.append(L("Avec pièce jointe")) }72 if isAnswered { parts.append(L("Répondu")) }73 if threadCount > 1 { parts.append(L("\(threadCount) messages")) }74 if let receivedBy { parts.append(L("Reçu par \(receivedBy)")) }75 return parts.filter { !$0.isEmpty }.joined(separator: ", ")76 }77}7879/// The appearance preferences a row follows.80struct RowStyle: Equatable, Sendable {81 var density: Density = .comfortable82 var previewLines = 283 var showAvatars = true84 @MainActor init(_ settings: AppSettings) { density = settings.density; previewLines = settings.previewLines; showAvatars = settings.showAvatars }85 init(density: Density = .comfortable, previewLines: Int = 2, showAvatars: Bool = true) { self.density = density; self.previewLines = previewLines; self.showAvatars = showAvatars }86}8788/// Initials on a hue derived from the address.89struct AvatarView: View {90 let name: String?91 let address: String92 var size: CGFloat = 3693 var body: some View {94 let style = AvatarStyle(name: name, address: address)95 Circle().fill(style.soft)96 .overlay(Text(style.initials).font(.system(size: size * 0.4, weight: .semibold, design: .rounded)).foregroundStyle(style.token).minimumScaleFactor(0.6))97 .frame(width: size, height: size)98 .accessibilityHidden(true)99 }100}101102/// A quick action revealed over the date while the pointer is on the row (macOS, iPadOS with a pointer).103struct RowQuickAction: Identifiable {104 let id: String105 let title: String106 let icon: String107 var destructive = false108 let run: @MainActor () -> Void109}110111struct ConversationRow: View {112 let data: ConversationRowData113 let style: RowStyle114 var quickActions: [RowQuickAction] = []115 @Environment(\.dynamicTypeSize) private var typeSize116 @State private var hovering = false117118 private var titleFont: Font { data.isUnread ? SPFont.headline : .body }119120 var body: some View {121 Group {122 if style.density.isSingleLine && !typeSize.isAccessibilitySize { single } else { stacked }123 }124 .padding(.vertical, style.density.verticalPadding)125 .frame(minHeight: style.density.minRowHeight, alignment: .center)126 .contentShape(Rectangle())127 #if os(macOS)128 .onHover { hovering = $0 }129 #endif130 .accessibilityElement(children: .ignore)131 .accessibilityLabel(Text(data.accessibilityLabel))132 }133134 // MARK: pieces135136 private var dot: some View {137 Circle().fill(data.isUnread ? AnyShapeStyle(SPColor.accent) : AnyShapeStyle(.clear)).frame(width: 8, height: 8)138 .spAnimation(SPMotion.fade, value: data.isUnread)139 }140 private var dateLabel: some View { Text(data.date.listLabel).font(SPFont.caption).foregroundStyle(SPColor.text3).lineLimit(1) }141 @ViewBuilder private var countPill: some View {142 if data.threadCount > 1 {143 Text("\(data.threadCount)").font(SPFont.caption).foregroundStyle(SPColor.text2).padding(.horizontal, 6).padding(.vertical, 1)144 .background(SPColor.surface3, in: Capsule())145 }146 }147 @ViewBuilder private var affordances: some View {148 HStack(spacing: 5) {149 if data.isAnswered { Image(systemName: "arrowshape.turn.up.left.fill").foregroundStyle(SPColor.text3) }150 if data.hasAttachment { Image(systemName: "paperclip").foregroundStyle(SPColor.text3) }151 ForEach(Array(data.labels.prefix(3).enumerated()), id: \.offset) { _, n in Circle().fill(LabelPalette.token(n)).frame(width: 7, height: 7) }152 if data.isSemantic { Image(systemName: "sparkles").foregroundStyle(SPColor.info) }153 if data.isSnoozed { Image(systemName: "clock.fill").foregroundStyle(SPColor.warn) }154 if data.needsFollowUp { Image(systemName: "bell.fill").foregroundStyle(SPColor.warn) }155 if data.isFlagged { Image(systemName: "flag.fill").foregroundStyle(SPColor.star) }156 if data.isPinned { Image(systemName: "pin.fill").foregroundStyle(SPColor.star) }157 }158 .imageScale(.small).symbolRenderingMode(.hierarchical)159 }160 @ViewBuilder private var trailingTop: some View {161 if hovering, !quickActions.isEmpty {162 HStack(spacing: 2) {163 ForEach(quickActions) { a in164 Button { a.run() } label: { Image(systemName: a.icon).frame(width: 22, height: 20) }165 .buttonStyle(.borderless).foregroundStyle(a.destructive ? SPColor.danger : SPColor.text2).help(a.title).accessibilityLabel(Text(a.title))166 }167 }168 .transition(.opacity)169 } else { dateLabel }170 }171172 // MARK: layouts173174 /// Comfortable and cozy: two or three lines beside the avatar.175 private var stacked: some View {176 HStack(alignment: .top, spacing: SPSpacing.m) {177 dot.padding(.top, style.showAvatars && !typeSize.isAccessibilitySize ? style.density.avatarSize / 2 - 4 : 6)178 if style.showAvatars, !typeSize.isAccessibilitySize { AvatarView(name: data.title, address: data.address, size: style.density.avatarSize) }179 VStack(alignment: .leading, spacing: 2) {180 if typeSize.isAccessibilitySize {181 // accessibility sizes: nothing side by side that would truncate — the date and the affordances get their own line182 Text(data.title).font(titleFont).foregroundStyle(SPColor.text).lineLimit(2)183 HStack(spacing: SPSpacing.s) { dateLabel; countPill; affordances }184 Text(data.subject).font(.body).foregroundStyle(SPColor.text).lineLimit(3)185 } else {186 HStack(alignment: .firstTextBaseline, spacing: SPSpacing.s) {187 Text(data.title).font(titleFont).foregroundStyle(SPColor.text).lineLimit(1)188 countPill189 Spacer(minLength: 4)190 trailingTop191 }192 HStack(alignment: .firstTextBaseline, spacing: SPSpacing.s) {193 Text(data.subject).font(SPFont.callout).fontWeight(data.isUnread ? .medium : .regular).foregroundStyle(data.isUnread ? SPColor.text : SPColor.text2).lineLimit(1)194 Spacer(minLength: 2)195 affordances196 }197 }198 if style.previewLines > 0, !data.preview.isEmpty {199 Text(data.preview).font(SPFont.callout).foregroundStyle(SPColor.text3).lineLimit(typeSize.isAccessibilitySize ? max(2, style.previewLines) : style.previewLines)200 }201 if data.receivedBy != nil || data.badge != nil {202 HStack(spacing: SPSpacing.s) {203 if let chip = data.receivedBy { AddressChip(address: chip) }204 if let badge = data.badge { Text(badge).font(.caption2.weight(.medium)).foregroundStyle(SPColor.info).padding(.horizontal, 6).padding(.vertical, 1.5).background(SPColor.infoSoft, in: Capsule()) }205 }206 .padding(.top, 2)207 }208 }209 }210 }211212 /// Compact: one line — sender, subject and preview, date. No avatar: the line belongs to the text.213 private var single: some View {214 HStack(alignment: .firstTextBaseline, spacing: SPSpacing.m) {215 dot.alignmentGuide(.firstTextBaseline) { $0[.bottom] - 2 }216 Text(data.title).font(data.isUnread ? SPFont.callout.weight(.semibold) : SPFont.callout).foregroundStyle(SPColor.text).lineLimit(1)217 .frame(width: 104, alignment: .leading)218 countPill.fixedSize()219 (Text(data.subject).foregroundStyle(data.isUnread ? SPColor.text : SPColor.text2).fontWeight(data.isUnread ? .medium : .regular)220 + Text(verbatim: style.previewLines > 0 && !data.preview.isEmpty ? " — \(data.preview)" : "").foregroundStyle(SPColor.text3))221 .font(SPFont.callout).lineLimit(1).frame(maxWidth: .infinity, alignment: .leading)222 if let chip = data.receivedBy { AddressChip(address: chip).fixedSize() }223 affordances.fixedSize()224 trailingTop.fixedSize()225 }226 }227}228229/// Selected rows are inset, rounded 10, filled with `accent-soft` — not full-bleed bars.230struct RowSelectionBackground: View {231 let isSelected: Bool232 var body: some View {233 ZStack {234 // opaque: on macOS the row background sits above the table's own full-bleed selection highlight, which must not show through235 SPColor.bg236 RoundedRectangle(cornerRadius: SPRadius.card, style: .continuous)237 .fill(isSelected ? AnyShapeStyle(SPColor.accentSoft) : AnyShapeStyle(.clear))238 .padding(.horizontal, 6).padding(.vertical, 1)239 }240 }241}242243/// A small coloured capsule naming the address (mailbox or alias) that received a message. The colour is stable per address.244struct AddressChip: View {245 let address: String246 var body: some View {247 let n = LabelPalette.index(for: address)248 Text(address.split(separator: "@").first.map(String.init) ?? address)249 .font(.caption2.weight(.medium)).lineLimit(1)250 .padding(.horizontal, 6).padding(.vertical, 1.5)251 .background(LabelPalette.soft(n), in: Capsule()).foregroundStyle(LabelPalette.token(n))252 .help(address)253 .accessibilityLabel(Text("Reçu par \(address)"))254 }255}256257/// A user label as a chip (reader header, future label pickers).258struct LabelChip: View {259 let name: String260 let paletteIndex: Int261 var body: some View {262 HStack(spacing: 4) { Circle().fill(LabelPalette.token(paletteIndex)).frame(width: 7, height: 7); Text(name).lineLimit(1) }263 .font(SPFont.caption).padding(.horizontal, 7).padding(.vertical, 2)264 .background(LabelPalette.soft(paletteIndex), in: Capsule()).foregroundStyle(LabelPalette.token(paletteIndex))265 }266}267