feat(menubar): 完善首排监控卡片与温度风扇采样
补充 GPU 渲染分块、内存与利用率统计,增加只读 SMC 温度及风扇信息和旋转动效。完善电池功率与耗电进程列表,统一首排布局并固定异步加载区域高度。同步验证用例、架构说明和 GPU 能力调研工具。
This commit is contained in:
@@ -5,12 +5,22 @@ nonisolated struct BatteryMetrics: Sendable {
|
||||
let charge: Double?
|
||||
let charging: Bool
|
||||
let externalPower: Bool
|
||||
var power: Double? = nil
|
||||
var minutesRemaining: Int?
|
||||
var cycles: Int?
|
||||
var health: Double?
|
||||
}
|
||||
|
||||
nonisolated enum BatteryMonitor {
|
||||
static func normalizedPower(voltageMillivolts: Double?, amperageMilliamps: Double?) -> Double? {
|
||||
guard let voltageMillivolts, let amperageMilliamps,
|
||||
voltageMillivolts.isFinite, amperageMilliamps.isFinite,
|
||||
voltageMillivolts > 0, voltageMillivolts <= 30_000,
|
||||
abs(amperageMilliamps) <= 20_000 else { return nil }
|
||||
let watts = abs(voltageMillivolts * amperageMilliamps) / 1_000_000
|
||||
return watts.isFinite && watts <= 500 ? watts : nil
|
||||
}
|
||||
|
||||
static func positiveMinutes(_ value: Int?) -> Int? {
|
||||
value.flatMap { $0 > 0 ? $0 : nil }
|
||||
}
|
||||
@@ -55,10 +65,24 @@ nonisolated enum BatteryMonitor {
|
||||
let charging = item[kIOPSIsChargingKey] as? Bool ?? false
|
||||
let external = item[kIOPSPowerSourceStateKey] as? String == kIOPSACPowerValue
|
||||
let minutes = charging ? item[kIOPSTimeToFullChargeKey] as? Int : external ? nil : item[kIOPSTimeToEmptyKey] as? Int
|
||||
let batteryData = properties["BatteryData"] as? [String: Any] ?? [:]
|
||||
let voltage = firstNumber([properties["Voltage"], batteryData["Voltage"], batteryData["AppleRawBatteryVoltage"]])
|
||||
let amperage = firstNumber([properties["InstantAmperage"], batteryData["InstantAmperage"], properties["Amperage"], batteryData["Amperage"]])
|
||||
return BatteryMetrics(charge: charge, charging: item[kIOPSIsChargingKey] as? Bool ?? false,
|
||||
externalPower: external, minutesRemaining: estimatedMinutes(charging: charging, externalPower: external, sourceMinutes: minutes, systemSeconds: IOPSGetTimeRemainingEstimate()),
|
||||
externalPower: external,
|
||||
power: normalizedPower(voltageMillivolts: voltage, amperageMilliamps: amperage),
|
||||
minutesRemaining: estimatedMinutes(charging: charging, externalPower: external, sourceMinutes: minutes, systemSeconds: IOPSGetTimeRemainingEstimate()),
|
||||
cycles: (properties["CycleCount"] as? Int).flatMap { $0 >= 0 ? $0 : nil })
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private static func firstNumber(_ values: [Any?]) -> Double? {
|
||||
for case let value? in values {
|
||||
if let number = value as? NSNumber, number.doubleValue.isFinite {
|
||||
return number.doubleValue
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user