fix(power): 完善电池卡片的共同供电判定、功率图标与预估续航
按适配器和电池的实际贡献判定共同供电,以双图标和双色柱展示两路功率。同步三语预估续航文案,在仅外接供电时隐藏估时行。
This commit is contained in:
@@ -15,9 +15,44 @@ nonisolated struct BatteryPowerFlow: Sendable {
|
||||
var load: PowerReading?
|
||||
var adapterCapacity: Double?
|
||||
|
||||
struct JointSupply: Sendable {
|
||||
let adapter: PowerReading
|
||||
let discharge: PowerReading
|
||||
var batteryFraction: Double { discharge.watts / (adapter.watts + discharge.watts) }
|
||||
}
|
||||
|
||||
// Classification floor in watts, separate from the resolution of the displayed readings.
|
||||
private static let minimumContribution = 0.5
|
||||
private(set) var registryJointSupply: JointSupply?
|
||||
|
||||
var jointSupply: JointSupply? {
|
||||
guard connected == true, let battery,
|
||||
battery.watts <= -Self.minimumContribution else { return nil }
|
||||
if battery.source == .smc { return registryJointSupply }
|
||||
guard let input, input.watts >= Self.minimumContribution else { return nil }
|
||||
// IOPS reports a single primary source. Override its battery-only indication
|
||||
// only if all three direct telemetry channels independently support the split.
|
||||
if externalSupply == false &&
|
||||
(input.source != .telemetry || battery.source != .telemetry || load?.source != .telemetry) { return nil }
|
||||
let discharge: PowerReading
|
||||
if battery.source == .telemetry || battery.source == .balance {
|
||||
discharge = PowerReading(watts: -battery.watts, source: battery.source)
|
||||
} else if let load, load.source == .telemetry, input.source == .telemetry {
|
||||
discharge = PowerReading(watts: load.watts - input.watts, source: .balance)
|
||||
} else { return nil }
|
||||
guard discharge.watts >= Self.minimumContribution else { return nil }
|
||||
if let load, load.source == .telemetry, input.source == .telemetry {
|
||||
// Reject conflicting measured channels rather than normalize an unexplained excess.
|
||||
let residual = abs(input.watts + discharge.watts - load.watts)
|
||||
guard residual <= max(1, 0.1 * load.watts) else { return nil }
|
||||
}
|
||||
return JointSupply(adapter: input, discharge: discharge)
|
||||
}
|
||||
|
||||
mutating func applyBatterySensor(_ watts: Double) {
|
||||
// The gauge may still report the last charging sample just after unplugging.
|
||||
guard watts <= 0 || (externalSupply != false && connected != false) else { return }
|
||||
if battery?.source != .smc { registryJointSupply = jointSupply }
|
||||
battery = PowerReading(watts: watts, source: .smc)
|
||||
// Estimates made from older registry measurements no longer share a sampling window.
|
||||
if load?.source == .balance { load = nil }
|
||||
@@ -37,8 +72,12 @@ nonisolated struct BatteryPowerFlow: Sendable {
|
||||
let threshold = battery.source == .balance ? 0.5 : 0.05
|
||||
if battery.watts >= threshold { return .charging }
|
||||
if battery.watts <= -threshold {
|
||||
if connected == true, let input, input.source != .balance, input.watts >= 0.05 { return .combined }
|
||||
if connected == false || externalSupply == false || input?.watts == 0 { return .battery }
|
||||
if jointSupply != nil { return .combined }
|
||||
if connected == false || externalSupply == false { return .battery }
|
||||
if let input, input.source != .balance {
|
||||
if input.watts < Self.minimumContribution { return .battery }
|
||||
if battery.watts > -Self.minimumContribution { return .external }
|
||||
}
|
||||
return .discharging
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user