Files
magpie/internal/gui/tests/panel-shortcut.test.cjs
T
yetone 3bebc38393 gui: a key combination opens the quick panel from any app (ben hanks)
ben hanks on Discord asked for a global shortcut that opens the menu bar
or tray panel. Settings › General has a new row, Quick panel shortcut,
with none set by default. Clicking its field records the next combination:
a letter, a digit, Space or F1–F20 with Ctrl, Alt/Option or Cmd/Win, or an
F key on its own. Esc or a click elsewhere stops recording, and the cross
clears the shortcut. It is shown the system's way (⇧⌘K on a Mac,
Ctrl+Alt+K elsewhere).

POST /api/settings/panel-shortcut checks the combination, has the running
app bind it through Wails' GlobalShortcut (Carbon hot keys on the Mac,
RegisterHotKey on Windows, XGrabKey on X11), and saves it only once it is
bound. One the system refuses is not saved, the old one stays bound, and
the status line says why. A pressed shortcut toggles the panel as a tray
click does. The shortcut belongs to each computer (KeepOwn), the Settings
page's own save leaves it alone, and the old Mac process lets go of it as
it hands over. On Wayland no app may grab keys, so the field is turned off
and the row names the command (`magpie panel`) to bind in the desktop's
own keyboard settings.

Every string is in en, zh, zh-TW, ja and de. No size budget was raised:
app.js is 1,315,252 of its 1,315,316 after older comments were cut.

Run at this head (rebased on origin/main 1534474c8):
- mote (macOS 27): gofmt; go vet for darwin, GOOS=linux -tags nogui and
  GOOS=windows on internal/gui and internal/settings; go build ./...:
  ok. go test: settings ok; gui ok except TestMainWindowCorners/
  macOS_26_SDK, red on macOS 27 already (#3949).
- Windows (xiaomi-gpu, win-run): go test internal/gui internal/settings.
  settings ok; gui has 5 reds, the same 5 on origin/main d19afb4c9
  (TestGatewayHeldByAContainer #2107/#3649; TestLodyImportHandoff,
  TestImportStatusUnreadableIsUnknown, TestAionUiImportRow and
  TestUpdateThatWontStart #4115).
- Linux (WSL magpie-desktop, wsl-run -tags nogui): internal/gui (the
  Wayland branch included) and internal/settings: ok.
- GUI: panel-shortcut.test.cjs (new) in Chromium and WebKit, in all 5
  languages, faking both a Mac and Windows: ok. Whole suite (make test-ui)
  on mote: unfinished at the push. Three other sessions' suites were on
  the same box (load 130 to 250), and after 1605 tests it was still in
  the "c" files. Its reds so far are agent-panel-name-width and
  connect-snippet-shell and context-map (each on #4304), and allow-ips
  zh, which is red alone on origin/main too (#4357).

Broken by hand, each red:
- leaving the shortcut out of the general Settings save:
  TestPanelShortcutSetting "the Settings page's save changed the
  shortcut";
- dropping KeepOwn's line: "a synced setup took this computer's shortcut";
- dropping the recorder's stopImmediatePropagation: panel-shortcut.test.cjs
  "a key typing needs was sent, or a window key ran".
TestSettingsSaveKeepsWhatItDoesNotSend now sets the new field.

Real build on mote: a dev build in a sandbox HOME bound a saved
Ctrl+Option+Shift+Cmd+F18 as it started, and a POST of F16 saved it and
bound it.

Not tried:
- a real key press on any system: mote gives ssh no Accessibility access
  to post events, and Windows' ssh session has no desktop;
- the Windows and X11 refusal of a combination another app holds (only
  the fake). On the Mac, Carbon takes a combination a second app
  registered too, so both apps get it and magpie can't say so. This was
  checked on mote with a helper holding F17;
- Wails' GlobalShortcuts portal on Wayland, which isn't used;
- a Linux GUI-tag build (WSL has no gtk4/webkitgtk-6.0).
2026-10-30 12:06:33 +08:00

172 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// ben hanks on Discord: a key combination that opens the tray's quick panel
// from any app. Settings › General has one row for it, none set by default,
// in both engines and every language, on a Mac and off it, with the API
// faked:
// - the field listens once clicked; a key with no Ctrl, Alt or Cmd/Win is
// not taken, and the window's own keys (text size) don't fire meanwhile;
// - a combination is sent as Wails names it, and shown in the system's way
// (⇧⌘K on a Mac, Ctrl+Alt+K elsewhere); Esc stops listening, sending none;
// - one the system refuses says so in the status line, the saved one kept;
// - the clear button takes it away; nothing the row does scrolls the page;
// - on Wayland the field is off and the row says the command to bind;
// - a browser tab (magpie web) has no row; a narrow window keeps it inside.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const words = {
en: { name: "Quick panel shortcut", record: "Record shortcut", type: "Type shortcut", taken: "Another app already uses this shortcut" },
zh: { name: "快捷面板快捷键", record: "录制快捷键", type: "请按下快捷键", taken: "其他应用已占用这个快捷键" },
"zh-TW": { name: "快捷面板快速鍵", record: "錄製快速鍵", type: "請按下快速鍵", taken: "其他應用程式已占用這個快速鍵" },
ja: { name: "クイックパネルのショートカット", record: "ショートカットを記録", type: "キーを押してください", taken: "このショートカットは他のアプリが使用中です" },
de: { name: "Tastenkürzel für das Schnellpanel", record: "Kürzel aufnehmen", type: "Kürzel drücken", taken: "Eine andere App nutzt dieses Kürzel bereits" },
};
function serve(lang, web, posted, st) {
const settings = () => ({ lang, theme: "light", tray: "panel", searchVendors: [], searchAPIs: [], panelKeys: { can: true }, ...st });
return async (r) => {
const url = new URL(r.request().url());
const json = (data, status = 200) => r.fulfill({ status, json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${web}};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: settings() });
if (url.pathname === "/api/settings/panel-shortcut") {
const { shortcut } = JSON.parse(r.request().postData());
posted.push(shortcut);
// the fake system holds Ctrl+Alt+J / ⌃⌥J for another app
if (/^Ctrl\+(Alt|Option)\+J$/.test(shortcut)) return json({ error: "Another app already uses this shortcut" }, 400);
st.panelShortcut = shortcut;
return json(settings());
}
if (url.pathname.startsWith("/api/settings/")) { posted.push(url.pathname); return json(settings()); }
if (url.pathname === "/api/settings") return json(settings());
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
const langs = Object.keys(words);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
langs.forEach((lang) => {
for (const platform of ["MacIntel", "Win32"]) {
const mac = platform === "MacIntel";
test(`${engine} ${lang} ${mac ? "mac" : "windows"}: the quick panel's shortcut is recorded, refused, cleared`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const w = words[lang];
const open = async (st, { web = false, width = 1000 } = {}) => {
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.addInitScript((p) => Object.defineProperty(navigator, "platform", { get: () => p }), platform);
const posted = [];
await page.route("**/*", serve(lang, web, posted, st));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=general");
await settingsDrawn(page);
return { page, posted, errors };
};
const { page, posted, errors } = await open({});
const rec = page.locator("#panelKey .key-rec"), clear = page.locator("#panelKey .key-clear");
assert.equal(await page.locator("#panelKeyRow .name").textContent(), w.name);
assert.equal(await rec.textContent(), w.record, "none is set by default");
assert.equal(await clear.isHidden(), true);
assert.equal(await page.locator("#panelKeyRow .sub").isHidden(), true);
await page.evaluate(() => Promise.all(document.getAnimations().map((a) => a.finished.catch(() => {}))));
await page.locator("#panelKeyRow").hover();
await page.mouse.wheel(0, 120);
await page.waitForTimeout(300);
// where the row is on the screen: nothing the row does moves it
const top = () => page.evaluate(() => { const v = document.querySelector("#view-settings"); return `${Math.round(document.querySelector("#panelKeyRow").getBoundingClientRect().top)} (${v.scrollTop}/${v.scrollHeight - v.clientHeight})`; });
const at = await top();
// listening: a key with no Ctrl, Alt or Cmd isn't taken, nor are
// the window's text size keys run
await rec.click();
assert.equal(await rec.textContent(), w.type);
await page.keyboard.press("Shift+K");
await page.keyboard.press((mac ? "Meta" : "Control") + "+Equal");
await page.keyboard.press("Digit5");
await page.waitForTimeout(100);
assert.deepEqual(posted, [], "a key typing needs was sent, or a window key ran");
assert.equal(await rec.textContent(), w.type);
// Esc stops it, sending none
await page.keyboard.press("Escape");
assert.equal(await rec.textContent(), w.record);
assert.equal(await page.locator("#view-settings").isVisible(), true, "Esc left Settings");
assert.deepEqual(posted, []);
// a combination, sent as Wails names it and shown the system's way
await rec.click();
await page.keyboard.press(mac ? "Meta+Shift+KeyK" : "Control+Alt+KeyK");
const want = mac ? "Shift+Cmd+K" : "Ctrl+Alt+K";
for (let i = 0; i < 50 && posted.length < 1; i++) await page.waitForTimeout(50);
assert.deepEqual(posted, [want]);
await page.waitForFunction((s) => document.querySelector("#panelKey .key-rec").textContent === s, mac ? "⇧⌘K" : "Ctrl+Alt+K");
assert.equal(await clear.isVisible(), true);
// one the system refuses: said, the saved one kept
await page.waitForFunction(() => prefsBusy === 0);
await rec.click();
await page.keyboard.press("Control+Alt+KeyJ");
for (let i = 0; i < 50 && posted.length < 2; i++) await page.waitForTimeout(50);
assert.deepEqual(posted, [want, mac ? "Ctrl+Option+J" : "Ctrl+Alt+J"]);
await page.waitForFunction((s) => document.querySelector("#status")?.textContent.includes(s), w.taken);
await page.waitForFunction((s) => document.querySelector("#panelKey .key-rec").textContent === s, mac ? "⇧⌘K" : "Ctrl+Alt+K");
// clicking away stops listening
await rec.click();
await page.locator("#panelKeyRow .name").click();
await page.waitForFunction((s) => document.querySelector("#panelKey .key-rec").textContent === s, mac ? "⇧⌘K" : "Ctrl+Alt+K");
// cleared
await page.waitForFunction(() => prefsBusy === 0);
await clear.click();
for (let i = 0; i < 50 && posted.length < 3; i++) await page.waitForTimeout(50);
assert.equal(posted[2], "");
await page.waitForFunction((s) => document.querySelector("#panelKey .key-rec").textContent === s, w.record);
assert.equal(await clear.isHidden(), true);
assert.equal(await top(), at, "the row moved on the screen");
assert.deepEqual(errors, []);
// the saved one the system refused as magpie started is said
const taken = await open({ panelShortcut: "Ctrl+Alt+K", panelKeys: { can: true, taken: "Ctrl+Alt+K" } });
assert.equal(await taken.page.locator("#panelKeyRow .sub").textContent(), w.taken);
// Wayland: off, with the command to bind
const way = await open({ panelKeys: { can: false, command: "/usr/bin/magpie panel" } });
assert.equal(await way.page.locator("#panelKey .key-rec").isDisabled(), true);
const sub = way.page.locator("#panelKeyRow .sub");
assert.equal(await sub.isVisible(), true);
assert.ok((await sub.textContent()).includes("/usr/bin/magpie panel"), await sub.textContent());
assert.ok(lang === "en" || !(await sub.textContent()).includes("keyboard settings"), "the Wayland line isn't translated");
assert.deepEqual(way.errors, []);
// a browser tab has no row
const tab = await open({}, { web: true });
assert.equal(await tab.page.locator("#panelKeyRow").isHidden(), true);
// a narrow window keeps the field inside its row
const narrow = await open({ panelShortcut: mac ? "Ctrl+Option+Shift+Cmd+K" : "Ctrl+Alt+Shift+Super+K" }, { width: 420 });
const [row, field, x] = await narrow.page.evaluate(() => ["#panelKeyRow", "#panelKey .key-rec", "#panelKey .key-clear"]
.map((s) => { const b = document.querySelector(s).getBoundingClientRect(); return { l: b.left, r: b.right }; }));
assert.ok(field.l >= row.l && x.r <= row.r + 0.5, `${JSON.stringify({ row, field, x })}`);
assert.equal(await narrow.page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true, "the page scrolls sideways");
assert.deepEqual(narrow.errors, []);
});
}
});
}