// The EMAIL face (one thread; references `email.png`, `email-rich.png`, the// email cases and the house references 9/39, 10/40, 74 and 75), on the// core's places (KINDS, 0a0a099c3): an envelope — its flap folded down// across the top of the framed card to a red wax seal with a sprig. Under it// who it is from (`subtitle`) under the core's "From", a red dot when it is// unread, and the people on it as small mounts; on a dashed rule the subject// typed heavy with the day by hand at its right; the snippet a page at a// time; and on the foot's rule how many emails, under the core's own label,// and the sum the mail names (an invoice's). A thread with a picture// attached (a boarding pass) mounts it under the subject. Every word is the// core's (`DeckCard`).importCottonPaperimportCottonValuesimportSwiftUIenumEmailFaceKnobs {staticlet inset = EdgeInsets(top: 0, leading: 26, bottom: 20, trailing: 24)/// The seal picture's width as a share of the flap's.staticlet sealShare: CGFloat = 0.62/// The picture's folds: their slope, and the seal's centre in the/// picture as shares of its width and height (`face-art-seal`).staticlet foldSlope: CGFloat = 0.49staticlet sealCentre = CGPoint(x: 0.503, y: 0.617)staticlet sealAspect: CGFloat = 390.0 / 243.0/// Below the seal, to the sender.staticlet top: CGFloat = 12staticlet fromSize: CGFloat = 16staticlet subjectSize: CGFloat = 18/// The subject's lines, and the most it shrinks to keep its words whole.staticlet subjectLines = 3staticlet subjectShrink: CGFloat = 0.8staticlet bodySize: CGFloat = 13.5staticlet bodyLeading: CGFloat = 6staticlet dateHand: CGFloat = 21staticlet dateTurn: Double = -3staticlet footSize: CGFloat = 13staticlet amountSize: CGFloat = 17staticlet unread: CGFloat = 9staticlet mount: CGFloat = 28/// A mount tucked over the one before it.staticlet mountTuck: CGFloat = -12/// Between a fact and the dashed rule either side of it.staticlet ruleGap: CGFloat = 10staticlet rowGap: CGFloat = 10staticlet pictureShare: CGFloat = 0.24staticlet pictureTurn: Double = -1.5}publicstructEmailFace<Picture: View>: View {let card: DeckCard/// A picture the thread carries (a boarding pass), filling its frame.var picture: Picture? @Environment(\.theme) privatevar themepublicinit(card: DeckCard, picture: Picture?) {self.card = cardself.picture = picture }publicvar body: someView {GeometryReader { sheet in VStack(alignment: .leading, spacing: 0) {EnvelopeTop(width: sheet.size.width - 20) .padding(.horizontal, 10) .padding(.top, 10) content(sheet.size) .padding(EmailFaceKnobs.inset) } } .onCardPaper(EmailFaceCatalogue.stock.laid(card.pace)) }privatefunc content(_ size: CGSize) -> someView { VStack(alignment: .leading, spacing: 0) { sender .padding(.top, EmailFaceKnobs.top)iflet subject = card.text("title") {DashedRule() .padding(.vertical, EmailFaceKnobs.ruleGap) subjectRow(subject) }DashedRule() .padding(.top, EmailFaceKnobs.ruleGap)iflet picture {MountedPrint(photo: picture) .frame(height: size.height * EmailFaceKnobs.pictureShare) .rotationEffect(.degrees(EmailFaceKnobs.pictureTurn)) .padding(.top, EmailFaceKnobs.rowGap + 2) .facePart(.picture) }iflet snippet = card.text("body").map(EmailSnippet.plain), !snippet.isEmpty {FacePages(text: snippet, size: EmailFaceKnobs.bodySize, leading: EmailFaceKnobs.bodyLeading) .padding(.top, EmailFaceKnobs.rowGap + 2) .facePart(.body) } else {Spacer(minLength: 0) } foot } }/// Who it is from under the core's "From", the unread dot before them,/// and the people on it as small mounts at the right.privatevar sender: someView { HStack(alignment: .bottom, spacing: 8) { VStack(alignment: .leading, spacing: FacePartsKnobs.labelGap) {iflet from = card.label("source") { FaceLabel(words: from) } HStack(alignment: .center, spacing: 8) {if card.fields["unread"] == .bool(true) {Circle() .fill(theme.print(.red)) .frame(width: EmailFaceKnobs.unread, height: EmailFaceKnobs.unread) .accessibilityLabel(ShellLines.line("faces", "unread")) }iflet from = card.text("subtitle") {Text(from) .font(CottonFonts.type(EmailFaceKnobs.fromSize)) .lineLimit(1) .minimumScaleFactor(EmailFaceKnobs.subjectShrink) .faceInk(.ink) .facePart(.subtitle) } } } .layoutPriority(1)Spacer(minLength: 4) HStack(spacing: 0) {ForEach(Array(card.entries("people").enumerated()), id: \.offset) { index, entry inFaceMount(name: entry["name"]?.text ?? "", picture: entry["picture"]?.text, size: EmailFaceKnobs.mount) .padding(.leading, index == 0 ? 0 : EmailFaceKnobs.mountTuck) } } .facePart(.people)iflet more = card.text("people_more") { footWords(more) } } }/// The subject typed heavy, its words whole on its lines, and the day/// written by hand at its right.privatefunc subjectRow(_ subject: String) -> someView { HStack(alignment: .firstTextBaseline, spacing: 12) {HeavyType(words: subject, size: EmailFaceKnobs.subjectSize) .lineLimit(EmailFaceKnobs.subjectLines) .minimumScaleFactor(EmailFaceKnobs.subjectShrink) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .accessibilityIdentifier("deck-title") .facePart(.title)iflet day = card.text("date") {Text(HandRun.tail(day)) .font(CottonFonts.hand(EmailFaceKnobs.dateHand)) .lineLimit(1) .fixedSize() .faceInk(.ink) .rotationEffect(.degrees(EmailFaceKnobs.dateTurn)) .facePart(.when) } } }/// On the foot's rule: how many emails under the core's label, once/// there is more than the one on the card, and the sum the mail names. @ViewBuilder privatevar foot: someView {let count = card.text("count").flatMap { (Int($0.filter(\.isNumber)) ?? 2) > 1 ? $0 : nil }let amount = card.text("amount")if count != nil || amount != nil {DashedRule() .padding(.top, EmailFaceKnobs.ruleGap) HStack(alignment: .firstTextBaseline, spacing: 10) {iflet count {iflet label = card.label("count") { FaceLabel(words: label) } footWords(count) }Spacer(minLength: 8)iflet amount {Text(amount) .font(CottonFonts.type(EmailFaceKnobs.amountSize)) .lineLimit(1) .faceInk(.ink) .facePart(.amount) } } .padding(.top, EmailFaceKnobs.ruleGap - 2) } }privatefunc footWords(_ words: String) -> someView {Text(words) .font(CottonFonts.type(EmailFaceKnobs.footSize)) .lineLimit(1) .faceInk(.ink) }}publicenumEmailSnippet {/// The snippet without a mail's stand-ins for what it could not carry/// (`[image: Google]`, the plain part of an HTML mail), and the blank/// lines they leave.publicstaticfunc plain(_ snippet: String) -> String { snippet .replacingOccurrences(of: #"\[image:[^\]]*\]"#, with: "", options: .regularExpression) .replacingOccurrences(of: #"\n[ \t]*\n([ \t]*\n)+"#, with: "\n\n", options: .regularExpression) .trimmingCharacters(in: .whitespacesAndNewlines) }}/// The envelope's flap folded down to the wax seal: the drawn folds meet at/// the seal's centre along the picture's own folds, so the two are one line.structEnvelopeTop: View {let width: CGFloat @Environment(\.theme) privatevar themestaticfunc depth(width: CGFloat) -> CGFloat { EmailFaceKnobs.foldSlope * width / 2 }var body: someView {let depth = Self.depth(width: width)let art = width * EmailFaceKnobs.sealSharelet artHeight = art / EmailFaceKnobs.sealAspectlet below = artHeight * (1 - EmailFaceKnobs.sealCentre.y) ZStack(alignment: .topLeading) {EnvelopeFlap() .fill(theme.colors.shadow.color.opacity(0.05)) .frame(width: width, height: depth)EnvelopeFlap() .stroke(theme.print(.pencil).opacity(0.4), lineWidth: 0.9) .frame(width: width, height: depth)FaceArt(name: "face-art-seal") .frame(width: art, height: artHeight) .offset(x: width / 2 - art * EmailFaceKnobs.sealCentre.x, y: depth - artHeight * EmailFaceKnobs.sealCentre.y) } .frame(width: width, height: depth + below, alignment: .topLeading) .blendMode(theme.printBlend) }}/// An envelope's flap folded down: the top edge and the two folds meeting/// at the middle.structEnvelopeFlap: Shape {func path(in rect: CGRect) -> Path {var path = Path() path.move(to: CGPoint(x: rect.minX, y: rect.minY)) path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY)) path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))return path }}/// The email face, by the kind's name, and the cards it is reviewed in.publicenumEmailFaceCatalogue {publicstaticlet stock: FaceStock = .framedpublicstaticlet ground: FaceGround = .paperpublicstaticlet face = Face(id: "email", specimens: [Specimen("contract-note", contractNote), Specimen("offers", offers)]) { card, picture, _ inEmailFace(card: card, picture: picture) }publicstaticlet contractNote = DeckCard(face: .email, fields: ["subtitle": .text("Priya at Example Broking"),"title": .text("Your contract note for 22 Oct"),"date": .text("Thu 22 Oct"),"body": .text("Your contract note is ready. Please find the details attached. Thank you for trading with us."),"count": .text("3"),"unread": .bool(true),"amount": .text("\u{20B9}499"),"people": .list([ .object(["name": .text("Priya Example")]), .object(["name": .text("Arjun Rao")]), ]), ])publicstaticlet offers = DeckCard(face: .email, fields: ["subtitle": .text("Offers at Some Shop"),"title": .text("70 percent off today only"),"date": .text("Thu 22 Oct"),"body": .text("Save 70 percent on your favourites. Today only, shop before midnight. Discover our latest offers in store."),"count": .text("1,204"),"unread": .bool(false), ])}