feat(menubar): 新增可配置的实时菜单栏指标与设置预览
使用竖条显示 CPU、GPU、内存与磁盘占用,分行展示上传和下载速率。支持全选、取消全选与空选恢复品牌图标,实时指标跟随面板刷新间隔。
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
|
||||
nonisolated enum MenuBarMetric: String, CaseIterable {
|
||||
case cpu, gpu, memory, disk, network
|
||||
|
||||
var titleKey: String { self == .disk ? "menuBar.disk" : "metric." + rawValue }
|
||||
}
|
||||
|
||||
nonisolated struct MenuBarReading: Equatable {
|
||||
enum Content: Equatable {
|
||||
case usage(Double?)
|
||||
case network(upload: String, download: String)
|
||||
}
|
||||
|
||||
let metric: MenuBarMetric
|
||||
let title: String
|
||||
let content: Content
|
||||
|
||||
static func readings(selection: Set<MenuBarMetric>, snapshot: MetricsSnapshot,
|
||||
title: (MenuBarMetric) -> String) -> [Self] {
|
||||
MenuBarMetric.allCases.filter(selection.contains).map { metric in
|
||||
let content: Content = switch metric {
|
||||
case .cpu: .usage(snapshot.cpu?.usage)
|
||||
case .gpu: .usage(snapshot.gpuUsage)
|
||||
case .memory: .usage(snapshot.memory?.usage)
|
||||
case .disk: .usage(snapshot.disk?.usage)
|
||||
case .network: .network(upload: compactRate(snapshot.uploadRate), download: compactRate(snapshot.downloadRate))
|
||||
}
|
||||
return Self(metric: metric, title: title(metric), content: content)
|
||||
}
|
||||
}
|
||||
|
||||
private static func compactRate(_ value: Double?) -> String {
|
||||
MetricFormat.rate(value).replacingOccurrences(of: ".0 ", with: " ")
|
||||
}
|
||||
|
||||
var accessibilityText: String {
|
||||
switch content {
|
||||
case let .usage(value): "\(title) \(MetricFormat.percent(value))"
|
||||
case let .network(upload, download): "\(title) ↑ \(upload), ↓ \(download)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated extension Array where Element == MenuBarReading {
|
||||
var accessibilitySummary: String {
|
||||
isEmpty ? "Kymo" : map(\.accessibilityText).joined(separator: ", ")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user