Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Phone watchers see the last 3 chat lines faintly over the bottom of the video, never while driving #378
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/288-phone-watchers-see-the-last-3-chat-lines → main 1 commit fork 1 thread · 1 unresolved
5 files
changed +123
−1
Software/so101/src/ui/chat-glass.js added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | // @ts-check | |
| 2 | /** | |
| 3 | * The last chat lines faintly over the bottom of the camera, for someone who is watching on a phone held upright | |
| 4 | * (ui/mobile.css draws #chatGlass in the camera's row only, so it never covers the model). The driver never sees it. | |
| 5 | * Lines fade after a few seconds, and a tap hides what is showing. Text only: the page uses textContent. | |
| 6 | */ | |
| 7 | ||
| 8 | const SHOWN = 3; | |
| 9 | const MAX = 160; | |
| 10 | ||
| 11 | /** @param {{ now?: () => number, ttl?: number }} [o] */ | |
| 12 | export function createGlass({ now = Date.now, ttl = 6000 } = {}) { | |
| 13 | /** @type {Array<{ name: string, text: string, at: number }>} */ let lines = []; | |
| 14 | let hiddenAt = -Infinity; | |
| 15 | return { | |
| 16 | push(/** @type {string} */ name, /** @type {string} */ text) { | |
| 17 | // eslint-disable-next-line no-control-regex | |
| 18 | const t = String(text).replace(/[\u0000-\u001f\u007f]+/g, " ").trim().slice(0, MAX); | |
| 19 | if (t) lines = [...lines, { name: String(name).slice(0, 40), text: t, at: now() }].slice(-SHOWN); | |
| 20 | }, | |
| 21 | /** A tap on the video hides the lines showing now; a line that arrives later shows again. */ | |
| 22 | tap() { hiddenAt = now(); }, | |
| 23 | /** The lines to draw: the last 3 that are still fresh and not tapped away; none while driving. */ | |
| 24 | show(/** @type {boolean} */ driving) { | |
| 25 | if (driving) return []; | |
| 26 | const t = now(); | |
| 27 | return lines.filter((l) => t - l.at < ttl && l.at > hiddenAt); | |
| 28 | }, | |
| 29 | /** Milliseconds until the oldest showing line fades, or 0 when none shows. */ | |
| 30 | nextFade(/** @type {boolean} */ driving) { | |
| 31 | const s = this.show(driving); | |
| 32 | return s.length ? Math.max(1, ttl - (now() - s[0].at)) : 0; | |
| 33 | }, | |
| 34 | }; | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * @param {HTMLElement} box the overlay | |
| 39 | * @param {(tag: string, props?: object) => HTMLElement} el | |
| 40 | * @param {() => boolean} mayDrive | |
| 41 | */ | |
| 42 | export function mountGlass(box, el, mayDrive) { | |
| 43 | const glass = createGlass(); | |
| 44 | /** @type {any} */ let timer = 0; | |
| 45 | function render() { | |
| 46 | clearTimeout(timer); | |
| 47 | const lines = glass.show(mayDrive()); | |
| 48 | box.replaceChildren(...lines.map((l) => line(el, l))); | |
| 49 | const wait = glass.nextFade(mayDrive()); | |
| 50 | if (wait) timer = setTimeout(render, wait); | |
| 51 | } | |
| 52 | box.addEventListener("click", () => { glass.tap(); render(); }); | |
| 53 | return { | |
| 54 | said(/** @type {Array<{ from?: { name: string }, text: string }>} */ messages) { | |
| 55 | for (const m of messages) if (m.from) glass.push(m.from.name, m.text); | |
| 56 | render(); | |
| 57 | }, | |
| 58 | /** The turn changed hands: a driver's lines go at once. */ | |
| 59 | refresh: render, | |
| 60 | }; | |
| 61 | } | |
| 62 | ||
| 63 | /** @param {(tag: string, props?: object) => HTMLElement} el @param {{ name: string, text: string }} l */ | |
| 64 | function line(el, l) { | |
| 65 | const d = el("div", { className: "gl" }); | |
| 66 | d.append(el("b", { textContent: l.name }), el("span", { textContent: l.text })); | |
| 67 | return d; | |
| 68 | } |
Software/so101/src/ui/chat-glass.test.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { createGlass } from "./chat-glass.js"; | |
| 3 | ||
| 4 | function room() { | |
| 5 | let t = 1000; | |
| 6 | const g = createGlass({ now: () => t, ttl: 6000 }); | |
| 7 | for (const n of ["a", "b", "c", "d"]) { g.push(n, `hi from ${n}`); t += 100; } | |
| 8 | return { g, at: (ms: number) => { t = ms; } }; | |
| 9 | } | |
| 10 | ||
| 11 | describe("chat over the camera", () => { | |
| 12 | test("a watcher sees the last 3 lines, a driver sees 0", () => { | |
| 13 | const { g } = room(); | |
| 14 | expect(g.show(false).map((l) => l.name)).toEqual(["b", "c", "d"]); | |
| 15 | expect(g.show(true)).toEqual([]); | |
| 16 | }); | |
| 17 | test("lines fade after a few seconds", () => { | |
| 18 | const { g, at } = room(); | |
| 19 | at(1000 + 6150); | |
| 20 | expect(g.show(false).map((l) => l.name)).toEqual(["c", "d"]); | |
| 21 | at(1000 + 7000); | |
| 22 | expect(g.show(false)).toEqual([]); | |
| 23 | expect(g.nextFade(false)).toBe(0); | |
| 24 | }); | |
| 25 | test("a tap hides them, and a later line shows again", () => { | |
| 26 | const { g, at } = room(); | |
| 27 | at(1500); g.tap(); | |
| 28 | expect(g.show(false)).toEqual([]); | |
| 29 | at(1600); g.push("e", "back"); | |
| 30 | expect(g.show(false).map((l) => l.name)).toEqual(["e"]); | |
| 31 | }); | |
| 32 | test("text is kept as text: control characters go and the length is bounded", () => { | |
| 33 | const g = createGlass(); | |
| 34 | g.push("x", "<img src=x onerror=1>\n" + "z".repeat(500)); | |
| 35 | const [l] = g.show(false); | |
| 36 | expect(l!.text).not.toContain("\n"); | |
| 37 | expect(l!.text.length).toBe(160); | |
| 38 | }); | |
| 39 | }); |
Software/so101/src/ui/lobby-panel.js +2 −1
| @@ -32,7 +32,7 @@ export const backoff = (attempt) => Math.min(250 * 2 ** Math.max(0, attempt - 1) | ||
| 32 | 32 | const tell = (what) => /** @type {any} */ (globalThis).dispatchEvent?.(new Event(what)); |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | * @param {any} page what the page owns: $, el, relayed, tab(), showTab, msg, applyLock, applyStatus, noStatus, words, admin, note | |
| 35 | * @param {any} page what the page owns: said(lines) for new chat lines, $, el, relayed, tab(), showTab, msg, applyLock, applyStatus, noStatus, words, admin, note | |
| 36 | 36 | */ |
| 37 | 37 | export function createRoom(page) { |
| 38 | 38 | const { $, el, relayed, msg } = page; |
| @@ -225,6 +225,7 @@ export function createRoom(page) { | ||
| 225 | 225 | const atEnd = chatBox.scrollHeight - chatBox.scrollTop - chatBox.clientHeight < 40; |
| 226 | 226 | if (replace) chatBox.replaceChildren(); |
| 227 | 227 | chatBox.append(...lines.map(chatLine)); |
| 228 | if (!replace) page.said?.(lines); | |
| 228 | 229 | if (!replace) while (chatBox.querySelectorAll(".line").length > 200) chatBox.querySelector(".line").remove(); |
| 229 | 230 | if (atEnd || replace) toEnd(); |
| 230 | 231 | else if (lines.length) pill.hidden = false; // the reader is up in the history: keep the place, offer the way down |
Software/so101/src/ui/mobile.css +5
| @@ -17,6 +17,11 @@ | ||
| 17 | 17 | #stage { display: contents; } |
| 18 | 18 | #stage > .pane { grid-row: 1; grid-column: 1; min-height: 0; } |
| 19 | 19 | #stage > #pane3 { grid-row: 3; } |
| 20 | /* Chat over the camera for a watcher (ui/chat-glass.js): in the camera's row only, never over the model; text, faint, 3 lines. */ | |
| 21 | #chatGlass { grid-row: 1; grid-column: 1; align-self: end; z-index: 1; display: flex; flex-direction: column; gap: 2px; padding: 0 calc(8px + var()) 6px calc(8px + var()); min-width: 0; } | |
| 22 | #chatGlass:empty { display: none; } | |
| 23 | #chatGlass .gl { font-size: 13px; line-height: 1.3; color: rgba(255,255,255,.8); text-shadow: 0 1px 2px #000; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } | |
| 24 | #chatGlass .gl b { margin-right: 6px; font-weight: 650; } | |
| 20 | 25 | /* The strip sits over the view's middle row, so it paints above it. */ |
| 21 | 26 | #strip { position: relative; z-index: 1; } |
| 22 | 27 | /* The model is always on here, so its view button goes; the camera buttons pick which camera is on top. */ |
Software/so101/src/viewer.html +9
| @@ -51,6 +51,7 @@ | ||
| 51 | 51 | input[type=number] { width: 88px; } |
| 52 | 52 | label.inline { display: inline-flex; gap: 8px; align-items: center; color: var(); font-size: 13px; } |
| 53 | 53 | #tableNote { min-height: 3em; } |
| 54 | #chatGlass { display: none; } /* ui/mobile.css shows it on a phone held upright */ | |
| 54 | 55 | #msg { overflow-wrap: anywhere; } |
| 55 | 56 | .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; } |
| 56 | 57 | [hidden] { display: none ; } |
| @@ -281,6 +282,7 @@ | ||
| 281 | 282 | <button id="fs" title="F">Fullscreen</button> |
| 282 | 283 | </div> |
| 283 | 284 | <div id="driverTag" aria-hidden="true"></div> |
| 285 | <div id="chatGlass" aria-hidden="true"></div> | |
| 284 | 286 | <div id="banner" role="alert"></div> |
| 285 | 287 | <div id="msg" role="status"></div> |
| 286 | 288 | <div id="refusal" role="status"></div> |
| @@ -1304,10 +1306,16 @@ | ||
| 1304 | 1306 | // The page follows suit: the Controls tab appears on your turn. Everything here arrives on one socket. |
| 1305 | 1307 | let wordsSent = false; |
| 1306 | 1308 | $("wordsForm").addEventListener("submit", (e) => { e.preventDefault(); wordsSent = true; say({ type: "words", words: $("wordsForm").elements.words.value.split(",").map((w) => w.trim()).filter(Boolean) }); }); |
| 1309 | /** @type {Promise<any> | undefined} */ let glass; | |
| 1307 | 1310 | const fallback = {}; // the last text per key, shown on the hold line when the notes module will not load |
| 1308 | 1311 | const note = noteSink(() => import("/ui/__VERSION__/notes.js").then((m) => m.mountNotes($, el)), (k, t) => { fallback[k] = t; $("holdLine").textContent = Object.values(fallback).join(" "); }); |
| 1309 | 1312 | const room = createRoom({ |
| 1310 | 1313 | $, el, relayed, msg, note, tab: () => tab, showTab, applyLock: () => applyLock(), applyStatus: (s) => applyStatus(s), noStatus: () => noStatus(), |
| 1314 | said(lines) { // phone portrait, watching: the last lines faintly over the camera (ui/chat-glass.js) | |
| 1315 | if (!stacked.matches) return; | |
| 1316 | glass ??= import("/ui/__VERSION__/chat-glass.js").then((m) => m.mountGlass($("chatGlass"), el, () => room.mayDrive()), () => null); | |
| 1317 | glass.then((g) => g?.said(lines)); | |
| 1318 | }, | |
| 1311 | 1319 | words(m) { const f = $("wordsForm"); if (document.activeElement !== f.elements.words) f.elements.words.value = m.words.join(", "); if (wordsSent) { wordsSent = false; msg(`Word filter saved: ${m.words.length} word${m.words.length === 1 ? "" : "s"}.`); } }, |
| 1312 | 1320 | admin: (saved) => fillAdmin(saved), |
| 1313 | 1321 | }); |
| @@ -1340,6 +1348,7 @@ | ||
| 1340 | 1348 | // Your turn just came: open the controls. It just ended: back to the chat. |
| 1341 | 1349 | if (mayDrive() !== drove) { |
| 1342 | 1350 | drove = mayDrive(); |
| 1351 | glass?.then((g) => g?.refresh()); | |
| 1343 | 1352 | if (relayed && phone.matches && dock) dock.go(drove ? "drive" : dock.panel === "drive" ? "chat" : dock.panel, true); |
| 1344 | 1353 | else if (relayed) showTab(drove ? "controls" : "chat"); |
| 1345 | 1354 | } |
new above
offline
The overlay is a click target over the camera. Add pointer-events:none, or keep it and check on a phone that it does not eat video taps.