feat: added isBatteryPowered field to the Battery_Usage struct. It allows removing the check if the power source is Battery Power in the different parts of the Battery module

This commit is contained in:
Serhiy Mytrovtsiy
2023-03-16 18:00:52 +01:00
parent c46aa38f3f
commit 7dd4e0ecf3
4 changed files with 8 additions and 6 deletions

View File

@@ -19,6 +19,7 @@ struct Battery_Usage: value_t {
var isCharged: Bool = false
var isCharging: Bool = false
var isLowPowerMode: Bool? = false
var isBatteryPowered: Bool = false
var optimizedChargingEngaged: Bool = false
var level: Double = 0
var cycles: Int = 0
@@ -140,7 +141,7 @@ public class Battery: Module {
case let widget as BatteryWidget:
widget.setValue(
percentage: value.level,
ACStatus: value.powerSource != "Battery Power",
ACStatus: !value.isBatteryPowered,
isCharging: value.isCharging,
lowPowerMode: value.isLowPowerMode,
optimizedCharging: value.optimizedChargingEngaged,
@@ -166,7 +167,7 @@ public class Battery: Module {
return
}
if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.lowLevelNotificationState {
if (value.level > notificationLevel || !value.isBatteryPowered) && self.lowLevelNotificationState {
if value.level > notificationLevel {
if let id = self.notificationID {
removeNotification(id)
@@ -201,7 +202,7 @@ public class Battery: Module {
return
}
if (value.level < notificationLevel || value.powerSource == "Battery Power") && self.highLevelNotificationState {
if (value.level < notificationLevel || value.isBatteryPowered) && self.highLevelNotificationState {
if value.level < notificationLevel {
if let id = self.notificationID {
removeNotification(id)

View File

@@ -256,7 +256,7 @@ internal class Popup: NSView, Popup_p {
self.sourceField?.stringValue = localizedString(value.powerSource)
self.timeField?.stringValue = ""
if value.powerSource == "Battery Power" {
if value.isBatteryPowered {
self.timeLabelField?.stringValue = "\(localizedString("Time to discharge")):"
if value.timeToEmpty != -1 && value.timeToEmpty != 0 {
self.timeField?.stringValue = Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds(short: self.timeFormat == "short")
@@ -319,7 +319,7 @@ internal class Popup: NSView, Popup_p {
self.batteryPowerField?.stringValue = "\(batteryPower.roundTo(decimalPlaces: 2)) W"
self.temperatureField?.stringValue = Temperature(value.temperature)
self.powerField?.stringValue = value.powerSource == "Battery Power" ? localizedString("Not connected") : "\(value.ACwatts) W"
self.powerField?.stringValue = value.isBatteryPowered ? localizedString("Not connected") : "\(value.ACwatts) W"
self.chargingStateField?.stringValue = value.isCharging ? localizedString("Yes") : localizedString("No")
})
}

View File

@@ -77,7 +77,7 @@ internal class Portal: NSStackView, Portal_p {
var seconds: Double = 0
if value.timeToEmpty != -1 && value.timeToEmpty != 0 {
seconds = Double((value.powerSource == "Battery Power" ? value.timeToEmpty : value.timeToCharge)*60)
seconds = Double((value.isBatteryPowered ? value.timeToEmpty : value.timeToCharge)*60)
}
self.timeField.stringValue = seconds != 0 ? seconds.printSecondsToHoursMinutesSeconds(short: self.timeFormat == "short") : ""

View File

@@ -69,6 +69,7 @@ internal class UsageReader: Reader<Battery_Usage> {
for ps in psList {
if let list = IOPSGetPowerSourceDescription(psInfo, ps).takeUnretainedValue() as? [String: Any] {
self.usage.powerSource = list[kIOPSPowerSourceStateKey] as? String ?? "AC Power"
self.usage.isBatteryPowered = self.usage.powerSource == "Battery Power"
self.usage.isCharged = list[kIOPSIsChargedKey] as? Bool ?? false
self.usage.isCharging = self.getBoolValue("IsCharging" as CFString) ?? false
self.usage.optimizedChargingEngaged = list["Optimized Battery Charging Engaged"] as? Int == 1