Clock calendar: left click slides out a month calendar
CI / test (push) Failing after 17s

Left-clicking the Digital Clock (or the Binary Clock) opens a calendar flyout that
slides up out of the panel; Escape, a click elsewhere or another click on the clock
slides it back in.

- lib/clock/calendar.mbt: month maths (leap years, weekday, ISO week numbers), a
  six-week grid, the flyout's state, drawing and hit testing. Header with the time
  (in the clock's own 12/24-hour format) and the long date; month and year with
  previous / Today / next buttons; weekday names; today circled in the accent colour,
  other months' days muted. Keys: Escape closes, t today, p/n previous/next month.
- Module settings (right click): "Week starts on" Sunday/Monday and "Show week
  numbers", for both clocks (saved per module like the other settings).
- compositor/shell/slide.mbt: popups of the clock modules slide (200 ms, ease-out in,
  ease-in out), clipped to their final place so they come out of the panel edge; the
  popup takes input at its final place at once. A popup below the panel would slide
  the other way. Theme comes from the module's tokens, so dark/light follow.
- CPU: has_timed_work() is true only while a slide runs, so the main loop only keeps
  the frame pace for those 200 ms; an open calendar redraws only when its minute or
  month changes.
- Tests: lib/clock calendar tests (wasm), compositor/host/calendar_test.mbt (open,
  slide finishes, month buttons, Escape / click outside / toggle, settings, binary
  clock). Host failures are the 5 known input_test ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjahaLE5XqnCWoEwKv
This commit is contained in:
Claude committed 2026-11-25 20:47:06 +00:00
1 parent 10659e7655
commit a215dd6bf7
9 files changed
+893 -18

No files matched your search

+597
View File
@@ -0,0 +1,597 @@
///| The clocks' calendar flyout: left-click a clock and a month calendar slides out of the panel
/// (the shell does the slide). Pure logic: the month maths, the layout, drawing, hit testing and
/// the flyout's state; the clock modules only forward events and hand back `render`'s ops.
///|
pub fn is_leap(year : Int) -> Bool {
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}
///| Days in `month` (1-12) of `year`.
pub fn days_in_month(year : Int, month : Int) -> Int {
match month {
2 => if is_leap(year) { 29 } else { 28 }
4 | 6 | 9 | 11 => 30
_ => 31
}
}
///| Day of the week of a date, 0 = Sunday .. 6 = Saturday (Sakamoto's method).
pub fn weekday_of(year : Int, month : Int, day : Int) -> Int {
let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
let y = if month < 3 { year - 1 } else { year }
(y + y / 4 - y / 100 + y / 400 + t[month - 1] + day) % 7
}
///| Day of the year, 1-366.
fn day_of_year(year : Int, month : Int, day : Int) -> Int {
let mut n = day
for m = 1; m < month; m = m + 1 {
n = n + days_in_month(year, m)
}
n
}
///| Weeks (52 or 53) in ISO year `year`.
fn iso_weeks_in_year(year : Int) -> Int {
// A year has 53 ISO weeks when it starts on a Thursday, or is a leap year starting on a Wednesday.
let jan1 = weekday_of(year, 1, 1)
if jan1 == 4 || (is_leap(year) && jan1 == 3) {
53
} else {
52
}
}
///| ISO 8601 week number (weeks start on Monday; week 1 holds the year's first Thursday).
pub fn iso_week(year : Int, month : Int, day : Int) -> Int {
let wd = (weekday_of(year, month, day) + 6) % 7 + 1 // Monday 1 .. Sunday 7
let w = (day_of_year(year, month, day) - wd + 10) / 7
if w < 1 {
iso_weeks_in_year(year - 1)
} else if w > iso_weeks_in_year(year) {
1
} else {
w
}
}
///|
let month_long : Array[String] = [
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December",
]
///|
let weekday_long : Array[String] = [
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
]
///|
let weekday_two : Array[String] = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
///| One cell of the month grid: a date, and whether it belongs to the month shown.
#derive(Eq, Debug)
pub(all) struct DayCell {
year : Int
month : Int
day : Int
in_month : Bool
}
///| The 42 days (six weeks) shown for `month` of `year`, starting on Monday or Sunday: the
/// month's days plus the end of the month before and the start of the month after. Always six
/// rows, so the flyout keeps its size from month to month.
pub fn month_grid(year : Int, month : Int, monday_first : Bool) -> Array[DayCell] {
let first = weekday_of(year, month, 1)
let lead = if monday_first { (first + 6) % 7 } else { first }
let (py, pm) = if month == 1 { (year - 1, 12) } else { (year, month - 1) }
let (ny, nm) = if month == 12 { (year + 1, 1) } else { (year, month + 1) }
let prev_days = days_in_month(py, pm)
let days = days_in_month(year, month)
let out : Array[DayCell] = []
for i = 0; i < lead; i = i + 1 {
out.push({ year: py, month: pm, day: prev_days - lead + 1 + i, in_month: false })
}
for d = 1; d <= days; d = d + 1 {
out.push({ year, month, day: d, in_month: true })
}
let mut d = 1
while out.length() < 42 {
out.push({ year: ny, month: nm, day: d, in_month: false })
d = d + 1
}
out
}
// ---------------------------------------------------------------- the flyout
///| What a point in the flyout is over.
#derive(Eq, Debug)
pub(all) enum CalendarHit {
Prev
Next
Today
Nothing
}
///| The calendar flyout of one clock module: whether it is open, which month it shows, the
/// "Module settings" choices (week start, week numbers) and the button under the pointer.
pub(all) struct Calendar {
mut open : Bool
mut year : Int
mut month : Int
mut monday_first : Bool
mut week_numbers : Bool
mut hover : CalendarHit
}
///|
pub fn Calendar::new() -> Calendar {
{ open: false, year: 2026, month: 1, monday_first: false, week_numbers: false, hover: Nothing }
}
// Layout (popup-local pixels).
///|
let cal_margin : Int = 12
///|
let cal_cell_w : Int = 38
///|
let cal_cell_h : Int = 30
///|
let cal_weeknum_w : Int = 30
///| Top of the month/navigation row, of the weekday names and of the day grid.
let cal_nav_y : Int = 80
///|
let cal_nav_h : Int = 28
///|
let cal_names_y : Int = 114
///|
let cal_grid_y : Int = 140
///|
let cal_btn_w : Int = 28
///|
let cal_today_w : Int = 56
///| Left edge of the day grid (after the week-number column when it shows).
fn Calendar::grid_x(self : Calendar) -> Int {
cal_margin + (if self.week_numbers { cal_weeknum_w } else { 0 })
}
///| The flyout's size: (w, h).
pub fn Calendar::size(self : Calendar) -> (Int, Int) {
(self.grid_x() + 7 * cal_cell_w + cal_margin, cal_grid_y + 6 * cal_cell_h + cal_margin)
}
///| The previous-month, Today and next-month buttons, right-aligned in the navigation row:
/// (hit, x, w).
fn Calendar::buttons(self : Calendar) -> Array[(CalendarHit, Int, Int)] {
let (w, _) = self.size()
let next_x = w - cal_margin - cal_btn_w
let today_x = next_x - 4 - cal_today_w
let prev_x = today_x - 4 - cal_btn_w
[(Prev, prev_x, cal_btn_w), (Today, today_x, cal_today_w), (Next, next_x, cal_btn_w)]
}
///| What popup-local (x, y) is over.
pub fn Calendar::hit(self : Calendar, x : Int, y : Int) -> CalendarHit {
if y < cal_nav_y || y >= cal_nav_y + cal_nav_h {
return Nothing
}
for b in self.buttons() {
let (h, bx, bw) = b
if x >= bx && x < bx + bw {
return h
}
}
Nothing
}
///| Show the month holding `now`.
pub fn Calendar::go_today(self : Calendar, now : @types.Datetime) -> Unit {
self.year = now.year.reinterpret_as_int()
self.month = now.month.to_int()
}
///| Move the month shown by `delta` months.
pub fn Calendar::shift(self : Calendar, delta : Int) -> Unit {
let m = self.year * 12 + (self.month - 1) + delta
self.year = m / 12
self.month = m % 12 + 1
}
///| Open (on the current month) or close the flyout.
pub fn Calendar::toggle(self : Calendar, now : @types.Datetime) -> Unit {
if self.open {
self.open = false
} else {
self.open = true
self.go_today(now)
}
self.hover = Nothing
}
///| A clock module's input, as the plasmoid contract hands it over (`popup`: the event is for
/// the flyout rather than the clock itself). A click on the clock toggles the flyout; a click
/// elsewhere (focus-lost) or Escape closes it. True when it should be redrawn.
pub fn Calendar::handle(
self : Calendar,
popup : Bool,
ev : @types.InputEvent,
now : @types.Datetime,
) -> Bool {
match (popup, ev) {
(false, PointerDown(_)) => {
self.toggle(now)
true
}
(_, FocusLost) =>
if self.open {
self.open = false
self.hover = Nothing
true
} else {
false
}
(true, KeyDown(k)) =>
match k {
"Escape" => {
self.open = false
self.hover = Nothing
true
}
"t" | "T" => {
self.go_today(now)
true
}
"Left" | "p" => {
self.shift(-1)
true
}
"Right" | "n" => {
self.shift(1)
true
}
_ => false
}
(true, PointerDown(p)) =>
match self.hit(p.x, p.y) {
Prev => {
self.shift(-1)
true
}
Next => {
self.shift(1)
true
}
Today => {
self.go_today(now)
true
}
Nothing => false
}
(true, PointerMove(p)) => {
let h = self.hit(p.x, p.y)
if h != self.hover {
self.hover = h
true
} else {
false
}
}
(true, PointerLeave) =>
if self.hover != Nothing {
self.hover = Nothing
true
} else {
false
}
_ => false
}
}
///| The header's date line, e.g. "Wednesday, 25 November 2026".
pub fn long_date(now : @types.Datetime) -> String {
let wd = now.weekday.to_int()
let m = now.month.to_int()
weekday_long[wd % 7] +
", " +
now.day.to_int().to_string() +
" " +
month_long[(m + 11) % 12] +
" " +
now.year.to_string()
}
///| Draw the flyout: the time and date, the month with its buttons, the weekday names and the
/// day grid (today circled in the accent colour; other months' days muted), optionally with ISO
/// week numbers on the left. `hour12`/`ampm` follow the clock's own time format.
pub fn Calendar::render(
self : Calendar,
t : @theme.Tokens,
now : @types.Datetime,
hour12? : Bool = false,
ampm? : Bool = true,
) -> Array[@types.DrawOp] {
let (w, h) = self.size()
let p = t.palette
let fs = t.font_size.reinterpret_as_int()
let r = t.radius.reinterpret_as_int()
let ops : Array[@types.DrawOp] = [
@ui.fill_rect(@ui.rect(0, 0, w, h), p.surface_popup, radius=r),
@ui.stroke_rect(@ui.rect(0, 0, w, h), p.border, width=1, radius=r),
]
// Header: the time, large, and today's date under it.
let time = if hour12 {
let t12 = @timefmt.hhmm12(now.hour.to_int(), now.minute.to_int())
if ampm { t12 } else { t12[:t12.length() - 3].to_owned() }
} else {
@timefmt.hhmm(now.hour.to_int(), now.minute.to_int())
}
ops.push(@ui.text(cal_margin + 4, 30, time, fs + 14, p.foreground, bold=true))
ops.push(@ui.text(cal_margin + 4, 58, long_date(now), fs - 1, p.foreground_muted))
ops.push(@ui.fill_rect(@ui.rect(cal_margin, 72, w - 2 * cal_margin, 1), p.border))
// Month and year, then the previous / Today / next buttons.
let nav_mid = cal_nav_y + cal_nav_h / 2
ops.push(
@ui.text(
cal_margin + 4,
nav_mid,
month_long[self.month - 1] + " " + self.year.to_string(),
fs,
p.foreground,
bold=true,
),
)
for b in self.buttons() {
let (kind, bx, bw) = b
let rect = @ui.rect(bx, cal_nav_y, bw, cal_nav_h)
if self.hover == kind {
ops.push(@ui.fill_rect(rect, p.surface_hover, radius=r))
}
let cx = bx + bw / 2
match kind {
Prev => {
ops.push(@ui.line(cx + 3, nav_mid - 6, cx - 3, nav_mid, p.foreground, width=2))
ops.push(@ui.line(cx - 3, nav_mid, cx + 3, nav_mid + 6, p.foreground, width=2))
}
Next => {
ops.push(@ui.line(cx - 3, nav_mid - 6, cx + 3, nav_mid, p.foreground, width=2))
ops.push(@ui.line(cx + 3, nav_mid, cx - 3, nav_mid + 6, p.foreground, width=2))
}
Today => {
ops.push(@ui.stroke_rect(rect, p.border, width=1, radius=r))
ops.push(@ui.text(cx, nav_mid, "Today", fs - 3, p.foreground, align=CENTER))
}
Nothing => ()
}
}
// Weekday names.
let gx = self.grid_x()
let names_mid = cal_names_y + 11
if self.week_numbers {
ops.push(@ui.text(cal_margin + cal_weeknum_w / 2 - 2, names_mid, "Wk", fs - 4, p.foreground_muted, align=CENTER))
}
for c = 0; c < 7; c = c + 1 {
let wd = if self.monday_first { (c + 1) % 7 } else { c }
ops.push(
@ui.text(gx + c * cal_cell_w + cal_cell_w / 2, names_mid, weekday_two[wd], fs - 4, p.foreground_muted, align=CENTER, bold=true),
)
}
// The days.
let cells = month_grid(self.year, self.month, self.monday_first)
let ty = now.year.reinterpret_as_int()
let tm = now.month.to_int()
let td = now.day.to_int()
for i = 0; i < cells.length(); i = i + 1 {
let cell = cells[i]
let row = i / 7
let col = i % 7
let cx = gx + col * cal_cell_w + cal_cell_w / 2
let cy = cal_grid_y + row * cal_cell_h + cal_cell_h / 2
if col == 0 && self.week_numbers {
// The ISO week of the row's Thursday (Monday-first rows) or Monday (Sunday-first rows).
let k = if self.monday_first { i + 3 } else { i + 1 }
let ref_ = cells[k]
ops.push(
@ui.text(cal_margin + cal_weeknum_w / 2 - 2, cy, iso_week(ref_.year, ref_.month, ref_.day).to_string(), fs - 4, @ui.with_alpha(p.foreground_muted, 170), align=CENTER),
)
}
let is_today = cell.year == ty && cell.month == tm && cell.day == td
let colr = if is_today {
ops.push(@ui.circle(cx, cy, cal_cell_h / 2 - 2, p.accent))
p.accent_foreground
} else if cell.in_month {
p.foreground
} else {
@ui.with_alpha(p.foreground_muted, 130)
}
ops.push(@ui.text(cx, cy, cell.day.to_string(), fs - 2, colr, align=CENTER, bold=is_today))
}
ops
}
///| The flyout's two "Module settings" items, appended to a clock's own.
fn Calendar::config_items(self : Calendar) -> Array[ConfigEntry] {
[
{
key: "week-start",
label: "Week starts on",
choices: ["Sunday", "Monday"],
value: if self.monday_first { "Monday" } else { "Sunday" },
},
{ key: "week-numbers", label: "Show week numbers", choices: [], value: on_off(self.week_numbers) },
]
}
///| Apply a calendar setting; true when it changed something.
fn Calendar::set(self : Calendar, key : String, value : String) -> Bool {
match (key, value) {
("week-start", "Monday") if !self.monday_first => { self.monday_first = true; true }
("week-start", "Sunday") if self.monday_first => { self.monday_first = false; true }
("week-numbers", "on") if !self.week_numbers => { self.week_numbers = true; true }
("week-numbers", "off") if self.week_numbers => { self.week_numbers = false; true }
_ => false
}
}
// ---------------------------------------------------------------- tests
///|
fn cal_time(y : Int, m : Int, d : Int) -> @types.Datetime {
{
year: y.reinterpret_as_uint(),
month: m.to_byte(),
day: d.to_byte(),
hour: b'\x0E',
minute: b'\x07',
second: b'\x00',
weekday: weekday_of(y, m, d).to_byte(),
}
}
///|
test "month maths" {
assert_eq(weekday_of(2026, 11, 25), 3) // a Wednesday
assert_eq(weekday_of(2000, 1, 1), 6)
assert_eq(days_in_month(2024, 2), 29)
assert_eq(days_in_month(2100, 2), 28)
assert_eq(days_in_month(2000, 2), 29)
assert_eq(days_in_month(2026, 4), 30)
// ISO weeks, including the edges of the year.
assert_eq(iso_week(2026, 11, 25), 48)
assert_eq(iso_week(2021, 1, 1), 53) // belongs to 2020's week 53
assert_eq(iso_week(2024, 12, 30), 1) // belongs to 2025's week 1
assert_eq(iso_week(2026, 1, 1), 1)
assert_eq(iso_week(2027, 1, 3), 53)
}
///|
test "the month grid is six weeks from the chosen first weekday" {
// November 2026 starts on a Sunday.
let sun = month_grid(2026, 11, false)
assert_eq(sun.length(), 42)
assert_eq(sun[0], { year: 2026, month: 11, day: 1, in_month: true })
assert_eq(sun[29].day, 30)
assert_eq(sun[30], { year: 2026, month: 12, day: 1, in_month: false })
let mon = month_grid(2026, 11, true)
assert_eq(mon[0], { year: 2026, month: 10, day: 26, in_month: false })
assert_eq(mon[6], { year: 2026, month: 11, day: 1, in_month: true })
// January wraps to the December before.
let jan = month_grid(2027, 1, true)
assert_eq(jan[0], { year: 2026, month: 12, day: 28, in_month: false })
}
///|
test "clicks open and close the flyout; its buttons change the month" {
let now = cal_time(2026, 11, 25)
let c = Calendar::new()
assert_true(c.handle(false, PointerDown({ x: 3, y: 3 }), now))
assert_true(c.open)
assert_eq((c.year, c.month), (2026, 11))
let click = fn(h : CalendarHit) {
for b in c.buttons() {
let (k, x, w) = b
if k == h {
return c.handle(true, PointerDown({ x: x + w / 2, y: cal_nav_y + 5 }), now)
}
}
false
}
assert_true(click(Next))
assert_true(click(Next))
assert_eq((c.year, c.month), (2027, 1))
assert_true(click(Prev))
assert_true(click(Prev))
assert_true(click(Prev))
assert_eq((c.year, c.month), (2026, 10))
assert_true(click(Today))
assert_eq((c.year, c.month), (2026, 11))
// A click on a day does nothing; hovering a button highlights it.
assert_false(c.handle(true, PointerDown({ x: 40, y: cal_grid_y + 10 }), now))
let (bx, bw) = match c.buttons()[0] { (_, x, w) => (x, w) }
assert_true(c.handle(true, PointerMove({ x: bx + bw / 2, y: cal_nav_y + 5 }), now))
assert_eq(c.hover, Prev)
assert_true(c.handle(true, PointerLeave, now))
// Escape closes; clicking the clock again toggles; a click elsewhere closes.
assert_true(c.handle(true, KeyDown("Escape"), now))
assert_false(c.open)
c.shift(5)
assert_true(c.handle(false, PointerDown({ x: 0, y: 0 }), now))
assert_eq((c.year, c.month), (2026, 11)) // reopens on the current month
assert_true(c.handle(false, PointerDown({ x: 0, y: 0 }), now))
assert_false(c.open)
assert_true(c.handle(false, PointerDown({ x: 0, y: 0 }), now))
assert_true(c.handle(false, FocusLost, now))
assert_false(c.open)
assert_false(c.handle(false, FocusLost, now))
}
///|
test "the flyout draws the header, the month and today" {
let now = cal_time(2026, 11, 25)
let c = Calendar::new()
c.toggle(now)
let texts = fn(ops : Array[@types.DrawOp]) {
let out : Array[String] = []
for op in ops {
match op {
Text(t) => out.push(t.text)
_ => ()
}
}
out
}
let tk = test_tokens()
let ops = c.render(tk, now, hour12=true)
let ts = texts(ops)
assert_eq(ts[0], "2:07 PM")
assert_eq(ts[1], "Wednesday, 25 November 2026")
assert_eq(ts[2], "November 2026")
assert_true(ts.contains("Today"))
assert_eq(ts.filter(s => s == "Su" || s == "Mo").length(), 2)
// Today is circled.
let circles = ops.filter(op => op is Circle(_))
assert_eq(circles.length(), 1)
// Not on another month.
c.shift(1)
assert_eq(c.render(tk, now).filter(op => op is Circle(_)).length(), 0)
// Week numbers widen it and add one number per row; Monday-first puts "Mo" first.
let (w0, h0) = c.size()
c.shift(-1)
assert_true(c.set("week-numbers", "on"))
assert_true(c.set("week-start", "Monday"))
assert_false(c.set("week-start", "Monday"))
let (w1, h1) = c.size()
assert_eq(h1, h0)
assert_eq(w1, w0 + cal_weeknum_w)
let ts = texts(c.render(tk, now))
assert_true(ts.contains("Wk"))
let names = ts.filter(s => s.length() == 2 && s[0] >= 'A' && s[0] <= 'Z' && s != "Wk")
assert_eq(names[0], "Mo")
assert_true(ts.contains("44") && ts.contains("48") && ts.contains("49"))
}
///|
test "the clocks' module settings include the calendar's" {
let c = ClockConfig::new()
let items = c.binary_items()
assert_eq(items.map(i => i.key), ["seconds", "week-start", "week-numbers"])
assert_eq(items[1].value, "Sunday")
assert_true(c.set("week-start", "Monday"))
assert_true(c.cal.monday_first)
assert_eq(c.digital_items().filter(i => i.key == "week-start")[0].value, "Monday")
assert_true(c.set("week-numbers", "on"))
assert_eq(c.binary_items()[2].value, "on")
assert_false(c.set("week-start", "Tuesday"))
}