Clock: AM/PM in 12-hour mode, and the default panel clock now has source
CI / test (push) Failing after 16s

clock-digital12 (the default panel's clock) was a prebuilt wasm with no source.
It is now a component built from lib/clock like the Digital Clock, starting in
12-hour mode with AM/PM, and it gets the same right-click Module settings.
Both digital clocks get a "Show AM/PM" switch, offered in 12-hour mode (on by
default).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhrQpz9texRcAtgECq
This commit is contained in:
Claude committed 2026-11-25 19:14:46 +00:00
1 parent 51b914f6cf
commit c32a31ffd7
19 files changed
+930 -10

No files matched your search

+52 -7
View File
@@ -6,6 +6,12 @@ pub fn digital_info() -> (String, String, Int, Int, Int) {
("org.chandra.clock.digital", "Digital Clock", 120, 36, 80)
}
///| The 12-hour digital clock (clock-digital12, the default panel's clock): same rendering
/// and settings as the digital clock, but it starts in 12-hour mode with AM/PM.
pub fn digital12_info() -> (String, String, Int, Int, Int) {
("org.chandra.clock.digital12", "Digital Clock (12-hour)", 120, 36, 80)
}
///|
pub fn render_digital(
size : @types.Size,
@@ -13,12 +19,15 @@ pub fn render_digital(
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 {
@timefmt.hhmm12(now.hour.to_int(), now.minute.to_int())
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())
}
@@ -185,11 +194,12 @@ pub(all) struct ClockConfig {
mut hour12 : Bool
mut show_date : Bool
mut seconds : Bool
mut ampm : Bool
}
///|
pub fn ClockConfig::new() -> ClockConfig {
{ hour12: false, show_date: true, seconds: true }
pub fn ClockConfig::new(hour12? : Bool = false) -> ClockConfig {
{ hour12, show_date: true, seconds: true, ampm: true }
}
///|
@@ -197,17 +207,21 @@ fn on_off(b : Bool) -> String {
if b { "on" } else { "off" }
}
///| The digital clock's settings: time format and whether the date line shows.
///| 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" },
},
{ key: "date", label: "Show date", choices: [], value: on_off(self.show_date) },
]
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.
@@ -232,6 +246,7 @@ pub fn ClockConfig::set(self : ClockConfig, key : String, value : String) -> Boo
_ => 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
}
@@ -246,7 +261,7 @@ test "clock settings" {
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_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)
@@ -258,5 +273,35 @@ test "clock settings" {
///| The digital clock module's settings (one module instance per wasm instance).
pub let digital_config : ClockConfig = ClockConfig::new()
///| The 12-hour digital clock module's settings: starts in 12-hour mode with AM/PM.
pub let digital12_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"])
}
+7 -2
View File
@@ -12,13 +12,17 @@ pub let binary_config : ClockConfig
pub fn binary_info() -> (String, String, Int32, Int32, Int32)
pub let digital12_config : ClockConfig
pub fn digital12_info() -> (String, String, Int32, Int32, Int32)
pub let digital_config : ClockConfig
pub fn digital_info() -> (String, String, Int32, Int32, Int32)
pub fn render_binary(@types.Size, @theme.Tokens, @types.Datetime, seconds? : Bool) -> Array[@types.DrawOp]
pub fn render_digital(@types.Size, @theme.Tokens, @types.Datetime, hour12? : Bool, show_date? : Bool) -> Array[@types.DrawOp]
pub fn render_digital(@types.Size, @theme.Tokens, @types.Datetime, hour12? : Bool, show_date? : Bool, ampm? : Bool) -> Array[@types.DrawOp]
// Errors
@@ -27,10 +31,11 @@ pub(all) struct ClockConfig {
mut hour12 : Bool
mut show_date : Bool
mut seconds : Bool
mut ampm : Bool
}
pub fn ClockConfig::binary_items(Self) -> Array[ConfigEntry]
pub fn ClockConfig::digital_items(Self) -> Array[ConfigEntry]
pub fn ClockConfig::new() -> Self
pub fn ClockConfig::new(hour12? : Bool) -> Self
pub fn ClockConfig::set(Self, String, String) -> Bool
#derive(Eq, @debug.Debug)