Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

stack streams on larger view #562

Merged opened this in answer to

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

quill/arm:quill/561-stack-streams-on-larger-view → main 1 commit fork 1 thread · 1 unresolved

  • Merged. quill/561-stack-streams-on-larger-view was merged into main as 9d451c1.

Review

Sign in to review this change.
4 files changed +157 −6
Software/so101/src/stack-layout.test.ts added +93
@@ -0,0 +1,93 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { FeetechBus } from "./bus.ts";
6 import { FakeBus } from "./fake-bus.ts";
7 import { startServer, type RunningServer } from "./server.ts";
8
9 // Cameras and the model on together, on a screen wider than a phone (an iPad either way up, a laptop): the cameras stack
10 // on the left, top camera first, and the model takes the larger space on the right. There is no browser here, so the
11 // layout is read from the served stylesheet and the boxes worked out at real view sizes.
12
13 const STACK = "(min-width: 601px), (orientation: landscape)";
14 type Rule = { media: string; selector: string; decls: Record<string, string> };
15
16 function rules(css: string): Rule[] {
17 const out: Rule[] = [];
18 const walk = (src: string, media: string) => {
19 let i = 0;
20 while (i < src.length) {
21 const open = src.indexOf("{", i);
22 if (open < 0) break;
23 let depth = 1;
24 let j = open + 1;
25 for (; j < src.length && depth; j++) depth += src[j] === "{" ? 1 : src[j] === "}" ? -1 : 0;
26 const head = src.slice(i, open).trim();
27 const inner = src.slice(open + 1, j - 1);
28 if (head.startsWith("@media")) walk(inner, head.slice(6).trim());
29 else if (!head.startsWith("@")) {
30 const decls: Record<string, string> = {};
31 for (const d of inner.split(/;(?![^(]*\))/)) {
32 const at = d.indexOf(":");
33 if (at > 0) decls[d.slice(0, at).trim().toLowerCase()] = d.slice(at + 1).trim();
34 }
35 out.push({ media, selector: head, decls });
36 }
37 i = j;
38 }
39 };
40 walk(css.replace(/\/\*[\s\S]*?\*\//g, ""), "");
41 return out;
42 }
43
44 const SPLIT = "#stage:has(> #pane3.on):has(> .pane.on:not(#pane3))";
45 const CAM = "#stage:has(> #pane3.on) > .pane.on:not(#pane3)";
46 const MODEL = "#stage:has(> .pane.on:not(#pane3)) > #pane3.on";
47 const stacked = (selector: string) => all.find((r) => r.media === STACK && r.selector === selector)?.decls ?? {};
48
49 /** The width of the camera column in a view w x h with n cameras on, from --camw: min(<a>cqw, <b>cqh * 16 / (9 * var(--cams))). */
50 function column(w: number, h: number, n: number): number {
51 const m = /^min\(([\d.]+)cqw, ([\d.]+)cqh \* 16 \/ \(9 \* var\(--cams\)\)\)$/.exec(stacked(SPLIT)["--camw"] ?? "");
52 expect(m).not.toBeNull();
53 return Math.min((Number(m![1]) * w) / 100, (((Number(m![2]) * h) / 100) * 16) / (9 * n));
54 }
55
56 let server: RunningServer;
57 let html: string;
58 let all: Rule[];
59
60 beforeAll(async () => {
61 const dataDir = mkdtempSync(join(tmpdir(), "so101-stack-"));
62 server = await startServer(new FeetechBus(new FakeBus([1, 2, 6])), { host: "127.0.0.1", port: 0, ids: [1, 2, 6], posesFile: join(dataDir, "poses.json"), watchdogMs: 60 });
63 html = await (await fetch(`http://127.0.0.1:${server.port}/`)).text();
64 all = rules([...html.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)].map((m) => m[1]!).join("\n"));
65 });
66 afterAll(() => Promise.race([server.close(), new Promise((r) => setTimeout(r, 500))]));
67
68 describe("cameras stacked beside the model", () => {
69 test("the cameras go in one column, top camera first, the model beside them full height", () => {
70 expect(stacked("#stage")["container-type"]).toBe("size");
71 expect(stacked(SPLIT)["flex-direction"]).toBe("column");
72 expect(stacked(SPLIT)["--cams"]).toBe("1");
73 expect(stacked("#stage:has(> .pane.on:not(#pane3) ~ .pane.on:not(#pane3))")["--cams"]).toBe("2");
74 expect(stacked(CAM)).toMatchObject({ flex: "none", width: "var(--camw)", "aspect-ratio": "16 / 9" });
75 expect(stacked(MODEL)).toMatchObject({ position: "absolute", top: "0", bottom: "0", right: "0", left: "calc(var(--camw) + 2px)" });
76 });
77
78 test("the phone held upright keeps its own rows (ui/mobile.css): every stacking rule sits under the query it fails", () => {
79 expect(all.filter((r) => r.selector.startsWith("#stage:has")).length).toBeGreaterThan(3);
80 expect(all.filter((r) => r.selector.startsWith("#stage:has")).every((r) => r.media === STACK)).toBe(true);
81 });
82
83 // The view (#app) at each size: on a wide landscape screen beside the 400 px sheet and above the 64 px strip; upright, min(42svh, 75vw) tall.
84 for (const [name, w, h] of [["iPad landscape", 1180 - 400, 820 - 64], ["iPad upright", 820, 0.42 * 1180], ["laptop", 1440 - 400, 900 - 64], ["iPad Pro landscape", 1366 - 400, 1024 - 64]] as const) {
85 test(`${name}: both cameras fit, one above the other, and the model has the larger space`, () => {
86 const cw = column(w, h, 2);
87 const model = w - cw - 2;
88 expect(model).toBeGreaterThan(cw);
89 expect(2 * ((cw * 9) / 16) + 2).toBeLessThanOrEqual(h + 2);
90 expect(cw).toBeGreaterThan(w * 0.3); // and the cameras are not shrunk to thumbnails
91 });
92 }
93 });
Software/so101/src/ui/views.js added +22
@@ -0,0 +1,22 @@
1 /**
2 * Which views are on when they first appear. Plain ESM, served at /ui/ and imported by the tests.
3 * A narrow screen shows one view at a time: the first camera when there is a real one, else the model. Anywhere wider,
4 * every camera and the model at once: viewer.html stacks the cameras on the left, top camera first, and gives the model
5 * the larger space on the right. The viewer's last choice, when there is one, wins over both.
6 * (The phone held upright always has the model under the camera; the page decides that before asking here.)
7 */
8
9 /**
10 * @param {string} key "cam:<id>" or "pane3" (the model)
11 * @param {{ narrow: boolean, shown: number, modelOk: boolean, chosen: Set<string> | null, cameraFirst: boolean, firstCam: string | undefined }} s
12 * narrow: one view at a time; shown: views on now; modelOk: the model loads; chosen: the views kept from last time;
13 * cameraFirst: a real camera is worth showing first; firstCam: the first camera's key, undefined with none
14 * @returns {boolean}
15 */
16 export function wanted(key, s) {
17 if (s.narrow && s.shown >= 1) return false;
18 if (key === "pane3" && !s.modelOk) return false;
19 if (s.chosen) return s.chosen.has(key);
20 if (key === "pane3") return !s.narrow || !s.cameraFirst || s.firstCam === undefined;
21 return s.cameraFirst && (!s.narrow || key === s.firstCam);
22 }
Software/so101/src/ui/views.test.ts added +27
@@ -0,0 +1,27 @@
1 import { describe, expect, test } from "bun:test";
2 import { wanted } from "./views.js";
3
4 type State = { narrow: boolean, shown: number, modelOk: boolean, chosen: Set<string> | null, cameraFirst: boolean, firstCam: string | undefined };
5 const base: State = { narrow: false, shown: 0, modelOk: true, chosen: null, cameraFirst: true, firstCam: "cam:1" };
6 const on = (s: Partial<State>) => ["cam:1", "cam:2", "pane3"].filter((k) => wanted(k, { ...base, ...s }));
7
8 describe("the views a page starts with", () => {
9 test("a wide screen (an iPad, a laptop) starts with every camera and the model", () => {
10 expect(on({})).toEqual(["cam:1", "cam:2", "pane3"]);
11 });
12 test("a narrow screen starts with the first camera alone", () => {
13 expect(on({ narrow: true })).toEqual(["cam:1"]);
14 expect(wanted("cam:2", { ...base, narrow: true, shown: 1 })).toBe(false);
15 });
16 test("with no real camera to show first (the simulation), the model", () => {
17 expect(on({ cameraFirst: false })).toEqual(["pane3"]);
18 expect(on({ narrow: true, cameraFirst: false })).toEqual(["pane3"]);
19 });
20 test("no model when it does not load", () => {
21 expect(on({ modelOk: false })).toEqual(["cam:1", "cam:2"]);
22 });
23 test("the viewer's last choice wins", () => {
24 expect(on({ chosen: new Set(["cam:2", "pane3"]) })).toEqual(["cam:2", "pane3"]);
25 expect(on({ chosen: new Set(["pane3"]), modelOk: false })).toEqual([]);
26 });
27 });
Software/so101/src/viewer.html +15 −6
@@ -18,6 +18,7 @@
1818 } }
1919 </script>
2020 <link rel="modulepreload" href="/ui/__VERSION__/camera-plan.js">
21 <link rel="modulepreload" href="/ui/__VERSION__/views.js">
2122 <link rel="modulepreload" href="/ui/__VERSION__/drive.js">
2223 <link rel="modulepreload" href="/ui/__VERSION__/frames.js">
2324 <link rel="stylesheet" href="/ui/__VERSION__/tabs.css">
@@ -69,6 +70,17 @@
6970 #banner button { margin-left: 10px; min-height: 32px; padding: 2px 10px; }
7071 .pane { flex: 1; min-width: 0; position: relative; background: #000; display: none; }
7172 .pane.on { display: block; }
73 /* Cameras and the model together, on any screen but a phone held upright (ui/mobile.css has its own rows): the cameras
74 stack in a column on the left, top camera first, each 16:9 and as wide as its share of the height allows, never more
75 than 45% of the view; the model takes the larger space on the right, full height. One camera or three, the same. */
76 @media (min-width: 601px), (orientation: landscape) {
77 #stage { container-type: size; }
78 #stage:has(> #pane3.on):has(> .pane.on:not(#pane3)) { --cams: 1; --camw: min(45cqw, 100cqh * 16 / (9 * var(--cams))); flex-direction: column; justify-content: center; }
79 #stage:has(> .pane.on:not(#pane3) ~ .pane.on:not(#pane3)) { --cams: 2; }

The stacking depends on :has() and cq units. Without them, the old row layout shows. That is fine, but say so in the README.

80 #stage:has(> .pane.on:not(#pane3) ~ .pane.on:not(#pane3) ~ .pane.on:not(#pane3)) { --cams: 3; }
81 #stage:has(> #pane3.on) > .pane.on:not(#pane3) { flex: none; width: var(--camw); height: auto; aspect-ratio: 16 / 9; }
82 #stage:has(> .pane.on:not(#pane3)) > #pane3.on { position: absolute; top: 0; bottom: 0; right: 0; left: calc(var(--camw) + 2px); }
83 }
7284 .pane img, .pane canvas { width: 100%; height: 100%; object-fit: contain; display: block; }
7385 .pane canvas[hidden], .pane .q[hidden] { display: none; }
7486 .pane .q { position: absolute; right: calc(8px + var(--safe-r, 0px)); bottom: 8px; }
@@ -494,6 +506,7 @@
494506 </div>
495507 <script type="module">
496508 import { cameraPlan, QUALITIES, samePlan } from "/ui/__VERSION__/camera-plan.js";
509 import { wanted as wantedView } from "/ui/__VERSION__/views.js";
497510 import { bindTray } from "/ui/__VERSION__/drive.js";
498511 import { Frames } from "/ui/__VERSION__/frames.js";
499512 import { holdText, warnText } from "/ui/__VERSION__/hold-line.js";
@@ -696,14 +709,10 @@
696709 setPane(key, on);
697710 keep(); viewer.recenter?.();
698711 }
699 /** Whether a view is on when it first appears: the viewer's last choice, else the camera when there is a real one, and the model beside it on a wide screen. */
712 /** Whether a view is on when it first appears: the viewer's last choice, else one view on a narrow screen and every camera with the model on a wider one (ui/views.js). */
700713 function wanted(key) {
701714 if (stacked.matches) return false; // fallback() turns on both
702 if (narrow.matches && shown() >= 1) return false;
703 if (key === "pane3" && !modelInfo?.ok) return false;
704 if (chosen) return chosen.has(key);
705 if (key === "pane3") return !narrow.matches || !cameraFirst || !cams.length;
706 return cameraFirst && key === cams[0]?.pane.dataset.key;
715 return wantedView(key, { narrow: narrow.matches, shown: shown(), modelOk: modelInfo?.ok === true, chosen, cameraFirst, firstCam: cams[0]?.pane.dataset.key });
707716 }
708717 /** At least one view on: the first camera, else the model. */
709718 function fallback() {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs