169 lines · 5.9 KB · SwiftRaw
// Cotton Chat's small parts: the round picture, the ticks, the marks// before a last line, the unread badge. Colours from the theme, words from// the core, the sans Notes chose.#if os(iOS)import SwiftUIimport UIKit/// Cotton's own mark where a face would stand (the Downloads chat, as Android's CottonMark): a round of ink, three/// knots of the ground in it.struct ChatCottonMark: View { let side: CGFloat @Environment(\.chatLook) private var look var body: some View { Canvas { context, size in let r = min(size.width, size.height) / 2 context.fill(Path(ellipseIn: CGRect(x: size.width / 2 - r, y: size.height / 2 - r, width: r * 2, height: r * 2)), with: .color(look.colours.text)) let u = r / 12 * 0.92 let at = { (x: CGFloat, y: CGFloat) in CGPoint(x: size.width / 2 + (x - 12) * u, y: size.height / 2 + (y - 11) * u) } for knot in [at(12, 8), at(7.6, 14), at(16.4, 14)] { let k = 3.6 * u context.fill(Path(ellipseIn: CGRect(x: knot.x - k, y: knot.y - k, width: k * 2, height: k * 2)), with: .color(look.colours.ground)) } } .frame(width: side, height: side) .accessibilityHidden(true) .accessibilityIdentifier("cotton-mark") }}/// A round picture: the person's photo, decoded off the main thread at the/// size it is drawn, else their typed initials on the core's tint; Cotton's mark where the core gives one.struct ChatAvatarView: View { let avatar: ChatAvatar let side: CGFloat @Environment(\.chatLook) private var look @Environment(\.chatBook) private var book @State private var image: UIImage? var body: some View { if avatar.mark != nil { ChatCottonMark(side: side) } else { face } } private var face: some View { ZStack { Circle().fill(look.colours.tint(avatar.tint)) Text(verbatim: avatar.initials) .font(look.font(side * 0.34, .semibold)) .foregroundStyle(look.colours.accent) .minimumScaleFactor(0.5) .lineLimit(1) .padding(side * 0.1) if let image { Image(uiImage: image).resizable().scaledToFill().clipShape(Circle()) } else if let photo = avatar.photo, book?.photosByKey ?? false { RecordPicture(avatarKey: photo, edge: side).clipShape(Circle()) } } .frame(width: side, height: side) .accessibilityHidden(true) .task(id: avatar.photo) { guard let photo = avatar.photo, let book, !book.photosByKey, let url = await book.picture(media: photo, message: nil, index: nil) else { image = nil return } image = await ChatPictures.shared.image(url, side: side) } }}/// A message's tick: one for sent, two for delivered, two in the read ink/// for read, a clock while sending, a mark when it failed.struct ChatTickView: View { let tick: ChatTick let ink: Color let readInk: Color var size: CGFloat = 12 @Environment(\.chatLook) private var look var body: some View { Group { switch tick { case .sending: Image(systemName: "clock") case .sent: Image(systemName: "checkmark") case .delivered: doubled(ink) case .read: doubled(readInk) case .failed: Image(systemName: "exclamationmark.circle.fill").foregroundStyle(look.colours.danger) } } .font(.system(size: size * look.scale, weight: .semibold)) .foregroundStyle(ink) .accessibilityHidden(true) } private func doubled(_ colour: Color) -> some View { ZStack(alignment: .leading) { Image(systemName: "checkmark") Image(systemName: "checkmark").offset(x: size * 0.4 * look.scale) } .padding(.trailing, size * 0.4 * look.scale) .foregroundStyle(colour) }}enum ChatMarks { /// The glyph a mark is drawn with. /// A mark as a screen reader says it: the core's own word for it. static func spoken(_ mark: ChatMark) -> String { let name = mark == .album ? ChatMark.photo.rawValue : mark.rawValue return ShellLines.line("chat", "app.mark." + name) } static func symbol(_ mark: ChatMark) -> String { switch mark { case .photo: "camera.fill" case .album: "photo.on.rectangle" case .video: "video.fill" case .voice: "mic.fill" case .audio: "music.note" case .file: "doc.fill" case .contact: "person.crop.circle" case .location: "mappin.and.ellipse" case .poll: "chart.bar.fill" case .deleted: "nosign" } }}/// The unread badge: a count, or a dot when marked unread by hand; dimmed/// when the conversation is muted.struct ChatBadge: View { let count: Int let marked: Bool let muted: Bool @Environment(\.chatLook) private var look var body: some View { let fill = muted ? look.colours.badgeMuted : look.colours.badge if count > 0 { Text(verbatim: count > 999 ? "999+" : String(count)) .font(look.font(13, .semibold)) .monospacedDigit() .foregroundStyle(look.colours.badgeText) .padding(.horizontal, 6) .frame(minWidth: 22 * look.scale, minHeight: 22 * look.scale) .background(Capsule().fill(fill)) } else if marked { Circle().fill(fill).frame(width: 12 * look.scale, height: 12 * look.scale) } }}private struct ChatBookKey: EnvironmentKey { static let defaultValue: (any ChatBook)? = nil}extension EnvironmentValues { var chatBook: (any ChatBook)? { get { self[ChatBookKey.self] } set { self[ChatBookKey.self] = newValue } }}#endif