import AppKit import EquipoKit enum MacType { static var name: NSFont { NSFont.systemFont(ofSize: 13, weight: .bold) } static var verb: NSFont { NSFont.systemFont(ofSize: 12) } static var time: NSFont { NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular) } static var eyebrow: NSFont { NSFont.systemFont(ofSize: 11, weight: .semibold) } static var title: NSFont { NSFont.systemFont(ofSize: 15, weight: .semibold) } static var compactTitle: NSFont { NSFont.systemFont(ofSize: 13, weight: .semibold) } static var small: NSFont { NSFont.systemFont(ofSize: 12) } static var smallStrong: NSFont { NSFont.systemFont(ofSize: 12, weight: .semibold) } static var caption: NSFont { NSFont.systemFont(ofSize: 11) } static var code: NSFont { NSFont.monospacedSystemFont(ofSize: 11, weight: .semibold) } static var line: NSFont { NSFont.systemFont(ofSize: 14) } static var thread: NSFont { NSFont.systemFont(ofSize: 13) } static var chip: NSFont { NSFont.systemFont(ofSize: 10, weight: .bold) } } enum MacMetric { static let gutter: CGFloat = 48 static let tile: CGFloat = 30 static let gap: CGFloat = 12 static let bylineGap: CGFloat = 8 static let pad: CGFloat = 16 static let radius: CGFloat = 14 /// Room around a raised card for its drawn shadow. static let shadow: CGFloat = 14 } /// A flipped view that draws with a closure. final class PaintView: NSView { override var isFlipped: Bool { true } var paint: (NSRect) -> Void init(frame: NSRect, paint: @escaping (NSRect) -> Void) { self.paint = paint; super.init(frame: frame) } required init?(coder: NSCoder) { fatalError() } override func draw(_ dirtyRect: NSRect) { paint(bounds) } } @MainActor func makeText(_ value: String, _ font: NSFont, _ color: NSColor = .webText, width: CGFloat = 10000, lineSpacing: CGFloat = 0, selectable: Bool = false, lines: Int = 0) -> NSTextField { let field = NSTextField(wrappingLabelWithString: value) let paragraph = NSMutableParagraphStyle() paragraph.lineSpacing = lineSpacing if lines == 1 { paragraph.lineBreakMode = .byTruncatingTail } field.attributedStringValue = NSAttributedString(string: value, attributes: [.font: font, .foregroundColor: color, .paragraphStyle: paragraph]) field.isSelectable = selectable field.maximumNumberOfLines = lines if lines == 1 { field.cell?.lineBreakMode = .byTruncatingTail; field.cell?.wraps = false; field.cell?.truncatesLastVisibleLine = true } let fitted = field.cell?.cellSize(forBounds: NSRect(x: 0, y: 0, width: width, height: 10000)) ?? NSSize(width: width, height: 16) field.frame.size = NSSize(width: min(width, ceil(fitted.width)), height: ceil(fitted.height)) return field } /// A card's answer buttons, as EquipoKit's ChoiceRow lays them out: side by /// side at equal widths only when every title fits its share, else one per /// line at full width, taller when a title has to wrap. Widths come from the /// titles, never the busy label, so a tap does not re-stack the row. Returns /// the bottom edge. @MainActor func placeChoices(_ buttons: [ActionButton], in view: NSView, x: CGFloat, y: CGFloat, width: CGFloat, gap: CGFloat = 10) -> CGFloat { guard !buttons.isEmpty else { return y } let font = ActionButton.titleFont let share = floor((width - gap * CGFloat(buttons.count - 1)) / CGFloat(buttons.count)) if buttons.allSatisfy({ ActionButton.idealWidth($0.title, font) <= share }) { for (index, b) in buttons.enumerated() { b.frame = NSRect(x: x + CGFloat(index) * (share + gap), y: y, width: share, height: 32) view.addSubview(b) } return y + 32 } var top = y for b in buttons { let wrapped = (b.title as NSString).boundingRect(with: NSSize(width: width - 2 * ActionButton.inset, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin], attributes: [.font: font]).height b.frame = NSRect(x: x, y: top, width: width, height: max(32, ceil(wrapped) + 14)) view.addSubview(b) top = b.frame.maxY + gap } return top - gap } func textWidth(_ value: String, _ font: NSFont) -> CGFloat { ceil((value as NSString).size(withAttributes: [.font: font]).width) } /// Centres a view vertically on a row of the given height. @MainActor func centre(_ view: NSView, x: CGFloat, row y: CGFloat, height: CGFloat) { view.frame.origin = NSPoint(x: x, y: (y + (height - view.frame.height) / 2).rounded()) } func sparklePath(in rect: NSRect) -> NSBezierPath { let s = min(rect.width, rect.height) / 24 let ox = rect.midX - 12 * s, oy = rect.midY - 12 * s func p(_ x: CGFloat, _ y: CGFloat) -> NSPoint { NSPoint(x: ox + x * s, y: oy + y * s) } let path = NSBezierPath() path.move(to: p(12, 3)) path.curve(to: p(21, 12), controlPoint1: p(13.1, 8.8), controlPoint2: p(15.2, 10.9)) path.curve(to: p(12, 21), controlPoint1: p(15.2, 13.1), controlPoint2: p(13.1, 15.2)) path.curve(to: p(3, 12), controlPoint1: p(10.9, 15.2), controlPoint2: p(8.8, 13.1)) path.curve(to: p(12, 3), controlPoint1: p(8.8, 10.9), controlPoint2: p(10.9, 8.8)) path.close() path.lineWidth = s * 1.6 path.lineJoinStyle = .round path.lineCapStyle = .round return path } /// The coordinator's bare, muted mark. Its name is only for VoiceOver. @MainActor func sparkleView(size: CGFloat = 22) -> NSView { let view = PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in NSColor.webSecondary.setStroke(); sparklePath(in: rect.insetBy(dx: 0.5, dy: 0.5)).stroke() } view.setAccessibilityElement(true); view.setAccessibilityRole(.image); view.setAccessibilityLabel(Specialist.coordinator.name) return view } /// A specialist's icon, centred in its tile by the symbol's own bounds. @MainActor func tileView(_ who: Specialist, size: CGFloat = MacMetric.tile) -> NSView { if who == .coordinator { let holder = FlippedView(frame: NSRect(x: 0, y: 0, width: size, height: size)) let mark = sparkleView(size: (size * 0.74).rounded()) mark.frame.origin = NSPoint(x: 0, y: ((size - mark.frame.height) / 2).rounded()) holder.addSubview(mark) return holder } let view = PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in let shape = NSBezierPath(roundedRect: rect.insetBy(dx: 0.5, dy: 1), xRadius: size * 0.3, yRadius: size * 0.3) NSGraphicsContext.saveGraphicsState() let shadow = NSShadow(); shadow.shadowColor = NSColor.webShadow.withAlphaComponent(0.08); shadow.shadowOffset = NSSize(width: 0, height: -1); shadow.shadowBlurRadius = 2 shadow.set() NSColor.webSurface.setFill(); shape.fill() NSGraphicsContext.restoreGraphicsState() NSColor.webLine.setStroke(); shape.lineWidth = 1; shape.stroke() if let image = symbolImage(who.symbol, size: size * 0.43, weight: .medium, color: .webText) { let r = NSRect(x: ((rect.width - image.size.width) / 2).rounded(), y: ((rect.height - 1 - image.size.height) / 2).rounded(), width: image.size.width, height: image.size.height) image.draw(in: r, from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil) } } view.setAccessibilityElement(false) return view } @MainActor func monogramView(_ name: String, size: CGFloat = 16) -> NSView { PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in NSColor.webNeutralBg.setFill() NSBezierPath(roundedRect: rect, xRadius: size * 0.28, yRadius: size * 0.28).fill() let letter = NSAttributedString(string: String(name.prefix(1)).uppercased(), attributes: [.font: NSFont.systemFont(ofSize: size * 0.62, weight: .bold), .foregroundColor: NSColor.webNeutral]) let s = letter.size() letter.draw(at: NSPoint(x: ((rect.width - s.width) / 2).rounded(), y: ((rect.height - s.height) / 2).rounded())) } } /// The byline: the author's tile in the gutter, then name, verb, project and /// time. One accessibility element, read as `RosterByline.spoken`. @MainActor func bylineView(_ byline: RosterByline, width: CGFloat) -> NSView { let view = FlippedView(frame: NSRect(x: 0, y: 0, width: width, height: MacMetric.tile)) let avatar = tileView(byline.who); avatar.frame.origin = .zero; view.addSubview(avatar) var x = MacMetric.gutter func add(_ item: NSView, gap: CGFloat = 6) { centre(item, x: x, row: 0, height: MacMetric.tile); view.addSubview(item); x += item.frame.width + gap } if byline.showsName { add(makeText(byline.who.name, MacType.name)) add(makeText(byline.verb, MacType.verb, .webSecondary)) } if let project = byline.project { if byline.showsName { add(makeText("ยท", MacType.verb, .webFaint)) } add(monogramView(project), gap: 4) add(makeText(project, MacType.verb, .webSecondary, lines: 1)) } if let time = byline.timeText { add(makeText(time, MacType.time, .webFaint)) } view.setAccessibilityElement(true); view.setAccessibilityRole(.group); view.setAccessibilityLabel(byline.spoken) return view } @MainActor func needsChipView() -> NSView { let label = NSAttributedString(string: EquipoStrings.text("chip.needs_you").uppercased(), attributes: [.font: MacType.chip, .foregroundColor: NSColor.webWarning, .kern: 0.3]) let width = ceil(label.size().width) + 16 + 15 let view = PaintView(frame: NSRect(x: 0, y: 0, width: width, height: 19)) { rect in NSColor.webWarningBg.setFill(); NSBezierPath(roundedRect: rect, xRadius: rect.height / 2, yRadius: rect.height / 2).fill() if let hand = symbolImage("hand.raised", size: 10, weight: .semibold, color: .webWarning) { hand.draw(in: NSRect(x: 8, y: ((rect.height - hand.size.height) / 2).rounded(), width: hand.size.width, height: hand.size.height), from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil) } label.draw(at: NSPoint(x: 23, y: ((rect.height - label.size().height) / 2).rounded())) } view.setAccessibilityElement(true); view.setAccessibilityLabel(EquipoStrings.text("chip.needs_you")) return view } extension StageTone { var ns: NSColor { self == .warning ? .webWarning : self == .negative ? .webNegative : .webPositive } var nsHalo: NSColor { self == .warning ? .webWarningBg : self == .negative ? .webNegativeBg : .webPositiveBg } var nsEyebrow: NSColor { self == .positive ? .webSecondary : ns } } @MainActor func button(_ title: String, _ style: ActionButton.Style = .choice, symbol: String? = nil, forced: ForcedState? = nil, busy: String? = nil, action: @escaping () -> Void = {}) -> ActionButton { let b = ActionButton(title, style: style, symbol: symbol, action: action) b.forced = forced b.busyLabel = busy let label = busy ?? title let font = NSFont.systemFont(ofSize: 13, weight: style == .link ? .regular : .medium) let extra: CGFloat = (symbol != nil ? 16 : 0) + (busy != nil ? 22 : 0) b.frame.size = NSSize(width: style == .link ? textWidth(label, font) + extra + 4 : max(64, textWidth(label, font) + 32 + extra), height: 32) return b } /// A raised card's plate: the surface, a rim and the web's two-step shadow. /// The view is larger than the card by `MacMetric.shadow` on every side. final class RaisedPlate: NSView { override var isFlipped: Bool { true } var flat: Bool var dashed = false let content = FlippedView() init(width: CGFloat, height: CGFloat, flat: Bool) { self.flat = flat let m = MacMetric.shadow super.init(frame: NSRect(x: 0, y: 0, width: width + 2 * m, height: height + 2 * m)) content.frame = NSRect(x: m, y: m, width: width, height: height) addSubview(content) } required init?(coder: NSCoder) { fatalError() } override func draw(_ dirtyRect: NSRect) { let card = bounds.insetBy(dx: MacMetric.shadow + 0.5, dy: MacMetric.shadow + 0.5) let shape = NSBezierPath(roundedRect: card, xRadius: MacMetric.radius, yRadius: MacMetric.radius) if !flat { for (alpha, blur, y) in [(0.06, 4.0, 2.0), (0.10, 14.0, 6.0)] as [(CGFloat, CGFloat, CGFloat)] { NSGraphicsContext.saveGraphicsState() let shadow = NSShadow() shadow.shadowColor = NSColor.webShadow.withAlphaComponent(NSAppearance.currentDrawing().bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? alpha * 4 : alpha) shadow.shadowBlurRadius = blur; shadow.shadowOffset = NSSize(width: 0, height: -y) shadow.set() NSColor.webSurface.setFill(); shape.fill() NSGraphicsContext.restoreGraphicsState() } } (flat ? NSColor.webSurface2 : NSColor.webSurface).setFill(); shape.fill() (flat ? NSColor.webLine : NSColor.webLightRim).setStroke(); shape.lineWidth = 1; shape.stroke() } } /// A sub-chat turn: yours on the right with "Sent", theirs with an avatar. @MainActor func threadTurnView(_ turn: ThreadTurn, width: CGFloat) -> NSView { let view = FlippedView(frame: NSRect(x: 0, y: 0, width: width, height: 0)) if turn.own { let body = makeText(turn.text, MacType.thread, .webText, width: min(width * 0.8, 440) - 24, selectable: true) let bubble = PaintView(frame: NSRect(x: width - body.frame.width - 24, y: 0, width: body.frame.width + 24, height: body.frame.height + 16)) { r in let p = NSBezierPath(roundedRect: r.insetBy(dx: 0.5, dy: 0.5), xRadius: 12, yRadius: 12) NSColor.webSoft.setFill(); p.fill(); NSColor.webLightRim.setStroke(); p.stroke() } body.frame.origin = NSPoint(x: 12, y: 8); bubble.addSubview(body); view.addSubview(bubble) let sent = makeText(EquipoStrings.text("thread.sent"), MacType.caption, .webFaint); sent.frame.origin = NSPoint(x: width - sent.frame.width, y: bubble.frame.maxY + 4); view.addSubview(sent) view.frame.size.height = sent.frame.maxY view.setAccessibilityElement(true); view.setAccessibilityLabel(EquipoStrings.text("thread.own_spoken \(turn.text)")) } else { let who = turn.who ?? .coordinator let avatar = tileView(who, size: 24); avatar.frame.origin = NSPoint(x: 0, y: 2); view.addSubview(avatar) let maxBody = min(width - 32, 460) - 24 let name = makeText(who.name, NSFont.systemFont(ofSize: 11, weight: .semibold), .webSecondary) let time = makeText(turn.timeText, MacType.time, .webFaint) let body = makeText(turn.text, MacType.thread, .webText, width: maxBody, selectable: true) let inner = max(body.frame.width, name.frame.width + 6 + time.frame.width) let bubble = PaintView(frame: NSRect(x: 32, y: 0, width: inner + 24, height: name.frame.height + 3 + body.frame.height + 14)) { r in let p = NSBezierPath(roundedRect: r.insetBy(dx: 0.5, dy: 0.5), xRadius: 12, yRadius: 12) NSColor.webSurface.setFill(); p.fill(); NSColor.webLine.setStroke(); p.stroke() } name.frame.origin = NSPoint(x: 12, y: 6); bubble.addSubview(name) time.frame.origin = NSPoint(x: name.frame.maxX + 6, y: 6); bubble.addSubview(time) body.frame.origin = NSPoint(x: 12, y: name.frame.maxY + 3); bubble.addSubview(body) view.addSubview(bubble) view.frame.size.height = bubble.frame.maxY view.setAccessibilityElement(true); view.setAccessibilityLabel("\(who.name), \(turn.timeText): \(turn.text)") } return view } @MainActor func readingView(_ who: Specialist) -> NSView { let label = makeText(EquipoStrings.text("thread.reading \(who.name)"), MacType.small, .webSecondary) let view = PaintView(frame: NSRect(x: 0, y: 0, width: label.frame.width + 28, height: max(18, label.frame.height))) { r in NSColor.webSecondary.setFill() for i in 0..<3 { NSBezierPath(ovalIn: NSRect(x: CGFloat(i) * 6, y: r.midY - 2, width: 4, height: 4)).fill() } } label.frame.origin = NSPoint(x: 28, y: 0); view.addSubview(label) view.setAccessibilityElement(true); view.setAccessibilityLabel(label.stringValue) return view } /// The card composer: a 46-point pill with a 44-point send target centred in it. final class MiniComposerView: NSView, NSTextFieldDelegate { override var isFlipped: Bool { true } let field = NSTextField() let send: ActionButton var focused: Bool { didSet { needsDisplay = true } } var onSend: ((String) -> Void)? init(width: CGFloat, placeholder: String, text: String = "", focused: Bool = false, busy: Bool = false) { self.focused = focused send = ActionButton(busy ? EquipoStrings.text("action.sending") : "", style: busy ? .primary : .miniSend, symbol: busy ? nil : "arrow.up") {} super.init(frame: NSRect(x: 0, y: 0, width: width, height: 46)) field.isBordered = false; field.drawsBackground = false; field.focusRingType = .none field.font = NSFont.systemFont(ofSize: 13); field.textColor = .webText field.placeholderAttributedString = NSAttributedString(string: placeholder, attributes: [.font: NSFont.systemFont(ofSize: 13), .foregroundColor: NSColor.webPlaceholder]) field.stringValue = text field.delegate = self field.setAccessibilityLabel(placeholder) let h = ceil(field.intrinsicContentSize.height) field.frame = NSRect(x: 16, y: ((46 - h) / 2).rounded(), width: width - 16 - 50, height: h) addSubview(field) send.hasContent = !text.trimmingCharacters(in: .whitespaces).isEmpty send.setAccessibilityLabel(busy ? EquipoStrings.text("action.sending") : EquipoStrings.text("composer.send")) send.frame = busy ? NSRect(x: width - 96, y: 5, width: 90, height: 36) : NSRect(x: width - 45, y: 1, width: 44, height: 44) send.actionBlock = { [weak self] in self?.submit() } addSubview(send) } required init?(coder: NSCoder) { fatalError() } func controlTextDidChange(_ obj: Notification) { send.hasContent = !field.stringValue.trimmingCharacters(in: .whitespaces).isEmpty focused = true } func controlTextDidEndEditing(_ obj: Notification) { focused = false } /// Return sends; the field is single-line, so there is no newline to keep. func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool { guard selector == #selector(NSResponder.insertNewline(_:)) else { return false } submit(); return true } func submit() { let value = field.stringValue.trimmingCharacters(in: .whitespacesAndNewlines) guard !value.isEmpty else { return } onSend?(value) field.stringValue = ""; send.hasContent = false } override func draw(_ dirtyRect: NSRect) { let shape = NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: 22.5, yRadius: 22.5) let on = focused || send.hasContent NSColor.webSurface.setFill(); shape.fill() (on ? NSColor.webAccent : NSColor.webBorder).setStroke(); shape.lineWidth = 1; shape.stroke() } }