mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-30 16:39:33 +01:00
fix(web): a dialog opened with the mouse hands the keyboard back to the 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 commit is contained in:
1 parent
5548c17c4a
commit
79f85415d8
7 files changed
+756
-2
No files matched your search
@@ -56,6 +56,8 @@ Frontend JS modules have `@fileoverview` with `@dependency`/`@loadorder` tags. L
|
||||
|
||||
**Command palette + shortcut registry**: `Ctrl/Cmd/Alt+K` opens the session palette; shortcuts live in a rebindable registry (`DEFAULT_SHORTCUTS`/`getShortcutRegistry()`/`matchesShortcutEvent()` in app.js, overrides in `settings.shortcutOverrides`). ⚠️ Palette-chord keys must ALSO be swallowed in `attachCustomKeyEventHandler` (terminal-ui.js) or xterm writes the control byte into the PTY. ⚠️ The global Escape handler (app.js) calls EVERY close method on every Escape, in the capture phase, so a close method that does more than hide (the palette and Session Manager restore focus) must return early when its overlay is not open. ⚠️ `saveAppSettings()` rebuilds settings from the DOM, so keys edited elsewhere (`shortcutOverrides`, `showTokenCount`, `showCost`) need explicit `_prev` carry-over. ⚠️ **Smart copy (`Ctrl+C`)**: with no selection it must `return true` without `preventDefault()` or the interrupt is lost; keep `copyTerminalSelection` out of `SHORTCUT_ACTIONS`. The gate tests the CLEANED selection (`CodemanCopySelection.clean`: trailing padding, plus a LEADING margin only up to the width the CLI declares in `capabilities.transcriptGutter`, never one derived from the pane); the strip is not idempotent, so clean once and pass the RAW selection on, and leave Alt+drag column selections untouched. → [architecture-invariants#command-palette-and-shortcut-registry](../../docs/architecture-invariants.md#command-palette-and-shortcut-registry)
|
||||
|
||||
**Where a dialog hands the keyboard back to** (`FocusTrap.focusToReturn()`, keyboard-accessory.js): a dialog that saves focus on open asks this, never `document.activeElement`. By the time a dialog opens, the mouse press that opened it has 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), and restoring that left the keyboard out of the terminal until it was clicked. The answer is the typing field (textarea, text-like input, editable region) that had focus at `pointerdown`, whenever focus now sits on the pressed control or on `<body>`. ⚠️ `pointerdown`, never `mousedown` or `click`: moving focus is `mousedown`'s default action, so only the earlier event still sees what had the keyboard. ⚠️ One press, from a typing field, by mouse: the field of an earlier press is not carried over (it can be behind a dialog opened since), a press that began on a button (a dialog opened inside a dialog) still returns to the pressed control, any key but a held modifier forgets the press (a keyboard user gets the control back), and a finger or pen records nothing (a focused field there means the on-screen keyboard). The one press that inherits a field is a press on an item inside `[role="menu"]`, from the press that opened the menu (a tile's menu button, then "Session options"); a menu that hands focus back to its trigger before running the item is not covered. ⚠️ Asking spends the press, so nothing pressed inside the opening dialog can hand the keyboard to a field behind it; call it once, when the dialog opens. Callers: `FocusTrap.activate()` (App Settings, Session Options, Help, Kill All, the phone composer) and `_rememberOverlayFocus()` (command palette, Session Manager); the local model window places its own focus. A dialog that never handed focus back (most of the small confirm dialogs) is not changed by this. Tests: `test/dialog-focus-return.test.ts`, `test/dialog-focus-return.browser.test.ts` (real Chromium, not in the gate).
|
||||
|
||||
**Per-device vs synced settings**: the `displayKeys` set in settings-ui.js is a **client-side merge policy**, not a wire filter. A display key seeds from the server only when localStorage has no value for it, which is what prevents one device overwriting another; `showPlanUsageLimits` is additionally `delete`d from the incoming payload outright. Separately, `SettingsUpdateSchema` is `.strict()` and simply **does not declare** `skin`, `showFileViewerButton`, `showCronButton`, `webglRendererEnabled`, `localEchoEnabled`, `keyboardHoldsRows`, `cjkInputEnabled`, `cjkInputHoldUntilEnter`, or `extendedKeyboardBar`, so sending one of those is a validation error. The rest (`showResponseViewer`, `showPlanUsageLimits`, `language`, and most `show*` keys) ARE in the schema and do persist server-side; they are per-device by client policy only. ⚠️ Adding a new per-device setting means deciding **both** questions: membership in `displayKeys`, and presence in the schema.
|
||||
|
||||
**Settings surface** (`#appSettingsModal` + `#sessionOptionsModal` + `#createCaseModal`): one `set-*` language shared through a single `:is(...)` id scope in styles.css. App Settings' rail is a table of contents over ONE scrolling document (`switchSettingsTab` scrolls); Session Options and Add Case really switch (`switchOptionsTab` / `switchCaseModalTab`), and their larger per-modal size blocks are the design, not drift. ⚠️ **The load/save contract is `getElementById` by id**: renaming or dropping a control id silently stops it loading or saving. ⚠️ The Session Options "Session" entry still keys off `context` (label-only rename). ⚠️ Add Case keeps its legacy `.form-row` markup via an adapter; every `<details>` there needs `.set-adv-chev` plus both marker suppressions. ⚠️ Model cards and the effort segment are views over hidden `<select>`s, which stay the source of truth. ⚠️ `.modal-tabs*` classes are retired; `admin-ui.js` needs `.set-rail-items` + `.set-doc` to survive any restructure. Guard: `test/app-settings-structure.test.ts`. → [architecture-invariants#settings-surface-app-settings-session-options-add-case](../../docs/architecture-invariants.md#settings-surface-app-settings-session-options-add-case)
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
*
|
||||
* - FocusTrap (class) — Traps Tab/Shift+Tab keyboard focus within a modal element.
|
||||
* Saves and restores previously focused element on deactivate. Used by the modal dialogs.
|
||||
* A dialog opened by a mouse press goes back to the field the user was typing in when
|
||||
* the press began (FocusTrap.focusToReturn), not to the pressed control.
|
||||
*
|
||||
* @globals {object} KeyboardAccessoryBar
|
||||
* @globals {object} PathPicker
|
||||
@@ -1846,6 +1848,13 @@ const KeyboardAccessoryBar = {
|
||||
/**
|
||||
* FocusTrap - Traps keyboard focus within an element (typically a modal).
|
||||
* Saves the previously focused element and restores focus when deactivated.
|
||||
*
|
||||
* What it saves is {@link FocusTrap.focusToReturn}, not `document.activeElement`.
|
||||
* By the time a dialog opens, the mouse press that opened it has already moved
|
||||
* focus: in Chromium and Firefox the pressed control took it, and in WebKit on
|
||||
* macOS (Safari, the Mac app) a button takes no focus from the mouse, so the
|
||||
* press clears focus to `<body>`. Handing either one back left the keyboard out
|
||||
* of the terminal the user was typing in until they clicked it again.
|
||||
*/
|
||||
class FocusTrap {
|
||||
constructor(element) {
|
||||
@@ -1854,8 +1863,96 @@ class FocusTrap {
|
||||
this.boundHandleKeydown = this.handleKeydown.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* The element a dialog that is about to open should hand the keyboard back to
|
||||
* when it closes.
|
||||
*
|
||||
* That is `document.activeElement`, with one exception: a dialog opened by a
|
||||
* mouse press while the user was typing in a field (a terminal's textarea, the
|
||||
* CJK field, a search box). The field is the answer when that press is the
|
||||
* only reason focus is somewhere else now, which is the case when focus sits
|
||||
* on the pressed control or on nothing at all.
|
||||
*
|
||||
* ⚠️ One press, and only from a typing field. A press made from a button (a
|
||||
* dialog opened from inside another dialog) still returns to the pressed
|
||||
* control, and the field of an earlier press is not carried over: it can be
|
||||
* behind a dialog that has opened since. The one exception is a press on a
|
||||
* menu item, see {@link FocusTrap._notePress}. A control reached with the
|
||||
* keyboard keeps the usual pattern too, because any key forgets the press.
|
||||
*
|
||||
* ⚠️ Asking spends the press. Whatever is pressed inside the dialog that is
|
||||
* opening starts with nothing remembered, so nothing in it can hand the
|
||||
* keyboard to a field behind it.
|
||||
*/
|
||||
static focusToReturn() {
|
||||
const active = document.activeElement || null;
|
||||
const press = FocusTrap._press;
|
||||
FocusTrap._press = null;
|
||||
if (!press || !press.field || press.field === active || press.field.isConnected === false) return active;
|
||||
const moved =
|
||||
!active ||
|
||||
active === document.body ||
|
||||
active === press.target ||
|
||||
(typeof active.contains === 'function' && active.contains(press.target));
|
||||
return moved ? press.field : active;
|
||||
}
|
||||
|
||||
/** True for an element text is typed into: a textarea, a text-like input, an editable region. */
|
||||
static isTypingField(el) {
|
||||
if (!el || el === document.body) return false;
|
||||
const tag = String(el.tagName || '').toUpperCase();
|
||||
if (tag === 'TEXTAREA') return true;
|
||||
if (tag === 'INPUT') return !FocusTrap.NON_TEXT_INPUT_TYPES.includes(String(el.type || 'text').toLowerCase());
|
||||
return el.isContentEditable === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remember what a mouse press began on and which typing field had the keyboard.
|
||||
*
|
||||
* ⚠️ `pointerdown`, not `mousedown` or `click`: moving focus is the default
|
||||
* action of `mousedown`, which comes after it, so `activeElement` is still the
|
||||
* element that had the keyboard before the press.
|
||||
*
|
||||
* ⚠️ A finger or a pen forgets the press and records nothing: on a touch device
|
||||
* a focused field means the on-screen keyboard, and handing focus back to it
|
||||
* would raise the keyboard when a dialog closes.
|
||||
*
|
||||
* ⚠️ A press on an item of a menu (`[role="menu"]`) keeps the field of the
|
||||
* press before it, the one that opened the menu: a tile's menu button and then
|
||||
* "Session options" are two presses and one way into a dialog. No other press
|
||||
* inherits a field.
|
||||
*/
|
||||
static _notePress(e) {
|
||||
if (!e || e.pointerType !== 'mouse') {
|
||||
FocusTrap._press = null;
|
||||
return;
|
||||
}
|
||||
const active = document.activeElement;
|
||||
const target = e.target || null;
|
||||
let field = FocusTrap.isTypingField(active) ? active : null;
|
||||
if (!field && FocusTrap._press && typeof target?.closest === 'function' && target.closest('[role="menu"]')) {
|
||||
field = FocusTrap._press.field;
|
||||
}
|
||||
FocusTrap._press = { target, field };
|
||||
}
|
||||
|
||||
/** Any key but a modifier held for the click (Shift+click) ends the press's say. */
|
||||
static _forgetPress(e) {
|
||||
if (e && FocusTrap.MODIFIER_KEYS.includes(e.key)) return;
|
||||
FocusTrap._press = null;
|
||||
}
|
||||
|
||||
/** Install the two document listeners behind {@link FocusTrap.focusToReturn}, once per page. */
|
||||
static watchPresses() {
|
||||
if (FocusTrap._watching) return;
|
||||
if (typeof document === 'undefined' || typeof document.addEventListener !== 'function') return;
|
||||
FocusTrap._watching = true;
|
||||
document.addEventListener('pointerdown', FocusTrap._notePress, true);
|
||||
document.addEventListener('keydown', FocusTrap._forgetPress, true);
|
||||
}
|
||||
|
||||
activate() {
|
||||
this.previouslyFocused = document.activeElement;
|
||||
this.previouslyFocused = FocusTrap.focusToReturn();
|
||||
this.element.addEventListener('keydown', this.boundHandleKeydown);
|
||||
|
||||
// Focus first focusable element after a brief delay (for CSS transitions)
|
||||
@@ -1907,3 +2004,11 @@ class FocusTrap {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The last mouse press, `{ target, field }`, or null. See {@link FocusTrap._notePress}. */
|
||||
FocusTrap._press = null;
|
||||
FocusTrap._watching = false;
|
||||
/** `<input>` types nobody types text into; every other type counts as a typing field. */
|
||||
FocusTrap.NON_TEXT_INPUT_TYPES = ['button', 'checkbox', 'color', 'file', 'hidden', 'image', 'radio', 'range', 'reset', 'submit'];
|
||||
FocusTrap.MODIFIER_KEYS = ['Shift', 'Control', 'Alt', 'Meta'];
|
||||
FocusTrap.watchPresses();
|
||||
@@ -376,7 +376,14 @@ Object.assign(CodemanApp.prototype, {
|
||||
* for, typing a filter the moment it opens.
|
||||
*/
|
||||
_rememberOverlayFocus(key) {
|
||||
this[key] = (typeof document !== 'undefined' && document.activeElement) || null;
|
||||
if (typeof document === 'undefined') {
|
||||
this[key] = null;
|
||||
return;
|
||||
}
|
||||
// The same answer a FocusTrap dialog saves (keyboard-accessory.js): an overlay
|
||||
// opened by a mouse press goes back to the field the user was typing in, not
|
||||
// to the pressed button.
|
||||
this[key] = (typeof FocusTrap === 'function' ? FocusTrap.focusToReturn() : document.activeElement) || null;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user