Files
chandra/components/battery/gen/interface/chandra/desktop/plasmoid/stub.mbt
T
Claude 88915842c2
CI / test (push) Failing after 18s
Battery panel module: charge, charging state, time left, low-battery warning
- 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
2026-11-25 19:53:42 +00:00

83 lines
2.0 KiB
MoonBit

///| Implementation of `chandra:desktop/plasmoid` for the battery module: charge as an icon and
/// percentage, charging state and time remaining, turning amber when low and red when critical.
/// A click opens the details; a critical battery opens them by itself once per discharge spell.
/// All drawing is in lib/battery; this file only adapts the `battery` capability.
///|
let open_ : Ref[Bool] = { val: false }
///| The open popup is the low-battery warning (not a click).
let warning : Ref[Bool] = { val: false }
///|
let low : @logic.LowWarning = @logic.LowWarning::new()
///| The host's reading, sampled about once a second.
fn reading() -> @logic.Reading {
let s = @battery.read()
{ percent: s.percent, state: s.state.ordinal(), secs_left: s.secs_left.reinterpret_as_int() }
}
///|
pub fn info() -> PlasmoidInfo {
let (id, name, w, h, min_w) = @logic.info()
{
id,
name,
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
min_width: min_w.reinterpret_as_uint(),
stretch: false,
}
}
///|
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
let r = reading()
if low.update(r) {
open_.val = true
warning.val = true
}
@logic.render(size, @theme.get_tokens(), r, @logic.config, open=open_.val && !warning.val)
}
///|
pub fn popup() -> PopupSurface? {
if !open_.val {
return None
}
let (w, h, ops) = @logic.render_popup(@theme.get_tokens(), reading(), warning=warning.val)
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
}
///|
fn close() -> Unit {
open_.val = false
warning.val = false
}
///|
pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
match (target, event) {
(WIDGET, PointerDown(_)) => {
if open_.val {
close()
} else {
open_.val = true
}
true
}
(POPUP, PointerDown(_)) => {
close()
true
}
(_, FocusLost) | (_, KeyDown("Escape")) =>
if open_.val {
close()
true
} else {
false
}
_ => false
}
}