AnyBot/mobile/trackpad.test.mjs

45 lines
4.0 KiB
JavaScript
Raw Normal View History

手機操作電腦補上虛擬觸控板和按鍵列:一指移游標、點一下按、點兩下按住拖曳、兩指捲動或右鍵;Ctrl/Alt/Shift/❖(Mac 是 ⌘⌥⌃⇧)點一下按住、點兩下鎖住;「快捷鍵」面板一格一個名字(下一個分頁、切換視窗、拷貝…);手機「接管」真的讓 Bot 停手。 - mobile/trackpad.js(新):觸控板手勢和按鍵列的狀態,Bot 的電腦(mobile/screen.html,noVNC)和「這台 Mac」(mobile/index.html #v-screen)共用一份;大腦(src/remote.rs)和帳號伺服器(relay/)都在 /trackpad.js 給它。手勢參考 OpenBot 手機 App 的觸控板模式(只借做法,不抄程式碼)。 - Bot 的電腦:接管後下方一排「觸控板|Ctrl Alt Shift ❖|Esc Tab ← ↑ ↓ → ⌫ Del ⏎ Home End PgUp PgDn|快捷鍵 ⌨ 打字」;觸控板經 noVNC 自己的 _handleMouse*(游標自己畫);關掉觸控板回到 noVNC 原本的點哪裡按哪裡。修飾鍵按著時打一個字=那組快捷鍵。 - 接管會鎖住螢幕(新 POST api/box/hold,跟螢幕電腦的 api/sync/screen hold 同一件事):交還、關畫面、手機鎖住或切走就放開,回來再鎖上。以前手機的接管只有點擊或按鍵後 8 秒 Bot 才停。 - 小畫面(GET api/box/thumb?bot=)是那個 Bot 自己的螢幕,對話詳細資訊和需要處理卡帶上 bot(以前一律是共用的)。 - 這台 Mac:大腦的 api/screen/mac 多了 move(移游標)、drag(整條路徑)和點擊的 modifiers(只收 cmd/shift/option/ctrl/alt),組合鍵照用 press_key 的「cmd+tab」;手機頁面換成同一套觸控板、⌘⌥⌃⇧、Mac 版快捷鍵;拿掉「右鍵:開」(兩指點就是右鍵),修好快點兩下會多送一兩下點擊。 - 測試:mobile/trackpad.test.mjs(點一下、滑動、兩指點、兩指捲動、點兩下拖曳、連點、長按、修飾鍵一次/鎖住、快捷鍵的字),cargo test 有 node 時跑它(remote::tests::the_touchpad_turns_fingers_into_mouse_and_keys)。 - 功能地圖 box.md 加 box-phone-drive 和一步驗證;FEATURES.md 加「手機接手:觸控板和按鍵列」。 已驗證: cargo test 877 個全過;relay 的 cargo test 27 個全過;clippy 沒有新警告;node mobile/trackpad.test.mjs 13 項 OK;scratch 手機頁面打開 screen#…&control=1:按鍵列、快捷鍵面板(Alt+Tab 等 18 格)、Ctrl 點一下變亮;「這台 Mac」頁面:⌘ 點兩下鎖住、Mac 版快捷鍵面板。 未驗證: 觸控板動到真的畫面——Bot 的電腦要 Docker(scratch 沒有、不准碰使用者的),這台 Mac 會動到使用者正在用的螢幕,所以兩邊的實際點擊、拖曳、捲動、鎖住都沒有實機跑;這碰到 computer 的路徑(新的 move/drag/修飾鍵),照規定要實機測試加你簽名才能發佈。帳號伺服器要 make update-server 才有 /trackpad.js(沒更新前經帳號開的畫面頁會載不到觸控板)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 00:48:31 +00:00
// 虛擬觸控板的手勢(mobile/trackpad.js):手指怎麼變成滑鼠和按鍵。`node mobile/trackpad.test.mjs`;
// cargo test 的 remote::tests::the_touchpad_turns_fingers_into_mouse_and_keys 也跑它(有 node 時)。
import { trackpad, modState, comboLabel } from "./trackpad.js";
const L = {}; const pad = { addEventListener: (t, f) => (L[t] = f), setPointerCapture() {}, style: {} };
let calls = [];
const host = { bounds: () => ({ w: 400, h: 300 }), move: (x, y) => calls.push(["move", Math.round(x), Math.round(y)]), down: (x, y, b) => calls.push(["down", b]), up: (x, y, b) => calls.push(["up", b]), click: (x, y, b, n) => calls.push(["click", b, n]), scroll: (x, y, dx, dy) => calls.push(["scroll", dx, dy]), show() {} };
const tp = trackpad(pad, host); tp.center();
const ev = (id, x, y) => ({ pointerId: id, clientX: x, clientY: y, preventDefault() {} });
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
const ok = (name, cond) => { console.log((cond ? "OK " : "FAIL ") + name, cond ? "" : JSON.stringify(calls)); if (!cond) process.exitCode = 1; };
// 1. tap
calls = []; L.pointerdown(ev(1, 100, 100)); await wait(50); L.pointerup(ev(1, 100, 100));
ok("點一下=左鍵一下", JSON.stringify(calls) === '[["click","left",1]]');
await wait(400);
// 2. one-finger move
calls = []; L.pointerdown(ev(1, 100, 100)); for (let i = 1; i <= 10; i++) { await wait(16); L.pointermove(ev(1, 100 + i * 5, 100)); } L.pointerup(ev(1, 150, 100));
ok("一指滑動=移動游標、不按", calls.length > 0 && calls.every((c) => c[0] === "move") && tp.pos.x > 250);
await wait(400);
// 3. two-finger tap
calls = []; L.pointerdown(ev(1, 100, 100)); L.pointerdown(ev(2, 160, 100)); await wait(60); L.pointerup(ev(1, 100, 100)); L.pointerup(ev(2, 160, 100));
ok("兩指點=右鍵", JSON.stringify(calls) === '[["click","right",1]]');
await wait(400);
// 4. two-finger scroll up (fingers move up → page goes down: dy > 0)
calls = []; L.pointerdown(ev(1, 100, 200)); L.pointerdown(ev(2, 160, 200)); for (let i = 1; i <= 10; i++) { await wait(16); L.pointermove(ev(1, 100, 200 - i * 8)); L.pointermove(ev(2, 160, 200 - i * 8)); } L.pointerup(ev(1, 100, 120)); L.pointerup(ev(2, 160, 120));
ok("兩指往上滑=往下捲", calls.length > 0 && calls.every((c) => c[0] === "scroll" && c[2] > 0) && !calls.some((c) => c[0] === "click"));
await wait(400);
// 5. double tap and drag
calls = []; L.pointerdown(ev(1, 200, 150)); await wait(40); L.pointerup(ev(1, 200, 150)); await wait(80);
L.pointerdown(ev(1, 202, 151)); for (let i = 1; i <= 6; i++) { await wait(16); L.pointermove(ev(1, 202 + i * 6, 151)); } L.pointerup(ev(1, 238, 151));
const kinds = calls.map((c) => c[0]);
ok("點兩下按住滑=拖曳", kinds[0] === "click" && kinds[1] === "down" && kinds.includes("move") && kinds[kinds.length - 1] === "up");
await wait(400);
// 6. double tap (no drag) = two clicks
calls = []; L.pointerdown(ev(1, 200, 150)); await wait(40); L.pointerup(ev(1, 200, 150)); await wait(80); L.pointerdown(ev(1, 201, 150)); await wait(40); L.pointerup(ev(1, 201, 150));
ok("點兩下=連點兩下", JSON.stringify(calls) === '[["click","left",1],["click","left",1]]');
await wait(400);
// 7. long press = right click, no left click after
calls = []; L.pointerdown(ev(1, 120, 120)); await wait(650); L.pointerup(ev(1, 120, 120));
ok("長按=右鍵", JSON.stringify(calls) === '[["click","right",1]]');
// 8. modifiers: once, lock, used
let n = 0; const m = modState(() => n++);
m.tap("ctrl"); ok("點一下修飾鍵=下一下按住", m.get("ctrl") === "once"); m.used(); ok("用過就放開", m.get("ctrl") === "");
m.tap("cmd"); m.tap("cmd"); ok("點兩下=鎖住", m.get("cmd") === "lock"); m.used(); ok("鎖住的用過不放開", m.get("cmd") === "lock"); m.tap("cmd"); ok("再點一下放開", m.get("cmd") === "");
ok("組合鍵的字", comboLabel(["ctrl", "shift"], "Tab") === "⌃⇧⇥" && comboLabel(["cmd"], "c") === "⌘C" && comboLabel(["ctrl", "shift"], "Tab", "linux") === "Ctrl+Shift+Tab" && comboLabel(["alt"], "Left", "linux") === "Alt+←");