88 lines
3.7 KiB
Swift
88 lines
3.7 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
private extension MenuBarMetric {
|
|
var colorName: String {
|
|
switch self {
|
|
case .cpu: "MetricCPU"
|
|
case .gpu: "MetricGPU"
|
|
case .memory: "MetricMemory"
|
|
case .disk: "MetricDisk"
|
|
case .network: "MetricNetwork"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The same drawing is hosted by the settings preview and by the native status button.
|
|
final class MenuBarMetricStrip: NSView {
|
|
private let titleFont = NSFont.systemFont(ofSize: 8.5, weight: .medium)
|
|
private let valueFont = NSFont.monospacedDigitSystemFont(ofSize: 10, weight: .semibold)
|
|
private let rateFont = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .medium)
|
|
private let gaugeWidth: CGFloat = 3
|
|
private let textInset: CGFloat = 6
|
|
private let spacing: CGFloat = 4
|
|
var readings: [MenuBarReading] = [] {
|
|
didSet {
|
|
guard readings != oldValue else { return }
|
|
invalidateIntrinsicContentSize()
|
|
needsDisplay = true
|
|
}
|
|
}
|
|
|
|
override var isFlipped: Bool { true }
|
|
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
|
|
|
private func width(of reading: MenuBarReading) -> CGFloat {
|
|
switch reading.content {
|
|
case .network:
|
|
return ceil(("↓ 999.9 MB/s" as NSString).size(withAttributes: [.font: rateFont]).width)
|
|
case .usage:
|
|
let valueWidth = ("100%" as NSString).size(withAttributes: [.font: valueFont]).width
|
|
let titleWidth = (reading.title as NSString).size(withAttributes: [.font: titleFont]).width
|
|
return textInset + ceil(max(titleWidth, valueWidth))
|
|
}
|
|
}
|
|
|
|
override var intrinsicContentSize: NSSize {
|
|
NSSize(width: readings.reduce(0) { $0 + width(of: $1) } + CGFloat(max(0, readings.count - 1)) * spacing,
|
|
height: 22)
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
var x: CGFloat = 0
|
|
let top = floor(bounds.midY - 11)
|
|
for item in readings {
|
|
switch item.content {
|
|
case let .usage(fraction):
|
|
let track = NSRect(x: x, y: top + 2, width: gaugeWidth, height: 18)
|
|
NSColor.labelColor.withAlphaComponent(0.18).setFill()
|
|
NSBezierPath(rect: track).fill()
|
|
if let fraction, fraction.isFinite, fraction > 0 {
|
|
let height = track.height * min(1, fraction)
|
|
(NSColor(named: item.metric.colorName) ?? .labelColor).setFill()
|
|
NSBezierPath(rect: NSRect(x: x, y: track.maxY - height, width: gaugeWidth, height: height)).fill()
|
|
}
|
|
drawText(item.title, x: x + textInset, y: top, font: titleFont)
|
|
drawText(MetricFormat.percent(fraction), x: x + textInset, y: top + 9, font: valueFont)
|
|
case let .network(upload, download):
|
|
drawText("↑ \(upload)", x: x, y: top, font: rateFont)
|
|
drawText("↓ \(download)", x: x, y: top + 10, font: rateFont)
|
|
}
|
|
x += width(of: item) + spacing
|
|
}
|
|
}
|
|
private func drawText(_ text: String, x: CGFloat, y: CGFloat, font: NSFont) {
|
|
(text as NSString).draw(at: NSPoint(x: x, y: y),
|
|
withAttributes: [.font: font, .foregroundColor: NSColor.labelColor])
|
|
}
|
|
}
|
|
|
|
struct MenuBarStripPreview: NSViewRepresentable {
|
|
let readings: [MenuBarReading]
|
|
func makeNSView(context: Context) -> MenuBarMetricStrip { MenuBarMetricStrip() }
|
|
func updateNSView(_ view: MenuBarMetricStrip, context: Context) { view.readings = readings }
|
|
func sizeThatFits(_ proposal: ProposedViewSize, nsView: MenuBarMetricStrip, context: Context) -> CGSize? {
|
|
nsView.intrinsicContentSize
|
|
}
|
|
}
|