Scoop API

scoop:scoop.ui.component:0.1.0

src/toast/clock.scoop

Selected production source · src/toast/clock.scoop

1package scoop.ui.component
3import scoop.platform.time.MonotonicInstant
4import scoop.time.*
6/** A paused clock retains the unused portion, independent of host mounting. */
7internal class ToastClock(duration: Duration?) {
8 var remaining: Duration? = duration
9 private var started: MonotonicInstant? = None
10 val deadline: MonotonicInstant? get() =
11 when ((started, remaining)) {
12 case(Some(val time), Some(val value)) -> Some(time + value)
13 else -> None
14 }
15 fun reset(duration: Duration?) {
16 remaining = duration;
17 started = None
18 }
19 fun pause(now: MonotonicInstant) {
20 when (deadline) {
21 case Some(val end) -> remaining =
22 Some(
23 if (end > now)
24 end - now
25 else
26 Duration.ZERO
27 )
28 case None -> {}
29 }
30 started = None
31 }
32 fun resume(now: MonotonicInstant) {
33 if (started == None && panelPresent(remaining))
34 started = Some(now)
35 }