Files
chandra/lib/clock/clock.mbt
T
Claude 4fc4a963b4
CI / test (push) Failing after 15s
Module settings in each module's right-click menu, and System settings
- New optional WIT export `chandra:desktop/module-config` (wit/plasmoid.wit): config-items (key,
  label, choices, current value; no choices = an on/off switch) and set-config. Hosts look it up
  by name, so modules without it (or prebuilt ones like Pulse) load as before.
- Right-click a panel module: "Module settings..." (or a greyed "No module settings"), then
  "System settings..." (the settings module, Session tab), then Replace with... and Remove widget.
  The "Modules..." row is gone; right-clicking empty panel space offers "System settings..." on the
  Panel tab instead. Module settings open in the same menu: on/off rows with a check box, choices
  under a heading with the current one ticked; clicking applies at once and the menu stays open.
- Choices are saved per module id in state.edn as :module-config {"clock-digital" {"format"
  "12-hour"}} (module_config.mbt; not an owned key, so save_state keeps it) and handed back through
  set-config whenever the module is loaded. Other instances of the module follow a change.
- Both hosts: wasmer stub (optional exports 8-10, canonical-ABI decoding in wasm_native.mbt) and
  host-wasm (chandra:host/plasmoids config-items/set-config, bound with a separate `configurable`
  world so the export stays optional).
- Adopted by the digital clock (Time format 24-hour/12-hour, Show date) and the binary clock (Show
  seconds); clock-widget now exports module-config.
- Tests: native menu end-to-end (open, pick 12-hour, heading inert, reload keeps it), state.edn
  round trip, EDN unit test, wasmer and host-wasm module-config tests, lib/clock settings test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjuApkJR4sVRYDpDLy
2026-11-25 18:58:43 +00:00

263 lines
7.5 KiB
MoonBit

///| Render logic for the clock widgets (pure: tokens + time in, ops out).
///|
pub fn digital_info() -> (String, String, Int, Int, Int) {
// id, name, preferred w, preferred h, min w
("org.chandra.clock.digital", "Digital Clock", 120, 36, 80)
}
///|
pub fn render_digital(
size : @types.Size,
t : @theme.Tokens,
now : @types.Datetime,
hour12? : Bool = false,
show_date? : Bool = true,
) -> 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()
let time = if hour12 {
@timefmt.hhmm12(now.hour.to_int(), now.minute.to_int())
} else {
@timefmt.hhmm(now.hour.to_int(), now.minute.to_int())
}
if !show_date {
return [@ui.text(w / 2, h / 2, time, fs + 4, t.palette.foreground, align=CENTER, bold=true)]
}
let date = @timefmt.date_line(
now.weekday.to_int(),
now.day.to_int(),
now.month.to_int(),
)
[
@ui.text(w / 2, h / 2 - fs / 2 + 1, time, fs + 4, t.palette.foreground, align=CENTER, bold=true),
@ui.text(w / 2, h / 2 + fs / 2 + 4, date, fs - 3, t.palette.foreground_muted, align=CENTER),
]
}
///|
pub fn binary_info() -> (String, String, Int, Int, Int) {
("org.chandra.clock.binary", "Binary Clock", 96, 36, 96)
}
///| BCD binary clock: six columns (H H M M S S), four rows; lit bits use the
/// accent colour, unused high bits of the tens digits are not drawn.
pub fn render_binary(
size : @types.Size,
t : @theme.Tokens,
now : @types.Datetime,
seconds? : Bool = true,
) -> Array[@types.DrawOp] {
let w = size.w.reinterpret_as_int()
let h = size.h.reinterpret_as_int()
let digits = [
(now.hour.to_int() / 10, 2),
(now.hour.to_int() % 10, 4),
(now.minute.to_int() / 10, 3),
(now.minute.to_int() % 10, 4),
]
if seconds {
digits.push((now.second.to_int() / 10, 3))
digits.push((now.second.to_int() % 10, 4))
}
let ops = []
let cols = digits.length()
// Columns are grouped HH MM SS with a wider gap between groups.
let group_gap = 8
let step_x = (w - (cols / 2 - 1) * group_gap) / cols
let step_y = h / 4
let r = (if step_x < step_y { step_x } else { step_y }) / 2 - 1
let lit = t.palette.accent
let off = @ui.with_alpha(t.palette.foreground_muted, 70)
for c = 0; c < cols; c = c + 1 {
let (d, nbits) = digits[c]
let bits = @timefmt.bcd_bits(d, nbits)
let secs = c >= 4
for i = 0; i < nbits; i = i + 1 {
// Row 3 is the least significant bit.
let row = 4 - nbits + i
let cx = c * step_x + step_x / 2 + (c / 2) * group_gap
let cy = row * step_y + step_y / 2
if bits[i] {
// Soft halo + solid dot; seconds are quieter so hours/minutes read first.
let col = if secs { @ui.with_alpha(lit, 150) } else { lit }
ops.push(@ui.circle(cx, cy, r + 2, @ui.with_alpha(lit, if secs { 28 } else { 55 })))
ops.push(@ui.circle(cx, cy, r, col))
} else {
ops.push(@ui.circle(cx, cy, r - 1, off))
}
}
}
ops
}
///|
test "digital clock in 12-hour mode" {
let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time(), hour12=true)
match ops[0] {
Text(t) => assert_eq(t.text, "2:07 PM")
_ => fail("expected text")
}
}
///|
fn test_tokens() -> @theme.Tokens {
let c = @ui.color(10, 20, 30, 255)
{
name: "t",
palette: {
desktop_top: c,
desktop_bottom: c,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
fn test_time() -> @types.Datetime {
{
year: 2026,
month: b'\x0B',
day: b'\x18',
hour: b'\x0E',
minute: b'\x07',
second: b'\x25',
weekday: b'\x02',
}
}
///|
test "digital clock shows HH:MM and the date" {
let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time())
assert_eq(ops.length(), 2)
match ops[0] {
Text(t) => assert_eq(t.text, "14:07")
_ => fail("expected text")
}
match ops[1] {
Text(t) => assert_eq(t.text, "Tue 24 Nov")
_ => fail("expected text")
}
}
///|
test "binary clock draws one dot per displayed bit" {
let ops = render_binary(@ui.size(96, 36), test_tokens(), test_time())
// 2+4+3+4+3+4 bits; lit bits draw a halo and a dot
assert_eq(ops.length(), 20 + 10)
let mut lit = 0
for op in ops {
match op {
Circle(c) => if c.filled && c.color.a >= 150 { lit += 1 }
_ => ()
}
}
// 14:07:37 -> 1,4,0,7,3,7 -> popcount 1+1+0+3+2+3
assert_eq(lit, 10)
}
// ---------------------------------------------------------------- module settings
///| One item of a clock's right-click "Module settings" menu (the `module-config` record, minus
/// the generated type): key, label, choices (empty: an on/off switch) and the current value.
#derive(Eq, Debug)
pub(all) struct ConfigEntry {
key : String
label : String
choices : Array[String]
value : String
}
///| What the user picked for a clock; each clock module keeps one.
pub(all) struct ClockConfig {
mut hour12 : Bool
mut show_date : Bool
mut seconds : Bool
}
///|
pub fn ClockConfig::new() -> ClockConfig {
{ hour12: false, show_date: true, seconds: true }
}
///|
fn on_off(b : Bool) -> String {
if b { "on" } else { "off" }
}
///| The digital clock's settings: time format and whether the date line shows.
pub fn ClockConfig::digital_items(self : ClockConfig) -> Array[ConfigEntry] {
[
{
key: "format",
label: "Time format",
choices: ["24-hour", "12-hour"],
value: if self.hour12 { "12-hour" } else { "24-hour" },
},
{ key: "date", label: "Show date", choices: [], value: on_off(self.show_date) },
]
}
///| The binary clock's settings: whether the seconds columns show.
pub fn ClockConfig::binary_items(self : ClockConfig) -> Array[ConfigEntry] {
[{ key: "seconds", label: "Show seconds", choices: [], value: on_off(self.seconds) }]
}
///| Apply one setting; true when it changed something. Unknown keys and values are ignored.
pub fn ClockConfig::set(self : ClockConfig, 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 {
"format" =>
match value {
"12-hour" if !self.hour12 => { self.hour12 = true; true }
"24-hour" if self.hour12 => { self.hour12 = false; true }
_ => false
}
"date" => flag(self.show_date, fn(b) { self.show_date = b })
"seconds" => flag(self.seconds, fn(b) { self.seconds = b })
_ => false
}
}
///|
test "clock settings" {
let c = ClockConfig::new()
assert_eq(c.digital_items()[0].value, "24-hour")
assert_true(c.set("format", "12-hour"))
assert_false(c.set("format", "12-hour")) // no change
assert_false(c.set("format", "bogus"))
assert_eq(c.digital_items()[0].value, "12-hour")
assert_true(c.set("date", "off"))
assert_eq(c.digital_items()[1].value, "off")
assert_false(c.set("nope", "on"))
let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time(), show_date=false)
assert_eq(ops.length(), 1)
assert_true(c.set("seconds", "off"))
let bin = render_binary(@ui.size(96, 36), test_tokens(), test_time(), seconds=false)
assert_eq(bin.length() < 30, true) // two columns fewer
}
///| The digital clock module's settings (one module instance per wasm instance).
pub let digital_config : ClockConfig = ClockConfig::new()
///| The binary clock module's settings.
pub let binary_config : ClockConfig = ClockConfig::new()