///| 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.moonplasma.clock.digital", "Digital Clock", 120, 36, 80) } ///| pub fn render_digital( size : @types.Size, t : @theme.Tokens, now : @types.Datetime, ) -> 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 = @timefmt.hhmm(now.hour.to_int(), now.minute.to_int()) 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.moonplasma.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, ) -> 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), (now.second.to_int() / 10, 3), (now.second.to_int() % 10, 4), ] let ops = [] let cols = digits.length() let step_x = w / cols let step_y = h / 4 let r = (if step_x < step_y { step_x } else { step_y }) / 2 - 1 for c = 0; c < cols; c = c + 1 { let (d, nbits) = digits[c] let bits = @timefmt.bcd_bits(d, nbits) 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 let cy = row * step_y + step_y / 2 if bits[i] { ops.push(@ui.circle(cx, cy, r, t.palette.accent)) } else { ops.push(@ui.circle(cx, cy, r, t.palette.border, filled=false)) } } } ops } ///| 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 assert_eq(ops.length(), 20) let mut lit = 0 for op in ops { match op { Circle(c) => if c.filled { lit += 1 } _ => () } } // 14:07:37 -> 1,4,0,7,3,7 -> popcount 1+1+0+3+2+3 assert_eq(lit, 10) }