Files
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

148 lines
4.5 KiB
MoonBit

///| 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
}