| 1 | import AppKit |
| 2 | import EquipoKit |
| 3 | |
| 4 | // Ported from the r7 mockups (native-mockups-r4: native/mockups/Mac/ |
| 5 | // RosterAppKit.swift, NativeTranscript.swift, NativeComposer.swift): the |
| 6 | // web's tokens and controls, drawn in AppKit. |
| 7 | |
| 8 | /// Hover, pressed and focus can be forced for a still capture; the live app |
| 9 | /// draws them from the pointer and the keyboard. |
| 10 | enum ForcedState { case hover, pressed, focus, disabled } |
| 11 | |
| 12 | extension NSColor { |
| 13 | static func web(_ light: UInt32, _ dark: UInt32) -> NSColor { |
| 14 | NSColor(name: nil) { appearance in |
| 15 | let value = appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? dark : light |
| 16 | return NSColor(srgbRed: Double((value >> 16) & 255) / 255, green: Double((value >> 8) & 255) / 255, blue: Double(value & 255) / 255, alpha: 1) |
| 17 | } |
| 18 | } |
| 19 | static let webText = web(0x18181B, 0xEDEDF0) |
| 20 | static let webFace = web(0xFFFFFF, 0x141518) |
| 21 | static let webPlaceholder = web(0x6B6B75, 0x8C8C96) |
| 22 | static let webPrimary = web(0x2257DF, 0x2F61E8) |
| 23 | static let webBorder = web(0xD0D4DC, 0x343843) |
| 24 | static let webBackground = web(0xFBFBFC, 0x0C0D0F) |
| 25 | static let webSecondary = web(0x52525C, 0xA1A1AA) |
| 26 | static let webAccent = web(0x1F56D6, 0x7AA2FF) |
| 27 | static let webSoft = web(0xECF1FF, 0x16234A) |
| 28 | static let webSoftBorder = web(0xC9D7FA, 0x27397A) |
| 29 | static let webLine = web(0xE6E6EA, 0x26282E) |
| 30 | static let webSurface2 = web(0xF3F3F5, 0x18191D) |
| 31 | static let webFaceHover = web(0xF4F5F8, 0x1B1D22) |
| 32 | static let webFacePressed = web(0xE9EBEF, 0x22242B) |
| 33 | static let webPrimaryHover = web(0x1D4DC8, 0x2956D0) |
| 34 | static let webPrimaryEdge = web(0x153CA0, 0x1F43A6) |
| 35 | static let webPositive = web(0x166534, 0x6EE7A8) |
| 36 | static let webNegative = web(0xB91C1C, 0xF79AA0) |
| 37 | } |
| 38 | |
| 39 | extension NSColor { |
| 40 | static let webFaint = web(0x6B6B75, 0x8C8C96) |
| 41 | static let webSurface = web(0xFFFFFF, 0x141518) |
| 42 | static let webLightRim = web(0xB8C5E5, 0x5D6986) |
| 43 | static let webLineStrong = web(0x868691, 0x71737C) |
| 44 | static let webWarning = web(0x854D0E, 0xF0C46A) |
| 45 | static let webWarningBg = web(0xFEF3C7, 0x2C2110) |
| 46 | static let webPositiveBg = web(0xDCFCE7, 0x10281C) |
| 47 | static let webNegativeBg = web(0xFEE2E2, 0x331819) |
| 48 | static let webNeutral = web(0x374151, 0xC3C9D1) |
| 49 | static let webNeutralBg = web(0xEEF0F2, 0x262C34) |
| 50 | static let webShadow = web(0x101828, 0x000000) |
| 51 | } |
| 52 | func symbolImage(_ name: String, size: CGFloat, weight: NSFont.Weight = .medium, color: NSColor) -> NSImage? { |
| 53 | NSImage(systemSymbolName: name, accessibilityDescription: nil)? |
| 54 | .withSymbolConfiguration(NSImage.SymbolConfiguration(pointSize: size, weight: weight).applying(NSImage.SymbolConfiguration(paletteColors: [color]))) |
| 55 | } |
| 56 | |
| 57 | /// The web's controls, drawn in AppKit: rest, hover, pressed, focus, |
| 58 | /// disabled and busy. |
| 59 | final class ActionButton: NSButton { |
| 60 | enum Style { case choice, primary, quiet, round, send, attachment, glyph, row, link, miniSend, aside } |
| 61 | var style: Style { didSet { needsDisplay = true } } |
| 62 | var symbol: String? { didSet { needsDisplay = true } } |
| 63 | var forced: ForcedState? { didSet { needsDisplay = true } } |
| 64 | /// The web's data-busy-label: shown with a spinner while a request runs. |
| 65 | var busyLabel: String? { didSet { updateSpinner() } } |
| 66 | /// For the card composer's send button: filled once there is something to send. |
| 67 | var hasContent = false { didSet { needsDisplay = true } } |
| 68 | var actionBlock: (() -> Void)? |
| 69 | private var hovering = false |
| 70 | private var tracking: NSTrackingArea? |
| 71 | private var spinner: NSProgressIndicator? |
| 72 | init(_ title: String, style: Style = .choice, symbol: String? = nil, action: @escaping () -> Void) { |
| 73 | self.style = style |
| 74 | self.symbol = symbol |
| 75 | super.init(frame: .zero) |
| 76 | self.title = title |
| 77 | actionBlock = action |
| 78 | isBordered = false |
| 79 | setButtonType(.momentaryPushIn) |
| 80 | focusRingType = .exterior |
| 81 | target = self |
| 82 | self.action = #selector(performAction) |
| 83 | if !title.isEmpty { setAccessibilityLabel(title) } |
| 84 | } |
| 85 | required init?(coder: NSCoder) { fatalError() } |
| 86 | static let titleFont = NSFont.systemFont(ofSize: 13, weight: .medium) |
| 87 | /// Padding each side of a filled button's title. |
| 88 | static let inset: CGFloat = 16 |
| 89 | static func idealWidth(_ title: String, _ font: NSFont = titleFont) -> CGFloat { max(64, textWidth(title, font) + 2 * inset) } |
| 90 | override var isFlipped: Bool { true } |
| 91 | @objc private func performAction() { actionBlock?() } |
| 92 | override var wantsUpdateLayer: Bool { false } |
| 93 | override func updateTrackingAreas() { |
| 94 | super.updateTrackingAreas() |
| 95 | if let tracking { removeTrackingArea(tracking) } |
| 96 | let area = NSTrackingArea(rect: .zero, options: [.mouseEnteredAndExited, .activeAlways, .inVisibleRect], owner: self) |
| 97 | addTrackingArea(area) |
| 98 | tracking = area |
| 99 | } |
| 100 | override func mouseEntered(with event: NSEvent) { hovering = true; needsDisplay = true } |
| 101 | override func mouseExited(with event: NSEvent) { hovering = false; needsDisplay = true } |
| 102 | private func updateSpinner() { |
| 103 | spinner?.removeFromSuperview(); spinner = nil |
| 104 | isEnabled = busyLabel == nil |
| 105 | if busyLabel != nil { |
| 106 | let s = NSProgressIndicator(frame: NSRect(x: 0, y: 0, width: 14, height: 14)) |
| 107 | s.style = .spinning; s.controlSize = .small; s.isDisplayedWhenStopped = true |
| 108 | if style == .primary { s.appearance = NSAppearance(named: .darkAqua) } |
| 109 | addSubview(s); spinner = s |
| 110 | setAccessibilityLabel(busyLabel) |
| 111 | } |
| 112 | needsLayout = true; needsDisplay = true |
| 113 | } |
| 114 | private var shapePath: NSBezierPath { |
| 115 | switch style { |
| 116 | case .choice, .primary: return NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: 6, yRadius: 6) |
| 117 | case .round: return NSBezierPath(ovalIn: bounds.insetBy(dx: 0.5, dy: 0.5)) |
| 118 | case .send, .attachment: return NSBezierPath(ovalIn: bounds.insetBy(dx: 6, dy: 6)) |
| 119 | case .miniSend: return NSBezierPath(ovalIn: bounds.insetBy(dx: 4, dy: 4)) |
| 120 | case .glyph: return NSBezierPath(ovalIn: bounds.insetBy(dx: 9, dy: 9)) |
| 121 | case .aside: return NSBezierPath(roundedRect: NSRect(x: (bounds.width - 28) / 2, y: (bounds.height - 28) / 2, width: 28, height: 28), xRadius: 6, yRadius: 6) |
| 122 | case .quiet, .row, .link: return NSBezierPath(roundedRect: bounds, xRadius: 6, yRadius: 6) |
| 123 | } |
| 124 | } |
| 125 | override var focusRingMaskBounds: NSRect { bounds } |
| 126 | override func drawFocusRingMask() { shapePath.fill() } |
| 127 | override func draw(_ dirtyRect: NSRect) { |
| 128 | let busy = busyLabel != nil |
| 129 | let enabled = (isEnabled || busy) && forced != .disabled |
| 130 | let pressed = (isHighlighted || forced == .pressed) && enabled |
| 131 | let hover = (hovering || forced == .hover) && enabled && !busy |
| 132 | let path = shapePath |
| 133 | NSGraphicsContext.current?.cgContext.setAlpha(enabled ? 1 : 0.45) |
| 134 | var textColor = NSColor.webText |
| 135 | var symbolColor = NSColor.webSecondary |
| 136 | switch style { |
| 137 | case .choice, .round: |
| 138 | (pressed ? NSColor.webFacePressed : hover ? NSColor.webFaceHover : NSColor.webFace).setFill(); path.fill() |
| 139 | NSColor.webBorder.setStroke(); path.lineWidth = 1; path.stroke() |
| 140 | case .primary, .send: |
| 141 | (pressed ? NSColor.webPrimaryEdge : hover ? NSColor.webPrimaryHover : NSColor.webPrimary).setFill(); path.fill() |
| 142 | if style == .primary { NSColor.webPrimaryEdge.setStroke(); path.lineWidth = 1; path.stroke() } |
| 143 | textColor = .white; symbolColor = .white |
| 144 | case .attachment: |
| 145 | (pressed ? NSColor.webFacePressed : hover ? NSColor.webFaceHover : NSColor.webSurface2).setFill(); path.fill() |
| 146 | case .miniSend: |
| 147 | if hasContent { (pressed ? NSColor.webPrimaryEdge : hover ? NSColor.webPrimaryHover : NSColor.webPrimary).setFill(); symbolColor = .white } |
| 148 | else { NSColor.webSurface2.setFill(); symbolColor = .webFaint } |
| 149 | path.fill() |
| 150 | case .glyph, .row, .aside: |
| 151 | if hover || pressed { (pressed ? NSColor.webFacePressed : NSColor.webFaceHover).setFill(); path.fill() } |
| 152 | case .quiet: |
| 153 | textColor = .webAccent |
| 154 | case .link: |
| 155 | textColor = hover ? .webText : .webSecondary; symbolColor = textColor |
| 156 | } |
| 157 | if forced == .focus { |
| 158 | let ring = NSBezierPath(roundedRect: bounds.insetBy(dx: -2, dy: -2), xRadius: 8, yRadius: 8) |
| 159 | NSColor.keyboardFocusIndicatorColor.setStroke(); ring.lineWidth = 3; ring.stroke() |
| 160 | } |
| 161 | var label = busyLabel ?? title |
| 162 | var attributes: [NSAttributedString.Key: Any] = [ |
| 163 | .font: NSFont.systemFont(ofSize: 13, weight: style == .quiet || style == .row || style == .link ? .regular : .medium), |
| 164 | .foregroundColor: textColor, |
| 165 | ] |
| 166 | if (style == .quiet || style == .link) && hover { attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue } |
| 167 | let filled = style == .choice || style == .primary |
| 168 | let room = bounds.width - 2 * Self.inset |
| 169 | // A busy label that does not fit beside its spinner gives way to the |
| 170 | // spinner alone (the accessibility label still says it). |
| 171 | if busy, filled, !label.isEmpty, NSAttributedString(string: label, attributes: attributes).size().width + 20 > room { label = "" } |
| 172 | // A title wider than the button wraps, centred, rather than spill out. |
| 173 | if filled, !busy, symbol == nil, !label.isEmpty, NSAttributedString(string: label, attributes: attributes).size().width > room + 0.5 { |
| 174 | let paragraph = NSMutableParagraphStyle(); paragraph.alignment = .center; paragraph.lineBreakMode = .byWordWrapping |
| 175 | attributes[.paragraphStyle] = paragraph |
| 176 | let wrapped = NSAttributedString(string: label, attributes: attributes) |
| 177 | let height = ceil(wrapped.boundingRect(with: NSSize(width: room, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin]).height) |
| 178 | wrapped.draw(with: NSRect(x: Self.inset, y: ((bounds.height - height) / 2).rounded(), width: room, height: height), options: [.usesLineFragmentOrigin]) |
| 179 | return |
| 180 | } |
| 181 | let text = NSAttributedString(string: label, attributes: attributes) |
| 182 | let textSize = label.isEmpty ? NSSize.zero : text.size() |
| 183 | let image = busy ? nil : symbol.flatMap { symbolImage($0, size: style == .send || style == .miniSend ? 13 : style == .glyph || style == .link || style == .aside ? 11 : 15, weight: style == .send || style == .miniSend ? .bold : .semibold, color: symbolColor) } |
| 184 | let imageSize = image?.size ?? .zero |
| 185 | let spin: CGFloat = busy ? (label.isEmpty ? 14 : 20) : 0 |
| 186 | let gap: CGFloat = image != nil && !label.isEmpty ? (style == .link ? 4 : 8) : 0 |
| 187 | var x: CGFloat |
| 188 | switch style { |
| 189 | case .quiet, .link: x = 0 |
| 190 | case .row: x = 10 |
| 191 | default: x = (bounds.width - textSize.width - imageSize.width - gap - spin) / 2 |
| 192 | } |
| 193 | if let spinner { spinner.frame.origin = NSPoint(x: x.rounded(), y: ((bounds.height - 14) / 2).rounded()); x += spin } |
| 194 | let drawImage = { (at: CGFloat) in |
| 195 | guard let image else { return } |
| 196 | image.draw(in: NSRect(x: at.rounded(), y: ((self.bounds.height - imageSize.height) / 2).rounded(), width: imageSize.width, height: imageSize.height), from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil) |
| 197 | } |
| 198 | if style == .link { |
| 199 | if !label.isEmpty { text.draw(at: NSPoint(x: x.rounded(), y: ((bounds.height - textSize.height) / 2).rounded())) } |
| 200 | drawImage(x + textSize.width + gap) |
| 201 | return |
| 202 | } |
| 203 | if image != nil { drawImage(x); x += imageSize.width + gap } |
| 204 | if !label.isEmpty { text.draw(at: NSPoint(x: x.rounded(), y: ((bounds.height - textSize.height) / 2).rounded())) } |
| 205 | } |
| 206 | } |
| 207 | final class FlippedView: NSView { override var isFlipped: Bool { true } } |
| 208 | final class CardView: NSView { |
| 209 | override var isFlipped: Bool { true } |
| 210 | let fill: NSColor |
| 211 | let stroke: NSColor? |
| 212 | let radius: CGFloat |
| 213 | init(fill: NSColor, stroke: NSColor? = nil, radius: CGFloat = 10) { |
| 214 | self.fill = fill; self.stroke = stroke; self.radius = radius |
| 215 | super.init(frame: .zero) |
| 216 | } |
| 217 | required init?(coder: NSCoder) { fatalError() } |
| 218 | override func draw(_ dirtyRect: NSRect) { |
| 219 | let path = NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: radius, yRadius: radius) |
| 220 | fill.setFill(); path.fill() |
| 221 | if let stroke { stroke.setStroke(); path.lineWidth = 1; path.stroke() } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |