Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Focus the composer when switching chat panes (#1153)

Joshua France1e29b670b61a
diff --git a/plugins/web-ui/src/pane-focus.ts b/plugins/web-ui/src/pane-focus.ts
index 2bbec4a49..0423512a1 100644
--- a/plugins/web-ui/src/pane-focus.ts
+++ b/plugins/web-ui/src/pane-focus.ts
@@ -49,3 +49,29 @@ export function preservingFocus(doc: Document, mutate: () => void): void {
active.focus();
applySelection(view, active, selection);
}
+
+export function focusComposerOnPaneClick(pane: HTMLElement, isActive: () => boolean): void {
+ let switching = false;
+ pane.tabIndex = -1;
+ pane.addEventListener(
+ "pointerdown",
+ (event) => {
+ switching =
+ !isActive() && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey;
+ },
+ true,
+ );
+ const cancel = (): void => {
+ switching = false;
+ };
+ pane.addEventListener("pointercancel", cancel);
+ pane.addEventListener("dragstart", cancel);
+ pane.addEventListener("click", (event) => {
+ const focus = switching;
+ cancel();
+ const doc = pane.ownerDocument;
+ if (!focus || event.defaultPrevented || doc.activeElement !== pane || doc.getSelection()?.isCollapsed === false)
+ return;
+ pane.querySelector<HTMLTextAreaElement>(".composer-input:not(:disabled)")?.focus({ preventScroll: true });
+ });
+}
diff --git a/plugins/web-ui/src/split.ts b/plugins/web-ui/src/split.ts
index 84144ced3..d322c0bb0 100644
--- a/plugins/web-ui/src/split.ts
+++ b/plugins/web-ui/src/split.ts
@@ -46,7 +46,7 @@ import {
type SplitEdge,
} from "./split-layout";
import { paneKindByKey, paneKindEntry } from "./pane-kinds";
-import { preservingFocus } from "./pane-focus";
+import { focusComposerOnPaneClick, preservingFocus } from "./pane-focus";
import { attachTooltip, tip } from "./tooltip";
import { icon, workingWave } from "./ui";
import { contextsState, scopeTitle } from "./contexts";
@@ -940,6 +940,7 @@ class PaneContent implements IContentRenderer {
this.panel = p.containerApi.getPanel(p.api.id) ?? null;
this.params = (p.params ?? {}) as PaneParams;
this.element.dataset.paneId = this.panelId;
+ focusComposerOnPaneClick(this.element, () => p.api.isActive);
paneContents.set(this.panelId, this);
this.resize.observe(this.element);
this.syncZones();
diff --git a/plugins/web-ui/test/pane-focus.test.ts b/plugins/web-ui/test/pane-focus.test.ts
index 0ccc1aca1..1a8b4286a 100644
--- a/plugins/web-ui/test/pane-focus.test.ts
+++ b/plugins/web-ui/test/pane-focus.test.ts
@@ -2,7 +2,7 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import { JSDOM } from "jsdom";
import { readFileSync } from "node:fs";
-import { preservingFocus, replaceChildrenPreservingFocus } from "../src/pane-focus.ts";
+import { focusComposerOnPaneClick, preservingFocus, replaceChildrenPreservingFocus } from "../src/pane-focus.ts";
test("re-rendered pane inputs keep focus and caret across multi-character typing", () => {
const dom = new JSDOM('<main><div><input data-focus-key="search" value="a"></div></main>');
@@ -47,3 +47,115 @@ test("a pane focusin never re-activates the pane it is already in", () => {
assert.match(focusPane, /if \(!panel \|\| panel\.api\.isActive\) return;/, "re-activation remounts the pane");
assert.match(focusPane, /preservingFocus\(document, \(\) => activatePanel\(panel\)\)/, "activation drops focus");
});
+
+function clickFixture(secondContent = '<textarea class="composer-input">draft</textarea>') {
+ const dom = new JSDOM(`
+ <main>
+ <section id="first">
+ <textarea class="composer-input"></textarea>
+ </section>
+ <section id="second">
+ <p>message</p>
+ ${secondContent}
+ </section>
+ </main>
+ `);
+ const doc = dom.window.document;
+ const first = doc.querySelector<HTMLElement>("#first")!;
+ const second = doc.querySelector<HTMLElement>("#second")!;
+ let active = false;
+ focusComposerOnPaneClick(second, () => active);
+ first.querySelector("textarea")!.focus();
+ function click(target: Element, options: MouseEventInit = {}, intervening?: () => void) {
+ target.dispatchEvent(new dom.window.MouseEvent("pointerdown", { bubbles: true, ...options }));
+ second.focus();
+ active = true;
+ intervening?.();
+ target.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true, cancelable: true, ...options }));
+ }
+ return { dom, doc, first, second, click };
+}
+
+test("switching panes by clicking content focuses the new composer without scrolling or moving its caret", () => {
+ const { doc, second, click } = clickFixture();
+ const composer = second.querySelector("textarea")!;
+ composer.setSelectionRange(2, 2);
+ const focus = composer.focus.bind(composer);
+ composer.focus = (options) => {
+ assert.deepEqual(options, { preventScroll: true });
+ focus(options);
+ };
+ click(second.querySelector("p")!);
+ assert.equal(doc.activeElement, composer);
+ assert.equal(composer.selectionStart, 2);
+});
+
+test("clicking the already active pane does not pull focus back to its composer", () => {
+ const { doc, second, click } = clickFixture();
+ const composer = second.querySelector("textarea")!;
+ click(second.querySelector("p")!);
+ composer.blur();
+ click(second.querySelector("p")!);
+ assert.equal(doc.activeElement, second);
+});
+
+for (const control of ['<input id="control">', '<div id="control" tabindex="0">custom control</div>']) {
+ test(`switching panes preserves focus on ${control}`, () => {
+ const { doc, second, click } = clickFixture(`${control}<textarea class="composer-input"></textarea>`);
+ const target = second.querySelector<HTMLElement>("#control")!;
+ click(target, {}, () => target.focus());
+ assert.equal(doc.activeElement, target);
+ });
+}
+
+for (const options of [{ button: 2 }, { ctrlKey: true }, { metaKey: true }, { shiftKey: true }, { altKey: true }]) {
+ test(`modified pointer click does not focus composer: ${JSON.stringify(options)}`, () => {
+ const { doc, second, click } = clickFixture();
+ click(second, options);
+ assert.equal(doc.activeElement, second);
+ });
+}
+
+for (const event of ["pointercancel", "dragstart"]) {
+ test(`${event} prevents a subsequent click from focusing the composer`, () => {
+ const { doc, dom, second, click } = clickFixture();
+ click(second, {}, () => second.dispatchEvent(new dom.window.Event(event, { bubbles: true })));
+ assert.equal(doc.activeElement, second);
+ });
+}
+
+test("selecting transcript text while switching panes leaves the selection intact", () => {
+ const { doc, second, click } = clickFixture();
+ click(second.querySelector("p")!, {}, () => {
+ const range = doc.createRange();
+ range.selectNodeContents(second.querySelector("p")!);
+ doc.getSelection()!.removeAllRanges();
+ doc.getSelection()!.addRange(range);
+ });
+ assert.equal(doc.getSelection()!.toString(), "message");
+ assert.equal(doc.activeElement, second);
+});
+
+for (const content of ["", '<textarea class="composer-input" disabled></textarea>']) {
+ test(`panes without an enabled composer do not receive input focus: ${content}`, () => {
+ const { doc, second, click } = clickFixture(content);
+ click(second);
+ assert.equal(doc.activeElement, second);
+ });
+}
+
+test("a prevented click does not focus the composer", () => {
+ const { doc, second, click } = clickFixture();
+ const target = second.querySelector("p")!;
+ target.addEventListener("click", (event) => event.preventDefault());
+ click(target);
+ assert.equal(doc.activeElement, second);
+});
+
+test("keyboard-generated clicks do not reuse the last pointer activation", () => {
+ const { doc, dom, first, second, click } = clickFixture();
+ click(second);
+ first.querySelector("textarea")!.focus();
+ second.dispatchEvent(new dom.window.MouseEvent("click", { bubbles: true }));
+ assert.equal(doc.activeElement, first.querySelector("textarea"));
+});