diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-10 13:03:07 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-10 13:03:07 +0100 |
| commit | 8ec2352c22780202fcd39982f1c3e65d2dae0cc1 (patch) | |
| tree | be44dfe4b18fe58c6b3b998be5c7c0cdcff46648 | |
| parent | 7a9d73a2021d0d14be556531adee83531aaa6c15 (diff) | |
| download | deck-it-8ec2352c22780202fcd39982f1c3e65d2dae0cc1.tar.gz deck-it-8ec2352c22780202fcd39982f1c3e65d2dae0cc1.zip | |
feat: "The PC is asleep" overlay, and the tablet may sleep too
A sleeping PC answers no poll, and the wake lock kept the tablet's screen
on over a frozen dashboard all night. After 15 s without any answer the
page shows an overlay and releases the lock; the first answer hides it
and takes the lock again. Checked on the tablet by stopping the server
for 21 s: no screen wake lock while stopped, Chrome's taken again 5 s
after it resumed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| -rw-r--r-- | AGENTS.md | 3 | ||||
| -rw-r--r-- | static/app.js | 33 | ||||
| -rw-r--r-- | static/deck.css | 9 | ||||
| -rw-r--r-- | static/index.html | 5 |
4 files changed, 48 insertions, 2 deletions
@@ -64,6 +64,9 @@ Keep the server dependency-free apart from that one package. (vendored `marked`, HTML escaped, images never fetched). - The page holds a Screen Wake Lock while visible, so the tablet never sleeps over the dashboard; nothing on the tablet's settings is changed. + After 15s with no answer from the server (the PC asleep, or deck-it + gone) `app.js` covers the page with a "The PC is asleep" overlay and + drops the lock, so the tablet sleeps too; the first answer takes both back. - Launchers are `[launch] apps`, desktop ids run with `gtk-launch`; label and icon come from the `.desktop` file. The client sends the id, which must be configured and installed. diff --git a/static/app.js b/static/app.js index 2f61c81..fb92352 100644 --- a/static/app.js +++ b/static/app.js @@ -228,6 +228,7 @@ function startPoll(name, panel, el) { const poll = async () => { try { const r = await fetch(`/api/${name}`); + awake(); if (r.ok) panel.update?.(await r.json(), el); } catch { // keep the last state; the next tick retries @@ -333,17 +334,45 @@ setInterval(refreshTheme, 5000); // dims or starts its screensaver. The lock drops whenever the page is hidden // and is taken again when it comes back. 127.0.0.1 is a secure context, which // the Wake Lock API requires. +const asleep = document.getElementById("asleep"); +let wakeLock = null; async function keepAwake() { try { - if (document.visibilityState === "visible") await navigator.wakeLock?.request("screen"); + if (document.visibilityState === "visible" && asleep.hidden && !wakeLock) { + wakeLock = (await navigator.wakeLock?.request("screen")) ?? null; + wakeLock?.addEventListener("release", () => { wakeLock = null; }); + } } catch { // refused (battery saver): the screen just follows its own timeout } } keepAwake(); +// A sleeping PC answers nothing: every poll fails or hangs. After 15 s +// without any answer the page says so and drops the wake lock, so the tablet +// sleeps too; the first answer takes both back. +// ponytail: a crashed server or a dropped tunnel reads as asleep as well. +const ASLEEP_AFTER = 15000; +let lastAnswer = Date.now(); +function awake() { + lastAnswer = Date.now(); + if (!asleep.hidden) { + asleep.hidden = true; + keepAwake(); + } +} +setInterval(() => { + if (asleep.hidden && Date.now() - lastAnswer > ASLEEP_AFTER) { + asleep.hidden = false; + wakeLock?.release(); + } +}, 2000); + document.addEventListener("visibilitychange", () => { - if (document.visibilityState === "visible") refreshers.forEach((fn) => fn()); + if (document.visibilityState === "visible") { + lastAnswer = Math.max(lastAnswer, Date.now() - ASLEEP_AFTER + 5000); // a moment for the refresh below + refreshers.forEach((fn) => fn()); + } keepAwake(); }); diff --git a/static/deck.css b/static/deck.css index faaf789..d998af4 100644 --- a/static/deck.css +++ b/static/deck.css @@ -677,3 +677,12 @@ select { .assistant .as-op.good { color: var(--udt-green); } .assistant .as-op.bad { color: var(--udt-red); } .assistant .md, .assistant .as-q { user-select: text; } + +/* The PC asleep: over everything, and the wake lock is dropped (app.js). */ +#asleep { + position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; + align-items: center; justify-content: center; gap: 10px; text-align: center; + background: color-mix(in srgb, var(--udt-crust) 92%, transparent); +} +#asleep[hidden] { display: none; } +.asleep-moon { font-size: 3rem; color: var(--udt-overlay0); } diff --git a/static/index.html b/static/index.html index b45904c..d199a35 100644 --- a/static/index.html +++ b/static/index.html @@ -40,6 +40,11 @@ <button id="reload" class="reload" aria-label="Reload the dashboard" title="Reload">⟳</button> </header> <div id="tab-body"></div> +<div id="asleep" hidden> + <div class="asleep-moon nf"></div> + <div class="off-title">The PC is asleep</div> + <div class="off-detail">The dashboard comes back when it wakes. Until then the tablet can sleep too.</div> +</div> <script src="vendor/gridstack/gridstack-all.js"></script> <script type="module" src="app.js"></script> </body> |
