Files
wangairnan 4b8deb2e48 feat(power): 展示电池实际充放电功率并及时响应电源切换
保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
2026-11-15 23:54:19 +08:00

40 lines
1.4 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 memory(_ value: UInt64?) -> String {
guard let value else { return "--" }
if value == 0 { return "0 B" }
return String(format: "%.1f GiB", Double(value) / 1_073_741_824)
}
static func rate(_ value: Double?) -> String {
guard let value, value.isFinite, value >= 0 else { return "--" }
let units = ["B/s", "KB/s", "MB/s", "GB/s", "TB/s"]
var scaled = value
var index = 0
while scaled >= 1000 && index < units.count - 1 { scaled /= 1000; index += 1 }
return String(format: "%.1f %@", scaled, units[index])
}
static func signedWatts(_ value: Double?) -> String {
guard let value, value.isFinite else { return "--" }
if abs(value) < 0.05 { return "0 W" }
return (value < 0 ? "−" : "+") + watts(abs(value))
}
static func watts(_ value: Double?) -> String {
guard let value, value.isFinite, value >= 0 else { return "--" }
return String(format: "%.1f W", value)
}
}