Skip to content

Decide the charge limit from the threshold, not the charge level - #10502

Closed
brightwalker25 wants to merge 1 commit into
omacom:quattrofrom
brightwalker25:fix-charge-threshold-predicate
Closed

brightwalker25 wants to merge 1 commit into
omacom:quattrofrom
brightwalker25:fix-charge-threshold-predicate

Conversation

@brightwalker25

Copy link
Copy Markdown

Fixes #10344 (the panel half), #7803 (the panel half), #10193.

The problem

Model.chargeThresholdActive() decides whether a charge limit is holding the battery back, and it drives the Charge limit / Holding pair in Panel.qml, the Threshold mode label, and the battery icon. It never reads a threshold. It infers one from the state of charge:

if (d.state === s.PendingCharge) return true
if (d.state === s.FullyCharged && fraction < 0.99) return true
if (d.state !== s.Charging || fraction >= 0.99) return false
return Number(d.changeRate || 0) <= 0.2 || Number(d.timeToFull || 0) >= 8 * 60 * 60

It infers because there is nothing to read: Quickshell.Services.UPower publishes no charge-threshold property at all. But inference cannot separate a configured limit from the other reasons a battery sits on AC without charging, and it gets three distinct cases wrong. None of them are edge cases.

A cap below a nearly full battery reads as no cap (#10344). Lowering a cap stops the charge but does not discharge the pack, so the state is FullyCharged at 99% while the firmware holds at 60. Line 61 returns false and the panel shows FULLY CHARGED with no charge limit row. This is the ordinary state for days on a machine that stays on mains.

A dead battery reads as a cap (#7803). PendingCharge only means AC is present and the EC is not charging, for any reason: a threshold hold, a battery fault, an insufficient supply, charge_behaviour=inhibit-charge. Returning true on it unconditionally reports a hold for a pack at 0% that the EC has given up on.

Hardware with no charge control reads as a cap (#10193). Apple's SMC leaves a battery alone until it falls to roughly 93%, which arrives as FullyCharged below 0.99 and renders a charge-limit row on a machine with no charge_control_end_threshold to show.

The fix

bin/omarchy-battery-status already reads the threshold from the hardware, already emits it as threshold, and already gates its own charge_holding block on that value existing. Panel.qml already displays that value on line 445. The panel simply dropped the guard half and re-derived the answer instead, which is why the CLI and the panel disagree on the same machine, as #10193 sets out.

So take the value the CLI already provides and decide from it:

function chargeThresholdActive(device, onBattery, thresholdEnd) {
  var d = device || {}
  if (!(d && d.isPresent && !onBattery)) return false

  var limit = Number(thresholdEnd)
  if (!isFinite(limit) || limit <= 0 || limit >= 100) return false

  return Math.round(batteryFraction(d) * 100) >= limit
}

A limit is in force when the hardware exposes one, it is set below full, and the charge has reached it. parseThresholdEnd() takes the end value out of the threshold field, which is 80% for a single value and 75-80% for a start-end pair, and returns NaN when the battery exposes no charge-control interface, since that is a meaningful answer rather than a missing one. A limit of 100 is the sysfs way of saying charge to full, so it is not a limit either.

Every state-based branch goes away. states is no longer a parameter of the predicate; batteryIcon() and modeLabel() still take it and now also forward the limit.

Scope

This does not change where the threshold value comes from. On hardware whose UPower hwdb CHARGE_LIMIT default masks the real sysfs value, the number displayed is still UPower's. #9612 fixes that half by preferring sysfs in omarchy-battery-status, and the two changes compose without touching the same code: this PR makes the panel ask the right question, #9612 makes the CLI give the right answer. On the ASUS machine in #10344 the panel goes from showing no limit at all to showing 75-80%, and lands on the true 60% once #9612 is in.

It also leaves omarchy-battery-status's own pending-charge branch alone, which is the CLI half of #7803 and is covered by #7915/#7895.

One behavioural note: the bar icon reads the same predicate, and the full refresh only runs while the panel is open, so the battery status is now also read once on startup. Without it the icon would sit on an unset limit until the panel was first opened.

Tests

test/shell.d/power-test.sh covers the three reported cases directly, plus the threshold parsing, a limit the battery is still charging toward, a limit of 100, and the icon while a limit is holding. The two assertions that encoded the old heuristic are replaced by ones that supply a real limit.

223 of the 230 tests in test/shell.d/ pass. The 5 failures (base, bin-style, config, snapper, unowned-system-paths) fail identically on an untouched quattro.

I do not have the ThinkPad from #7803 or the MacBook from #10193, so those two are covered by the unit tests and by the reported readings rather than on the hardware. #10344 is my own machine.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VADsy8CLE8n9hpfWUKt3vM

chargeThresholdActive() inferred whether a charge limit was in force from
the battery's state and level, because UPower publishes no charge-threshold
property for the panel to read. Inference cannot separate a configured limit
from the other reasons a battery sits on AC without charging, and it got three
distinct cases wrong:

- A cap lowered below a nearly full battery read as no cap at all. Lowering a
  cap stops the charge but does not discharge the pack, so the state is
  FullyCharged at 99% and `fraction >= 0.99` returned false while the firmware
  was holding at 60. This is the normal state for days on mains (#10344).
- A dead battery read as a cap. PendingCharge only means AC is present and the
  EC is not charging, for any reason; returning true on it unconditionally
  reported "Holding at 75-80%" for a pack at 0% (#7803).
- Hardware with no charge control read as a cap. Apple's SMC leaves a battery
  alone until it falls to roughly 93%, which arrives as FullyCharged below
  0.99 and rendered a charge-limit row on a machine with no
  charge_control_end_threshold at all (#10193).

omarchy-battery-status already reads the threshold from the hardware and emits
it, and already gates its own charge_holding block on that value existing.
The panel dropped that half of the guard and re-derived the answer instead,
which is why the two disagree on the same machine. Take the value the CLI
already provides and decide from it: a limit is in force when the hardware
exposes one, it is set below full, and the charge has reached it.

The bar icon reads the same predicate, so the battery status is now also read
once on startup rather than only while the panel is open, otherwise the icon
would sit on an unset limit until the panel was first opened.

This does not touch where the threshold value comes from. On hardware whose
UPower hwdb default masks the real sysfs value the number shown is still
UPower's; #9612 fixes that half, and the two changes compose.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VADsy8CLE8n9hpfWUKt3vM
@magate

magate commented Sep 7, 2026

Copy link
Copy Markdown

Hardware verification of 6e18debd4a2d222c95d6b47d57e9d184cdf2c45c on a MacBookPro12,1: this fixes the false charge-limit display on this machine.

Environment: Omarchy 4.0.2-1, Quickshell 0.3.1-1, UPower 1.91.3-1. AC connected, UPower reports fully-charged at approximately 98.6%, neither sysfs charge-control threshold file exists, and omarchy-battery-status --shell emits no threshold field.

Test method: temporarily loaded the exact, unmodified Model.js and Panel.qml from this commit through a local clone of the power widget, then restarted the shell. The remaining desktop and battery-status script stayed on the installed version. Restored the previous local widget after testing.

  • Before this fix, the stock widget showed THRESHOLD, Charge limit: -, and Battery state: Holding, with the non-charging battery icon.
  • With this PR, the bar showed the full battery/lightning icon before opening the panel. The open panel showed FULLY CHARGED, the same icon, and 98%, without the false charge-limit/holding rows. Visually inspected in the running desktop.
  • The commit's test/shell.d/power-test.sh passed all 37 assertions. This was a focused test, not test/all or the graphical acceptance suite.

Two follow-up observations from isolated model checks, distinct from the hardware verification above:

  1. A PendingCharge battery at 77% with a 75-80% start/stop range returns false from chargeThresholdActive(..., false, parseThresholdEnd('75-80%')). Could this mislabel a battery still holding while it drifts down toward its restart threshold? This machine cannot exercise configured charge limits, so this is a simulated case for review, not a hardware-confirmed failure.
  2. modeLabel() still returns Charging for FullyCharged at 98.6% with no threshold. The visible hero label is correct because Panel.qml handles fullyCharged separately. An assertion for the exact helper label, rather than merely !== 'Threshold', would expose this inconsistency.

No live unplug/replug, configured-limit, or failed-battery hardware testing was performed. This report was prepared with AI assistance; no screenshots or diagnostic logs are attached.

@bjarneo

bjarneo commented Sep 21, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #10196, which covers the same charge threshold gate. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 21, 2026
@brightwalker25
brightwalker25 deleted the fix-charge-threshold-predicate branch September 25, 2026 17:34
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.

Battery panel ignores charge_control_end_threshold and misreports the charge limit

3 participants