Skip to content

fix(web): a dialog opened with the mouse hands the keyboard back to the terminal - #857

Open
Ark0N wants to merge 1 commit into
masterfrom
fix/dialog-focus-restore
Open

Ark0N wants to merge 1 commit into
masterfrom
fix/dialog-focus-restore

Conversation

@Ark0N

@Ark0N Ark0N commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

What was wrong

Open App Settings or Session Options with a mouse click, close it, and type: nothing reaches the terminal until the terminal is clicked again. It was first seen in the Mac app and is measured in Chromium below, with Escape and with the close button.

The dialogs are built on FocusTrap (src/web/public/keyboard-accessory.js), which saved document.activeElement when the dialog opened and focused it again on close. By the time a dialog opens, the mouse press that opened it has already moved focus:

  • In Chromium and Firefox the pressed control takes focus, so the dialog handed the keyboard back to the gear button. Keys went to that button, and Space or Enter opened the dialog again.
  • In WebKit on macOS (Safari and the Mac app) a button takes no focus from the mouse. The press clears focus to <body>, and focusing <body> again does nothing.

An earlier fix for the command palette and the Session Manager (af4cc45) measured these dialogs opened without a mouse. That path worked then and is unchanged.

The fix

A dialog now hands the keyboard back to the field the user was typing in when the mouse press began.

FocusTrap.focusToReturn() replaces document.activeElement as what a dialog saves on open. Two capture listeners on the document feed it:

  • pointerdown records which element the press began on and, if the focused element is a typing field (a textarea, a text-like input, an editable region), that field. pointerdown is used because moving focus is the default action of mousedown, which comes after it, so the earlier event still sees what had the keyboard.
  • keydown forgets the press, except for a modifier held for the click.

When a dialog opens, the recorded field is the answer if the press is the only reason focus is somewhere else: focus sits on the pressed control (or on an element that contains the press target, such as a button pressed on its icon) or on <body>. In every other case the answer is document.activeElement, as before.

The field is a terminal's own textarea in practice, so with the tile grid or the split open the keyboard goes back to the pane it was in, not to the main terminal.

What keeps the rule narrow:

  • A keyboard user is not affected. A control reached with Tab and opened with Enter gets focus back when the dialog closes, the usual pattern, because the key forgot any earlier press.
  • Only a typing field is remembered. A press that began on a button, for example a dialog opened from inside another dialog, still returns to the pressed control.
  • One press. The field of an earlier press is not carried into the next one, since a dialog may have opened in between and the field would be behind it. For the same reason asking spends the press: whatever is pressed inside the opening dialog starts with nothing remembered.
  • One exception, menus. A press on an item inside [role="menu"] keeps the field of the press that opened the menu. A tile's menu button followed by "Session options" is two presses and one way into a dialog.
  • Mouse only. A finger or a pen records nothing. On a touch device a focused field means the on-screen keyboard, and handing focus back would raise it when a dialog closes. Phone behaviour is unchanged.

Callers: FocusTrap.activate() (App Settings, Session Options, Help, Kill All, the phone's prompt composer) and _rememberOverlayFocus() in panels-ui.js (command palette, Session Manager), so the two places that save focus follow one rule.

Tested

Against a running server, Chromium 156, a real shell session, a real mouse and keyboard (document.activeElement after the dialog closed, and whether a typed character reached the terminal's onData):

Dialog Opened by Before After
App Settings (header gear) mouse gear button, typing lost terminal, typing arrives
Session Options (tab gear) mouse the tab's gear, typing lost terminal, typing arrives
App Settings, press clears focus mouse <body>, typing lost terminal, typing arrives
Session Options, press clears focus mouse <body>, typing lost terminal, typing arrives
App Settings Enter on the focused gear gear button gear button (unchanged)

Each mouse row was run closing with Escape and with the dialog's close button, with the same result. "Press clears focus" is the macOS WebKit behaviour emulated in Chromium: a capture mousedown listener that cancels the default and blurs the focused element when a button is pressed.

Also with the fix, same setup:

  • Tile grid open, two tiles: App Settings opened from each tile in turn, the keyboard went back to that tile and the typed character reached that tile's terminal.
  • Split open: the same for Pane A and Pane B.
  • A tile's menu button, then "Session options": before, focus ended on <body> and typing was lost. After, it is back in the tile's terminal, also with the press clearing focus.

Automated:

  • test/dialog-focus-return.test.ts (23 tests, in the gate): the rule against a fake document, including the keyboard path, the nested-dialog path, the menu exception, touch and pen, a field removed from the page, and the two callers.
  • test/dialog-focus-return.browser.test.ts (11 tests, real Chromium, browser suite): a real mouse and keyboard on a page that holds the shipping keyboard-accessory.js, a textarea and a dialog built on FocusTrap. It opens the dialog from a button, from an element that cannot take focus, from an icon inside a focusable element and through a menu, and types afterwards. 6 of the first 9 tests fail when activate() saves document.activeElement again.
  • npm test passes (764 files, 17,335 tests), as do typecheck, lint, format and the frontend checks.
  • The 16 browser-suite files that open one of these dialogs or read focus pass (134 tests).
  • The four phone-suite files that open settings or read focus (test/mobile/settings, accessibility, keyboard, tabs) give the same result with and without the change on the development host: 110 pass and the same 8 fail, which is that suite's state on master there.

Not tested

  • WebKit on macOS itself. The symptom was first seen in the Mac app on a MacBook. The fix was not run there: Playwright's macOS WebKit does not start on the Mac mini testbed (no screen session), and its Linux build is missing system libraries on the development host. What stands in for it is the emulation above and the browser test's element that cannot take focus, which both produce the same state (<body> focused when the dialog opens). The assumption that pointerdown reaches the page before focus moves is verified in Chromium only.
  • Firefox.

Not changed

  • Dialogs that never handed focus back, which is most of the small confirm dialogs. They are not built on FocusTrap and do not save focus at all. Measured for one of them: the close-session confirm opened from a tab's close button and cancelled leaves focus on that close button, before and after this change. Giving them the same treatment means adding a save and a restore to each, which is left for a follow-up.
  • The local model window, which places its own focus.
  • A menu that hands focus back to its trigger before it runs the item. The rule sees focus on the trigger, not on the pressed item, and returns to the trigger.

…he terminal

After App Settings or Session Options was opened with a mouse click and
closed, nothing typed reached the terminal until it was clicked again.
FocusTrap saved document.activeElement when the dialog opened, and by then
the press had already moved focus: onto the pressed control in Chromium and
Firefox, onto <body> in WebKit on macOS, where a button takes no focus from
the mouse.

FocusTrap.focusToReturn() is what a dialog saves now. A pointerdown listener
records the typing field that had focus when a mouse press began (pointerdown
comes before mousedown, whose default action moves focus), and that field is
the answer when focus sits on the pressed control or on <body> as the dialog
opens. Any key forgets the press, so a control reached with the keyboard gets
focus back as before. Only a typing field is remembered, for one press (a
press on a menu item inherits it from the press that opened the menu) and
only for a mouse, so touch is unchanged. Asking spends the press, so nothing
pressed inside the opening dialog can hand the keyboard to a field behind it.

The command palette and the Session Manager save the same answer.

Measured in Chromium against a running server: App Settings and Session
Options opened by mouse ended on the clicked gear before and in the terminal
after, also with the press clearing focus as WebKit on macOS does, with the
tile grid and the split open (the keyboard returns to the pane it was in),
and through a tile's menu. Not run in WebKit on macOS itself.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants