// 虛擬觸控板的手勢(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",2]]'); await wait(400); // 6b. a slow tap (longer than a quick one, shorter than a long press) is still a click calls = []; L.pointerdown(ev(1, 120, 120)); await wait(400); L.pointerup(ev(1, 120, 120)); ok("按慢一點也算點一下", JSON.stringify(calls) === '[["click","left",1]]'); await wait(400); // 6c. the system cancels the touch: nothing is clicked; in a drag, the button is let go calls = []; L.pointerdown(ev(1, 120, 120)); await wait(40); L.pointercancel(ev(1, 120, 120)); ok("被系統打斷不算點", calls.length === 0); 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+←");