Skip to content

feat: show the battery's charge, power and health in its icon, with the percentage in a tooltip - #238

Merged
M4n0x merged 1 commit into
mainfrom
feat/237-battery-icon
Oct 22, 2026
Merged

M4n0x merged 1 commit into
mainfrom
feat/237-battery-icon

Conversation

@M4n0x

@M4n0x M4n0x commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Problem

The battery widget always draws a full battery, with a bolt while charging, next to its percentage. It can't show how full the battery is, that the Mac is on its adapter without charging, or that macOS recommends servicing the battery. Fixes #237.

Changes

  • The battery. BatteryGlyph.swift draws a battery like the macOS menu bar's, sized by iconSize and drawn in the text color:

    • the fill follows the charge and turns red at 10 % or less on battery power;
    • a bolt shows it charging, and a plug shows it on the adapter without charging;
    • an orange badge appears when macOS recommends service.

    A new battery layout block draws it ({"type":"battery"}, with an optional source), so custom layouts, the Display choices and popups can use it. The block is added to the schema's block types.

  • The provider. PowerMonitor.output(battery:) turns IOKit's description into the output, which makes it testable. It adds:

    • a tooltip: 62%, 3:20 left, 45%, charging, full in 1:05, 80%, plugged in, not charging or 100%, charged, with Service recommended on a second line when it applies;
    • fields.health (normal or service);
    • an icon that follows the charge in quarters.
  • Battery health. Health is service when BatteryHealthCondition is set, or when BatteryHealth is Poor. On the MacBook this was built on, BatteryHealth reads Check Battery while System Settings shows Condition Normal and 83 %, so that key alone would warn wrongly. System Settings' capacity figure comes from a private call that returns kIOReturnNotPrivileged.

  • Default layout and Display. A battery widget without a layout draws only the battery. A Mac without a battery still shows the plug and AC. Icon only is battery's first Display choice and maps to the default, and every battery choice uses the battery block as its icon. A Display layout saved earlier, with a symbol icon, shows as Custom layout.

  • Samples.

    • The theme cards, the setup demo and the package list preview draw the battery instead of a symbol and a percentage.
    • The docs images give the battery widget a stand-in battery source instead of a command provider, so CI redraws them with the new default.
  • Docs.

    • popups.md documents the block.
    • providers.md documents the new fields.
    • create-a-widget.md covers the default look and the Display choices.
    • themes.md updates the theme card sample.

Surfaces:

  • Decoding: a new block type, and no new properties.
  • Layers: nothing new to configure.
  • Editing: the Display choices.
  • Docs: the four pages above.
  • Examples: the presentation example's battery is sample text, so it is unchanged.

Testing

  • swift test on this branch: 276 XCTest and 404 Swift Testing tests pass.
  • The new BatteryTests cover:
    • tooltips and icons while discharging, charging, plugged in, full and estimating;
    • health, including the Check Battery reading with an empty condition;
    • block decoding;
    • the default layout, also on a Mac without a battery;
    • the Display choices;
    • the drawn pixels: the fill grows with the charge, it turns red only when low on battery power, the badge appears only for service, and power changes the drawing but not its size.
  • BarView renders offscreen in five looks, light and dark, over dark and light backdrops:
    • the built-in look;
    • an opaque strip;
    • a translucent tile theme;
    • Rosé Pine;
    • a black strip with 16 pt icons and 15 pt text.
  • Not checked:
    • the tooltip on a running bar;
    • a battery that macOS really flags for service, which was only simulated;
    • a desktop Mac, which is covered by unit tests only.
  • With Xcode 26.3, main doesn't compile locally because BarView.body takes too long to type-check. CI's Xcode builds it. I ran these tests with that fix applied; it is in its own PR.

Before/after screenshots follow in a comment.

Merging

A merge commit or rebase merge, so the feat commit reaches the changelog.

Claude Opus 5.5 via Claude Code

…he percentage in a tooltip

The battery widget draws a battery like the macOS menu bar's: its fill
follows the charge and turns red at 10 % or less on battery power, a bolt
shows it charging and a plug shows the Mac on its adapter without
charging. An orange badge marks a battery macOS recommends servicing.
The percentage moves to the tooltip, with the time left or until full.

A new `battery` layout block draws it, so custom layouts, the Display
choices and popups can use it. The provider adds `fields.health` and a
`tooltip`, and its `icon` follows the charge in quarters.

Health comes from `BatteryHealthCondition`, or a `BatteryHealth` of Poor:
`BatteryHealth` can read "Check Battery" while System Settings shows the
condition as Normal.

Fixes #237
@M4n0x
M4n0x requested a review from Seb-GRAF as a code owner October 22, 2026 13:26
@M4n0x
M4n0x merged commit 29d50f4 into main Oct 22, 2026
7 checks passed
@M4n0x
M4n0x deleted the feat/237-battery-icon branch October 22, 2026 13:43
@stablebar-releases

Copy link
Copy Markdown

🎉 This PR is included in version 0.6.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show the battery's charge, power and health in its icon, with the percentage in a tooltip

1 participant