feat(menubar): 实现趋势面板外侧详情与三语外观设置

接入有界趋势、平滑滚动、菜单式外侧详情、底部操作与三语文案,装配独立外观设置和 Dock 生命周期。
合并跟随系统外观刷新、自选颜色入口与色块绘制修复,保留用户确认的配色联动、重启保存及滚动验收。
多场景视觉与长期资源验证边界仍记录于 docs。
This commit is contained in:
2026-10-30 00:45:34 +08:00
parent 631b412d15
commit 0c846a2127
31 changed files with 2033 additions and 42 deletions
+15
View File
@@ -0,0 +1,15 @@
import Foundation
nonisolated enum MetricFormat {
static func percent(_ value: Double?) -> String {
guard let value, value.isFinite else { return "--" }
return "\(Int((min(1, max(0, value)) * 100).rounded()))%"
}
static func bytes(_ value: UInt64?) -> String {
guard let value else { return "--" }
return String(format: "%.1f GB", Double(value) / 1_000_000_000)
}
static func decimal(_ value: Double) -> String { String(format: "%.1f", value) }
}
+20
View File
@@ -0,0 +1,20 @@
import Foundation
nonisolated struct TrendSample: Sendable {
let date: Date
let cpu: Double?
let gpu: Double?
let memory: Double?
}
/// Bounded sampling history; no timer or UI visibility dependence.
nonisolated struct MetricHistory: Sendable {
static let capacity = 60
private(set) var samples: [TrendSample] = []
mutating func append(_ snapshot: MetricsSnapshot) {
samples.append(TrendSample(date: snapshot.date, cpu: snapshot.cpu?.usage,
gpu: snapshot.gpuUsage, memory: snapshot.memory?.usage))
if samples.count > Self.capacity { samples.removeFirst(samples.count - Self.capacity) }
}
}
+1
View File
@@ -22,6 +22,7 @@ nonisolated struct MetricsSnapshot: Sendable {
var cpu: CPUMetrics?
var memory: MemoryMetrics?
var load: [Double] = []
var uptime: TimeInterval = 0
// GPU remains unavailable until a reliable IOReport subscription is validated.
var gpuUsage: Double? = nil
}
+1
View File
@@ -11,6 +11,7 @@ actor SystemSampler {
var result = MetricsSnapshot()
result.cpu = cpu.sample()
result.memory = MemoryMonitor.sample()
result.uptime = ProcessInfo.processInfo.systemUptime
var load = [Double](repeating: 0, count: 3)
if getloadavg(&load, 3) == 3 { result.load = load }
return result
@@ -0,0 +1,36 @@
import Foundation
import Observation
nonisolated enum AppAppearance: String, CaseIterable, Identifiable {
case system, light, dark
var id: String { rawValue }
}
nonisolated enum AccentChoice: String, CaseIterable, Identifiable {
case system, blue, purple, teal, green, orange, pink, custom
var id: String { rawValue }
}
@MainActor @Observable
final class AppearancePreferences {
@ObservationIgnored private let defaults: UserDefaults
var appearance: AppAppearance { didSet { defaults.set(appearance.rawValue, forKey: "appearance") } }
var accent: AccentChoice { didSet { defaults.set(accent.rawValue, forKey: "accent") } }
var red: Double { didSet { defaults.set(red, forKey: "accent.red") } }
var green: Double { didSet { defaults.set(green, forKey: "accent.green") } }
var blue: Double { didSet { defaults.set(blue, forKey: "accent.blue") } }
init(defaults: UserDefaults = .standard) {
self.defaults = defaults
appearance = AppAppearance(rawValue: defaults.string(forKey: "appearance") ?? "") ?? .system
accent = AccentChoice(rawValue: defaults.string(forKey: "accent") ?? "") ?? .system
red = Self.component(defaults.object(forKey: "accent.red"), fallback: 0.2)
green = Self.component(defaults.object(forKey: "accent.green"), fallback: 0.5)
blue = Self.component(defaults.object(forKey: "accent.blue"), fallback: 0.9)
}
private static func component(_ value: Any?, fallback: Double) -> Double {
guard let value = value as? Double, value.isFinite else { return fallback }
return min(1, max(0, value))
}
}
+2
View File
@@ -4,6 +4,7 @@ import Observation
@MainActor @Observable
final class MetricsStore {
private(set) var snapshot = MetricsSnapshot()
private(set) var history = MetricHistory()
private(set) var isPaused = false
@ObservationIgnored private var task: Task<Void, Never>?
@ObservationIgnored private let sampler = SystemSampler()
@@ -19,6 +20,7 @@ final class MetricsStore {
let next = await sampler.sample()
guard !Task.isCancelled else { return }
self?.snapshot = next
self?.history.append(next)
SamplingDiagnostics.record(next)
do { try await Task.sleep(for: .seconds(2)) }
catch { return }