Files
chandra/compositor/host/calendar_test.mbt
T
Claude a215dd6bf7
CI / test (push) Failing after 17s
Clock calendar: left click slides out a month calendar
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
2026-11-25 20:47:06 +00:00

97 lines
4.2 KiB
MoonBit

///| The clocks' calendar flyout driven through the shell: a left click on the clock slides it
/// out of the panel, its buttons change the month, and Escape, a click elsewhere or another click
/// on the clock slide it away. Needs the modules: run scripts/build-modules.sh.
///|
fn cal_texts(comp : @host.ChandraCompositor, id : String) -> Array[String] {
comp.shell.slot_popup_texts(comp.shell.slot_of(id)).map(t => t.0)
}
///| Wait out the slide, drawing frames like the main loop does while one runs.
fn finish_slide(comp : @host.ChandraCompositor) -> Unit {
let mut n = 0
while comp.has_timed_work() && n < 200 {
@sys.sleep_ms(5)
ignore(comp.compose_frame())
n = n + 1
}
}
///|
test "left click on the clock slides out a calendar; Escape, a click elsewhere or the clock close it" {
let comp = new_comp("calendar")
let slot = comp.shell.slot_of("clock-digital")
let clock = rect_of(comp, "clock-digital")
assert_true(clock.w > 0)
assert_false(comp.has_timed_work()) // an idle desktop doesn't animate
click_module(comp, "clock-digital", 1U)
let pr = comp.shell.slot_popup_rect(slot)
assert_true(pr.w > 0)
assert_true(pr.y + pr.h <= clock.y) // above the panel
assert_true(pr.x + pr.w <= 1280)
// It is sliding: frames keep coming until it sits in place.
assert_true(comp.has_timed_work())
assert_true(comp.shell.slot_slide_offset(slot) > 0)
@sys.sleep_ms(60)
ignore(@sys.write_file_bytes("_build/calendar_sliding.png", @renderer.encode_png(comp.compose_frame())))
finish_slide(comp)
assert_false(comp.has_timed_work())
assert_eq(comp.shell.slot_slide_offset(slot), 0)
ignore(@sys.write_file_bytes("_build/calendar_open.png", @renderer.encode_png(comp.compose_frame())))
// The header, the month and the buttons.
let month = cal_texts(comp, "clock-digital")[2] // e.g. "November 2026"
assert_true(month.contains(" 20"))
assert_true(cal_texts(comp, "clock-digital").contains("Today"))
// The buttons sit right-aligned in the month row: next, Today, previous.
let row_y = pr.y + 80 + 14
click_at(comp, pr.x + pr.w - 12 - 14, row_y, 10U) // next month
assert_true(cal_texts(comp, "clock-digital")[2] != month)
click_at(comp, pr.x + pr.w - 12 - 28 - 4 - 28, row_y, 20U) // Today
assert_eq(cal_texts(comp, "clock-digital")[2], month)
assert_false(comp.has_timed_work()) // changing month doesn't slide
// Escape slides it away; input passes through at once.
comp.handle_input(@nested.InputEvent::Key(1U, true), 30U)
ignore(comp.compose_frame())
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
assert_true(comp.has_timed_work())
finish_slide(comp)
assert_false(comp.has_timed_work())
// Clicking the clock again toggles it.
click_module(comp, "clock-digital", 40U)
assert_true(comp.shell.slot_popup_rect(slot).w > 0)
click_module(comp, "clock-digital", 50U)
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
// A click on the desktop closes it too.
click_module(comp, "clock-digital", 60U)
assert_true(comp.shell.slot_popup_rect(slot).w > 0)
click_at(comp, 600, 200, 70U)
assert_eq(comp.shell.slot_popup_rect(slot).w, 0)
finish_slide(comp)
assert_false(comp.has_timed_work())
}
///|
test "the calendar's week start and week numbers are the clock's module settings" {
let comp = new_comp("calendar-cfg")
let slot = comp.shell.slot_of("clock-digital")
comp.shell.set_module_setting(slot, "week-start", "Monday")
comp.shell.set_module_setting(slot, "week-numbers", "on")
assert_eq(comp.shell.module_setting("clock-digital", "week-start"), Some("Monday"))
click_module(comp, "clock-digital", 1U)
comp.shell.dark_mode = false
comp.apply_theme()
finish_slide(comp)
ignore(@sys.write_file_bytes("_build/calendar_light.png", @renderer.encode_png(comp.compose_frame())))
let texts = cal_texts(comp, "clock-digital")
assert_true(texts.contains("Wk"))
let names = texts.filter(s => s == "Mo" || s == "Su")
assert_eq(names, ["Mo", "Su"])
// The binary clock has the calendar too.
assert_true(comp.shell.unload_slot(slot))
assert_true(comp.shell.load_module("clock-binary"))
ignore(comp.compose_frame())
click_module(comp, "clock-binary", 10U)
assert_true(comp.shell.slot_popup_rect(comp.shell.slot_of("clock-binary")).w > 0)
assert_true(cal_texts(comp, "clock-binary").contains("Today"))
}