Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Mobile drive sheet: a bottom sheet with two snap points - peek (status, STOP, gripper) and open (~45dvh, joint controls) #218
quill/arm:quill/92-mobile-drive-sheet-a-bottom-sheet-with-t → main 4 commits fork
6 files
changed +223
−4
Software/README.md +8
| @@ -309,6 +309,14 @@ module (`src/ui/tabs.js`) has loaded; until then, or if it fails to load, the | ||
| 309 | 309 | sheet's own tabs stay. STOP is in the status strip above, in no tab. The bar is |
| 310 | 310 | absent on a wide screen and on a phone on its side. |
| 311 | 311 | |
| 312 | On your turn, Drive's controls are a **bottom sheet** with two heights: **peek** | |
| 313 | (96 px: a handle and the gripper's Open / Close, under the strip with STOP, so | |
| 314 | most of the screen is the view) and **open** (45% of the screen: the joint | |
| 315 | controls too). Drag the handle and let go to settle on the nearer height, flick | |
| 316 | it to go the way it went, or tap it (or press Enter on it) to switch. It opens by | |
| 317 | itself when your turn starts. With reduced motion asked for, it moves without | |
| 318 | animating. The sheet is `src/ui/sheet.js`, loaded with the bar. | |
| 319 | ||
| 312 | 320 | **The Machine tab: the arm, not a magic box.** Anyone watching can open the |
| 313 | 321 | **Machine** tab (a column beside the view on a wide screen). It shows, pushed |
| 314 | 322 | as it happens with nothing polled: |
Software/so101/src/server.test.ts +5 −2
| @@ -181,8 +181,11 @@ describe("viewer page", () => { | ||
| 181 | 181 | expect(r.headers.get("content-type")).toBe("text/javascript"); |
| 182 | 182 | expect(r.headers.get("cache-control")).toContain("immutable"); |
| 183 | 183 | const version = path!.split("/")[2]; |
| 184 | const css = await fetch(`${base}/ui/${version}/tabs.css`); | |
| 185 | expect([css.status, css.headers.get("content-type")]).toEqual([200, "text/css"]); | |
| 184 | for (const name of ["tabs.css", "sheet.css"]) { | |
| 185 | expect(page).toContain(`/ui/${version}/${name}`); | |
| 186 | const css = await fetch(`${base}/ui/${version}/${name}`); | |
| 187 | expect([css.status, css.headers.get("content-type")]).toEqual([200, "text/css"]); | |
| 188 | } | |
| 186 | 189 | expect((await fetch(`${base}/ui/${version}/reach.test.ts`)).status).toBe(404); |
| 187 | 190 | expect((await fetch(`${base}/ui/${version}/nothing.js`)).status).toBe(404); |
| 188 | 191 | expect((await fetch(`${base}/ui/${version}/..%2Fserver.ts`)).status).toBe(404); |
Software/so101/src/ui/sheet.css added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | /* The phone-portrait drive sheet (src/ui/sheet.js): on your turn the Drive panel's sheet is a bottom sheet with two points, | |
| 2 | peek (96px: the handle and the gripper, under the strip with STOP) and open (45dvh: the joint controls as well). | |
| 3 | Every rule keys on [data-sheet], which the page sets once the module has loaded: until then the sheet is the dock's. */ | |
| 4 | #peek { display: none; } | |
| 5 | @media (max-width: 640px) and (orientation: portrait) { | |
| 6 | html #shell.docked.mayDrive[data-panel="drive"][data-sheet] { grid-template-rows: minmax(0, 1fr) auto auto calc(56px + var()); } | |
| 7 | .docked.mayDrive[data-panel="drive"][data-sheet] #sheet { height: 96px; overflow: hidden; border-top: 1px solid var(); transition: height .22s cubic-bezier(.2, .8, .2, 1); } | |
| 8 | .docked.mayDrive[data-panel="drive"][data-sheet="open"] #sheet { height: 45dvh; } | |
| 9 | .docked.mayDrive[data-panel="drive"][data-sheet].dragging #sheet { transition: none; } | |
| 10 | .docked.mayDrive[data-panel="drive"][data-sheet] #peek { flex: none; display: grid; grid-template-rows: 32px 56px; height: 96px; padding: 0 calc(12px + var()) 8px calc(12px + var()); } | |
| 11 | /* the handle: the whole width is the grip, the bar is only its mark; it takes the finger from the page (no scroll, no zoom) */ | |
| 12 | #grab { display: grid; place-items: center; min-height: 0; width: 100%; padding: 0; border: 0; border-radius: 0; background: none; touch-action: none; -webkit-user-select: none; user-select: none; cursor: ns-resize; } | |
| 13 | #grab:hover { background: none; } | |
| 14 | #grab::before { content: ""; width: 40px; height: 5px; border-radius: 3px; background: var(); } | |
| 15 | #grab:hover::before, .dragging #grab::before { background: var(); } | |
| 16 | #sheetGrip { display: grid; grid-template-columns: auto 1fr 1fr; gap: 8px; align-items: center; } | |
| 17 | #sheetGrip > span { color: var(); font-size: 12px; font-weight: 650; letter-spacing: .06em; text-transform: uppercase; padding-right: 4px; } | |
| 18 | #sheetGrip button { min-height: 48px; font-weight: 650; touch-action: none; -webkit-user-select: none; user-select: none; } | |
| 19 | #sheetGrip button.held { background: var(); border-color: var(); color: var(); } | |
| 20 | /* the joint controls: a finger scrolls them, and the lever fits what open leaves of 45dvh; in peek they are out of reach */ | |
| 21 | .docked.mayDrive[data-panel="drive"][data-sheet] #tab-controls { padding-top: 4px; touch-action: pan-y; } | |
| 22 | .docked.mayDrive[data-panel="drive"][data-sheet] #one { height: clamp(150px, calc(45dvh - 200px), 300px); } | |
| 23 | .docked.mayDrive[data-panel="drive"][data-sheet="peek"]:not(.dragging) #tab-controls { visibility: hidden; transition: visibility 0s .22s; } | |
| 24 | } | |
| 25 | @media (prefers-reduced-motion: reduce) { #sheet, #tab-controls { transition: none ; } } |
Software/so101/src/ui/sheet.js added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | // The phone's drive sheet: two snap points, peek (the gripper under the strip with STOP) and open (the joint controls). | |
| 2 | // A drag on the handle follows the finger; letting go snaps to the nearer point, or to the one a flick heads for. A tap toggles. | |
| 3 | ||
| 4 | /** Peek's height in CSS pixels: the handle and the gripper row. Open is this share of the viewport's height. */ | |
| 5 | export const PEEK = 96; | |
| 6 | export const OPEN_SHARE = 0.45; | |
| 7 | /** A finger that moved less than this, in pixels, tapped; a release faster than this, in px/ms, is a flick. */ | |
| 8 | export const TAP = 6; | |
| 9 | export const FLICK = 0.4; | |
| 10 | ||
| 11 | /** The two heights for a viewport `vh` pixels tall; open is never below peek. */ | |
| 12 | export const points = (vh) => ({ peek: PEEK, open: Math.max(PEEK, Math.round((Number.isFinite(vh) ? vh : 0) * OPEN_SHARE)) }); | |
| 13 | ||
| 14 | /** The height under a finger that started at `start` and has moved `dy` (down is positive), held between the two points. */ | |
| 15 | export const dragHeight = (start, dy, { peek, open }) => { | |
| 16 | const h = start - (Number.isFinite(dy) ? dy : 0); | |
| 17 | return Math.min(open, Math.max(peek, Number.isFinite(h) ? h : peek)); | |
| 18 | }; | |
| 19 | ||
| 20 | /** Where a released sheet settles: a flick goes the way it went (up, a negative velocity, opens); otherwise the nearer point. */ | |
| 21 | export const snapTo = (height, { peek, open }, velocity = 0) => { | |
| 22 | if (Number.isFinite(velocity) && Math.abs(velocity) >= FLICK) return velocity < 0 ? "open" : "peek"; | |
| 23 | return height >= (peek + open) / 2 ? "open" : "peek"; | |
| 24 | }; | |
| 25 | ||
| 26 | export const other = (state) => (state === "open" ? "peek" : "open"); | |
| 27 | ||
| 28 | /** | |
| 29 | * The sheet on the page: `shell.dataset.sheet` names the point, which the stylesheet turns into a height; `grab` is the | |
| 30 | * handle (a button: a tap or Enter toggles). While a finger drags it the height is set inline and the transition is off. | |
| 31 | */ | |
| 32 | export function mountSheet(shell, sheet, grab, state = "open") { | |
| 33 | const me = { state, set }; | |
| 34 | function set(next) { | |
| 35 | me.state = next === "peek" ? "peek" : "open"; | |
| 36 | shell.dataset.sheet = me.state; | |
| 37 | grab.setAttribute("aria-expanded", String(me.state === "open")); | |
| 38 | grab.setAttribute("aria-label", me.state === "open" ? "Lower the joint controls" : "Raise the joint controls"); | |
| 39 | } | |
| 40 | let drag = null; | |
| 41 | grab.addEventListener("pointerdown", (e) => { | |
| 42 | if (drag || e.button > 0) return; | |
| 43 | e.preventDefault(); | |
| 44 | try { grab.setPointerCapture(e.pointerId); } catch {} | |
| 45 | drag = { id: e.pointerId, y: e.clientY, start: sheet.getBoundingClientRect().height, at: [[e.timeStamp, e.clientY]], moved: false }; | |
| 46 | }); | |
| 47 | grab.addEventListener("pointermove", (e) => { | |
| 48 | if (e.pointerId !== drag?.id) return; | |
| 49 | const dy = e.clientY - drag.y; | |
| 50 | if (!drag.moved && Math.abs(dy) < TAP) return; | |
| 51 | drag.moved = true; | |
| 52 | shell.classList.add("dragging"); | |
| 53 | sheet.style.height = `${dragHeight(drag.start, dy, points(innerHeight))}px`; | |
| 54 | drag.at = [...drag.at.slice(-4), [e.timeStamp, e.clientY]]; | |
| 55 | }); | |
| 56 | const end = (e) => { | |
| 57 | if (e.pointerId !== drag?.id) return; | |
| 58 | const d = drag; drag = null; | |
| 59 | shell.classList.remove("dragging"); | |
| 60 | if (e.type === "pointercancel" && d.moved) return void ((sheet.style.height = ""), set(me.state)); | |
| 61 | if (!d.moved) return void set(other(me.state)); | |
| 62 | const [[t0, y0]] = d.at, [t1, y1] = d.at.at(-1), v = t1 > t0 ? (y1 - y0) / (t1 - t0) : 0; | |
| 63 | sheet.style.height = ""; | |
| 64 | set(snapTo(dragHeight(d.start, e.clientY - d.y, points(innerHeight)), points(innerHeight), v)); | |
| 65 | }; | |
| 66 | for (const ev of ["pointerup", "pointercancel"]) grab.addEventListener(ev, end); | |
| 67 | grab.addEventListener("click", (e) => { if (e.detail === 0) set(other(me.state)); }); // the keyboard's: a pointer has already toggled on pointerup | |
| 68 | set(state); | |
| 69 | return me; | |
| 70 | } |
Software/so101/src/ui/sheet.test.ts added +104
| @@ -0,0 +1,104 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { dragHeight, FLICK, mountSheet, other, PEEK, points, snapTo } from "./sheet.js"; | |
| 3 | ||
| 4 | describe("the phone's drive sheet", () => { | |
| 5 | test("peek is 96px and open is 45% of the viewport, never below peek", () => { | |
| 6 | expect(points(844)).toEqual({ peek: 96, open: 380 }); | |
| 7 | expect(points(915)).toEqual({ peek: 96, open: 412 }); | |
| 8 | expect(points(100).open).toBe(PEEK); | |
| 9 | expect(points(Number.NaN).open).toBe(PEEK); | |
| 10 | }); | |
| 11 | ||
| 12 | test("a drag follows the finger, up raises it, and stays between the two points", () => { | |
| 13 | const p = points(844); | |
| 14 | expect(dragHeight(96, -100, p)).toBe(196); | |
| 15 | expect(dragHeight(380, 50, p)).toBe(330); | |
| 16 | expect(dragHeight(96, -1000, p)).toBe(380); | |
| 17 | expect(dragHeight(380, 1000, p)).toBe(96); | |
| 18 | expect(dragHeight(200, Number.NaN, p)).toBe(200); | |
| 19 | }); | |
| 20 | ||
| 21 | test("a slow release settles on the nearer point", () => { | |
| 22 | const p = points(844); | |
| 23 | expect(snapTo(240, p)).toBe("open"); | |
| 24 | expect(snapTo(240, p, -FLICK / 2)).toBe("open"); | |
| 25 | expect(snapTo(230, p, FLICK / 2)).toBe("peek"); | |
| 26 | expect(snapTo(96, p)).toBe("peek"); | |
| 27 | expect(snapTo(380, p)).toBe("open"); | |
| 28 | }); | |
| 29 | ||
| 30 | test("a flick goes the way it went, wherever it was let go", () => { | |
| 31 | const p = points(844); | |
| 32 | expect(snapTo(110, p, -FLICK)).toBe("open"); | |
| 33 | expect(snapTo(370, p, FLICK)).toBe("peek"); | |
| 34 | expect(snapTo(370, p, Number.POSITIVE_INFINITY)).toBe("open"); // not a number it can trust: the nearer point | |
| 35 | }); | |
| 36 | ||
| 37 | test("a tap toggles", () => { | |
| 38 | expect(other("open")).toBe("peek"); | |
| 39 | expect(other("peek")).toBe("open"); | |
| 40 | }); | |
| 41 | }); | |
| 42 | ||
| 43 | /** Just enough of the DOM for mountSheet: event targets with attributes, a dataset, a class list and a measured height. */ | |
| 44 | function page(height = 96) { | |
| 45 | const classes = new Set<string>(); | |
| 46 | const shell = Object.assign(new EventTarget(), { dataset: {} as Record<string, string>, classList: { add: (c: string) => classes.add(c), remove: (c: string) => classes.delete(c) } }); | |
| 47 | const sheet = Object.assign(new EventTarget(), { style: { height: "" }, getBoundingClientRect: () => ({ height }) }); | |
| 48 | const attrs: Record<string, string> = {}; | |
| 49 | const grab = Object.assign(new EventTarget(), { setAttribute: (k: string, v: string) => (attrs[k] = v), setPointerCapture() {} }); | |
| 50 | return { shell, sheet, grab, attrs, classes }; | |
| 51 | } | |
| 52 | const pointer = (type: string, clientY: number, timeStamp: number, pointerId = 1) => { | |
| 53 | const e = new Event(type, { cancelable: true }); | |
| 54 | Object.defineProperties(e, { clientY: { value: clientY }, pointerId: { value: pointerId }, button: { value: 0 }, timeStamp: { value: timeStamp } }); | |
| 55 | return e; | |
| 56 | }; | |
| 57 | ||
| 58 | describe("the sheet on the page", () => { | |
| 59 | (globalThis as { innerHeight?: number }).innerHeight = 844; | |
| 60 | ||
| 61 | test("starts where it is told, and a tap on the handle toggles it, aria-expanded with it", () => { | |
| 62 | const p = page(); | |
| 63 | const me = mountSheet(p.shell, p.sheet, p.grab, "peek"); | |
| 64 | expect([p.shell.dataset.sheet, p.attrs["aria-expanded"]]).toEqual(["peek", "false"]); | |
| 65 | p.grab.dispatchEvent(pointer("pointerdown", 700, 0)); | |
| 66 | p.grab.dispatchEvent(pointer("pointerup", 702, 80)); | |
| 67 | expect([me.state, p.shell.dataset.sheet, p.attrs["aria-expanded"]]).toEqual(["open", "open", "true"]); | |
| 68 | }); | |
| 69 | ||
| 70 | test("a slow drag follows the finger inline and settles on the nearer point when let go", () => { | |
| 71 | const p = page(96); | |
| 72 | const me = mountSheet(p.shell, p.sheet, p.grab, "peek"); | |
| 73 | p.grab.dispatchEvent(pointer("pointerdown", 750, 0)); | |
| 74 | p.grab.dispatchEvent(pointer("pointermove", 600, 1000)); | |
| 75 | expect(p.sheet.style.height).toBe("246px"); | |
| 76 | expect(p.classes.has("dragging")).toBe(true); | |
| 77 | p.grab.dispatchEvent(pointer("pointerup", 600, 2000)); | |
| 78 | expect([me.state, p.sheet.style.height, p.classes.has("dragging")]).toEqual(["open", "", false]); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a short flick down from open lands on peek", () => { | |
| 82 | const p = page(380); | |
| 83 | const me = mountSheet(p.shell, p.sheet, p.grab); | |
| 84 | p.grab.dispatchEvent(pointer("pointerdown", 460, 0)); | |
| 85 | p.grab.dispatchEvent(pointer("pointermove", 480, 10)); | |
| 86 | p.grab.dispatchEvent(pointer("pointermove", 500, 20)); | |
| 87 | p.grab.dispatchEvent(pointer("pointerup", 500, 20)); | |
| 88 | expect(me.state).toBe("peek"); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a cancelled drag goes back where it was; another finger is ignored; the keyboard's click toggles", () => { | |
| 92 | const p = page(96); | |
| 93 | const me = mountSheet(p.shell, p.sheet, p.grab, "peek"); | |
| 94 | p.grab.dispatchEvent(pointer("pointerdown", 750, 0)); | |
| 95 | p.grab.dispatchEvent(pointer("pointermove", 500, 100)); | |
| 96 | p.grab.dispatchEvent(pointer("pointerup", 500, 200, 2)); | |
| 97 | p.grab.dispatchEvent(pointer("pointercancel", 500, 300)); | |
| 98 | expect([me.state, p.sheet.style.height]).toEqual(["peek", ""]); | |
| 99 | const key = new Event("click"); | |
| 100 | Object.defineProperty(key, "detail", { value: 0 }); | |
| 101 | p.grab.dispatchEvent(key); | |
| 102 | expect(me.state).toBe("open"); | |
| 103 | }); | |
| 104 | }); |
Software/so101/src/viewer.html +11 −2
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | <link rel="modulepreload" href="/ui/__VERSION__/drive.js"> |
| 22 | 22 | <link rel="modulepreload" href="/ui/__VERSION__/frames.js"> |
| 23 | 23 | <link rel="stylesheet" href="/ui/__VERSION__/tabs.css"> |
| 24 | <link rel="stylesheet" href="/ui/__VERSION__/sheet.css"> | |
| 24 | 25 | <style> |
| 25 | 26 | /* Phone portrait first: the view on top, a status strip with the E-stop under it, a sheet of tabs below that. |
| 26 | 27 | Landscape on a phone: the view fills the screen and the strip and the sheet float over it. |
| @@ -332,6 +333,10 @@ | ||
| 332 | 333 | </section> |
| 333 | 334 | |
| 334 | 335 | <section id="sheet" aria-label="Chat, queue and controls"> |
| 336 | <div id="peek"> | |
| 337 | <button id="grab" type="button" aria-controls="tab-controls" aria-expanded="true" aria-label="Lower the joint controls"></button> | |
| 338 | <div id="sheetGrip" role="group" aria-label="Gripper"><span>Grip</span><button id="peekOpen" type="button">Open</button><button id="peekClose" type="button">Close</button></div> | |
| 339 | </div> | |
| 335 | 340 | <div id="notice" role="status" hidden></div> |
| 336 | 341 | <div class="tab" id="tab-chat" role="tabpanel" aria-labelledby="t-chat"> |
| 337 | 342 | <div id="chat" role="log" aria-label="Chat"></div> |
| @@ -588,12 +593,13 @@ | ||
| 588 | 593 | }); |
| 589 | 594 | // The phone's bottom bar (ui/tabs.js, fetched on a phone in portrait only) |
| 590 | 595 | const phone = matchMedia("(max-width: 640px) and (orientation: portrait)"); |
| 591 | let dock = null; | |
| 596 | let dock = null, sheet = null; // and the drive sheet (ui/sheet.js) in it: peek or open | |
| 592 | 597 | async function drawDock(force = false) { |
| 593 | 598 | shell.classList.toggle("docked", phone.matches && dock !== null); |
| 594 | 599 | if (!phone.matches) return; |
| 595 | 600 | try { dock ??= (await import("/ui/__VERSION__/tabs.js")).mountDock($("dock"), shell, showTab, () => ({ mod: isOwner(), lobby: relayed })); } catch { return; } |
| 596 | 601 | dock.sync(force); |
| 602 | try { sheet ??= (await import("/ui/__VERSION__/sheet.js")).mountSheet(shell, $("sheet"), $("grab")); } catch {} | |
| 597 | 603 | } |
| 598 | 604 | addEventListener("hashchange", () => phone.matches && dock?.go(location.hash)); phone.onchange = () => drawDock(true); |
| 599 | 605 | $("sayText").addEventListener("focus", () => { shell.classList.add("typing"); setTimeout(() => (chatBox.scrollTop = chatBox.scrollHeight), 250); }); |
| @@ -1430,6 +1436,8 @@ | ||
| 1430 | 1436 | labelRight(); |
| 1431 | 1437 | bindSurface($("gripOpen"), driver, "open", () => ({ 6: UP[6] }), { haptic: tap }); |
| 1432 | 1438 | bindSurface($("gripClose"), driver, "close", () => ({ 6: -UP[6] }), { haptic: tap }); |
| 1439 | bindSurface($("peekOpen"), driver, "peekOpen", () => ({ 6: UP[6] }), { haptic: tap }); | |
| 1440 | bindSurface($("peekClose"), driver, "peekClose", () => ({ 6: -UP[6] }), { haptic: tap }); | |
| 1433 | 1441 | |
| 1434 | 1442 | // Keep the screen on while it is your turn: a phone that dims mid-drive is a hand off the controls. |
| 1435 | 1443 | let wakeLock = null, waking = false, wantAwake = false; |
| @@ -1541,7 +1549,7 @@ | ||
| 1541 | 1549 | shell.classList.toggle("mayDrive", mayDrive()); |
| 1542 | 1550 | $("t-admin").hidden = !owner; |
| 1543 | 1551 | $("t-chat").hidden = $("t-queue").hidden = !relayed; |
| 1544 | for (const id of ["joints", "poses", "drive", "reach", "touch", "sticks"]) $(id).inert = !can; | |
| 1552 | for (const id of ["joints", "poses", "drive", "reach", "touch", "sticks", "sheetGrip"]) $(id).inert = !can; | |
| 1545 | 1553 | shell.classList.toggle("driving", can); |
| 1546 | 1554 | if (!can) driver.releaseAll(); |
| 1547 | 1555 | wake(can); |
| @@ -1555,6 +1563,7 @@ | ||
| 1555 | 1563 | // Your turn just came: open the controls. It just ended: back to the chat. |
| 1556 | 1564 | if (mayDrive() !== drove) { |
| 1557 | 1565 | drove = mayDrive(); |
| 1566 | if (drove) sheet?.set("open"); | |
| 1558 | 1567 | if (relayed && phone.matches && dock) dock.go(drove ? "drive" : dock.panel === "drive" ? "chat" : dock.panel, true); |
| 1559 | 1568 | else if (relayed) showTab(drove ? "controls" : "chat"); |
| 1560 | 1569 | if (drove && sideways.matches) setBare(true); // on its side, a turn starts on the thumb sticks |
new above
offline