Battery panel module: charge, charging state, time left, low-battery warning
CI / test (push) Failing after 18s
CI / test (push) Failing after 18s
- New `battery` panel module (components/battery, logic in lib/battery): a battery icon filled to
the charge with the percentage and time remaining ("73% 2:05"; time to full while charging).
The fill turns green when charging or full, amber at 20 % and below, and at 10 % the whole
button turns red and a "Battery low" popup opens by itself, once per discharge spell. A click
opens the details (charge, state, time left in words). Without a battery it shows a plug.
- Right-click "Module settings": Show percentage, Show time remaining, and Without a battery
(Show a plug / Show nothing). Saved per module in state.edn like the clocks' settings.
- New `battery` capability (wit/capabilities.wit: read -> status {percent, charge-state,
secs-left}). Served by the wasmer stub (host ints 7-9), host-wasm and wasmhost (publish-battery).
- Host side (compositor/shell/battery.mbt): reads /sys/class/power_supply, every battery
together (energy_* / power_now, or charge_* / current_now; capacity when that is all there
is; device-scope batteries like mice are skipped). The `system` snapshot the Power tab uses
now reads the battery through the same code. Time left follows a running average of the
rate, starting over when the state changes.
- A fresh panel gets the battery module before the clock only on a machine with a battery.
- wasm_stub: MAX_INST 512 -> 2048. The host test binary creates ~70 compositors without
unloading their modules; at 512 the last tests' panels came up half empty.
- Tests: sysfs reading against a fake power supply (energy and charge units, two packs,
charging, full, capacity only, no battery), the running average, the module in the panel
(text, settings, click, the critical warning, the plug), the rendering logic, and a
host-wasm test of the module.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hky51dmaUqiYW746Gz
This commit is contained in:
48 files changed
+2143
-17
No files matched your search
@@ -211,6 +211,10 @@ impl chandra::host::plasmoids::Host for State {
|
||||
sh.suspend_min = suspend;
|
||||
}
|
||||
|
||||
fn publish_battery(&mut self, percent: i32, state: u32, secs_left: u32) {
|
||||
self.pl().shared.borrow_mut().battery = (percent, state, secs_left);
|
||||
}
|
||||
|
||||
fn publish_system(&mut self, s: chandra::host::plasmoids::SystemSnapshot) {
|
||||
use plasmoid::ph::chandra::desktop::system::Snapshot;
|
||||
self.pl().shared.borrow_mut().system = Some(Snapshot {
|
||||
|
||||
@@ -79,6 +79,8 @@ pub struct Shared {
|
||||
pub suspend_min: u32,
|
||||
pub inspector_on: bool,
|
||||
pub system: Option<d::system::Snapshot>,
|
||||
/// The battery (`battery.read`): percent (-1: none), charge-state index, seconds left.
|
||||
pub battery: (i32, u32, u32),
|
||||
/// (id, title, app id, focused)
|
||||
pub windows: Vec<(u64, String, String, bool)>,
|
||||
pub requests: VecDeque<Req>,
|
||||
@@ -387,6 +389,21 @@ impl d::icons::Host for PState {
|
||||
}
|
||||
}
|
||||
|
||||
impl d::battery::Host for PState {
|
||||
fn read(&mut self) -> d::battery::Status {
|
||||
use d::battery::ChargeState as C;
|
||||
let (percent, state, secs_left) = self.shared.borrow().battery;
|
||||
let state = match state {
|
||||
1 => C::Discharging,
|
||||
2 => C::Charging,
|
||||
3 => C::Full,
|
||||
4 => C::NotCharging,
|
||||
_ => C::None,
|
||||
};
|
||||
d::battery::Status { percent: if state == C::None { -1 } else { percent }, state, secs_left }
|
||||
}
|
||||
}
|
||||
|
||||
impl d::system::Host for PState {
|
||||
fn read(&mut self) -> d::system::Snapshot {
|
||||
self.shared.borrow().system.clone().unwrap_or(d::system::Snapshot {
|
||||
@@ -860,4 +877,28 @@ mod tests {
|
||||
assert!(matches!(rt.shared.borrow_mut().requests.pop_front(), Some(Req::PowerAction(1))));
|
||||
rt.unload(h);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn battery_module_shows_charge_time_left_and_warns() {
|
||||
let mut rt = runtime();
|
||||
let l = rt.load(&module("battery")).expect("battery loads");
|
||||
assert!(l.imports.contains(&"battery".to_string()));
|
||||
let h = l.handle;
|
||||
let size = Size { w: 104, h: 34 };
|
||||
// No battery: a plug and no text.
|
||||
assert!(!rt.render(h, size).is_empty());
|
||||
assert!(texts(&rt.render(h, size)).is_empty());
|
||||
rt.shared.borrow_mut().battery = (64, 1, 2 * 3600 + 5 * 60);
|
||||
assert_eq!(texts(&rt.render(h, size)), vec!["64% 2:05".to_string()]);
|
||||
assert!(rt.set_config(h, "time", "off"));
|
||||
assert_eq!(texts(&rt.render(h, size)), vec!["64%".to_string()]);
|
||||
// Critical: the warning opens by itself; a click on it closes it.
|
||||
rt.shared.borrow_mut().battery = (7, 1, 600);
|
||||
rt.render(h, size);
|
||||
let ts = texts(&rt.popup(h).expect("warning open").ops);
|
||||
assert!(ts.contains(&"Battery low: 7%".to_string()));
|
||||
assert!(rt.on_event(h, true, InputEvent::PointerDown(Point { x: 5, y: 5 })));
|
||||
assert!(rt.popup(h).is_none());
|
||||
rt.unload(h);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user