// A chat's words in a speech bubble: a coding session's last line while it // waits on the owner (`SessionFace`). import CottonPaper import SwiftUI /// Words in a sage speech bubble, its tail at the lower left. public struct SpeechBubble: View { let words: String let size: CGFloat var mine = false var lines = 4 @Environment(\.theme) private var theme public init(words: String, size: CGFloat, mine: Bool = false, lines: Int = 4) { self.words = words self.size = size self.mine = mine self.lines = lines } public var body: some View { Text(words) .font(CottonFonts.type(size)) .lineLimit(lines) .faceInk(.ink) .fixedSize(horizontal: false, vertical: true) .padding(.horizontal, 14) .padding(.vertical, 12) .background( PaperSurface(dye: mine ? .cream : .sage, edge: .cut, seed: "bubble-\(words.count)") .saturation(mine ? 1 : 1.4) .clipShape(ChatSlipShape(tailLeading: !mine)) .padding(mine ? .trailing : .leading, -ChatSlipShape.tail) .shadow(color: theme.colors.shadow.color.opacity(0.14), radius: 1.2, x: 0, y: 1) ) } } /// A rounded slip with a small tail at its lower outer corner; the tail /// stands in the rect's first `tail` points on its side. struct ChatSlipShape: Shape { static let tail: CGFloat = 11 var tailLeading = true func path(in rect: CGRect) -> Path { let tail = Self.tail let body = CGRect(x: rect.minX + (tailLeading ? tail : 0), y: rect.minY, width: rect.width - tail, height: rect.height) let slip = Path(roundedRect: body, cornerRadius: 10, style: .continuous) let x = tailLeading ? body.minX : body.maxX let dir: CGFloat = tailLeading ? -1 : 1 // A curled tail, flaring out along the slip's foot. Joined as a // union: added as a second subpath, the overlap would wind to a hole. var curl = Path() curl.move(to: CGPoint(x: x - dir * 2, y: body.maxY - 18)) curl.addQuadCurve(to: CGPoint(x: x + dir * tail, y: body.maxY), control: CGPoint(x: x + dir * 1, y: body.maxY - 3)) curl.addLine(to: CGPoint(x: x - dir * 16, y: body.maxY)) curl.addLine(to: CGPoint(x: x - dir * 16, y: body.maxY - 18)) curl.closeSubpath() return slip.union(curl) } }