///| 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, ampm? : 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 { let t12 = @timefmt.hhmm12(now.hour.to_int(), now.minute.to_int()) // "2:07 PM" -> "2:07" when the AM/PM suffix is switched off. if ampm { t12 } else { t12[:t12.length() - 3].to_owned() } } 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 mut ampm : Bool } ///| pub fn ClockConfig::new(hour12? : Bool = false) -> ClockConfig { { hour12, show_date: true, seconds: true, ampm: true } } ///| fn on_off(b : Bool) -> String { if b { "on" } else { "off" } } ///| The digital clock's settings: time format, AM/PM (12-hour only) and the date line. pub fn ClockConfig::digital_items(self : ClockConfig) -> Array[ConfigEntry] { let items : Array[ConfigEntry] = [ { key: "format", label: "Time format", choices: ["24-hour", "12-hour"], value: if self.hour12 { "12-hour" } else { "24-hour" }, }, ] if self.hour12 { items.push({ key: "ampm", label: "Show AM/PM", choices: [], value: on_off(self.ampm) }) } items.push({ key: "date", label: "Show date", choices: [], value: on_off(self.show_date) }) items } ///| 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 }) "ampm" => flag(self.ampm, fn(b) { self.ampm = 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()[2].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): it starts in /// 12-hour mode with AM/PM; "Time format" in its right-click settings switches to 24-hour. /// (It replaced the separate 12-hour clock module, clock-digital12.) pub let digital_config : ClockConfig = ClockConfig::new(hour12=true) ///| The binary clock module's settings. pub let binary_config : ClockConfig = ClockConfig::new() ///| test "12-hour clock shows AM/PM, and it can be switched off" { let c = ClockConfig::new(hour12=true) let items = c.digital_items() assert_eq(items.map(i => i.key), ["format", "ampm", "date"]) assert_eq(items[1].value, "on") 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") } let am = { ..test_time(), hour: b'\x00' } match render_digital(@ui.size(120, 36), test_tokens(), am, hour12=true)[0] { Text(t) => assert_eq(t.text, "12:07 AM") _ => fail("expected text") } assert_true(c.set("ampm", "off")) let ops = render_digital(@ui.size(120, 36), test_tokens(), test_time(), hour12=true, ampm=c.ampm) match ops[0] { Text(t) => assert_eq(t.text, "2:07") _ => fail("expected text") } // AM/PM is only offered in 12-hour mode assert_true(c.set("format", "24-hour")) assert_eq(c.digital_items().map(i => i.key), ["format", "date"]) }