Repository navigation
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 saveddocument.activeElementwhen 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:<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()replacesdocument.activeElementas what a dialog saves on open. Two capture listeners on the document feed it:pointerdownrecords 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.pointerdownis used because moving focus is the default action ofmousedown, which comes after it, so the earlier event still sees what had the keyboard.keydownforgets 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 isdocument.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:
[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.Callers:
FocusTrap.activate()(App Settings, Session Options, Help, Kill All, the phone's prompt composer) and_rememberOverlayFocus()inpanels-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.activeElementafter the dialog closed, and whether a typed character reached the terminal'sonData):<body>, typing lost<body>, typing lostEach 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
mousedownlistener that cancels the default and blurs the focused element when a button is pressed.Also with the fix, same setup:
<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 shippingkeyboard-accessory.js, a textarea and a dialog built onFocusTrap. 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 whenactivate()savesdocument.activeElementagain.npm testpasses (764 files, 17,335 tests), as do typecheck, lint, format and the frontend checks.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
<body>focused when the dialog opens). The assumption thatpointerdownreaches the page before focus moves is verified in Chromium only.Not changed
FocusTrapand 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.