feat(menubar): 新增可配置的实时菜单栏指标与设置预览
使用竖条显示 CPU、GPU、内存与磁盘占用,分行展示上传和下载速率。支持全选、取消全选与空选恢复品牌图标,实时指标跟随面板刷新间隔。
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
@MainActor @Observable final class MenuBarPreferences {
|
||||
private static let key = "menuBar.metrics"
|
||||
@ObservationIgnored private let defaults: UserDefaults
|
||||
@ObservationIgnored var onDemandChange: ((Bool) -> Void)?
|
||||
private(set) var selected: Set<MenuBarMetric>
|
||||
|
||||
var allSelected: Bool { selected.count == MenuBarMetric.allCases.count }
|
||||
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
selected = defaults.stringArray(forKey: Self.key).map { Set($0.compactMap(MenuBarMetric.init(rawValue:))) }
|
||||
?? Set(MenuBarMetric.allCases)
|
||||
}
|
||||
|
||||
func set(_ metric: MenuBarMetric, enabled: Bool) {
|
||||
var next = selected
|
||||
if enabled { next.insert(metric) } else { next.remove(metric) }
|
||||
replaceSelection(with: next)
|
||||
}
|
||||
|
||||
func setAllSelected(_ enabled: Bool) {
|
||||
replaceSelection(with: enabled ? Set(MenuBarMetric.allCases) : [])
|
||||
}
|
||||
|
||||
private func replaceSelection(with next: Set<MenuBarMetric>) {
|
||||
guard selected != next else { return }
|
||||
let wasEmpty = selected.isEmpty
|
||||
selected = next
|
||||
defaults.set(MenuBarMetric.allCases.filter(selected.contains).map(\.rawValue), forKey: Self.key)
|
||||
if wasEmpty != selected.isEmpty { onDemandChange?(!selected.isEmpty) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user