oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import AppKit
2import EquipoKit
3
4enum MacType {
5 static var name: NSFont { NSFont.systemFont(ofSize: 13, weight: .bold) }
6 static var verb: NSFont { NSFont.systemFont(ofSize: 12) }
7 static var time: NSFont { NSFont.monospacedDigitSystemFont(ofSize: 11, weight: .regular) }
8 static var eyebrow: NSFont { NSFont.systemFont(ofSize: 11, weight: .semibold) }
9 static var title: NSFont { NSFont.systemFont(ofSize: 15, weight: .semibold) }
10 static var compactTitle: NSFont { NSFont.systemFont(ofSize: 13, weight: .semibold) }
11 static var small: NSFont { NSFont.systemFont(ofSize: 12) }
12 static var smallStrong: NSFont { NSFont.systemFont(ofSize: 12, weight: .semibold) }
13 static var caption: NSFont { NSFont.systemFont(ofSize: 11) }
14 static var code: NSFont { NSFont.monospacedSystemFont(ofSize: 11, weight: .semibold) }
15 static var line: NSFont { NSFont.systemFont(ofSize: 14) }
16 static var thread: NSFont { NSFont.systemFont(ofSize: 13) }
17 static var chip: NSFont { NSFont.systemFont(ofSize: 10, weight: .bold) }
18}
19enum MacMetric {
20 static let gutter: CGFloat = 48
21 static let tile: CGFloat = 30
22 static let gap: CGFloat = 12
23 static let bylineGap: CGFloat = 8
24 static let pad: CGFloat = 16
25 static let radius: CGFloat = 14
26 /// Room around a raised card for its drawn shadow.
27 static let shadow: CGFloat = 14
28}
29
30/// A flipped view that draws with a closure.
31final class PaintView: NSView {
32 override var isFlipped: Bool { true }
33 var paint: (NSRect) -> Void
34 init(frame: NSRect, paint: @escaping (NSRect) -> Void) { self.paint = paint; super.init(frame: frame) }
35 required init?(coder: NSCoder) { fatalError() }
36 override func draw(_ dirtyRect: NSRect) { paint(bounds) }
37}
38@MainActor func makeText(_ value: String, _ font: NSFont, _ color: NSColor = .webText, width: CGFloat = 10000, lineSpacing: CGFloat = 0, selectable: Bool = false, lines: Int = 0) -> NSTextField {
39 let field = NSTextField(wrappingLabelWithString: value)
40 let paragraph = NSMutableParagraphStyle()
41 paragraph.lineSpacing = lineSpacing
42 if lines == 1 { paragraph.lineBreakMode = .byTruncatingTail }
43 field.attributedStringValue = NSAttributedString(string: value, attributes: [.font: font, .foregroundColor: color, .paragraphStyle: paragraph])
44 field.isSelectable = selectable
45 field.maximumNumberOfLines = lines
46 if lines == 1 { field.cell?.lineBreakMode = .byTruncatingTail; field.cell?.wraps = false; field.cell?.truncatesLastVisibleLine = true }
47 let fitted = field.cell?.cellSize(forBounds: NSRect(x: 0, y: 0, width: width, height: 10000)) ?? NSSize(width: width, height: 16)
48 field.frame.size = NSSize(width: min(width, ceil(fitted.width)), height: ceil(fitted.height))
49 return field
50}
51/// A card's answer buttons, as EquipoKit's ChoiceRow lays them out: side by
52/// side at equal widths only when every title fits its share, else one per
53/// line at full width, taller when a title has to wrap. Widths come from the
54/// titles, never the busy label, so a tap does not re-stack the row. Returns
55/// the bottom edge.
56@MainActor func placeChoices(_ buttons: [ActionButton], in view: NSView, x: CGFloat, y: CGFloat, width: CGFloat, gap: CGFloat = 10) -> CGFloat {
57 guard !buttons.isEmpty else { return y }
58 let font = ActionButton.titleFont
59 let share = floor((width - gap * CGFloat(buttons.count - 1)) / CGFloat(buttons.count))
60 if buttons.allSatisfy({ ActionButton.idealWidth($0.title, font) <= share }) {
61 for (index, b) in buttons.enumerated() {
62 b.frame = NSRect(x: x + CGFloat(index) * (share + gap), y: y, width: share, height: 32)
63 view.addSubview(b)
64 }
65 return y + 32
66 }
67 var top = y
68 for b in buttons {
69 let wrapped = (b.title as NSString).boundingRect(with: NSSize(width: width - 2 * ActionButton.inset, height: .greatestFiniteMagnitude),
70 options: [.usesLineFragmentOrigin], attributes: [.font: font]).height
71 b.frame = NSRect(x: x, y: top, width: width, height: max(32, ceil(wrapped) + 14))
72 view.addSubview(b)
73 top = b.frame.maxY + gap
74 }
75 return top - gap
76}
77func textWidth(_ value: String, _ font: NSFont) -> CGFloat {
78 ceil((value as NSString).size(withAttributes: [.font: font]).width)
79}
80/// Centres a view vertically on a row of the given height.
81@MainActor func centre(_ view: NSView, x: CGFloat, row y: CGFloat, height: CGFloat) {
82 view.frame.origin = NSPoint(x: x, y: (y + (height - view.frame.height) / 2).rounded())
83}
84func sparklePath(in rect: NSRect) -> NSBezierPath {
85 let s = min(rect.width, rect.height) / 24
86 let ox = rect.midX - 12 * s, oy = rect.midY - 12 * s
87 func p(_ x: CGFloat, _ y: CGFloat) -> NSPoint { NSPoint(x: ox + x * s, y: oy + y * s) }
88 let path = NSBezierPath()
89 path.move(to: p(12, 3))
90 path.curve(to: p(21, 12), controlPoint1: p(13.1, 8.8), controlPoint2: p(15.2, 10.9))
91 path.curve(to: p(12, 21), controlPoint1: p(15.2, 13.1), controlPoint2: p(13.1, 15.2))
92 path.curve(to: p(3, 12), controlPoint1: p(10.9, 15.2), controlPoint2: p(8.8, 13.1))
93 path.curve(to: p(12, 3), controlPoint1: p(8.8, 10.9), controlPoint2: p(10.9, 8.8))
94 path.close()
95 path.lineWidth = s * 1.6
96 path.lineJoinStyle = .round
97 path.lineCapStyle = .round
98 return path
99}
100/// The coordinator's bare, muted mark. Its name is only for VoiceOver.
101@MainActor func sparkleView(size: CGFloat = 22) -> NSView {
102 let view = PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in
103 NSColor.webSecondary.setStroke(); sparklePath(in: rect.insetBy(dx: 0.5, dy: 0.5)).stroke()
104 }
105 view.setAccessibilityElement(true); view.setAccessibilityRole(.image); view.setAccessibilityLabel(Specialist.coordinator.name)
106 return view
107}
108/// A specialist's icon, centred in its tile by the symbol's own bounds.
109@MainActor func tileView(_ who: Specialist, size: CGFloat = MacMetric.tile) -> NSView {
110 if who == .coordinator {
111 let holder = FlippedView(frame: NSRect(x: 0, y: 0, width: size, height: size))
112 let mark = sparkleView(size: (size * 0.74).rounded())
113 mark.frame.origin = NSPoint(x: 0, y: ((size - mark.frame.height) / 2).rounded())
114 holder.addSubview(mark)
115 return holder
116 }
117 let view = PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in
118 let shape = NSBezierPath(roundedRect: rect.insetBy(dx: 0.5, dy: 1), xRadius: size * 0.3, yRadius: size * 0.3)
119 NSGraphicsContext.saveGraphicsState()
120 let shadow = NSShadow(); shadow.shadowColor = NSColor.webShadow.withAlphaComponent(0.08); shadow.shadowOffset = NSSize(width: 0, height: -1); shadow.shadowBlurRadius = 2
121 shadow.set()
122 NSColor.webSurface.setFill(); shape.fill()
123 NSGraphicsContext.restoreGraphicsState()
124 NSColor.webLine.setStroke(); shape.lineWidth = 1; shape.stroke()
125 if let image = symbolImage(who.symbol, size: size * 0.43, weight: .medium, color: .webText) {
126 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)
127 image.draw(in: r, from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil)
128 }
129 }
130 view.setAccessibilityElement(false)
131 return view
132}
133@MainActor func monogramView(_ name: String, size: CGFloat = 16) -> NSView {
134 PaintView(frame: NSRect(x: 0, y: 0, width: size, height: size)) { rect in
135 NSColor.webNeutralBg.setFill()
136 NSBezierPath(roundedRect: rect, xRadius: size * 0.28, yRadius: size * 0.28).fill()
137 let letter = NSAttributedString(string: String(name.prefix(1)).uppercased(), attributes: [.font: NSFont.systemFont(ofSize: size * 0.62, weight: .bold), .foregroundColor: NSColor.webNeutral])
138 let s = letter.size()
139 letter.draw(at: NSPoint(x: ((rect.width - s.width) / 2).rounded(), y: ((rect.height - s.height) / 2).rounded()))
140 }
141}
142
143/// The byline: the author's tile in the gutter, then name, verb, project and
144/// time. One accessibility element, read as `RosterByline.spoken`.
145@MainActor func bylineView(_ byline: RosterByline, width: CGFloat) -> NSView {
146 let view = FlippedView(frame: NSRect(x: 0, y: 0, width: width, height: MacMetric.tile))
147 let avatar = tileView(byline.who); avatar.frame.origin = .zero; view.addSubview(avatar)
148 var x = MacMetric.gutter
149 func add(_ item: NSView, gap: CGFloat = 6) { centre(item, x: x, row: 0, height: MacMetric.tile); view.addSubview(item); x += item.frame.width + gap }
150 if byline.showsName {
151 add(makeText(byline.who.name, MacType.name))
152 add(makeText(byline.verb, MacType.verb, .webSecondary))
153 }
154 if let project = byline.project {
155 if byline.showsName { add(makeText("·", MacType.verb, .webFaint)) }
156 add(monogramView(project), gap: 4)
157 add(makeText(project, MacType.verb, .webSecondary, lines: 1))
158 }
159 if let time = byline.timeText { add(makeText(time, MacType.time, .webFaint)) }
160 view.setAccessibilityElement(true); view.setAccessibilityRole(.group); view.setAccessibilityLabel(byline.spoken)
161 return view
162}
163
164@MainActor func needsChipView() -> NSView {
165 let label = NSAttributedString(string: EquipoStrings.text("chip.needs_you").uppercased(), attributes: [.font: MacType.chip, .foregroundColor: NSColor.webWarning, .kern: 0.3])
166 let width = ceil(label.size().width) + 16 + 15
167 let view = PaintView(frame: NSRect(x: 0, y: 0, width: width, height: 19)) { rect in
168 NSColor.webWarningBg.setFill(); NSBezierPath(roundedRect: rect, xRadius: rect.height / 2, yRadius: rect.height / 2).fill()
169 if let hand = symbolImage("hand.raised", size: 10, weight: .semibold, color: .webWarning) {
170 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)
171 }
172 label.draw(at: NSPoint(x: 23, y: ((rect.height - label.size().height) / 2).rounded()))
173 }
174 view.setAccessibilityElement(true); view.setAccessibilityLabel(EquipoStrings.text("chip.needs_you"))
175 return view
176}
177extension StageTone {
178 var ns: NSColor { self == .warning ? .webWarning : self == .negative ? .webNegative : .webPositive }
179 var nsHalo: NSColor { self == .warning ? .webWarningBg : self == .negative ? .webNegativeBg : .webPositiveBg }
180 var nsEyebrow: NSColor { self == .positive ? .webSecondary : ns }
181}
182@MainActor func button(_ title: String, _ style: ActionButton.Style = .choice, symbol: String? = nil, forced: ForcedState? = nil, busy: String? = nil, action: @escaping () -> Void = {}) -> ActionButton {
183 let b = ActionButton(title, style: style, symbol: symbol, action: action)
184 b.forced = forced
185 b.busyLabel = busy
186 let label = busy ?? title
187 let font = NSFont.systemFont(ofSize: 13, weight: style == .link ? .regular : .medium)
188 let extra: CGFloat = (symbol != nil ? 16 : 0) + (busy != nil ? 22 : 0)
189 b.frame.size = NSSize(width: style == .link ? textWidth(label, font) + extra + 4 : max(64, textWidth(label, font) + 32 + extra), height: 32)
190 return b
191}
192
193/// A raised card's plate: the surface, a rim and the web's two-step shadow.
194/// The view is larger than the card by `MacMetric.shadow` on every side.
195final class RaisedPlate: NSView {
196 override var isFlipped: Bool { true }
197 var flat: Bool
198 var dashed = false
199 let content = FlippedView()
200 init(width: CGFloat, height: CGFloat, flat: Bool) {
201 self.flat = flat
202 let m = MacMetric.shadow
203 super.init(frame: NSRect(x: 0, y: 0, width: width + 2 * m, height: height + 2 * m))
204 content.frame = NSRect(x: m, y: m, width: width, height: height)
205 addSubview(content)
206 }
207 required init?(coder: NSCoder) { fatalError() }
208 override func draw(_ dirtyRect: NSRect) {
209 let card = bounds.insetBy(dx: MacMetric.shadow + 0.5, dy: MacMetric.shadow + 0.5)
210 let shape = NSBezierPath(roundedRect: card, xRadius: MacMetric.radius, yRadius: MacMetric.radius)
211 if !flat {
212 for (alpha, blur, y) in [(0.06, 4.0, 2.0), (0.10, 14.0, 6.0)] as [(CGFloat, CGFloat, CGFloat)] {
213 NSGraphicsContext.saveGraphicsState()
214 let shadow = NSShadow()
215 shadow.shadowColor = NSColor.webShadow.withAlphaComponent(NSAppearance.currentDrawing().bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? alpha * 4 : alpha)
216 shadow.shadowBlurRadius = blur; shadow.shadowOffset = NSSize(width: 0, height: -y)
217 shadow.set()
218 NSColor.webSurface.setFill(); shape.fill()
219 NSGraphicsContext.restoreGraphicsState()
220 }
221 }
222 (flat ? NSColor.webSurface2 : NSColor.webSurface).setFill(); shape.fill()
223 (flat ? NSColor.webLine : NSColor.webLightRim).setStroke(); shape.lineWidth = 1; shape.stroke()
224 }
225}
226/// A sub-chat turn: yours on the right with "Sent", theirs with an avatar.
227@MainActor func threadTurnView(_ turn: ThreadTurn, width: CGFloat) -> NSView {
228 let view = FlippedView(frame: NSRect(x: 0, y: 0, width: width, height: 0))
229 if turn.own {
230 let body = makeText(turn.text, MacType.thread, .webText, width: min(width * 0.8, 440) - 24, selectable: true)
231 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
232 let p = NSBezierPath(roundedRect: r.insetBy(dx: 0.5, dy: 0.5), xRadius: 12, yRadius: 12)
233 NSColor.webSoft.setFill(); p.fill(); NSColor.webLightRim.setStroke(); p.stroke()
234 }
235 body.frame.origin = NSPoint(x: 12, y: 8); bubble.addSubview(body); view.addSubview(bubble)
236 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)
237 view.frame.size.height = sent.frame.maxY
238 view.setAccessibilityElement(true); view.setAccessibilityLabel(EquipoStrings.text("thread.own_spoken \(turn.text)"))
239 } else {
240 let who = turn.who ?? .coordinator
241 let avatar = tileView(who, size: 24); avatar.frame.origin = NSPoint(x: 0, y: 2); view.addSubview(avatar)
242 let maxBody = min(width - 32, 460) - 24
243 let name = makeText(who.name, NSFont.systemFont(ofSize: 11, weight: .semibold), .webSecondary)
244 let time = makeText(turn.timeText, MacType.time, .webFaint)
245 let body = makeText(turn.text, MacType.thread, .webText, width: maxBody, selectable: true)
246 let inner = max(body.frame.width, name.frame.width + 6 + time.frame.width)
247 let bubble = PaintView(frame: NSRect(x: 32, y: 0, width: inner + 24, height: name.frame.height + 3 + body.frame.height + 14)) { r in
248 let p = NSBezierPath(roundedRect: r.insetBy(dx: 0.5, dy: 0.5), xRadius: 12, yRadius: 12)
249 NSColor.webSurface.setFill(); p.fill(); NSColor.webLine.setStroke(); p.stroke()
250 }
251 name.frame.origin = NSPoint(x: 12, y: 6); bubble.addSubview(name)
252 time.frame.origin = NSPoint(x: name.frame.maxX + 6, y: 6); bubble.addSubview(time)
253 body.frame.origin = NSPoint(x: 12, y: name.frame.maxY + 3); bubble.addSubview(body)
254 view.addSubview(bubble)
255 view.frame.size.height = bubble.frame.maxY
256 view.setAccessibilityElement(true); view.setAccessibilityLabel("\(who.name), \(turn.timeText): \(turn.text)")
257 }
258 return view
259}
260@MainActor func readingView(_ who: Specialist) -> NSView {
261 let label = makeText(EquipoStrings.text("thread.reading \(who.name)"), MacType.small, .webSecondary)
262 let view = PaintView(frame: NSRect(x: 0, y: 0, width: label.frame.width + 28, height: max(18, label.frame.height))) { r in
263 NSColor.webSecondary.setFill()
264 for i in 0..<3 { NSBezierPath(ovalIn: NSRect(x: CGFloat(i) * 6, y: r.midY - 2, width: 4, height: 4)).fill() }
265 }
266 label.frame.origin = NSPoint(x: 28, y: 0); view.addSubview(label)
267 view.setAccessibilityElement(true); view.setAccessibilityLabel(label.stringValue)
268 return view
269}
270
271/// The card composer: a 46-point pill with a 44-point send target centred in it.
272final class MiniComposerView: NSView, NSTextFieldDelegate {
273 override var isFlipped: Bool { true }
274 let field = NSTextField()
275 let send: ActionButton
276 var focused: Bool { didSet { needsDisplay = true } }
277 var onSend: ((String) -> Void)?
278 init(width: CGFloat, placeholder: String, text: String = "", focused: Bool = false, busy: Bool = false) {
279 self.focused = focused
280 send = ActionButton(busy ? EquipoStrings.text("action.sending") : "", style: busy ? .primary : .miniSend, symbol: busy ? nil : "arrow.up") {}
281 super.init(frame: NSRect(x: 0, y: 0, width: width, height: 46))
282 field.isBordered = false; field.drawsBackground = false; field.focusRingType = .none
283 field.font = NSFont.systemFont(ofSize: 13); field.textColor = .webText
284 field.placeholderAttributedString = NSAttributedString(string: placeholder, attributes: [.font: NSFont.systemFont(ofSize: 13), .foregroundColor: NSColor.webPlaceholder])
285 field.stringValue = text
286 field.delegate = self
287 field.setAccessibilityLabel(placeholder)
288 let h = ceil(field.intrinsicContentSize.height)
289 field.frame = NSRect(x: 16, y: ((46 - h) / 2).rounded(), width: width - 16 - 50, height: h)
290 addSubview(field)
291 send.hasContent = !text.trimmingCharacters(in: .whitespaces).isEmpty
292 send.setAccessibilityLabel(busy ? EquipoStrings.text("action.sending") : EquipoStrings.text("composer.send"))
293 send.frame = busy ? NSRect(x: width - 96, y: 5, width: 90, height: 36) : NSRect(x: width - 45, y: 1, width: 44, height: 44)
294 send.actionBlock = { [weak self] in self?.submit() }
295 addSubview(send)
296 }
297 required init?(coder: NSCoder) { fatalError() }
298 func controlTextDidChange(_ obj: Notification) {
299 send.hasContent = !field.stringValue.trimmingCharacters(in: .whitespaces).isEmpty
300 focused = true
301 }
302 func controlTextDidEndEditing(_ obj: Notification) { focused = false }
303 /// Return sends; the field is single-line, so there is no newline to keep.
304 func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool {
305 guard selector == #selector(NSResponder.insertNewline(_:)) else { return false }
306 submit(); return true
307 }
308 func submit() {
309 let value = field.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
310 guard !value.isEmpty else { return }
311 onSend?(value)
312 field.stringValue = ""; send.hasContent = false
313 }
314 override func draw(_ dirtyRect: NSRect) {
315 let shape = NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: 22.5, yRadius: 22.5)
316 let on = focused || send.hasContent
317 NSColor.webSurface.setFill(); shape.fill()
318 (on ? NSColor.webAccent : NSColor.webBorder).setStroke(); shape.lineWidth = 1; shape.stroke()
319 }
320}
321