// The MACHINE face (the approved Codex designs in ~/lanes/faces/polish: // codex-14 and codex-12), on the core's places: the framed cream card, its // Mac drawn left (the model the card names, `MachineReading.art`), its name // heavy beside it over its facts typed a line each (`facts`), three brass // dials across the card (`memory_dial`, `disk_dial`, `load_dial`, each // `{used, total}`), and at the foot when it last reported, by hand. Every // word is the core's (`DeckCard`); the dials are its numbers. import CottonPaper import CottonValues import SwiftUI public struct MachineFace: View { let card: DeckCard @Environment(\.theme) private var theme public init(card: DeckCard) { self.card = card } public var body: some View { GeometryReader { sheet in let width = sheet.size.width - GaugeFacesKnobs.inset.leading - GaugeFacesKnobs.inset.trailing VStack(alignment: .leading, spacing: 0) { if let name = card.text("title") { HeavyType(words: name, size: GaugeFacesKnobs.nameSize) .lineLimit(1) .minimumScaleFactor(0.5) .accessibilityIdentifier("deck-title") .facePart(.title) } // The Mac takes the room the words leave, never more than // its share of the card's width. FaceArt(name: MachineReading.art(card)) .frame(maxWidth: width * GaugeFacesKnobs.artShare, maxHeight: .infinity) .frame(maxWidth: .infinity) .padding(.vertical, GaugeFacesKnobs.artGap) .layoutPriority(-1) facts let dials = MachineReading.dials(card) if !dials.isEmpty { HStack(alignment: .top, spacing: 0) { ForEach(dials, id: \.slot) { dial in VStack(spacing: GaugeFacesKnobs.factGap) { BrassDial(share: dial.share, wash: dial.wash) .frame(width: width * GaugeFacesKnobs.dialShare, height: width * GaugeFacesKnobs.dialShare) if let label = card.label(dial.slot) { labelled(label) } } .frame(maxWidth: .infinity) } } .padding(.top, GaugeFacesKnobs.dialGap) } if let reported = card.text("date") { FaceHairline() .padding(.top, GaugeFacesKnobs.footGap) HStack(alignment: .firstTextBaseline, spacing: GaugeFacesKnobs.factGap * 3) { if let label = card.label("date") { labelled(label) } Text(HandRun.tail(reported)) .font(CottonFonts.hand(GaugeFacesKnobs.footHand)) .lineLimit(1) .minimumScaleFactor(0.7) .faceInk(.ink) } .padding(.top, GaugeFacesKnobs.footGap / 2) .facePart(.when) } } .padding(GaugeFacesKnobs.inset) } .onCardPaper(MachineFaceCatalogue.stock.laid(card.pace)) } /// The facts a line each, a label beside its words; a label too long /// for the label column is written with its words on one line across /// the table, so no label wraps. private var facts: some View { Grid(alignment: .leading, horizontalSpacing: GaugeFacesKnobs.factGap * 2, verticalSpacing: GaugeFacesKnobs.factGap) { ForEach(Array(MachineReading.facts(card).enumerated()), id: \.offset) { _, fact in if let label = fact.label, MachineReading.spans(label) { GridRow { HStack(alignment: .firstTextBaseline, spacing: GaugeFacesKnobs.factGap * 2) { labelled(label) typed(fact.said) } .gridCellColumns(2) } } else { GridRow(alignment: .firstTextBaseline) { if let label = fact.label { labelled(label) } typed(fact.said) } } } } } private func labelled(_ label: String) -> some View { Text(label.lowercased()) .font(CottonFonts.type(GaugeFacesKnobs.tableSize)) .lineLimit(1) .fixedSize() .faceInk(.ink) } private func typed(_ words: String) -> some View { Text(words) .font(CottonFonts.type(GaugeFacesKnobs.tableSize)) .lineLimit(1) .minimumScaleFactor(0.7) .faceInk(.ink) } } /// What a machine's card says, read for its face: the facts beside its /// name, its three dials, and which Mac is drawn. enum MachineReading { struct Fact: Equatable { var label: String? let said: String } struct Dial: Equatable { let slot: String /// How far round, nought to one. let share: Double let wash: PaperInk? } /// The dials the card names, in the face's order, each with its wash: /// memory green, disk the slate of the theme's ornament, load red. static let dialSlots: [(slot: String, wash: PaperInk?)] = [("memory_dial", .green), ("disk_dial", nil), ("load_dial", .red)] static func dials(_ card: DeckCard) -> [Dial] { dialSlots.compactMap { named -> Dial? in guard case .object(let dial)? = card.fields[named.slot], case .number(let used)? = dial["used"], case .number(let total)? = dial["total"], total > 0 else { return nil } return Dial(slot: named.slot, share: min(1, max(0, used / total)), wash: named.wash) } } /// The card's facts, a label and its words each; a card from before the /// facts says its chip and its body's lines instead. static func facts(_ card: DeckCard) -> [Fact] { let listed = card.entries("facts").compactMap { entry -> Fact? in guard case .text(let said)? = entry["said"], !said.isEmpty else { return nil } if case .text(let label)? = entry["label"] { return Fact(label: label, said: said) } return Fact(label: nil, said: said) } if !listed.isEmpty { return listed } let lines = [card.text("subtitle")] + (card.text("body") ?? "").split(whereSeparator: \.isNewline).map(String.init) + [card.text("count")] return lines.compactMap { $0 }.map { Fact(label: nil, said: $0) } } /// A label too long for the label column: the type is a typewriter's, /// every letter the same width, so its letters are its width. static func spans(_ label: String) -> Bool { label.count > GaugeFacesKnobs.labelLetters } /// The Mac drawn for the machine: the first picture whose model the /// card's words name, the desk Mac when none does. static func art(_ card: DeckCard) -> String { let words = [card.text("model"), card.text("title")].compactMap { $0?.lowercased() } for (model, art) in GaugeFacesKnobs.macArt where words.contains(where: { $0.contains(model) }) { return art } return GaugeFacesKnobs.deskMac } } /// A round dial in a brass bezel: a cream face, a ring of ticks, a /// watercolour arc along its scale (stronger to the needle), and a dark /// needle on a brass hub at the share. struct BrassDial: View { let share: Double var wash: PaperInk? @Environment(\.theme) private var theme var body: some View { GeometryReader { box in let size = min(box.size.width, box.size.height) let sweep = GaugeFacesKnobs.dialSweep let shown = max(0, min(1, share)) let washInk = wash.map { theme.print($0) } ?? theme.faceStocks.ornament ZStack { Circle().fill(brass(size)) Circle().inset(by: size * GaugeFacesKnobs.bezel).fill(theme.faceStocks.printMount) Circle().inset(by: size * GaugeFacesKnobs.bezel).stroke(theme.faceStocks.shadow.opacity(0.35), lineWidth: 1) DialArc(from: 0, to: 1, sweep: sweep) .stroke(washInk.opacity(0.22), style: StrokeStyle(lineWidth: size * GaugeFacesKnobs.washBand, lineCap: .butt)) .padding(size * GaugeFacesKnobs.washInset) .blendMode(theme.printBlend) DialArc(from: 0, to: shown, sweep: sweep) .stroke(washInk.opacity(0.55), style: StrokeStyle(lineWidth: size * GaugeFacesKnobs.washBand, lineCap: .butt)) .padding(size * GaugeFacesKnobs.washInset) .blur(radius: 0.6) .blendMode(theme.printBlend) ForEach(0...GaugeFacesKnobs.dialTicks, id: \.self) { tick in let long = tick % GaugeFacesKnobs.dialMajor == 0 Rectangle() .fill(theme.print(.ink).opacity(0.75)) .frame(width: long ? 1.2 : 0.7, height: size * (long ? 0.07 : 0.04)) .offset(y: -size * GaugeFacesKnobs.tickRadius) .rotationEffect(.degrees(-sweep / 2 + sweep * Double(tick) / Double(GaugeFacesKnobs.dialTicks))) } DialNeedle(length: size * GaugeFacesKnobs.needleLength, width: size * GaugeFacesKnobs.needleWidth) .fill(theme.print(.ink)) .frame(width: size, height: size) .rotationEffect(.degrees(-sweep / 2 + sweep * shown)) .shadow(color: theme.faceStocks.shadow.opacity(0.3), radius: 0.8, x: 0.6, y: 0.8) Circle() .fill(brass(size * GaugeFacesKnobs.hub)) .overlay(Circle().stroke(theme.faceStocks.shadow.opacity(0.6), lineWidth: 0.6)) .frame(width: size * GaugeFacesKnobs.hub, height: size * GaugeFacesKnobs.hub) } .frame(width: size, height: size) .shadow(color: theme.faceStocks.shadow.opacity(0.25), radius: 2, x: 0, y: 1.5) } .accessibilityHidden(true) } /// Brass: the theme's buff burnished with its shadow, lit from the top left. private func brass(_ size: CGFloat) -> some ShapeStyle { AngularGradient( colors: [ theme.faceStocks.buff, theme.faceStocks.shadow.opacity(0.55), theme.faceStocks.buff, theme.faceStocks.shadow.opacity(0.75), theme.faceStocks.buff, ], center: .center, angle: .degrees(-45) ) } } /// An arc along a dial's scale, `from` to `to` of a `sweep` centred on up. private struct DialArc: Shape { let from: Double let to: Double let sweep: Double func path(in rect: CGRect) -> Path { var path = Path() let start = -90 - sweep / 2 + sweep * from let end = -90 - sweep / 2 + sweep * to guard end > start else { return path } path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: min(rect.width, rect.height) / 2, startAngle: .degrees(start), endAngle: .degrees(end), clockwise: false) return path } } /// A tapered needle pointing up from the centre, with a short tail. private struct DialNeedle: Shape { let length: CGFloat let width: CGFloat func path(in rect: CGRect) -> Path { let centre = CGPoint(x: rect.midX, y: rect.midY) var path = Path() path.move(to: CGPoint(x: centre.x, y: centre.y - length)) path.addLine(to: CGPoint(x: centre.x + width / 2, y: centre.y)) path.addLine(to: CGPoint(x: centre.x, y: centre.y + length * 0.18)) path.addLine(to: CGPoint(x: centre.x - width / 2, y: centre.y)) path.closeSubpath() return path } } /// The machine face, by the kind's name, and the cards it is reviewed in. public enum MachineFaceCatalogue { public static let stock: FaceStock = .framed public static let ground: FaceGround = .paper public static let face = Face(id: "machine", specimens: [Specimen("studio", studio)]) { card, _, _ in MachineFace(card: card) } public static let studio = DeckCard(face: .machine, fields: [ "title": .text("studio-mac"), "subtitle": .text("M4 Pro"), "facts": .list([ .object(["label": .text("Chip"), "said": .text("M4 Pro")]), .object(["label": .text("Memory"), "said": .text("48 GB")]), .object(["label": .text("Disk"), "said": .text("312 GB free")]), .object(["label": .text("Load"), "said": .text("1.4")]), .object(["label": .text("Coding sessions"), "said": .text("3")]), ]), "memory_dial": .object(["used": .number(22), "total": .number(48)]), "disk_dial": .object(["used": .number(614), "total": .number(926)]), "load_dial": .object(["used": .number(1.4), "total": .number(14)]), "date": .text("2 minutes ago"), ]) }