import AppKit import EquipoKit // Ported from the r7 mockups (native-mockups-r4: native/mockups/Mac/ // RosterAppKit.swift, NativeTranscript.swift, NativeComposer.swift): the // web's tokens and controls, drawn in AppKit. /// Hover, pressed and focus can be forced for a still capture; the live app /// draws them from the pointer and the keyboard. enum ForcedState { case hover, pressed, focus, disabled } extension NSColor { static func web(_ light: UInt32, _ dark: UInt32) -> NSColor { NSColor(name: nil) { appearance in let value = appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? dark : light return NSColor(srgbRed: Double((value >> 16) & 255) / 255, green: Double((value >> 8) & 255) / 255, blue: Double(value & 255) / 255, alpha: 1) } } static let webText = web(0x18181B, 0xEDEDF0) static let webFace = web(0xFFFFFF, 0x141518) static let webPlaceholder = web(0x6B6B75, 0x8C8C96) static let webPrimary = web(0x2257DF, 0x2F61E8) static let webBorder = web(0xD0D4DC, 0x343843) static let webBackground = web(0xFBFBFC, 0x0C0D0F) static let webSecondary = web(0x52525C, 0xA1A1AA) static let webAccent = web(0x1F56D6, 0x7AA2FF) static let webSoft = web(0xECF1FF, 0x16234A) static let webSoftBorder = web(0xC9D7FA, 0x27397A) static let webLine = web(0xE6E6EA, 0x26282E) static let webSurface2 = web(0xF3F3F5, 0x18191D) static let webFaceHover = web(0xF4F5F8, 0x1B1D22) static let webFacePressed = web(0xE9EBEF, 0x22242B) static let webPrimaryHover = web(0x1D4DC8, 0x2956D0) static let webPrimaryEdge = web(0x153CA0, 0x1F43A6) static let webPositive = web(0x166534, 0x6EE7A8) static let webNegative = web(0xB91C1C, 0xF79AA0) } extension NSColor { static let webFaint = web(0x6B6B75, 0x8C8C96) static let webSurface = web(0xFFFFFF, 0x141518) static let webLightRim = web(0xB8C5E5, 0x5D6986) static let webLineStrong = web(0x868691, 0x71737C) static let webWarning = web(0x854D0E, 0xF0C46A) static let webWarningBg = web(0xFEF3C7, 0x2C2110) static let webPositiveBg = web(0xDCFCE7, 0x10281C) static let webNegativeBg = web(0xFEE2E2, 0x331819) static let webNeutral = web(0x374151, 0xC3C9D1) static let webNeutralBg = web(0xEEF0F2, 0x262C34) static let webShadow = web(0x101828, 0x000000) } func symbolImage(_ name: String, size: CGFloat, weight: NSFont.Weight = .medium, color: NSColor) -> NSImage? { NSImage(systemSymbolName: name, accessibilityDescription: nil)? .withSymbolConfiguration(NSImage.SymbolConfiguration(pointSize: size, weight: weight).applying(NSImage.SymbolConfiguration(paletteColors: [color]))) } /// The web's controls, drawn in AppKit: rest, hover, pressed, focus, /// disabled and busy. final class ActionButton: NSButton { enum Style { case choice, primary, quiet, round, send, attachment, glyph, row, link, miniSend, aside } var style: Style { didSet { needsDisplay = true } } var symbol: String? { didSet { needsDisplay = true } } var forced: ForcedState? { didSet { needsDisplay = true } } /// The web's data-busy-label: shown with a spinner while a request runs. var busyLabel: String? { didSet { updateSpinner() } } /// For the card composer's send button: filled once there is something to send. var hasContent = false { didSet { needsDisplay = true } } var actionBlock: (() -> Void)? private var hovering = false private var tracking: NSTrackingArea? private var spinner: NSProgressIndicator? init(_ title: String, style: Style = .choice, symbol: String? = nil, action: @escaping () -> Void) { self.style = style self.symbol = symbol super.init(frame: .zero) self.title = title actionBlock = action isBordered = false setButtonType(.momentaryPushIn) focusRingType = .exterior target = self self.action = #selector(performAction) if !title.isEmpty { setAccessibilityLabel(title) } } required init?(coder: NSCoder) { fatalError() } static let titleFont = NSFont.systemFont(ofSize: 13, weight: .medium) /// Padding each side of a filled button's title. static let inset: CGFloat = 16 static func idealWidth(_ title: String, _ font: NSFont = titleFont) -> CGFloat { max(64, textWidth(title, font) + 2 * inset) } override var isFlipped: Bool { true } @objc private func performAction() { actionBlock?() } override var wantsUpdateLayer: Bool { false } override func updateTrackingAreas() { super.updateTrackingAreas() if let tracking { removeTrackingArea(tracking) } let area = NSTrackingArea(rect: .zero, options: [.mouseEnteredAndExited, .activeAlways, .inVisibleRect], owner: self) addTrackingArea(area) tracking = area } override func mouseEntered(with event: NSEvent) { hovering = true; needsDisplay = true } override func mouseExited(with event: NSEvent) { hovering = false; needsDisplay = true } private func updateSpinner() { spinner?.removeFromSuperview(); spinner = nil isEnabled = busyLabel == nil if busyLabel != nil { let s = NSProgressIndicator(frame: NSRect(x: 0, y: 0, width: 14, height: 14)) s.style = .spinning; s.controlSize = .small; s.isDisplayedWhenStopped = true if style == .primary { s.appearance = NSAppearance(named: .darkAqua) } addSubview(s); spinner = s setAccessibilityLabel(busyLabel) } needsLayout = true; needsDisplay = true } private var shapePath: NSBezierPath { switch style { case .choice, .primary: return NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: 6, yRadius: 6) case .round: return NSBezierPath(ovalIn: bounds.insetBy(dx: 0.5, dy: 0.5)) case .send, .attachment: return NSBezierPath(ovalIn: bounds.insetBy(dx: 6, dy: 6)) case .miniSend: return NSBezierPath(ovalIn: bounds.insetBy(dx: 4, dy: 4)) case .glyph: return NSBezierPath(ovalIn: bounds.insetBy(dx: 9, dy: 9)) case .aside: return NSBezierPath(roundedRect: NSRect(x: (bounds.width - 28) / 2, y: (bounds.height - 28) / 2, width: 28, height: 28), xRadius: 6, yRadius: 6) case .quiet, .row, .link: return NSBezierPath(roundedRect: bounds, xRadius: 6, yRadius: 6) } } override var focusRingMaskBounds: NSRect { bounds } override func drawFocusRingMask() { shapePath.fill() } override func draw(_ dirtyRect: NSRect) { let busy = busyLabel != nil let enabled = (isEnabled || busy) && forced != .disabled let pressed = (isHighlighted || forced == .pressed) && enabled let hover = (hovering || forced == .hover) && enabled && !busy let path = shapePath NSGraphicsContext.current?.cgContext.setAlpha(enabled ? 1 : 0.45) var textColor = NSColor.webText var symbolColor = NSColor.webSecondary switch style { case .choice, .round: (pressed ? NSColor.webFacePressed : hover ? NSColor.webFaceHover : NSColor.webFace).setFill(); path.fill() NSColor.webBorder.setStroke(); path.lineWidth = 1; path.stroke() case .primary, .send: (pressed ? NSColor.webPrimaryEdge : hover ? NSColor.webPrimaryHover : NSColor.webPrimary).setFill(); path.fill() if style == .primary { NSColor.webPrimaryEdge.setStroke(); path.lineWidth = 1; path.stroke() } textColor = .white; symbolColor = .white case .attachment: (pressed ? NSColor.webFacePressed : hover ? NSColor.webFaceHover : NSColor.webSurface2).setFill(); path.fill() case .miniSend: if hasContent { (pressed ? NSColor.webPrimaryEdge : hover ? NSColor.webPrimaryHover : NSColor.webPrimary).setFill(); symbolColor = .white } else { NSColor.webSurface2.setFill(); symbolColor = .webFaint } path.fill() case .glyph, .row, .aside: if hover || pressed { (pressed ? NSColor.webFacePressed : NSColor.webFaceHover).setFill(); path.fill() } case .quiet: textColor = .webAccent case .link: textColor = hover ? .webText : .webSecondary; symbolColor = textColor } if forced == .focus { let ring = NSBezierPath(roundedRect: bounds.insetBy(dx: -2, dy: -2), xRadius: 8, yRadius: 8) NSColor.keyboardFocusIndicatorColor.setStroke(); ring.lineWidth = 3; ring.stroke() } var label = busyLabel ?? title var attributes: [NSAttributedString.Key: Any] = [ .font: NSFont.systemFont(ofSize: 13, weight: style == .quiet || style == .row || style == .link ? .regular : .medium), .foregroundColor: textColor, ] if (style == .quiet || style == .link) && hover { attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue } let filled = style == .choice || style == .primary let room = bounds.width - 2 * Self.inset // A busy label that does not fit beside its spinner gives way to the // spinner alone (the accessibility label still says it). if busy, filled, !label.isEmpty, NSAttributedString(string: label, attributes: attributes).size().width + 20 > room { label = "" } // A title wider than the button wraps, centred, rather than spill out. if filled, !busy, symbol == nil, !label.isEmpty, NSAttributedString(string: label, attributes: attributes).size().width > room + 0.5 { let paragraph = NSMutableParagraphStyle(); paragraph.alignment = .center; paragraph.lineBreakMode = .byWordWrapping attributes[.paragraphStyle] = paragraph let wrapped = NSAttributedString(string: label, attributes: attributes) let height = ceil(wrapped.boundingRect(with: NSSize(width: room, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin]).height) wrapped.draw(with: NSRect(x: Self.inset, y: ((bounds.height - height) / 2).rounded(), width: room, height: height), options: [.usesLineFragmentOrigin]) return } let text = NSAttributedString(string: label, attributes: attributes) let textSize = label.isEmpty ? NSSize.zero : text.size() 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) } let imageSize = image?.size ?? .zero let spin: CGFloat = busy ? (label.isEmpty ? 14 : 20) : 0 let gap: CGFloat = image != nil && !label.isEmpty ? (style == .link ? 4 : 8) : 0 var x: CGFloat switch style { case .quiet, .link: x = 0 case .row: x = 10 default: x = (bounds.width - textSize.width - imageSize.width - gap - spin) / 2 } if let spinner { spinner.frame.origin = NSPoint(x: x.rounded(), y: ((bounds.height - 14) / 2).rounded()); x += spin } let drawImage = { (at: CGFloat) in guard let image else { return } 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) } if style == .link { if !label.isEmpty { text.draw(at: NSPoint(x: x.rounded(), y: ((bounds.height - textSize.height) / 2).rounded())) } drawImage(x + textSize.width + gap) return } if image != nil { drawImage(x); x += imageSize.width + gap } if !label.isEmpty { text.draw(at: NSPoint(x: x.rounded(), y: ((bounds.height - textSize.height) / 2).rounded())) } } } final class FlippedView: NSView { override var isFlipped: Bool { true } } final class CardView: NSView { override var isFlipped: Bool { true } let fill: NSColor let stroke: NSColor? let radius: CGFloat init(fill: NSColor, stroke: NSColor? = nil, radius: CGFloat = 10) { self.fill = fill; self.stroke = stroke; self.radius = radius super.init(frame: .zero) } required init?(coder: NSCoder) { fatalError() } override func draw(_ dirtyRect: NSRect) { let path = NSBezierPath(roundedRect: bounds.insetBy(dx: 0.5, dy: 0.5), xRadius: radius, yRadius: radius) fill.setFill(); path.fill() if let stroke { stroke.setStroke(); path.lineWidth = 1; path.stroke() } } }