Skip to content

feat(battery): show charge/discharge rate in the bar widget - #3210

Open
SpyrosPsarras wants to merge 1 commit into
AvengeMedia:masterfrom
SpyrosPsarras:feat/battery-power-draw
Open

feat(battery): show charge/discharge rate in the bar widget#3210
SpyrosPsarras wants to merge 1 commit into
AvengeMedia:masterfrom
SpyrosPsarras:feat/battery-power-draw

Conversation

@SpyrosPsarras

Copy link
Copy Markdown

Description

The battery popout already shows the UPower energy rate, but the bar widget could only show percentage and remaining time. Telling how fast the battery is charging or draining meant opening the popout, or running a third-party plugin that shells out to /sys/class/power_supply/BAT*/power_now on a timer.

This adds two independent, opt-in toggles to the battery widget — Show Charge Rate and Show Discharge Rate — which render the signed rate alongside the existing text.

Charging charging
Discharging (with percentage + remaining time also enabled) discharging

Design notes

  • No new process or polling. The value is BatteryService.changeRate, the same aggregate the popout already renders.
  • Costs no bar space unless it has something to say. Below 0.05W — idle, fully charged, or parked at a charge limit — the text is empty and the widget collapses back to its previous width.
  • Stays narrow. One decimal below 10W (-8.4W), whole watts above (+28W); vertical bars get a whole-watt line so the value fits the bar thickness.
  • Two toggles instead of one + an "Only on Battery" sub-row. Charging and discharging are genuinely separate interests (charger throughput vs. idle drain), and an "only on battery" modifier is meaningless for a discharge readout. This also keeps both labels inside the 270px menu without eliding.
  • Follows the existing widget-setting pattern: a global default in SettingsData/SettingsSpec.js plus a per-widget-instance override plumbed through Lists.qml / WidgetsTab.qml / WidgetsTabSection.qml. Both default to false, so nothing changes for existing users until they opt in.

The string composition in Battery.qml was refactored from stacked early-returns to a parts array so a third element could be added without a combinatorial explosion; the output is byte-identical to master for every existing combination of percentage/time toggles.

BatteryService gains two small reusable pieces: signedChangeRate (positive charging, negative draining, zero idle) and formatPowerRate(compact). Happy to follow up by pointing BatteryPopout at them too — it currently derives the sign from isCharging || isPluggedIn, which mislabels a plugged-in-but-draining machine as +. Left out here to keep the diff focused.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that changes existing behavior)
  • Refactor / internal cleanup
  • Documentation
  • Other

Related issues

None.

Screenshots / video

Above. Both captured on a live session: charging at 28.3W reported by upower renders +28W; discharging at 15.4W renders -15W.

Checklist

  • My code follows the conventions in CONTRIBUTING.md
  • I have tested my changes locally
  • New user-facing strings are wrapped in I18n.tr() with translator context, reusing existing terms where possible
  • Go changes: ran make fmt, added/updated tests, make test passes, and go mod tidy is clean — n/a, no Go changes
  • QML changes: ran make lint-qml with no new warnings
  • I have opened a corresponding pull request in dlx-docs to document any new behaviors

Testing done: ran the branch against a live niri session and exercised charging, discharging, and the charge-limit/idle case (verified the readout disappears at 0W rather than showing +0W); toggled both settings from the widget context menu and confirmed the per-widget override persists; make lint-qml passes; check_term_variants.py and extract_settings_index.py are clean (the new strings live in a widget context menu, so the settings search index is unchanged); catalogs left alone per CONTRIBUTING.

On the last checkbox — glad to open a dlx-docs PR for the two new toggles if you'd like them documented; just say where they belong.

The battery popout already surfaces the UPower energy rate, but the bar
widget could only show percentage and remaining time, so telling how fast
the battery is charging or draining meant opening the popout (or running a
third-party plugin that polls /sys/class/power_supply).

Add two independent, opt-in toggles to the battery widget - "Show Charge
Rate" and "Show Discharge Rate" - that render the signed rate next to the
existing text, e.g. "+28W" while charging and "77% (2h 11m) -19W" while
draining. Both default to off, and follow the established pattern: a global
default in SettingsData plus a per-widget-instance override.

The value comes from BatteryService.changeRate, so there is no extra process
or polling. It hides itself when the rate is below 0.05W (idle, fully
charged, or sitting at a charge limit) so it costs no bar space unless it
has something to say. Precision adapts to keep the pill narrow: one decimal
below 10W, whole watts above; vertical bars get a whole-watt line.
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