From 712f0e6f8803f247d119a0bc56e9204098facadc Mon Sep 17 00:00:00 2001 From: Federico Imberti <66013290+FI-153@users.noreply.github.com> Date: Mon, 15 Dec 2025 16:13:42 +0100 Subject: [PATCH] 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 --- DynamicIsland/managers/BatteryActivityManager.swift | 8 ++++++++ DynamicIsland/models/Constants.swift | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/DynamicIsland/managers/BatteryActivityManager.swift b/DynamicIsland/managers/BatteryActivityManager.swift index 61b1c66..17bc024 100644 --- a/DynamicIsland/managers/BatteryActivityManager.swift +++ b/DynamicIsland/managers/BatteryActivityManager.swift @@ -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 { stopMonitoring() NotificationCenter.default.removeObserver(self) diff --git a/DynamicIsland/models/Constants.swift b/DynamicIsland/models/Constants.swift index 4d6dc3d..41cd135 100644 --- a/DynamicIsland/models/Constants.swift +++ b/DynamicIsland/models/Constants.swift @@ -483,7 +483,7 @@ extension Defaults.Keys { // MARK: Battery static let showPowerStatusNotifications = Key("showPowerStatusNotifications", default: true) - static let showBatteryIndicator = Key("showBatteryIndicator", default: true) + static let showBatteryIndicator = Key("showBatteryIndicator", default: BatteryActivityManager.shared.hasBattery()) static let showBatteryPercentage = Key("showBatteryPercentage", default: true) static let showPowerStatusIcons = Key("showPowerStatusIcons", default: true) static let playLowBatteryAlertSound = Key("playLowBatteryAlertSound", default: true)