feat(menubar): 新增可配置的实时菜单栏指标与设置预览

使用竖条显示 CPU、GPU、内存与磁盘占用,分行展示上传和下载速率。支持全选、取消全选与空选恢复品牌图标,实时指标跟随面板刷新间隔。
This commit is contained in:
2026-11-15 22:52:37 +08:00
parent 6300db3d15
commit fe62b7910b
24 changed files with 667 additions and 28 deletions
@@ -15,6 +15,8 @@ import Observation
#if DEBUG || KYMO_AUDIT
private(set) weak var lastHiddenContent: NSView?
#endif
private let metricStrip = MenuBarMetricStrip()
private let readings: () -> [MenuBarReading]
private var statusItem: NSStatusItem?
private var localMouse: Any?
private var globalMouse: Any?
@@ -28,10 +30,12 @@ import Observation
private var isHiding = false
private var preferredContentHeight = PanelLayout.defaultHeight
init<Content: View>(installStatusItem: Bool = true, visibility: @escaping (Bool) -> Void,
init<Content: View>(installStatusItem: Bool = true, readings: @escaping () -> [MenuBarReading] = { [] },
visibility: @escaping (Bool) -> Void,
menuTitle: @escaping (String) -> String = { $0 },
openSettings: @escaping () -> Void = {},
@ViewBuilder content: @escaping (PanelPresentation) -> Content) {
self.readings = readings
self.visibility = visibility
self.menuTitle = menuTitle
self.openSettings = openSettings
@@ -54,13 +58,13 @@ import Observation
presentation.onPinChange = { [weak self] pinned in self?.panel.level = pinned ? .floating : .normal }
if installStatusItem {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
item.button?.image = NSImage(named: "BrandMark")
item.button?.image?.isTemplate = true
item.button?.setAccessibilityLabel("Kymo")
item.button?.target = self
item.button?.action = #selector(statusItemClicked)
item.button?.sendAction(on: [.leftMouseUp, .rightMouseUp])
statusItem = item
metricStrip.setAccessibilityElement(false)
item.button?.addSubview(metricStrip)
refreshStatusItem()
}
tokens.append(NotificationCenter.default.addObserver(forName: NSApplication.didChangeScreenParametersNotification, object: nil, queue: .main) { [weak self] _ in
MainActor.assumeIsolated { self?.place() }
@@ -79,6 +83,29 @@ import Observation
})
}
private func refreshStatusItem() {
guard let statusItem, let button = statusItem.button else { return }
let current = withObservationTracking { readings() } onChange: { [weak self] in
Task { @MainActor [weak self] in self?.refreshStatusItem() }
}
metricStrip.readings = current
metricStrip.isHidden = current.isEmpty
if current.isEmpty {
button.image = NSImage(named: "BrandMark")
button.image?.isTemplate = true
statusItem.length = NSStatusItem.squareLength
} else {
button.image = nil
let size = metricStrip.intrinsicContentSize
let length = size.width + 8
if statusItem.length != length { statusItem.length = length }
metricStrip.frame = NSRect(x: 4, y: 0, width: size.width, height: button.bounds.height)
metricStrip.autoresizingMask = [.height]
}
button.setAccessibilityLabel("Kymo")
button.setAccessibilityValue(current.accessibilitySummary)
}
@objc func togglePanel() { panel.isVisible ? hide() : show() }
@objc private func statusItemClicked() {