Skip to content
Merged
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Expand Down
4 changes: 2 additions & 2 deletions js/developer/environment/bundles.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "7275da57703d8a9d06991d4dffc2bed2976bcfa78329171d99deeb522bced5b1",
"js/developer/environment/runtime.mjs": "54fb854a6e2560d9659f792edd1bd4f11dd3fe6d45d586e302ebfc60d1cacf75",
"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",
Expand Down
43 changes: 42 additions & 1 deletion js/developer/environment/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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';
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -689,6 +692,44 @@ 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 that had
// 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,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; }
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);
};
// 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) || !answers()) return;
setTimeout(() => {
// 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();
}, 0);
};
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`);
Expand Down
16 changes: 12 additions & 4 deletions js/developer/environment/workspace.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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. */
Expand All @@ -398,4 +404,6 @@ export async function createWorkspace({bridge, element, frame}) {
close: () => apply('close'),
placeSaved: () => store.saved(),
};
layout.workspace = workspace;
return workspace;
}
93 changes: 93 additions & 0 deletions test/web/developer-specs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,99 @@ 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 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.
terminal.selectAll();
assert(terminal.hasSelection(), 'The terminal must hold a selection');
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();
// 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';
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();
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'), {moveFocus: false});
await settle();
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.
const layoutState = () => page.documentElement.getAttribute('data-webfront-workspace');
assert(layoutState() === 'terminal', `The page must lay the terminal out alone: ${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);
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();
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'],
[{format: 1, name: 'empty', version: '0.1.0'}, 'Usage: wf dev <entry.tsx>']]) {
Expand Down
Loading