Add function to determine if the computer has battery

Also the default value for the "showBatteryIndicator" is the result of the function: if the device is a MacBook it defaults to true like before, otherwise to false
This commit is contained in:
Federico Imberti 2025-12-15 16:13:42 +01:00 • committed by Hariharan Mudaliar
commit 712f0e6f88
No known key found for this signature in database
GPG key ID: 025583EB0BE5640E
2 changed files with 9 additions and 1 deletions

View file

@ -304,6 +304,14 @@ class BatteryActivityManager {
} }
} }
/// Determines if the device has a battery
/// - Returns: True if the computer has a battery, false otherwhise
func hasBattery() -> Bool {
guard let snapshot = IOPSCopyPowerSourcesInfo()?.takeRetainedValue() else { return false }
guard let sources = IOPSCopyPowerSourcesList(snapshot)?.takeRetainedValue() as? [CFTypeRef] else { return false }
return !sources.isEmpty
}
deinit { deinit {
stopMonitoring() stopMonitoring()
NotificationCenter.default.removeObserver(self) NotificationCenter.default.removeObserver(self)

View file

@ -483,7 +483,7 @@ extension Defaults.Keys {
// MARK: Battery // MARK: Battery
static let showPowerStatusNotifications = Key<Bool>("showPowerStatusNotifications", default: true) static let showPowerStatusNotifications = Key<Bool>("showPowerStatusNotifications", default: true)
static let showBatteryIndicator = Key<Bool>("showBatteryIndicator", default: true) static let showBatteryIndicator = Key<Bool>("showBatteryIndicator", default: BatteryActivityManager.shared.hasBattery())
static let showBatteryPercentage = Key<Bool>("showBatteryPercentage", default: true) static let showBatteryPercentage = Key<Bool>("showBatteryPercentage", default: true)
static let showPowerStatusIcons = Key<Bool>("showPowerStatusIcons", default: true) static let showPowerStatusIcons = Key<Bool>("showPowerStatusIcons", default: true)
static let playLowBatteryAlertSound = Key<Bool>("playLowBatteryAlertSound", default: true) static let playLowBatteryAlertSound = Key<Bool>("playLowBatteryAlertSound", default: true)