fix(power): 完善电池卡片的共同供电判定、功率图标与预估续航
按适配器和电池的实际贡献判定共同供电,以双图标和双色柱展示两路功率。同步三语预估续航文案,在仅外接供电时隐藏估时行。
This commit is contained in:
@@ -4,7 +4,15 @@ import IOKit.ps
|
||||
nonisolated struct BatteryMetrics: Sendable {
|
||||
let charge: Double?
|
||||
var flow = BatteryPowerFlow()
|
||||
var minutesRemaining: Int?
|
||||
var minutesToFull: Int?
|
||||
var minutesToEmpty: Int?
|
||||
var minutesRemaining: Int? {
|
||||
switch flow.state {
|
||||
case .charging: minutesToFull
|
||||
case .battery, .discharging, .combined: minutesToEmpty
|
||||
case .external, .unknown: nil
|
||||
}
|
||||
}
|
||||
var cycles: Int?
|
||||
var health: Double?
|
||||
}
|
||||
@@ -19,7 +27,11 @@ nonisolated struct BatteryMonitor {
|
||||
mutating func accepts(_ next: [NSNumber?], at now: Date, invalidate: Bool) -> Bool {
|
||||
if next != stamp { updated = now; stamp = next }
|
||||
else if invalidate { updated = .distantPast }
|
||||
return (0...120).contains(now.timeIntervalSince(updated))
|
||||
return isFresh(at: now)
|
||||
}
|
||||
|
||||
func isFresh(at now: Date) -> Bool {
|
||||
(0...120).contains(now.timeIntervalSince(updated))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +54,7 @@ nonisolated struct BatteryMonitor {
|
||||
}
|
||||
|
||||
static func positiveMinutes(_ value: Int?) -> Int? {
|
||||
value.flatMap { $0 > 0 ? $0 : nil }
|
||||
value.flatMap { (1..<65535).contains($0) ? $0 : nil }
|
||||
}
|
||||
|
||||
static func estimatedMinutes(charging: Bool, externalPower: Bool, sourceMinutes: Int?, systemSeconds: Double) -> Int? {
|
||||
@@ -77,14 +89,15 @@ nonisolated struct BatteryMonitor {
|
||||
let state = item[kIOPSPowerSourceStateKey] as? String
|
||||
let supply: Bool? = state == kIOPSACPowerValue ? true : state == kIOPSBatteryPowerValue ? false : nil
|
||||
let charging = item[kIOPSIsChargingKey] as? Bool
|
||||
let minutes = charging == true ? item[kIOPSTimeToFullChargeKey] as? Int : item[kIOPSTimeToEmptyKey] as? Int
|
||||
var estimate: Int?
|
||||
if let supply, let charging {
|
||||
estimate = Self.estimatedMinutes(charging: charging, externalPower: supply, sourceMinutes: minutes,
|
||||
systemSeconds: IOPSGetTimeRemainingEstimate())
|
||||
}
|
||||
return BatteryMetrics(charge: charge, flow: powerFlow(properties, externalSupply: supply, charging: charging),
|
||||
minutesRemaining: estimate,
|
||||
let flow = powerFlow(properties, externalSupply: supply, charging: charging)
|
||||
let systemMinutes = Self.estimatedMinutes(charging: false, externalPower: supply != false,
|
||||
sourceMinutes: item[kIOPSTimeToEmptyKey] as? Int, systemSeconds: IOPSGetTimeRemainingEstimate())
|
||||
let now = Date()
|
||||
let recentPack = Self.packIsRecent(properties, at: now)
|
||||
let packMinutes = recentPack && packStamp.isFresh(at: now) ? Self.positiveMinutes(properties["AvgTimeToEmpty"] as? Int) : nil
|
||||
return BatteryMetrics(charge: charge, flow: flow,
|
||||
minutesToFull: charging == true ? Self.positiveMinutes(item[kIOPSTimeToFullChargeKey] as? Int) : nil,
|
||||
minutesToEmpty: systemMinutes ?? packMinutes,
|
||||
cycles: (properties["CycleCount"] as? Int).flatMap { $0 >= 0 ? $0 : nil })
|
||||
}
|
||||
return nil
|
||||
@@ -106,6 +119,10 @@ nonisolated struct BatteryMonitor {
|
||||
return input?.source == .telemetry ? input?.watts : nil
|
||||
}
|
||||
|
||||
private static func packIsRecent(_ properties: [String: Any], at now: Date) -> Bool {
|
||||
scalar(properties["UpdateTime"]).map { (0...120).contains(now.timeIntervalSince1970 - $0) } ?? true
|
||||
}
|
||||
|
||||
private static func scalar(_ raw: Any?) -> Double? {
|
||||
guard let number = raw as? NSNumber, CFGetTypeID(number) != CFBooleanGetTypeID(),
|
||||
number.doubleValue.isFinite else { return nil }
|
||||
@@ -141,8 +158,7 @@ nonisolated struct BatteryMonitor {
|
||||
|
||||
// IORegistry can observe physical disconnection before IOPowerSources catches up.
|
||||
if connected == false { flow.externalSupply = false; flow.charging = false }
|
||||
let packDate = Self.scalar(properties["UpdateTime"])
|
||||
let recent = packDate.map { (0...120).contains(now.timeIntervalSince1970 - $0) } ?? true
|
||||
let recent = Self.packIsRecent(properties, at: now)
|
||||
let voltage = properties["Voltage"] ?? pack["Voltage"] ?? pack["AppleRawBatteryVoltage"]
|
||||
let instant = properties["InstantAmperage"] ?? pack["InstantAmperage"]
|
||||
let average = properties["Amperage"] ?? pack["Amperage"]
|
||||
|
||||
Reference in New Issue
Block a user