Repository navigation
Add a Batteries widget, and battery levels in the Bluetooth widget - #43
Open
masonjmiller wants to merge 1 commit into
Open
masonjmiller wants to merge 1 commit into
masonjmiller wants to merge 1 commit into
Conversation
Like the macOS Batteries widget: a ring per battery round its device's icon, with the percentage, a bolt while charging, red at 20% or less and yellow in Low Power Mode, or a list with names and bars. A small tile shows the first batteries that fit, as the macOS widget's small size does. BatteryReader gets levels from where macOS keeps them: power sources for this Mac and a UPS, the I/O Registry's BatteryPercent for Magic keyboards, mice and trackpads (and other accessories that report it), and System Information's Bluetooth report for AirPods, earbud by earbud and their case. iPhone, iPad and Apple Watch levels aren't available to apps. The Bluetooth widget said it showed battery levels but always had nil; it now takes them from the same reader, matched by Bluetooth address, and gives trackpads and mice their own icons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds Batteries, a System widget like the macOS Batteries widget. It covers this Mac, AirPods, and Magic Keyboard, Mouse and Trackpad, each shown as a ring round its device's icon with the percentage underneath.
It also makes the Bluetooth widget's battery levels work. The widget's description says it shows them, but
BTDevice.batteryLevelwas alwaysnil.Why
The XENEON EDGE sits right by the keyboard and trackpad, and macOS shows their levels only in Notification Center or the menu bar.
How
BatteryReaderreads them, and everything in it is nonisolated and returns Sendable values.BatteryPercent, come from the I/O Registry, matched withIOPropertyExistsMatch.BatteryStatusFlagsisn't documented. 0 and 4 mean on battery and anything else means charging, which is how other battery apps read it.system_profiler -json SPBluetoothDataType:device_batteryLevelLeft,Right,CaseandMain, with a leading+while charging. Only connected devices are read, since disconnected ones keep their last levels.BatteryService(ServiceKey.batteries) runs only while a Batteries widget is placed, on its own utility queue. It reads power sources every 5 s, so plugging in shows straight away, and accessories every 30 s. It publishes only when something changed.Choices worth a look
system_profileras a subprocess. AirPods' levels appear only there, unless you use private frameworks. One read takes about 0.2 s. It runs every 30 s for Batteries and every 60 s for Bluetooth, and only while one of those widgets is placed.Screenshots
These are rendered off-screen from the widget's own view at its default 5×2. The first uses this Mac's real readings: a MacBook, a Magic Keyboard and a Magic Trackpad.
The rest use sample readings, to show AirPods, charging and a low battery:
The list style:
A 2×1 tile, showing the first four that fit:
Testing
Scripts/ci-local.sh allpasses: swift-format lint, zero compiler warnings, and 408 Swift Testing tests in 41 suites. 21 are new, in four suites inTests/EdgeControlTests/Batteries/:The fixtures are synthetic, with made-up names and addresses.
On a MacBook (Mac17,8, macOS 27.2), the service reads this Mac, a Magic Keyboard and a Magic Trackpad. The Bluetooth widget shows the keyboard's and the trackpad's levels.
Not yet checked on real hardware: AirPods (none were connected while testing) and Logitech MX devices.
Checklist
🤖 Generated with Claude Code