Skip to content

Add a Batteries widget, and battery levels in the Bluetooth widget - #43

Open
masonjmiller wants to merge 1 commit into
kemalandic:mainfrom
masonjmiller:batteries-widget
Open

masonjmiller wants to merge 1 commit into
kemalandic:mainfrom
masonjmiller:batteries-widget

Conversation

@masonjmiller

Copy link
Copy Markdown

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.batteryLevel was always nil.

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

  • Show: Rings (the default) or List, which adds names and a bar each. AirPods show the lower earbud, with both levels in the list when they differ, and their case on its own.
  • Colors: the widget's color, green by default. A battery at 20% or less turns red, and the Mac in Low Power Mode turns yellow, as in the macOS widget. A bolt next to the percentage means it's charging.
  • Order and small tiles: this Mac comes first, then earbuds and their case, headphones, keyboards, trackpads and mice. A tile too small for every ring shows the first ones that fit at a readable size, as the macOS widget's small size shows four.
  • Where the levels come from: BatteryReader reads them, and everything in it is nonisolated and returns Sendable values.
    • This Mac and a UPS come from IOPowerSources, including Low Power Mode.
    • Magic Keyboard, Mouse and Trackpad, and anything else that reports BatteryPercent, come from the I/O Registry, matched with IOPropertyExistsMatch. BatteryStatusFlags isn't documented. 0 and 4 mean on battery and anything else means charging, which is how other battery apps read it.
    • AirPods and other Bluetooth headphones come from system_profiler -json SPBluetoothDataType: device_batteryLevelLeft, Right, Case and Main, with a leading + while charging. Only connected devices are read, since disconnected ones keep their last levels.
    • When the registry and System Information both report an accessory, matched by Bluetooth address, the registry's reading wins. It's read more often and knows when the accessory is charging.
    • iPhone, iPad and Apple Watch levels aren't available to apps, and the guide says so.
  • Service: 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.
  • Bluetooth widget: it takes levels from the same reader, matched by address. The registry is read every sample and System Information once a minute. Trackpads and mice now get their own icons, where the Bluetooth device class filed them as keyboards.
  • Docs: the README lists the widget (28 widgets, System 10), and the in-app guide has a Batteries section.

Choices worth a look

  • system_profiler as 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.
  • No private frameworks. That is why iPhone, iPad and Apple Watch aren't included.

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 Batteries widget with three green rings: a laptop at 100%, a keyboard at 66% and a trackpad at 52%

The rest use sample readings, to show AirPods, charging and a low battery:

Six rings: a charging Mac at 100%, AirPods at 75%, a charging AirPods case at 45%, a keyboard at 68%, a trackpad at 14% in red, and a mouse at 52%

The list style:

The list style: each device's icon, name, a bar and its percentage. AirPods show L 80% · R 75% under the name, and the trackpad's bar is red

A 2×1 tile, showing the first four that fit:

A 2×1 tile with four rings: the Mac, AirPods, the AirPods case and the keyboard

Testing

  • Scripts/ci-local.sh all passes: swift-format lint, zero compiler warnings, and 408 Swift Testing tests in 41 suites. 21 are new, in four suites in Tests/EdgeControlTests/Batteries/:

    • Reading: power sources, registry entries (including the status flags and renamed accessories), and System Information's report (AirPods earbud by earbud and their case, other headphones, and disconnected devices left out).
    • Merging: the registry's reading winning, the order, and levels by address for the Bluetooth widget.
    • Ring layout: what fits, and what a small tile shows.
    • The Bluetooth widget's icons.

    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

  • Tested on macOS 14+ (macOS 27.2)
  • No third-party dependencies added
  • Follows existing code style

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant