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

+96
View File
@@ -0,0 +1,96 @@
///| 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"))
}
+2 -1
View File
@@ -581,7 +581,8 @@ pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
self.rec is Some(_) ||
self.shot_request is Some(_) ||
self.rec_request is Some(_) ||
self.shot_toast is Some(_)
self.shot_toast is Some(_) ||
self.shell.animating()
}
///|
+8 -4
View File
@@ -179,6 +179,12 @@ pub(all) struct Widget {
mut popup_rect : @scene.Rect
mut popup_ops : Array[@types.DrawOp]
mut popup_hot : Bool
/// Popup slide (slide.mbt): when the running slide began (monotonic ms, 0: none), whether it
/// slides in or out, and while sliding out, where the popup was and what it showed.
mut slide_t0 : UInt64
mut slide_in : Bool
mut ghost_rect : @scene.Rect
mut ghost_ops : Array[@types.DrawOp]
}
///| A wasm module file found on the module path.
@@ -941,12 +947,10 @@ pub fn DesktopShell::render(
}
}
}
// Popups sit above the panel, left-aligned with their widget.
// Popups sit above the panel, left-aligned with their widget; some slide in and out.
for wd in self.widgets {
self.refresh_popup(wd)
if wd.popup_rect.w > 0 {
translate(wd.popup_ops, wd.popup_rect.x, wd.popup_rect.y, panel_ops)
}
self.draw_popup(wd, panel_ops)
}
if self.menu_open {
self.render_menu(w, tokens, panel_ops)
+147
View File
@@ -0,0 +1,147 @@
///| Popups that slide out of the panel when they open and back into it when they close (the
/// clocks' calendar). The popup keeps its final place for input from the first frame; only the
/// drawing moves, clipped to that place, so it looks like it comes out of the panel's edge.
/// Frames are only drawn at the frame pace while a slide runs (`animating`, which the
/// compositor's main loop checks); otherwise nothing is redrawn for it.
///| Modules whose popups slide.
let slide_modules : Array[String] = ["clock-digital", "clock-binary"]
///| How long a slide takes, in ms.
pub let slide_ms : UInt64 = 200
///|
fn Widget::slides(self : Widget) -> Bool {
slide_modules.contains(self.module_id)
}
///| How far into the running slide we are, 0.0 .. 1.0.
fn Widget::slide_progress(self : Widget, now : UInt64) -> Double {
if self.slide_t0 == 0 || now <= self.slide_t0 {
return 0.0
}
let p = (now - self.slide_t0).to_double() / slide_ms.to_double()
if p > 1.0 {
1.0
} else {
p
}
}
///| Start (or turn around) a slide, `slide_in` or out. A slide that is turned around midway
/// carries on from where it got to.
fn Widget::start_slide(self : Widget, slide_in : Bool) -> Unit {
let now = @sys.monotonic_ms()
let left = if self.slide_t0 != 0 {
let p = self.slide_progress(now)
if p < 1.0 {
((1.0 - p) * slide_ms.to_double()).to_uint64()
} else {
0UL
}
} else {
0UL
}
// Turning around at progress p starts the other way at progress 1 - p.
self.slide_t0 = if now > left { now - left } else { 1UL }
self.slide_in = slide_in
}
///| The widget's popup just opened.
fn DesktopShell::slide_in(self : DesktopShell, w : Widget) -> Unit {
ignore(self)
if w.slides() {
w.ghost_ops = []
w.ghost_rect = { x: 0, y: 0, w: 0, h: 0 }
w.start_slide(true)
}
}
///| The widget's popup just closed: it was at `rect` showing `ops`; slide that back in.
fn DesktopShell::slide_out(
self : DesktopShell,
w : Widget,
rect : @scene.Rect,
ops : Array[@types.DrawOp],
) -> Unit {
ignore(self)
if w.slides() {
w.ghost_rect = rect
w.ghost_ops = ops
w.start_slide(false)
}
}
///| True while a popup is sliding: the compositor then keeps drawing frames.
pub fn DesktopShell::animating(self : DesktopShell) -> Bool {
self.widgets.iter().any(w => w.slide_t0 != 0)
}
///| How far (pixels) a popup of height `h` is pushed back into the panel at slide progress `p`:
/// sliding in eases out (fast, then settling), sliding out eases in.
pub fn slide_offset(h : Int, p : Double, slide_in : Bool) -> Int {
let shown = if slide_in {
let q = 1.0 - p
1.0 - q * q * q
} else {
1.0 - p * p
}
((1.0 - shown) * h.to_double()).to_int()
}
///| Draw `w`'s popup (or the one sliding away) into `out`.
fn DesktopShell::draw_popup(self : DesktopShell, w : Widget, out : Array[@scene.DrawOp]) -> Unit {
let (rect, ops) = if w.popup_rect.w > 0 {
(w.popup_rect, w.popup_ops)
} else {
(w.ghost_rect, w.ghost_ops)
}
if rect.w <= 0 {
w.slide_t0 = 0
return
}
if w.slide_t0 == 0 {
if w.popup_rect.w > 0 {
translate(ops, rect.x, rect.y, out)
}
return
}
let p = w.slide_progress(@sys.monotonic_ms())
if p >= 1.0 {
// Done: drawn in place (or gone).
w.slide_t0 = 0
w.ghost_ops = []
w.ghost_rect = { x: 0, y: 0, w: 0, h: 0 }
if w.popup_rect.w > 0 {
translate(ops, rect.x, rect.y, out)
}
return
}
// A popup above the panel slides down into it; one below a top panel slides up.
let dir = if rect.y < self.panel_rect.y { 1 } else { -1 }
let dy = dir * slide_offset(rect.h, p, w.slide_in)
out.push(@scene.DrawOp::PushClip(rect))
translate(ops, rect.x, rect.y + dy, out)
out.push(@scene.DrawOp::PopClip)
}
///| The popup's drawn vertical offset from its place right now (0 when not sliding); for tests.
pub fn DesktopShell::slot_slide_offset(self : DesktopShell, slot : String) -> Int {
match self.widget_by_slot(slot) {
Some(w) if w.slide_t0 != 0 => {
let rect = if w.popup_rect.w > 0 { w.popup_rect } else { w.ghost_rect }
slide_offset(rect.h, w.slide_progress(@sys.monotonic_ms()), w.slide_in)
}
_ => 0
}
}
///|
test "slide offsets run from the panel to the popup's place and back" {
assert_eq(slide_offset(300, 0.0, true), 300)
assert_eq(slide_offset(300, 1.0, true), 0)
assert_true(slide_offset(300, 0.5, true) < 150) // eases out: most of the way after half the time
assert_eq(slide_offset(300, 0.0, false), 0)
assert_eq(slide_offset(300, 1.0, false), 300)
assert_true(slide_offset(300, 0.5, false) < 150) // eases in
}
+13
View File
@@ -309,6 +309,10 @@ pub fn DesktopShell::load_path(self : DesktopShell, path : String) -> Widget? {
popup_rect: { x: 0, y: 0, w: 0, h: 0 },
popup_ops: [],
popup_hot: false,
slide_t0: 0,
slide_in: false,
ghost_rect: { x: 0, y: 0, w: 0, h: 0 },
ghost_ops: [],
}
self.next_slot = self.next_slot + 1
self.apply_module_cfg(w)
@@ -695,8 +699,14 @@ fn DesktopShell::refresh_popup(self : DesktopShell, w : Widget) -> Unit {
if self.widget_by_slot(w.slot) is None {
return
}
let was_open = w.popup_rect.w > 0
let old_rect = w.popup_rect
let old_ops = w.popup_ops
match w.pl.popup() {
None => {
if was_open {
self.slide_out(w, old_rect, old_ops)
}
w.popup_rect = { x: 0, y: 0, w: 0, h: 0 }
w.popup_ops = []
w.popup_hot = false
@@ -705,6 +715,9 @@ fn DesktopShell::refresh_popup(self : DesktopShell, w : Widget) -> Unit {
let (px, py) = @panel.popup_position(self.screen_w, self.screen_h - self.panel_h, w.rect.x, ps.w, ps.h, 6)
w.popup_rect = { x: px, y: py, w: ps.w, h: ps.h }
w.popup_ops = ps.ops
if !was_open {
self.slide_in(w)
}
}
}
}