diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index fc8dd3daf..bcd398f14 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -3509,7 +3509,9 @@ function openRowMenu(anchor, acts) { if (o.tip) b.title = o.tip; // an item that is off can say why, under its name and as its title if (o.why) { b.classList.add("has-why"); b.title = o.why; b.querySelector(".rm-name").append(el("span", "rm-why", o.why)); } - b.onclick = (e) => { e.stopPropagation(); closeAgentMenu(); o.run(); }; + // the click goes along, so an item that opens something below can + // bring it into view (scrollOnPurpose) + b.onclick = (e) => { e.stopPropagation(); closeAgentMenu(); o.run(e); }; b.onmouseenter = () => b.focus({ preventScroll: true }); box.append(b); if (!o.off) items.push(b); diff --git a/internal/gui/assets/routing.css b/internal/gui/assets/routing.css index f4dfa7079..4294c9e56 100644 --- a/internal/gui/assets/routing.css +++ b/internal/gui/assets/routing.css @@ -437,7 +437,7 @@ button.rt-session { cursor: pointer; } .rt-gsel { display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 14px; } .rt-group.rt-gpick { cursor: default; } .rt-group.rt-gpick.on { background: var(--card-2); } -.rt-gedit { cursor: default; } +.rt-gedit { cursor: default; scroll-margin-top: 12px; } .rt-gedit .ehead.fold { cursor: pointer; } .rt-gedit .segs { max-width: 100%; } .rt-gedit .ctx-wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } diff --git a/internal/gui/assets/routing.js b/internal/gui/assets/routing.js index 168c09544..7efde5434 100644 --- a/internal/gui/assets/routing.js +++ b/internal/gui/assets/routing.js @@ -3678,15 +3678,18 @@ const name = tplName(tp); groupAction("template", { kind: tp.kind, name }, t("Added {name}", { name })); } - // editTemplate: the template in the editor, to change before it is saved - async function editTemplate(tp) { + // editTemplate: the template in the editor, filled in, to change before + // it is saved, brought into view: the groups can sit well below the + // click that asked for it. Its top, name first: the editor is taller + // than the window, and centred it opens halfway down the form + async function editTemplate(tp, ev) { if (groupDirty() && !(await confirmDiscard())) return; const g = tp.group; gSel = null; gEdit = { id: "", draft: { name: tplName(tp), slug: g.id, members: [...g.members], match: [], matched: [], fast: [], off: [], routing: g.routing || "", affinity: "", rules: (g.rules || []).map((r) => ({ ...r, intent: r.intent || "", agents: [...(r.agents || [])], folders: [], exceptFolders: [], time: null })), classifier: g.classifier || "", effort: "", levels: [] } }; renderGroups(); const ed = gList.querySelector(".rt-gedit"); - ed?.scrollIntoView({ block: "nearest" }); + if (ed && window.scrollOnPurpose?.(ev)) ed.scrollIntoView({ block: "start", behavior: "smooth" }); ed?.querySelector("input")?.focus({ preventScroll: true }); } function templateCards(tpls) { @@ -3710,7 +3713,7 @@ add.onclick = () => addTemplate(tp); const edit = el("button", "text rt-gtpl-edit", t("Edit first")); edit.type = "button"; - edit.onclick = () => editTemplate(tp); + edit.onclick = (e) => editTemplate(tp, e); acts.append(add, edit); c.append(mem, acts); } @@ -3719,8 +3722,10 @@ box.append(cards); return box; } - // templateButton: the templates as a menu by New group, each added in a - // click, once the user has groups of their own + // templateButton: the templates as a menu by New group, once the user + // has groups of their own. A pick opens the template filled in, in the + // editor and in view, rather than adding a group out of sight (owner, + // 10-14: a group added somewhere below left the user lost) function templateButton(tpls) { const b = el("button", "text rt-gtplbtn"); b.type = "button"; @@ -3733,7 +3738,7 @@ if (again) return; openRowMenu(b, tpls.map((tp) => tp.need ? { name: tplName(tp), icon: PLUS, off: true, why: tplNeed(tp), run: () => {} } - : { name: tplName(tp), icon: PLUS, tip: t(TPL[tp.kind]?.[1] || "") + "\n" + tplMembers(tp), run: () => addTemplate(tp) })); + : { name: tplName(tp), icon: PLUS, tip: t(TPL[tp.kind]?.[1] || "") + "\n" + tplMembers(tp), run: (e) => editTemplate(tp, e) })); }; return b; } diff --git a/internal/gui/tests/group-template.test.cjs b/internal/gui/tests/group-template.test.cjs index c833a06d4..cf5173e3b 100644 --- a/internal/gui/tests/group-template.test.cjs +++ b/internal/gui/tests/group-template.test.cjs @@ -6,7 +6,9 @@ // and has no buttons. Add posts groups/template with the kind and its name // in the reader's language; once the user has a group, the cards give way // to a Templates menu by New group. Edit first opens the editor with the -// template in it. At 420px, in every language, Chromium and WebKit. +// template in it. 10-14: a pick from the menu does the same, filled in and +// brought into view, not a group added out of sight. At 420px, in every +// language, Chromium and WebKit. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -40,9 +42,10 @@ const KEYS = ["Hard to strong, easy to cheap", "A hard task goes to the stronges "The strongest model of each provider, each behind the other: one down doesn't stop the agent", "Needs models from two providers: {name} is the only one", "Needs a model cheap beside {name}", "Add a provider first", "Added {name}", "Start from a template: made of your own models, added in a click", "Edit first", "Templates"]; -function serve(lang, posts) { +function serve(lang, posts, mine = 0) { // a group magpie found is no group of the user's: the cards still show - let list = [{ id: "auto-big", name: "Big", members: ["a/big"], ready: true, auto: true, memberInfo: info(["a/big"]) }]; + let list = [{ id: "auto-big", name: "Big", members: ["a/big"], ready: true, auto: true, memberInfo: info(["a/big"]) }, + ...Array.from({ length: mine }, (_, i) => ({ id: `mine-${i}`, name: `Mine ${i}`, members: ["a/big", "a/mini"], routing: "order", ready: true, memberInfo: info(["a/big", "a/mini"]) }))]; const groups = () => ({ models, pools: [], deciders: [], found: true, groups: list, templates }); const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; return async (r) => { @@ -115,6 +118,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(posts, [{ kind: "smart", name: w.smart }]); await page.locator(".rt-group[data-id=smart-split]").waitFor(); assert.equal(await page.locator(".rt-gedit").count(), 0, "adding opened the editor"); + assert.equal(await at(), before, "a click moved the page"); // the user has a group now: a menu by New group, not the cards assert.equal(await cards.count(), 0); const btn = page.locator(".rt-gtplbtn"); @@ -127,9 +131,10 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert(box.x >= 0 && box.x + box.width <= 420, `menu off the window: ${JSON.stringify(box)}`); assert.equal(await menu.getByRole("menuitem").nth(2).isDisabled(), true, "steady can't be made"); await menu.getByRole("menuitem", { name: w.thrifty }).click(); - await page.locator(".rt-group[data-id=cheap-first]").waitFor(); - assert.deepEqual(posts[1], { kind: "thrifty", name: w.thrifty }); - assert.equal(await at(), before, "a click moved the page"); + const ed = page.locator(".rt-gedit"); + await ed.waitFor(); + assert.equal(await ed.locator("input").first().inputValue(), w.thrifty); + assert.equal(posts.length, 1, "picking a template added it unseen"); if (lang !== "en") { const missing = await page.evaluate(([l, ks]) => ks.filter((k) => !I18N[l][k]), [lang, KEYS]); @@ -138,6 +143,39 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); + test(`${engine} ${lang}: a template picked from the menu opens filled in, in view`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await (await browser.newContext({ viewport: { width: 420, height: 900 }, reducedMotion: "reduce" })).newPage(); + t.after(async () => { + if (process.env.ARTIFACT_DIR) await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-group-template-menu.png`) }); + await browser.close(); + }); + page.setDefaultTimeout(5000); + const errors = [], posts = []; + // WebKit's notice when a layout outruns its observers, not a fault + page.on("pageerror", (e) => { if (!/^ResizeObserver loop completed with undelivered notifications/.test(e.message)) errors.push(e.message); }); + // enough groups of the user's that the editor, last in the list, + // opens below the window + await page.route("**/*", serve(lang, posts, 12)); + await page.goto("http://magpie.test/?view=routing"); + const btn = page.locator(".rt-gtplbtn"); + await btn.waitFor(); + await page.waitForTimeout(500); + await page.mouse.move(1, 1); + await btn.click(); + await page.locator(".row-menu").getByRole("menuitem", { name: w.thrifty }).click(); + const ed = page.locator(".rt-gedit"); + await ed.waitFor(); + assert.equal(await ed.locator("input").first().inputValue(), w.thrifty); + assert.deepEqual(posts, [], "picking a template added it unseen"); + await page.waitForFunction(() => { + const r = document.querySelector(".rt-gedit").getBoundingClientRect(); + return r.top >= 0 && r.top < innerHeight - 60; + }, null, { timeout: 3000 }).catch(() => assert.fail("the editor the pick opened is out of sight")); + assert.equal(await page.evaluate(() => document.activeElement === document.querySelector(".rt-gedit input")), true, "the template's name isn't focused"); + assert.deepEqual(errors, []); + }); + test(`${engine} ${lang}: Edit first opens a template in the editor`, async (t) => { const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); const page = await (await browser.newContext({ viewport: { width: 420, height: 1400 }, reducedMotion: "reduce" })).newPage();