Update battery icon promptly on power changes

This commit is contained in:
David Heinemeier Hansson
2026-06-29 10:49:43 -05:00
parent f67ed326df
commit 924254ed11
5 changed files with 60 additions and 9 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/bin/bash
set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
write_supply() {
local name="$1"
local type="$2"
local online="$3"
mkdir -p "$tmp_dir/$name"
printf '%s\n' "$type" >"$tmp_dir/$name/type"
printf '%s\n' "$online" >"$tmp_dir/$name/online"
}
write_supply AC Mains 0
write_supply USBC USB 1
OMARCHY_POWER_SUPPLY_PATH="$tmp_dir" "$ROOT/bin/omarchy-power-present" ||
fail "power present detects online USB-C supply"
pass "power present detects online USB-C supply"
printf '0\n' >"$tmp_dir/USBC/online"
if OMARCHY_POWER_SUPPLY_PATH="$tmp_dir" "$ROOT/bin/omarchy-power-present"; then
fail "power present rejects offline supplies"
fi
pass "power present rejects offline supplies"
write_supply AC Mains 1
OMARCHY_POWER_SUPPLY_PATH="$tmp_dir" "$ROOT/bin/omarchy-power-present" ||
fail "power present detects online mains supply"
pass "power present detects online mains supply"
+11 -1
View File
@@ -27,6 +27,16 @@ assert(!power.chargeThresholdActive({ isPresent: true, percentage: 0.8, state: s
assert(!power.chargeThresholdActive({ isPresent: true, percentage: 0.5, state: states.Discharging }, false, states), 'power does not flag discharging as threshold')
assertEqual(power.modeLabel({ isPresent: true, percentage: 1, state: states.FullyCharged }, false, states), 'Fully charged', 'power labels full battery')
assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, true, states), 'On battery', 'power labels battery mode')
assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, false, states), 'On battery', 'power labels discharging battery mode')
assertEqual(power.modeLabel({ isPresent: true, percentage: 0.5, state: states.Discharging }, false, states), 'Charging', 'power treats external power as newer than stale discharging state')
assert(power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, false, states).length > 0, 'power maps battery icons')
assertEqual(
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Discharging }, false, states),
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging, changeRate: 1.0, timeToFull: 120 }, false, states),
'power shows charging icon when external power is present before battery state refreshes'
)
assertEqual(
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Charging }, true, states),
power.batteryIcon({ isPresent: true, percentage: 0.4, state: states.Discharging }, true, states),
'power shows battery icon when unplugged before battery state refreshes'
)
JS