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