feat(menubar): 新增可配置的实时菜单栏指标与设置预览
使用竖条显示 CPU、GPU、内存与磁盘占用,分行展示上传和下载速率。支持全选、取消全选与空选恢复品牌图标,实时指标跟随面板刷新间隔。
This commit is contained in:
+20
-2
@@ -17,6 +17,7 @@ import AppKit
|
||||
private let awake = KeepAwakeService()
|
||||
private let presentation = SettingsPresentation()
|
||||
private let appearance = AppearancePreferences()
|
||||
private let menuBarPreferences = MenuBarPreferences()
|
||||
private let language = LanguagePreferences()
|
||||
private var openSettingsAction: (() -> Void)?
|
||||
private var scenes: AnyObject?
|
||||
@@ -35,10 +36,21 @@ import AppKit
|
||||
MainActor.assumeIsolated { self?.metrics.setDisplaySleeping(sleeping) }
|
||||
})
|
||||
}
|
||||
let readings = { [metrics, menuBarPreferences, language] () -> [MenuBarReading] in
|
||||
let selected = menuBarPreferences.selected
|
||||
guard !selected.isEmpty else { return [] }
|
||||
let locale = language.locale
|
||||
return MenuBarReading.readings(selection: selected, snapshot: metrics.snapshot) {
|
||||
AppLocalization.string($0.titleKey, locale: locale)
|
||||
}
|
||||
}
|
||||
menuBarPreferences.onDemandChange = { [metrics] in metrics.setMenuBarEnabled($0) }
|
||||
metrics.setMenuBarEnabled(!menuBarPreferences.selected.isEmpty)
|
||||
let settings = NSHostingSceneRepresentation {
|
||||
Settings {
|
||||
AppLanguageScope(preferences: language, onChange: { self.configureMainMenu() }) { [self] in
|
||||
AppearanceSettingsView(preferences: appearance, language: language, sampling: metrics.sampling, presentation: presentation)
|
||||
AppearanceSettingsView(preferences: appearance, language: language, sampling: metrics.sampling,
|
||||
menuBar: menuBarPreferences, readings: readings, presentation: presentation)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +59,7 @@ import AppKit
|
||||
openSettingsAction = { settings.environment.openSettings() }
|
||||
configureMainMenu()
|
||||
metrics.setDetailDemand(processes: true, slowMetrics: true)
|
||||
menuBar = MenuPanelController(visibility: { [metrics] in metrics.setPanelVisible($0) },
|
||||
menuBar = MenuPanelController(readings: readings, visibility: { [metrics] in metrics.setPanelVisible($0) },
|
||||
menuTitle: { [language] key in
|
||||
AppLocalization.string("status." + key, locale: language.locale, table: "StatusItem")
|
||||
}, openSettings: { [weak self] in self?.settingsCommand() }) { [self] panel in
|
||||
@@ -104,5 +116,11 @@ import AppKit
|
||||
scenes = nil
|
||||
}
|
||||
|
||||
// Finder or Spotlight remains a settings entry point when macOS hides a wide status item.
|
||||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||||
if !flag { settingsCommand() }
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }
|
||||
}
|
||||
|
||||
@@ -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: ", ")
|
||||
}
|
||||
}
|
||||
@@ -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) }
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,14 @@ final class MetricsStore {
|
||||
private(set) var slowMetricsRequested = true
|
||||
let sampling: SamplingPreferences
|
||||
private var panelDemand: Bool { isPanelVisible && !isDisplaySleeping }
|
||||
private var menuBarEnabled = false
|
||||
private var liveDemand: Bool { panelDemand || (menuBarEnabled && !isDisplaySleeping) }
|
||||
private var processDemand: Bool { panelDemand && processUpdatesRequested }
|
||||
var effectiveMilliseconds: Int { panelDemand ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
||||
private var hasSamplingDemand: Bool { !isSystemSleeping && (panelDemand || sampling.backgroundEnabled) }
|
||||
var effectiveMilliseconds: Int { liveDemand ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
||||
private var hasSamplingDemand: Bool { !isSystemSleeping && (liveDemand || sampling.backgroundEnabled) }
|
||||
// Permit timer coalescing without changing the requested cadence or delta timestamps.
|
||||
private var schedulingTolerance: Duration {
|
||||
.milliseconds(min(effectiveMilliseconds / 10, panelDemand ? 200 : 1_000))
|
||||
.milliseconds(min(effectiveMilliseconds / 10, liveDemand ? 200 : 1_000))
|
||||
}
|
||||
@ObservationIgnored private var generation = 0
|
||||
@ObservationIgnored private var visibilityGeneration = 0
|
||||
@@ -36,7 +38,7 @@ final class MetricsStore {
|
||||
self.sampling.onChange = { [weak self] in
|
||||
guard let self, !self.isPaused,
|
||||
self.effectiveMilliseconds != self.scheduledInterval || self.hasSamplingDemand != (self.task != nil) else { return }
|
||||
self.reschedule(reset: false, sampleImmediately: false)
|
||||
self.reschedule(reset: self.task == nil, sampleImmediately: self.task == nil)
|
||||
}
|
||||
start()
|
||||
}
|
||||
@@ -134,14 +136,25 @@ final class MetricsStore {
|
||||
|
||||
func setDisplaySleeping(_ sleeping: Bool) {
|
||||
guard sleeping != isDisplaySleeping else { return }
|
||||
let wasActive = hasSamplingDemand
|
||||
isDisplaySleeping = sleeping
|
||||
updateDemand(reset: !sampling.backgroundEnabled, sampleImmediately: panelDemand)
|
||||
updateDemand(reset: !wasActive || !hasSamplingDemand, sampleImmediately: liveDemand)
|
||||
}
|
||||
|
||||
func setPanelVisible(_ visible: Bool) {
|
||||
guard isPanelVisible != visible else { return }
|
||||
let wasActive = hasSamplingDemand
|
||||
isPanelVisible = visible
|
||||
updateDemand(reset: !sampling.backgroundEnabled, sampleImmediately: panelDemand)
|
||||
updateDemand(reset: !wasActive || !hasSamplingDemand, sampleImmediately: panelDemand)
|
||||
}
|
||||
|
||||
func setMenuBarEnabled(_ enabled: Bool) {
|
||||
guard menuBarEnabled != enabled else { return }
|
||||
let wasLive = liveDemand
|
||||
let wasActive = hasSamplingDemand
|
||||
menuBarEnabled = enabled
|
||||
guard wasLive != liveDemand, !isPaused, !isSystemSleeping else { return }
|
||||
reschedule(reset: !wasActive || !hasSamplingDemand, sampleImmediately: enabled)
|
||||
}
|
||||
|
||||
/// The caller supplies data needs, without introducing UI types into Core.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -6,6 +6,8 @@ struct AppearanceSettingsView: View {
|
||||
@Bindable var preferences: AppearancePreferences
|
||||
@Bindable var language: LanguagePreferences
|
||||
@Bindable var sampling: SamplingPreferences
|
||||
let menuBar: MenuBarPreferences
|
||||
let readings: () -> [MenuBarReading]
|
||||
let presentation: SettingsPresentation
|
||||
@Environment(\.locale) private var locale
|
||||
@State private var selection = SettingsPage.general
|
||||
@@ -14,6 +16,7 @@ struct AppearanceSettingsView: View {
|
||||
Group {
|
||||
switch selection {
|
||||
case .general: generalPage
|
||||
case .menuBar: MenuBarSettingsPage(preferences: menuBar, readings: readings())
|
||||
case .about: aboutPage
|
||||
}
|
||||
}
|
||||
@@ -21,7 +24,7 @@ struct AppearanceSettingsView: View {
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .principal) {
|
||||
NeutralTabPicker(title: "settings.title", selection: $selection,
|
||||
options: [(.general, "settings.general"), (.about, "settings.about")])
|
||||
options: [(.general, "settings.general"), (.menuBar, "settings.menuBar"), (.about, "settings.about")])
|
||||
}
|
||||
.sharedBackgroundVisibility(.hidden)
|
||||
}
|
||||
@@ -120,5 +123,5 @@ private extension AppAppearance {
|
||||
}
|
||||
|
||||
private enum SettingsPage: Hashable {
|
||||
case general, about
|
||||
case general, menuBar, about
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import SwiftUI
|
||||
|
||||
struct MenuBarSettingsPage: View {
|
||||
let preferences: MenuBarPreferences
|
||||
let readings: [MenuBarReading]
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 18) {
|
||||
HStack(spacing: 8) {
|
||||
Image(systemName: "apple.logo").accessibilityHidden(true)
|
||||
Spacer(minLength: 0)
|
||||
if readings.isEmpty {
|
||||
Image("BrandMark").renderingMode(.template)
|
||||
.resizable().scaledToFit().frame(width: 18, height: 18)
|
||||
} else {
|
||||
MenuBarStripPreview(readings: readings).fixedSize()
|
||||
}
|
||||
Text(verbatim: "09:41").font(.system(size: 11, weight: .medium))
|
||||
}
|
||||
.padding(.horizontal, 10)
|
||||
.frame(height: 36)
|
||||
.background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 8))
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityAddTraits(.isImage)
|
||||
.accessibilityLabel(Text("menuBar.preview \(readings.accessibilitySummary)"))
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack {
|
||||
Text("menuBar.items").font(.headline)
|
||||
Spacer()
|
||||
Button {
|
||||
preferences.setAllSelected(!preferences.allSelected)
|
||||
} label: {
|
||||
if preferences.allSelected { Text("menuBar.deselectAll") }
|
||||
else { Text("action.selectAll") }
|
||||
}
|
||||
.controlSize(.small)
|
||||
}
|
||||
VStack(spacing: 0) {
|
||||
ForEach(MenuBarMetric.allCases, id: \.self) { metric in
|
||||
if metric != .cpu { Divider() }
|
||||
HStack {
|
||||
Toggle(isOn: Binding(get: { preferences.selected.contains(metric) },
|
||||
set: { preferences.set(metric, enabled: $0) })) {
|
||||
Text(LocalizedStringKey(metric.titleKey))
|
||||
}
|
||||
.toggleStyle(.checkbox)
|
||||
Spacer()
|
||||
Text(descriptionKey(for: metric)).foregroundStyle(.secondary)
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.frame(height: 42)
|
||||
}
|
||||
}
|
||||
.background(.background, in: RoundedRectangle(cornerRadius: 8))
|
||||
.overlay(RoundedRectangle(cornerRadius: 8).strokeBorder(.quaternary))
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
.padding(8)
|
||||
}
|
||||
|
||||
private func descriptionKey(for metric: MenuBarMetric) -> LocalizedStringKey {
|
||||
switch metric {
|
||||
case .cpu, .gpu: "menuBar.utilization"
|
||||
case .memory: "menuBar.usedFraction"
|
||||
case .disk: "menuBar.diskSpace"
|
||||
case .network: "menuBar.networkRate"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3289,6 +3289,213 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"settings.menuBar" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Menu Bar"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "菜单栏"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "選單列"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.preview %@" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Menu bar preview: %@"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "菜单栏预览:%@"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "選單列預覽:%@"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.items" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Displayed items"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "显示项目"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "顯示項目"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.deselectAll" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Deselect All"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "取消全选"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "取消全選"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.disk" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Disk"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "磁盘"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "磁碟"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.utilization" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Utilization"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "使用率"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "使用率"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.usedFraction" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Percent used"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "已用比例"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "已用比例"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.diskSpace" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Disk space used"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "空间已用比例"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "空間已用比例"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"menuBar.networkRate" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Upload and download rates"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "上传和下载速率"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "上傳和下載速率"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"settings.title" : {
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
@@ -3318,19 +3525,19 @@
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "When off, hiding the panel stops recording and leaves gaps in charts. When on, recording follows the background interval. Pausing or system sleep stops recording."
|
||||
"value" : "Menu bar items update at the panel interval while the display is awake. Enable background recording to keep trends when neither is displayed. Pausing or system sleep stops recording."
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "关闭时,隐藏面板停止记录,图表保留空档。开启后按后台间隔记录;暂停或系统睡眠期间不记录。"
|
||||
"value" : "亮屏时,菜单栏指标按面板间隔刷新。面板和菜单栏均不展示指标时,可开启后台记录以延续趋势。暂停或系统睡眠时停止记录。"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "關閉時,隱藏面板停止記錄,圖表保留空檔。開啟後按背景間隔記錄;暫停或系統睡眠期間不記錄。"
|
||||
"value" : "螢幕亮起時,選單列指標依面板間隔更新。面板與選單列均未顯示指標時,可開啟背景記錄以延續趨勢。暫停或系統睡眠時停止記錄。"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user