Files
nandi 905dab8725
CI / test (push) Failing after 52s
Add a built-in on-screen keyboard for handset sessions
- Opens for text-input-v3 fields, terminals, launcher search, Network password, emoji picker and the password dialog; a KB pill summons it manually.
- Enabled on CHASSIS=handset; CHANDRA_OSK=1|0 overrides, and it stays off under CHANDRA_TEST_CONFIG.
- Work area and panel popups shrink above the keyboard so focused content stays visible.
- Keys send evdev codes with Shift, Ctrl and Alt modifiers.
- chandra-ctl: `screenshot [PATH]` saves a PNG without opening the editor.
- Key names cover the full punctuation set and shifted symbols.
- Launcher plasmoid min width drops from 60 to 36.
2026-11-28 00:18:32 -08:00

365 lines
11 KiB
MoonBit

///| Render logic for the battery panel module (pure: tokens + a reading in, ops out).
///
/// The module (components/battery) only adapts the `battery` capability to `Reading` and keeps
/// one `BatteryConfig` and one `LowWarning`; everything it draws is decided here.
///| What the host reported: the `battery` capability's `status`, with the charge state as its
/// index (STATE_*).
#derive(Eq, Debug)
pub(all) struct Reading {
/// 0..100, or -1 without a battery.
percent : Int
state : Int
/// Estimated seconds until empty (discharging) or full (charging); 0 unknown.
secs_left : Int
}
///| Charge states, in the order of the `charge-state` enum.
pub let state_none : Int = 0
///|
pub let state_discharging : Int = 1
///|
pub let state_charging : Int = 2
///|
pub let state_full : Int = 3
///|
pub let state_not_charging : Int = 4
///| Below this (discharging) the icon turns amber.
pub let low_percent : Int = 20
///| At or below this (discharging) the module turns red and opens its low-battery warning.
pub let critical_percent : Int = 10
///|
pub fn info() -> (String, String, Int, Int, Int) {
// id, name, preferred w, preferred h, min w
("org.chandra.battery", "Battery", 76, 34, 34)
}
///|
pub fn Reading::has_battery(self : Reading) -> Bool {
self.percent >= 0 && self.state != state_none
}
///| 0 normal, 1 low, 2 critical. Only a discharging battery is ever low.
pub fn Reading::level(self : Reading) -> Int {
if !self.has_battery() || self.state != state_discharging {
0
} else if self.percent <= critical_percent {
2
} else if self.percent <= low_percent {
1
} else {
0
}
}
///| "2:05" (hours:minutes), rounded down to the minute; "" when unknown.
pub fn short_time(secs : Int) -> String {
if secs <= 0 {
return ""
}
let m = secs / 60
let mm = m % 60
(m / 60).to_string() + ":" + (if mm < 10 { "0" } else { "" }) + mm.to_string()
}
///| "2 h 05 min" / "35 min"; "" when unknown.
pub fn long_time(secs : Int) -> String {
if secs <= 0 {
return ""
}
let m = secs / 60
if m < 60 {
return m.to_string() + " min"
}
let mm = m % 60
(m / 60).to_string() + " h " + (if mm < 10 { "0" } else { "" }) + mm.to_string() + " min"
}
///| One line describing the state: "Discharging, 2 h 05 min left".
pub fn Reading::describe(self : Reading) -> String {
if !self.has_battery() {
return "No battery: on mains power"
}
let t = long_time(self.secs_left)
match self.state {
1 => if t == "" { "Discharging" } else { "Discharging, " + t + " left" }
2 => if t == "" { "Charging" } else { "Charging, full in " + t }
3 => "Fully charged, plugged in"
4 => "Plugged in, not charging"
_ => ""
}
}
// ---------------------------------------------------------------- settings
///| One item of the right-click "Module settings" menu (the `module-config` record).
#derive(Eq, Debug)
pub(all) struct ConfigEntry {
key : String
label : String
choices : Array[String]
value : String
}
///| What the user picked for the module.
pub(all) struct BatteryConfig {
mut show_percent : Bool
mut show_time : Bool
/// Without a battery: draw a plug (true) or nothing.
mut plug_without_battery : Bool
}
///|
pub fn BatteryConfig::new() -> BatteryConfig {
{ show_percent: true, show_time: true, plug_without_battery: true }
}
///|
fn on_off(b : Bool) -> String {
if b { "on" } else { "off" }
}
///|
pub fn BatteryConfig::items(self : BatteryConfig) -> Array[ConfigEntry] {
[
{ key: "percent", label: "Show percentage", choices: [], value: on_off(self.show_percent) },
{ key: "time", label: "Show time remaining", choices: [], value: on_off(self.show_time) },
{
key: "no-battery",
label: "Without a battery",
choices: ["Show a plug", "Show nothing"],
value: if self.plug_without_battery { "Show a plug" } else { "Show nothing" },
},
]
}
///| Apply one setting; true when it changed something. Unknown keys and values are ignored.
pub fn BatteryConfig::set(self : BatteryConfig, key : String, value : String) -> Bool {
let flag = fn(cur : Bool, set : (Bool) -> Unit) -> Bool {
match value {
"on" if !cur => { set(true); true }
"off" if cur => { set(false); true }
_ => false
}
}
match key {
"percent" => flag(self.show_percent, fn(b) { self.show_percent = b })
"time" => flag(self.show_time, fn(b) { self.show_time = b })
"no-battery" =>
match value {
"Show a plug" if !self.plug_without_battery => { self.plug_without_battery = true; true }
"Show nothing" if self.plug_without_battery => { self.plug_without_battery = false; true }
_ => false
}
_ => false
}
}
///| The module's settings (one module instance per wasm instance).
pub let config : BatteryConfig = BatteryConfig::new()
// ---------------------------------------------------------------- low-battery warning
///| Opens the warning once per discharge spell when the charge reaches `critical_percent`; it
/// arms again after the machine is plugged in (or charges above it).
pub(all) struct LowWarning {
mut fired : Bool
}
///|
pub fn LowWarning::new() -> LowWarning {
{ fired: false }
}
///| Feed a reading; true when the warning should open now.
pub fn LowWarning::update(self : LowWarning, r : Reading) -> Bool {
if r.level() == 2 {
if self.fired {
false
} else {
self.fired = true
true
}
} else {
self.fired = false
false
}
}
// ---------------------------------------------------------------- drawing
///|
let red : @types.Color = @ui.color(235, 64, 52, 255)
///|
let amber : @types.Color = @ui.color(240, 165, 30, 255)
///|
let green : @types.Color = @ui.color(70, 185, 90, 255)
///| Icon width (body plus the terminal nub).
let icon_w : Int = 24
///| A horizontal battery at (x, cy): outline, nub, fill for the charge, and a bolt when charging.
fn battery_icon(x : Int, cy : Int, r : Reading, fg : @types.Color, fill : @types.Color) -> Array[@types.DrawOp] {
let bw = 21
let bh = 12
let y = cy - bh / 2
let ops = [
@ui.stroke_rect(@ui.rect(x, y, bw, bh), fg, width=1, radius=2),
@ui.fill_rect(@ui.rect(x + bw, cy - 3, 3, 6), fg, radius=1),
]
let inner = bw - 4
let pct = if r.percent < 0 { 0 } else if r.percent > 100 { 100 } else { r.percent }
let fw = if pct == 0 { 0 } else if inner * pct / 100 < 1 { 1 } else { inner * pct / 100 }
if fw > 0 {
ops.push(@ui.fill_rect(@ui.rect(x + 2, y + 2, fw, bh - 4), fill, radius=1))
}
if r.state == state_charging {
// A bolt over the middle, outlined in the panel colour so it shows on the fill.
let mx = x + bw / 2
ops.push(@ui.line(mx + 2, y - 1, mx - 3, cy + 1, fg, width=2))
ops.push(@ui.line(mx - 3, cy + 1, mx + 3, cy - 1, fg, width=2))
ops.push(@ui.line(mx + 3, cy - 1, mx - 2, y + bh + 1, fg, width=2))
}
ops
}
///| A mains plug at (x, cy): two prongs, the head, and the cable.
fn plug_icon(x : Int, cy : Int, fg : @types.Color) -> Array[@types.DrawOp] {
[
@ui.line(x + 6, cy - 8, x + 6, cy - 4, fg, width=2),
@ui.line(x + 12, cy - 8, x + 12, cy - 4, fg, width=2),
@ui.fill_rect(@ui.rect(x + 3, cy - 4, 13, 7), fg, radius=2),
@ui.line(x + 9, cy + 3, x + 9, cy + 8, fg, width=2),
]
}
///| The text after the icon: "73%", "73% 2:05", "2:05" or "".
pub fn panel_text(r : Reading, c : BatteryConfig) -> String {
if !r.has_battery() {
return ""
}
let parts : Array[String] = []
if c.show_percent {
parts.push(r.percent.to_string() + "%")
}
if c.show_time && (r.state == state_discharging || r.state == state_charging) {
let t = short_time(r.secs_left)
if t != "" {
parts.push(t)
}
}
parts.join(" ")
}
///| The panel button. Critical: a red chip with white content; open popup: the accent chip.
pub fn render(
size : @types.Size,
t : @theme.Tokens,
r : Reading,
c : BatteryConfig,
open? : Bool = false,
) -> Array[@types.DrawOp] {
let w = size.w.reinterpret_as_int()
let h = size.h.reinterpret_as_int()
let fs = t.font_size.reinterpret_as_int() - 2
let radius = t.radius.reinterpret_as_int()
let p = t.palette
let ops : Array[@types.DrawOp] = []
if !r.has_battery() && !c.plug_without_battery {
return ops
}
let level = r.level()
let fg = if level == 2 {
@ui.color(255, 255, 255, 255)
} else if open {
p.accent_foreground
} else {
p.foreground
}
if level == 2 {
ops.push(@ui.fill_rect(@ui.rect(0, 0, w, h), red, radius~))
} else if open {
ops.push(@ui.fill_rect(@ui.rect(0, 0, w, h), p.accent, radius~))
}
let cy = h / 2
if !r.has_battery() {
ops.append(plug_icon(w / 2 - 9, cy, fg))
return ops
}
let fill = if level == 2 {
fg
} else if level == 1 {
amber
} else if r.state == state_charging || r.state == state_full {
green
} else {
fg
}
// Text only when it fits; the icon and text are centred together.
let txt = panel_text(r, c)
let tw = if txt == "" { 0 } else { @ui.text_width(txt, fs) }
let show_txt = tw > 0 && icon_w + 6 + tw + 8 <= w
let total = if show_txt { icon_w + 6 + tw } else { icon_w }
let x0 = (w - total) / 2
ops.append(battery_icon(x0, cy, r, fg, fill))
if show_txt {
ops.push(@ui.text(x0 + icon_w + 6, cy, txt, fs, fg))
}
ops
}
///| Popup width; rows are `popup_row_h` tall.
pub let popup_w : Int = 260
///|
let popup_row_h : Int = 24
///| The details popup (a click on the module, or the low-battery warning): the charge in large
/// type, the state with time left, and on a critical battery a line asking to plug in.
pub fn render_popup(t : @theme.Tokens, r : Reading, warning? : Bool = false) -> (Int, Int, Array[@types.DrawOp]) {
let fs = t.font_size.reinterpret_as_int()
let radius = t.radius.reinterpret_as_int()
let p = t.palette
let lines : Array[(String, Int, @types.Color, Bool)] = [] // text, size, colour, bold
if !r.has_battery() {
lines.push(("Power", fs, p.foreground, true))
lines.push((r.describe(), fs - 2, p.foreground_muted, false))
} else {
let critical = r.level() == 2
let head = if warning && critical { "Battery low: " } else { "Battery " }
lines.push(
(
head + r.percent.to_string() + "%",
fs + 2,
if critical { red } else { p.foreground },
true,
),
)
lines.push((r.describe(), fs - 2, p.foreground_muted, false))
if critical {
lines.push(("Plug in the computer to keep working.", fs - 2, p.foreground, false))
}
}
let m = 10
let h = m * 2 + lines.length() * popup_row_h
let w = popup_w
let ops = [
@ui.fill_rect(@ui.rect(0, 0, w, h), p.surface_popup, radius~),
@ui.stroke_rect(@ui.rect(0, 0, w, h), if warning { red } else { p.border }, width=1, radius~),
]
for i, l in lines {
let (s, size, col, bold) = l
ops.push(@ui.text(m + 4, m + i * popup_row_h + popup_row_h / 2, s, size, col, bold~))
}
(w, h, ops)
}