From 9f5ef2896c4bf04e93fdc525230dc4537cc3bd8e Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:48:17 +0100 Subject: [PATCH 1/7] Give the focus back to the developer terminal after a click on the page around it (#608) While the terminal has the window, the page around it (the title and the notice of the bootstrap page, blank areas) is not part of xterm.js. A click there moved the focus to the body: the cursor became a hollow box and the keys went nowhere, until a click inside the terminal. Once the click ends, the terminal takes the focus back, unless the click selected text, landed on a control, or an interface or the floating window owns the window. The regression spec clicks outside the terminal, then types. --- docs/CHANGELOG.md | 1 + js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 13 ++++++++++++- test/web/developer-specs.mjs | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e8e1aaca..ed7b4cfb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -26,6 +26,7 @@ these entries stays in the GitHub releases and the Git history. - Show the WebKitGTK window when the page reports its first frame, or when a one-second timer fires, even if the page has not painted, instead of at once; the embedded pages, the starter and the examples use the cheap `sans-serif` and `monospace` font stacks (#611, `docs/WEBKITGTK.md`). ### Fixed +- Keep the input focus of the developer terminal after a click on the page around it (title, notice, blank areas) while the terminal has the window, selection and controls excepted (#608). - Remove the white flash of an embedded view between the developer environment's bootstrap page and the project's page, by giving the window the environment's dark colour (`#141821`) when the application sets none; on macOS the view then no longer paints its own white behind it (#611, `docs/WKWEBVIEW.md`). - Select detected GPU architectures supported by nvcc by default for the host-loop CUDA renderer (`docs/HOST_LOOP_EXAMPLE.md`). diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 26592f3f..250e34a6 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "7275da57703d8a9d06991d4dffc2bed2976bcfa78329171d99deeb522bced5b1", + "js/developer/environment/runtime.mjs": "8c10bd6eaf085272691bfef778276f41ee6f8ae27847e4437740057935a7f205", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index 218bf2b8..1ca1e630 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -17,7 +17,7 @@ import {parseTestOptions, specFiles, jasmineCore, runnerFiles, runInFrame, repor import {generateProvider, applicationPage} from './embed.mjs'; import {typescriptPackage, parseCheckOptions, checkHost} from './typecheck.mjs'; import {pinnedStarters} from './bootstrap.mjs'; -import {createWorkspace, isManaged} from './workspace.mjs'; +import {createWorkspace, isManaged, State} from './workspace.mjs'; import {connectAutomation as connectAgent, clipText, automationHelp, onAutomationNotice} from './automation.mjs'; import {ls, cat, tree, mounts, fileCommandHelp, withheld, withheldReason} from './files.mjs'; import {dependencyTree, why as whyInstalled, info as packageInfo} from './dependencies.mjs'; @@ -689,6 +689,17 @@ export async function mount({bridge, config, terminal: element, preview, onExit, const fitTerminal = () => { if (element.clientWidth && element.clientHeight) fit.fit(); }; if (fitToElement) new ResizeObserver(fitTerminal).observe(element); terminal.onData(data => editor.handle(data)); + // While the terminal has the window, the page around it (title, notice, blank areas) is not part of it: a click there + // would move the focus to the body and the keys would go nowhere (#608). Once the click ends the terminal takes the + // focus back, unless it selected text, a control took the click, or an interface owns the window. + const page = element.ownerDocument; + page.addEventListener('mouseup', event => { + if (workspace && workspace.state !== State.terminal) return; + if (event.target.closest?.('a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]')) return; + setTimeout(() => { + if (!terminal.hasSelection() && !page.getSelection()?.toString()) terminal.focus(); + }, 0); + }); // Automation must be visible to the person using the terminal: say so once it is connected, grants and attach path included. onAutomationNotice(text => { write(`\r\x1b[K\x1b[33m${text}\x1b[0m\r\n`); diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 30f2e946..22034d27 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1298,6 +1298,20 @@ try { const lastRows = count => rows().slice(cursorRow() - count + 1, cursorRow() + 1); await type('echo hello\r'); assert(JSON.stringify(lastRows(3)) === JSON.stringify(['> echo hello', 'hello', '> ']), `A C++ command must run from the terminal: ${lastRows(3)}`); + // A click on the page around the terminal (its title, a notice, a blank area) keeps the input focus (#608). + { + const textarea = host.querySelector('.xterm-helper-textarea'); + const page = host.ownerDocument; + const outside = page.body; + assert(!host.contains(outside) && textarea.ownerDocument.activeElement === textarea, 'The terminal must have the focus before the click'); + // A browser moves the focus to the body when a press lands outside any focusable element. + textarea.blur(); + for (const type of ['mousedown', 'mouseup', 'click']) + outside.dispatchEvent(new MouseEvent(type, {bubbles: true, cancelable: true, button: 0})); + await eventually(() => page.activeElement === textarea, 'A click on the page around the terminal must leave the terminal focused', {every: 10}); + await type('echo after-click\r'); + assert(lastRows(2)[0] === 'after-click', `Typing after a click outside the terminal must run: ${lastRows(3)}`); + } // Initial dev errors must finish the session command, without requiring Ctrl+C (#560). for (const [manifest, message] of [[null, 'No webfront.project.json: run wf init'], [{format: 1, name: 'empty', version: '0.1.0'}, 'Usage: wf dev ']]) { From 60e818027c9bfb6d7f230c36c91d3e0e7a79e75e Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:55:54 +0100 Subject: [PATCH 2/7] Give the focus back only to the terminal that had it when a page holds several (#608) Each mount() added a listener on the whole document, so a click inside the first terminal also fired the second one's, which took the focus a moment later and sent the typed commands to the wrong session. The page now remembers which terminal was focused last: only that one answers a click on the page around the terminals, and a click inside any terminal is left to xterm.js. A listener removes itself once its terminal has left the page. The regression spec mounts a second terminal and clicks in each, then on the page. --- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 20 +++++++++++++++----- test/web/developer-specs.mjs | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 250e34a6..c133b3fc 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "8c10bd6eaf085272691bfef778276f41ee6f8ae27847e4437740057935a7f205", + "js/developer/environment/runtime.mjs": "a4cb4aea07e3a9ce9c1c2d74a884371d7ef610e79b378e5a684457a33cfa94cf", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index 1ca1e630..db1b8438 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -638,6 +638,9 @@ export async function complete(line, {bridge, cppCommands = new Map()}) { return matching(await projectPaths(bridge, word)); } +/** The terminal of each page that had the focus last: the one a click on the page around the terminals gives it back to. */ +const focusedTerminal = new WeakMap(); + /** * Mounts the terminal in `element`. Each page has its own session: its history comes from the project, its C++ * commands run in this browser's session. `onExit(code)` ends the session after exit (by default, closes the window). @@ -690,16 +693,23 @@ export async function mount({bridge, config, terminal: element, preview, onExit, if (fitToElement) new ResizeObserver(fitTerminal).observe(element); terminal.onData(data => editor.handle(data)); // While the terminal has the window, the page around it (title, notice, blank areas) is not part of it: a click there - // would move the focus to the body and the keys would go nowhere (#608). Once the click ends the terminal takes the - // focus back, unless it selected text, a control took the click, or an interface owns the window. + // would move the focus to the body and the keys would go nowhere (#608). Once the click ends, the terminal that had + // the focus takes it back, unless the click selected text, landed on a control or in a terminal (xterm.js keeps its + // own focus), or an interface owns the window. A page may hold several terminals: only the last one focused answers. const page = element.ownerDocument; - page.addEventListener('mouseup', event => { + terminal.textarea.addEventListener('focus', () => focusedTerminal.set(page, terminal)); + const takeFocusBack = event => { + if (!element.isConnected) return page.removeEventListener('mouseup', takeFocusBack); + const last = focusedTerminal.get(page); + if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click + if (last !== terminal) return; if (workspace && workspace.state !== State.terminal) return; - if (event.target.closest?.('a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]')) return; + if (event.target.closest?.('.xterm,a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]')) return; setTimeout(() => { if (!terminal.hasSelection() && !page.getSelection()?.toString()) terminal.focus(); }, 0); - }); + }; + page.addEventListener('mouseup', takeFocusBack); // Automation must be visible to the person using the terminal: say so once it is connected, grants and attach path included. onAutomationNotice(text => { write(`\r\x1b[K\x1b[33m${text}\x1b[0m\r\n`); diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 22034d27..e91a5914 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1311,6 +1311,29 @@ try { await eventually(() => page.activeElement === textarea, 'A click on the page around the terminal must leave the terminal focused', {every: 10}); await type('echo after-click\r'); assert(lastRows(2)[0] === 'after-click', `Typing after a click outside the terminal must run: ${lastRows(3)}`); + // Two terminals in the page: a click gives the focus back to the one that had it, and to no other. + const spare = page.createElement('div'); + spare.style.cssText = 'width:320px;height:120px'; + page.body.append(spare); + const other = await runtime.mount({bridge: terminalBridge, config, terminal: spare, preview: context.preview, onExit: () => {}, fitToElement: false}); + try { + const settle = () => delay(50); // the focus is given back after the click, in a task of its own + terminal.focus(); + host.querySelector('.xterm-screen').dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + await settle(); + assert(page.activeElement === terminal.textarea, 'A click in a terminal must not give the focus to another terminal'); + other.terminal.focus(); + spare.querySelector('.xterm-screen').dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + await settle(); + assert(page.activeElement === other.terminal.textarea, 'A click in the second terminal must keep it focused'); + other.terminal.textarea.blur(); + page.body.dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + await eventually(() => page.activeElement === other.terminal.textarea, 'A click on the page must give the focus back to the terminal that had it', {every: 10}); + } finally { + other.terminal.dispose(); + spare.remove(); + terminal.focus(); + } } // Initial dev errors must finish the session command, without requiring Ctrl+C (#560). for (const [manifest, message] of [[null, 'No webfront.project.json: run wf init'], From c0ea4b849931ef79b26d3c70b33e2274ee0aa9ee Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:00:24 +0100 Subject: [PATCH 3/7] Judge the click by where the press started, not by an older selection (#608) terminal.hasSelection() stays true after a selection made earlier, so a later click on a blank area of the page never gave the focus back and the keys still went nowhere. The page now notes where each press starts. The terminal takes the focus back only when the press began outside any terminal and control; a click outside cannot create an xterm.js selection, which is left alone. A selection of the page's own text still keeps the focus where the user put it. The regression spec selects the terminal's text, clicks the page and types. --- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 28 ++++++++++++++++----------- test/web/developer-specs.mjs | 17 +++++++++++++--- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index c133b3fc..8d6dc74d 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "a4cb4aea07e3a9ce9c1c2d74a884371d7ef610e79b378e5a684457a33cfa94cf", + "js/developer/environment/runtime.mjs": "41a7330f5961291ab41c9e8ddffc314c5528c5705c19d7639e26b61d321bdf98", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index db1b8438..358cb4bb 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -694,22 +694,28 @@ export async function mount({bridge, config, terminal: element, preview, onExit, terminal.onData(data => editor.handle(data)); // While the terminal has the window, the page around it (title, notice, blank areas) is not part of it: a click there // would move the focus to the body and the keys would go nowhere (#608). Once the click ends, the terminal that had - // the focus takes it back, unless the click selected text, landed on a control or in a terminal (xterm.js keeps its - // own focus), or an interface owns the window. A page may hold several terminals: only the last one focused answers. + // the focus takes it back, unless the press started on a control or in a terminal (xterm.js keeps its own focus and + // selection, an older one included), the click selected text of the page, or an interface owns the window. A page + // may hold several terminals: only the last one focused answers. const page = element.ownerDocument; terminal.textarea.addEventListener('focus', () => focusedTerminal.set(page, terminal)); - const takeFocusBack = event => { - if (!element.isConnected) return page.removeEventListener('mouseup', takeFocusBack); + const ownsClick = target => !target.closest?.('.xterm,a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]'); + let pressedOnPage = false; + const press = event => { + if (!element.isConnected) { page.removeEventListener('mousedown', press, true); page.removeEventListener('mouseup', release); return; } + pressedOnPage = ownsClick(event.target); + }; + const release = event => { + const started = pressedOnPage; + pressedOnPage = false; + if (!started || !ownsClick(event.target)) return; const last = focusedTerminal.get(page); if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click - if (last !== terminal) return; - if (workspace && workspace.state !== State.terminal) return; - if (event.target.closest?.('.xterm,a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]')) return; - setTimeout(() => { - if (!terminal.hasSelection() && !page.getSelection()?.toString()) terminal.focus(); - }, 0); + if (last !== terminal || (workspace && workspace.state !== State.terminal)) return; + setTimeout(() => { if (!page.getSelection()?.toString()) terminal.focus(); }, 0); }; - page.addEventListener('mouseup', takeFocusBack); + page.addEventListener('mousedown', press, true); + page.addEventListener('mouseup', release); // Automation must be visible to the person using the terminal: say so once it is connected, grants and attach path included. onAutomationNotice(text => { write(`\r\x1b[K\x1b[33m${text}\x1b[0m\r\n`); diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index e91a5914..5ecb5819 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1311,6 +1311,17 @@ try { await eventually(() => page.activeElement === textarea, 'A click on the page around the terminal must leave the terminal focused', {every: 10}); await type('echo after-click\r'); assert(lastRows(2)[0] === 'after-click', `Typing after a click outside the terminal must run: ${lastRows(3)}`); + // An older selection of the terminal does not keep the focus away: a click that selects nothing gives it back. + const click = target => ['mousedown', 'mouseup', 'click'].forEach(type => target.dispatchEvent(new MouseEvent(type, {bubbles: true, cancelable: true, button: 0}))); + terminal.selectAll(); + assert(terminal.hasSelection(), 'The terminal must hold a selection'); + textarea.blur(); + click(page.body); + await eventually(() => page.activeElement === textarea, 'A click on the page must give the focus back although the terminal still holds an older selection', {every: 10}); + assert(terminal.hasSelection(), 'The click must leave the terminal\'s selection alone'); + await type('echo after-selection\r'); + assert(lastRows(2)[0] === 'after-selection', `Typing after a click that follows a selection must run: ${lastRows(3)}`); + terminal.clearSelection(); // Two terminals in the page: a click gives the focus back to the one that had it, and to no other. const spare = page.createElement('div'); spare.style.cssText = 'width:320px;height:120px'; @@ -1319,15 +1330,15 @@ try { try { const settle = () => delay(50); // the focus is given back after the click, in a task of its own terminal.focus(); - host.querySelector('.xterm-screen').dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + click(host.querySelector('.xterm-screen')); await settle(); assert(page.activeElement === terminal.textarea, 'A click in a terminal must not give the focus to another terminal'); other.terminal.focus(); - spare.querySelector('.xterm-screen').dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + click(spare.querySelector('.xterm-screen')); await settle(); assert(page.activeElement === other.terminal.textarea, 'A click in the second terminal must keep it focused'); other.terminal.textarea.blur(); - page.body.dispatchEvent(new MouseEvent('mouseup', {bubbles: true, cancelable: true, button: 0})); + click(page.body); await eventually(() => page.activeElement === other.terminal.textarea, 'A click on the page must give the focus back to the terminal that had it', {every: 10}); } finally { other.terminal.dispose(); From 5ed7244184d1c1c42a956b445c070fad83a41d55 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:05:55 +0100 Subject: [PATCH 4/7] Find the terminal to refocus from the focus at the press, not from focus events (#608) The last focused terminal was recorded from the textarea's focus event, which a window without the system focus never receives (several browsers on one display): no terminal then answered a click on the page. The press, which comes before the browser moves the focus, now records the terminal that holds it. The regression spec follows the browser's order: the focus moves to the body after the press, and clicks in a terminal leave it. --- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 2 ++ test/web/developer-specs.mjs | 18 +++++++++--------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 8d6dc74d..016368f9 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "41a7330f5961291ab41c9e8ddffc314c5528c5705c19d7639e26b61d321bdf98", + "js/developer/environment/runtime.mjs": "8b53e2d3842afe284754a082da34d67e617f497edb5592c7c87ca1df366426d1", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index 358cb4bb..053fa794 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -704,6 +704,8 @@ export async function mount({bridge, config, terminal: element, preview, onExit, const press = event => { if (!element.isConnected) { page.removeEventListener('mousedown', press, true); page.removeEventListener('mouseup', release); return; } pressedOnPage = ownsClick(event.target); + // The press comes before the browser moves the focus: whoever holds it now is the terminal to give it back to. + if (page.activeElement === terminal.textarea) focusedTerminal.set(page, terminal); }; const release = event => { const started = pressedOnPage; diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 5ecb5819..4a4d4440 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1304,18 +1304,19 @@ try { const page = host.ownerDocument; const outside = page.body; assert(!host.contains(outside) && textarea.ownerDocument.activeElement === textarea, 'The terminal must have the focus before the click'); - // A browser moves the focus to the body when a press lands outside any focusable element. - textarea.blur(); - for (const type of ['mousedown', 'mouseup', 'click']) - outside.dispatchEvent(new MouseEvent(type, {bubbles: true, cancelable: true, button: 0})); + // A press outside any focusable element moves the focus to the body, after the press event. + const click = (target, {moveFocus = true} = {}) => { + target.dispatchEvent(new MouseEvent('mousedown', {bubbles: true, cancelable: true, button: 0})); + if (moveFocus) page.activeElement?.blur(); + for (const type of ['mouseup', 'click']) target.dispatchEvent(new MouseEvent(type, {bubbles: true, cancelable: true, button: 0})); + }; + click(outside); await eventually(() => page.activeElement === textarea, 'A click on the page around the terminal must leave the terminal focused', {every: 10}); await type('echo after-click\r'); assert(lastRows(2)[0] === 'after-click', `Typing after a click outside the terminal must run: ${lastRows(3)}`); // An older selection of the terminal does not keep the focus away: a click that selects nothing gives it back. - const click = target => ['mousedown', 'mouseup', 'click'].forEach(type => target.dispatchEvent(new MouseEvent(type, {bubbles: true, cancelable: true, button: 0}))); terminal.selectAll(); assert(terminal.hasSelection(), 'The terminal must hold a selection'); - textarea.blur(); click(page.body); await eventually(() => page.activeElement === textarea, 'A click on the page must give the focus back although the terminal still holds an older selection', {every: 10}); assert(terminal.hasSelection(), 'The click must leave the terminal\'s selection alone'); @@ -1330,14 +1331,13 @@ try { try { const settle = () => delay(50); // the focus is given back after the click, in a task of its own terminal.focus(); - click(host.querySelector('.xterm-screen')); + click(host.querySelector('.xterm-screen'), {moveFocus: false}); await settle(); assert(page.activeElement === terminal.textarea, 'A click in a terminal must not give the focus to another terminal'); other.terminal.focus(); - click(spare.querySelector('.xterm-screen')); + click(spare.querySelector('.xterm-screen'), {moveFocus: false}); await settle(); assert(page.activeElement === other.terminal.textarea, 'A click in the second terminal must keep it focused'); - other.terminal.textarea.blur(); click(page.body); await eventually(() => page.activeElement === other.terminal.textarea, 'A click on the page must give the focus back to the terminal that had it', {every: 10}); } finally { From 392730ade7f6fa18a90c164e26be680cc92ccf1d Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:12:38 +0100 Subject: [PATCH 5/7] Leave the focus to the field a label hands it to (#608) A label was not among the elements that take a click, and the focus given back to the terminal after the click came after the browser had moved it to the label's field: the typing went into the terminal. A label now takes the click, and whatever field of the page holds the focus once the click has ended keeps it. The regression spec clicks a label and a page area while a field is focused. --- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 11 ++++++++--- test/web/developer-specs.mjs | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index 016368f9..ff94e067 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "8b53e2d3842afe284754a082da34d67e617f497edb5592c7c87ca1df366426d1", + "js/developer/environment/runtime.mjs": "367b20b9f90cfa851ef9c811e6a5150d3d78206d5bbc6e5ea6f8516b559e7a61", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index 053fa794..b2bda001 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -694,12 +694,12 @@ export async function mount({bridge, config, terminal: element, preview, onExit, terminal.onData(data => editor.handle(data)); // While the terminal has the window, the page around it (title, notice, blank areas) is not part of it: a click there // would move the focus to the body and the keys would go nowhere (#608). Once the click ends, the terminal that had - // the focus takes it back, unless the press started on a control or in a terminal (xterm.js keeps its own focus and + // the focus takes it back, unless the press started on a control, a label or in a terminal (xterm.js keeps its own focus and // selection, an older one included), the click selected text of the page, or an interface owns the window. A page // may hold several terminals: only the last one focused answers. const page = element.ownerDocument; terminal.textarea.addEventListener('focus', () => focusedTerminal.set(page, terminal)); - const ownsClick = target => !target.closest?.('.xterm,a,button,input,select,textarea,summary,iframe,[contenteditable],[tabindex]'); + const ownsClick = target => !target.closest?.('.xterm,a,button,input,select,textarea,label,summary,iframe,[contenteditable],[tabindex]'); let pressedOnPage = false; const press = event => { if (!element.isConnected) { page.removeEventListener('mousedown', press, true); page.removeEventListener('mouseup', release); return; } @@ -714,7 +714,12 @@ export async function mount({bridge, config, terminal: element, preview, onExit, const last = focusedTerminal.get(page); if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click if (last !== terminal || (workspace && workspace.state !== State.terminal)) return; - setTimeout(() => { if (!page.getSelection()?.toString()) terminal.focus(); }, 0); + setTimeout(() => { + // The click may have given the focus to a field of the page (a label does): it stays there. + const held = page.activeElement; + if (held && held !== page.body && held !== page.documentElement && held !== terminal.textarea) return; + if (!page.getSelection()?.toString()) terminal.focus(); + }, 0); }; page.addEventListener('mousedown', press, true); page.addEventListener('mouseup', release); diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 4a4d4440..971082d5 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1323,6 +1323,26 @@ try { await type('echo after-selection\r'); assert(lastRows(2)[0] === 'after-selection', `Typing after a click that follows a selection must run: ${lastRows(3)}`); terminal.clearSelection(); + // A label hands the focus to its field, and the terminal leaves it there. + const field = page.createElement('input'), label = page.createElement('label'); + field.id = 'webfront-608-field'; + label.htmlFor = field.id; + label.textContent = 'Name'; + page.body.append(label, field); + try { + click(label); + await delay(50); + assert(page.activeElement === field, `A click on a label must leave the focus to its field: ${page.activeElement?.tagName}`); + // Whatever takes the click, the terminal does not take the focus from a field of the page. + field.focus(); + click(page.body, {moveFocus: false}); + await delay(50); + assert(page.activeElement === field, 'A click that leaves a field focused must not move the focus to the terminal'); + } finally { + label.remove(); + field.remove(); + terminal.focus(); + } // Two terminals in the page: a click gives the focus back to the one that had it, and to no other. const spare = page.createElement('div'); spare.style.cssText = 'width:320px;height:120px'; From 531d8cd8cc8c6fd40657368fade150167caca0df Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:30:33 +0100 Subject: [PATCH 6/7] Share the workspace state with every terminal of the page (#608) Only the first terminal mounted with a preview receives the workspace object; the others get null because the preview is already laid out. A second terminal that had the focus therefore ignored the state: with the terminal floating or an interface in the window, a click on the page gave it the focus back and took the keys away from the interface. The workspace now registers itself for its preview frame, and every terminal reads its state from there (it does not refocus while the state is unknown). The regression spec focuses the second terminal, shows an interface, clicks the page and checks that no terminal takes the focus. --- js/developer/environment/bundles.json | 4 ++-- js/developer/environment/runtime.mjs | 6 ++++-- js/developer/environment/workspace.mjs | 16 ++++++++++++---- test/web/developer-specs.mjs | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index ff94e067..d08cc654 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,12 +17,12 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "367b20b9f90cfa851ef9c811e6a5150d3d78206d5bbc6e5ea6f8516b559e7a61", + "js/developer/environment/runtime.mjs": "11567a9646ceca280f48e96ce2309c988bd89bccdca122ca8ebf0b11ccccbc55", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", "js/developer/environment/terminal.mjs": "1d81a28a6e6cfd5bdb1ff8854e17a273b7bcfebf2c97d469b8706db636f3799e", - "js/developer/environment/workspace.mjs": "4fe794b140515579c4f80a226d8b1beca254a3488e2e4d9f6ba580e077a11558", + "js/developer/environment/workspace.mjs": "05d3474f1581eff1b41cea38318d20166e672997c2fec94d16ba8dd50edd09fb", "js/developer/environment/tarball.mjs": "1c1e1aea067910e1fec74e85b4bf9fbefed9b38187a04d8d2764f0b9a96fca39", "js/developer/environment/commands.mjs": "a759f48584b78aed79e9965fd202d2ec108a6948ef6f1b8d689fed87a458b265", "js/developer/environment/files.mjs": "199336f7f3d23f6370da352993bf4686738b1f394b79aa9883769d19824540e0", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index b2bda001..53745c87 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -17,7 +17,7 @@ import {parseTestOptions, specFiles, jasmineCore, runnerFiles, runInFrame, repor import {generateProvider, applicationPage} from './embed.mjs'; import {typescriptPackage, parseCheckOptions, checkHost} from './typecheck.mjs'; import {pinnedStarters} from './bootstrap.mjs'; -import {createWorkspace, isManaged, State} from './workspace.mjs'; +import {createWorkspace, isManaged, layoutState, State} from './workspace.mjs'; import {connectAutomation as connectAgent, clipText, automationHelp, onAutomationNotice} from './automation.mjs'; import {ls, cat, tree, mounts, fileCommandHelp, withheld, withheldReason} from './files.mjs'; import {dependencyTree, why as whyInstalled, info as packageInfo} from './dependencies.mjs'; @@ -700,6 +700,8 @@ export async function mount({bridge, config, terminal: element, preview, onExit, const page = element.ownerDocument; terminal.textarea.addEventListener('focus', () => focusedTerminal.set(page, terminal)); const ownsClick = target => !target.closest?.('.xterm,a,button,input,select,textarea,label,summary,iframe,[contenteditable],[tabindex]'); + // The workspace belongs to the first terminal mounted with the preview, but every terminal of the page answers to its state. + const shownState = () => workspace ? workspace.state : preview && isManaged(preview) ? layoutState(preview) : State.terminal; let pressedOnPage = false; const press = event => { if (!element.isConnected) { page.removeEventListener('mousedown', press, true); page.removeEventListener('mouseup', release); return; } @@ -713,7 +715,7 @@ export async function mount({bridge, config, terminal: element, preview, onExit, if (!started || !ownsClick(event.target)) return; const last = focusedTerminal.get(page); if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click - if (last !== terminal || (workspace && workspace.state !== State.terminal)) return; + if (last !== terminal || shownState() !== State.terminal) return; setTimeout(() => { // The click may have given the focus to a field of the page (a label does): it stays there. const held = page.activeElement; diff --git a/js/developer/environment/workspace.mjs b/js/developer/environment/workspace.mjs index 4ea4203d..b415bcd4 100644 --- a/js/developer/environment/workspace.mjs +++ b/js/developer/environment/workspace.mjs @@ -202,9 +202,14 @@ ${floating} .webfront-terminal-bar{display:flex} :root[${attribute}=terminal] .webfront-terminal-toggle,:root[${attribute}=terminal] .webfront-interface{display:none} `; -const managed = new WeakSet(); +const layouts = new WeakMap(); /** Whether a workspace already lays out this preview frame: a page has one, whatever the terminals mounted in it. */ -export const isManaged = frame => managed.has(frame); +export const isManaged = frame => layouts.has(frame); +/** + * The state of the workspace that lays out this preview frame, for every terminal of the page and not only the one that + * created it; `undefined` while the workspace is being created (it does not know yet whether an interface exists). + */ +export const layoutState = frame => layouts.get(frame)?.workspace?.state; /** * Lays the page out around the terminal's `element` and the preview `frame`, which starts hidden. Call it before the @@ -213,7 +218,8 @@ export const isManaged = frame => managed.has(frame); * the `status` shown on the tab and command(running) to say that a command runs, toggle(), open({focus}) and close(), `button` and `panel`, the `shortcut` in use ("F9") and placeSaved(). */ export async function createWorkspace({bridge, element, frame}) { - managed.add(frame); + const layout = {workspace: null}; + layouts.set(frame, layout); const page = element.ownerDocument, view = page.defaultView, root = page.documentElement; const [interfaceExists, remembered, shortcut] = await Promise.all([hasInterface(bridge), readPlace(bridge), readShortcut(bridge)]); const keys = shortcutText(shortcut); @@ -379,7 +385,7 @@ export async function createWorkspace({bridge, element, frame}) { view.addEventListener('resize', render); render(); - return { + const workspace = { get state() { return state; }, get status() { return tabStatus(session); }, /** Tells whether a command runs in the terminal. */ @@ -398,4 +404,6 @@ export async function createWorkspace({bridge, element, frame}) { close: () => apply('close'), placeSaved: () => store.saved(), }; + layout.workspace = workspace; + return workspace; } diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 971082d5..3b9e2516 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1360,6 +1360,24 @@ try { assert(page.activeElement === other.terminal.textarea, 'A click in the second terminal must keep it focused'); click(page.body); await eventually(() => page.activeElement === other.terminal.textarea, 'A click on the page must give the focus back to the terminal that had it', {every: 10}); + // The workspace belongs to the first terminal, yet the second one answers to its state: with an interface in the + // window, a click on the page does not take the keys away from it. + const layoutState = () => page.documentElement.getAttribute('data-webfront-workspace'); + assert(layoutState() === 'terminal', `The page must lay the terminal out alone: ${layoutState()}`); + context.preview.dispatchEvent(new Event('webfront-preview-navigating')); + assert(layoutState() === 'floating', `A shown interface must float the terminal: ${layoutState()}`); + try { + other.terminal.focus(); + assert(page.activeElement === other.terminal.textarea, 'The second terminal must hold the focus'); + click(page.body); + await delay(100); + assert(page.activeElement !== other.terminal.textarea && page.activeElement !== terminal.textarea, + `No terminal must take the focus back while an interface is shown: ${page.activeElement?.className}`); + } finally { + context.preview.hidden = false; + context.preview.hidden = true; + await eventually(() => layoutState() === 'terminal', 'The terminal must have the window again', {every: 10}); + } } finally { other.terminal.dispose(); spare.remove(); From 0dec9a6da2b601ac72cae1a299b2f56bdaf8a020 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:50:42 +0100 Subject: [PATCH 7/7] Check again, when the focus is given back, that the terminal still answers (#608) The state of the workspace and the last focused terminal were checked at the end of the click, but the focus is given back in a task of its own. A click handler or a navigation that opened the interface in between still got the terminal's focus() call, with the interface now owning the window. The checks are one function, run at the release and again in the deferred task. The regression spec changes the state right after the click ends and checks that the second terminal does not take the focus. --- js/developer/environment/bundles.json | 2 +- js/developer/environment/runtime.mjs | 15 ++++++++++----- test/web/developer-specs.mjs | 15 +++++++++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index d08cc654..f8383c01 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -17,7 +17,7 @@ "js/developer/environment/packages.mjs": "37fe2a410e211264cfeaedbff3df50f7e62b02f8088130127d94f92081e2a5ad", "js/developer/environment/starter.mjs": "fd43db1861121577c48a0327436ce39cb9076d7cad0e9e273d40955747cc6a48", "js/developer/environment/paths.mjs": "3b6114df78eb193161c5c84499dda084cb7ff91c56a4496371c30295d00880e3", - "js/developer/environment/runtime.mjs": "11567a9646ceca280f48e96ce2309c988bd89bccdca122ca8ebf0b11ccccbc55", + "js/developer/environment/runtime.mjs": "54fb854a6e2560d9659f792edd1bd4f11dd3fe6d45d586e302ebfc60d1cacf75", "js/developer/environment/automation.mjs": "79405c89ba7025c1774bc13c9b78d814a07592755dcde018c8659a8015c4b8ed", "js/developer/environment/development.mjs": "1f32f911610c72ad3e935de1c2b138fa275ce9eda04bb63c824d5e09923305a6", "js/developer/environment/testing.mjs": "aeb584505fab5957b9c02e78536e12f7d7f3d12f8adb2e17f1a1a6e395e43f3c", diff --git a/js/developer/environment/runtime.mjs b/js/developer/environment/runtime.mjs index 53745c87..e640354f 100644 --- a/js/developer/environment/runtime.mjs +++ b/js/developer/environment/runtime.mjs @@ -709,15 +709,20 @@ export async function mount({bridge, config, terminal: element, preview, onExit, // The press comes before the browser moves the focus: whoever holds it now is the terminal to give it back to. if (page.activeElement === terminal.textarea) focusedTerminal.set(page, terminal); }; + // This terminal is the one to give the focus back to: it held it last, it is on the page, and it has the window. + const answers = () => { + const last = focusedTerminal.get(page); + if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click + return last === terminal && element.isConnected && shownState() === State.terminal; + }; const release = event => { const started = pressedOnPage; pressedOnPage = false; - if (!started || !ownsClick(event.target)) return; - const last = focusedTerminal.get(page); - if (last && !last.element?.isConnected) focusedTerminal.delete(page); // that terminal is gone: nobody answers this click - if (last !== terminal || shownState() !== State.terminal) return; + if (!started || !ownsClick(event.target) || !answers()) return; setTimeout(() => { - // The click may have given the focus to a field of the page (a label does): it stays there. + // The click, or what it triggered, may have changed things since: an interface may own the window, another + // terminal may hold the focus, and a field of the page may have it (a label gives it to its field). + if (!answers()) return; const held = page.activeElement; if (held && held !== page.body && held !== page.documentElement && held !== terminal.textarea) return; if (!page.getSelection()?.toString()) terminal.focus(); diff --git a/test/web/developer-specs.mjs b/test/web/developer-specs.mjs index 3b9e2516..3e138985 100644 --- a/test/web/developer-specs.mjs +++ b/test/web/developer-specs.mjs @@ -1360,13 +1360,20 @@ try { assert(page.activeElement === other.terminal.textarea, 'A click in the second terminal must keep it focused'); click(page.body); await eventually(() => page.activeElement === other.terminal.textarea, 'A click on the page must give the focus back to the terminal that had it', {every: 10}); - // The workspace belongs to the first terminal, yet the second one answers to its state: with an interface in the - // window, a click on the page does not take the keys away from it. + // The workspace belongs to the first terminal, yet the second one answers to its state. const layoutState = () => page.documentElement.getAttribute('data-webfront-workspace'); assert(layoutState() === 'terminal', `The page must lay the terminal out alone: ${layoutState()}`); - context.preview.dispatchEvent(new Event('webfront-preview-navigating')); - assert(layoutState() === 'floating', `A shown interface must float the terminal: ${layoutState()}`); try { + // The state changes after the click ends, before the focus is given back: the window now floats, and the + // terminal that owns it takes the keys, not the second one. + other.terminal.focus(); + click(page.body); + context.preview.dispatchEvent(new Event('webfront-preview-navigating')); + assert(layoutState() === 'floating', `A shown interface must float the terminal: ${layoutState()}`); + await delay(100); + assert(page.activeElement !== other.terminal.textarea, + 'The second terminal must not take the focus back once an interface has the window'); + // With the interface already shown, a click on the page does not give the focus to any terminal. other.terminal.focus(); assert(page.activeElement === other.terminal.textarea, 'The second terminal must hold the focus'); click(page.body);