feat: removed Low power mode status from the battery module since it does not work. The API does not provide updates via the notification center. Read ProcessInfo does not updates the value too. pmset returns only manually managed mode. So it cannot be used to determine if low power mode is enabled.

This commit is contained in:
Serhiy Mytrovtsiy
2023-07-07 16:52:37 +02:00
parent 8a98cfd645
commit 8789531c80
4 changed files with 3 additions and 63 deletions

View File

@@ -17,7 +17,6 @@ public class BatteryWidget: WidgetWrapper {
private var iconState: Bool = true
private var colorState: Bool = false
private var hideAdditionalWhenFull: Bool = true
private var lowPowerModeState: Bool = true
private var percentage: Double? = nil
private var time: Int = 0
@@ -44,7 +43,6 @@ public class BatteryWidget: WidgetWrapper {
self.iconState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_icon", defaultValue: self.iconState)
self.colorState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState)
self.hideAdditionalWhenFull = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_hideAdditionalWhenFull", defaultValue: self.hideAdditionalWhenFull)
self.lowPowerModeState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_lowPowerMode", defaultValue: self.lowPowerMode)
}
if preview {
@@ -150,10 +148,7 @@ public class BatteryWidget: WidgetWrapper {
let innerWidth: CGFloat = max(1, maxWidth * CGFloat(percentage))
let innerOffset: CGFloat = -offset + borderWidth + 1
var colorState = self.colorState
var color = percentage.batteryColor(color: colorState)
if self.lowPowerModeState {
color = percentage.batteryColor(color: colorState, lowPowerMode: self.lowPowerMode)
}
let color = percentage.batteryColor(color: colorState)
if self.additional == "innerPercentage" && !self.ACStatus {
colorState = false
@@ -333,7 +328,7 @@ public class BatteryWidget: WidgetWrapper {
return rowWidth
}
public func setValue(percentage: Double? = nil, ACStatus: Bool? = nil, isCharging: Bool? = nil, lowPowerMode: Bool? = nil, optimizedCharging: Bool? = nil, time: Int? = nil) {
public func setValue(percentage: Double? = nil, ACStatus: Bool? = nil, isCharging: Bool? = nil, optimizedCharging: Bool? = nil, time: Int? = nil) {
var updated: Bool = false
let timeFormat: String = Store.shared.string(key: "\(self.title)_timeFormat", defaultValue: self.timeFormat)
@@ -357,10 +352,6 @@ public class BatteryWidget: WidgetWrapper {
self.timeFormat = timeFormat
updated = true
}
if let state = lowPowerMode, self.lowPowerMode != state {
self.lowPowerMode = state
updated = true
}
if let state = optimizedCharging, self.optimizedCharging != state {
self.optimizedCharging = state
updated = true
@@ -396,12 +387,6 @@ public class BatteryWidget: WidgetWrapper {
state: self.hideAdditionalWhenFull
))
view.addArrangedSubview(toggleSettingRow(
title: localizedString("Low power mode"),
action: #selector(toggleLowPowerMode),
state: self.lowPowerModeState
))
view.addArrangedSubview(toggleSettingRow(
title: localizedString("Colorize"),
action: #selector(toggleColor),
@@ -443,18 +428,6 @@ public class BatteryWidget: WidgetWrapper {
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: self.colorState)
self.display()
}
@objc private func toggleLowPowerMode(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
} else {
state = sender is NSButton ? (sender as! NSButton).state: nil
}
self.lowPowerModeState = state! == .on ? true : false
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_lowPowerMode", value: self.lowPowerModeState)
self.display()
}
}
public class BatteryDetailsWidget: WidgetWrapper {

View File

@@ -18,7 +18,6 @@ struct Battery_Usage: value_t, Codable {
var state: String? = nil
var isCharged: Bool = false
var isCharging: Bool = false
var isLowPowerMode: Bool? = false
var isBatteryPowered: Bool = false
var optimizedChargingEngaged: Bool = false
var level: Double = 0
@@ -143,7 +142,6 @@ public class Battery: Module {
percentage: value.level,
ACStatus: !value.isBatteryPowered,
isCharging: value.isCharging,
lowPowerMode: value.isLowPowerMode,
optimizedCharging: value.optimizedChargingEngaged,
time: value.timeToEmpty == 0 && value.timeToCharge != 0 ? value.timeToCharge : value.timeToEmpty
)

View File

@@ -20,11 +20,7 @@ internal class Popup: PopupWrapper {
private let dashboardHeight: CGFloat = 90
private var detailsHeight: CGFloat {
var count: CGFloat = 7
if #available(macOS 12.0, *) {
count += 1
}
return (22 * count) + Constants.Popup.separatorHeight
return (22 * 7) + Constants.Popup.separatorHeight
}
private let batteryHeight: CGFloat = (22 * 4) + Constants.Popup.separatorHeight
private let adapterHeight: CGFloat = (22 * 2) + Constants.Popup.separatorHeight
@@ -44,7 +40,6 @@ internal class Popup: PopupWrapper {
private var capacityField: NSTextField? = nil
private var cyclesField: NSTextField? = nil
private var lastChargeField: NSTextField? = nil
private var lowPowerModeField: NSTextField? = nil
private var amperageField: NSTextField? = nil
private var voltageField: NSTextField? = nil
@@ -169,10 +164,6 @@ internal class Popup: PopupWrapper {
self.timeField = t.1
self.lastChargeField = popupRow(container, title: "\(localizedString("Last charge")):", value: "").1
if #available(macOS 12.0, *) {
self.lowPowerModeField = popupRow(container, title: "\(localizedString("Low power mode")):", value: localizedString("Unknown")).1
}
view.addSubview(separator)
view.addSubview(container)
@@ -311,10 +302,6 @@ internal class Popup: PopupWrapper {
self.lastChargeField?.toolTip = localizedString("Unknown")
}
if let powerMode = value.isLowPowerMode {
self.lowPowerModeField?.stringValue = powerMode ? localizedString("Enabled") : localizedString("Disabled")
}
self.amperageField?.stringValue = "\(abs(value.amperage)) mA"
self.voltageField?.stringValue = "\(value.voltage.roundTo(decimalPlaces: 2)) V"
let batteryPower = value.voltage * (Double(abs(value.amperage))/1000)

View File

@@ -39,10 +39,6 @@ internal class UsageReader: Reader<Battery_Usage> {
CFRunLoopAddSource(self.loop, source, .defaultMode)
self.read()
if #available(macOS 12.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(self.lowModeChanged), name: Notification.Name.NSProcessInfoPowerStateDidChange, object: nil)
}
}
public override func stop() {
@@ -54,10 +50,6 @@ internal class UsageReader: Reader<Battery_Usage> {
CFRunLoopRemoveSource(runLoop, source, .defaultMode)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
public override func read() {
let psInfo = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let psList = IOPSCopyPowerSourcesList(psInfo).takeRetainedValue() as [CFTypeRef]
@@ -73,9 +65,6 @@ internal class UsageReader: Reader<Battery_Usage> {
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
if #available(macOS 12.0, *) {
self.usage.isLowPowerMode = ProcessInfo.processInfo.isLowPowerModeEnabled
}
self.usage.level = Double(list[kIOPSCurrentCapacityKey] as? Int ?? 0) / 100
if let time = list[kIOPSTimeToEmptyKey] as? Int {
@@ -153,13 +142,6 @@ internal class UsageReader: Reader<Battery_Usage> {
}
return nil
}
@objc private func lowModeChanged() {
if #available(macOS 12.0, *) {
self.usage.isLowPowerMode = ProcessInfo.processInfo.isLowPowerModeEnabled
self.callback(self.usage)
}
}
}
public class ProcessReader: Reader<[TopProcess]> {