-
Notifications
You must be signed in to change notification settings - Fork 739
feat(chat): focus the composer on keyboard input #2325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
58 changes: 58 additions & 0 deletions
58
src/renderer/src/features/chat-page/composables/useComposerTypeToFocus.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { useEventListener } from '@vueuse/core' | ||
| import type { Ref } from 'vue' | ||
| import { hasInteractiveKeyboardFocus, isEditableKeyboardTarget } from '@/lib/keyboardFocus' | ||
| import { resolveComposerTypeToFocusIntent } from '../model/typeToFocus' | ||
|
|
||
| export type ComposerTypeToFocusHandle = { | ||
| focusInput?: () => void | ||
| focusAndInsertText?: (text: string) => void | ||
| } | ||
|
|
||
| type UseComposerTypeToFocusOptions = { | ||
| /** False for read-only sessions, inert composers, and blocking interactions. */ | ||
| isEnabled: () => boolean | ||
| chatInputRef: Ref<ComposerTypeToFocusHandle | null> | ||
| } | ||
|
|
||
| /** | ||
| * Routes the first keystroke of a window-level typing session into the composer. | ||
| * | ||
| * Owns its own window listener instead of joining `useChatPageEventBridge` | ||
| * because the new-thread page has no event bridge but needs the same behavior. | ||
| * Both pages share this composable; the listener detaches with the component | ||
| * scope. | ||
| */ | ||
| export function useComposerTypeToFocus(options: UseComposerTypeToFocusOptions): void { | ||
| useEventListener(window, 'keydown', (event: KeyboardEvent) => { | ||
| const intent = resolveComposerTypeToFocusIntent(event, { | ||
| isEnabled: options.isEnabled(), | ||
| isEditableTarget: isEditableKeyboardTarget(event.target), | ||
| hasInteractiveFocus: hasInteractiveKeyboardFocus(event.target) | ||
| }) | ||
|
|
||
| if (intent.kind === 'ignore') { | ||
| return | ||
| } | ||
|
|
||
| const chatInput = options.chatInputRef.value | ||
| if (!chatInput?.focusInput) { | ||
| return | ||
| } | ||
|
|
||
| if (intent.kind === 'focus-only') { | ||
| // Input method composition: focusing is enough, the IME owns the text. | ||
| chatInput.focusInput() | ||
| return | ||
| } | ||
|
|
||
| // Suppress the native default (notably Space scrolling the transcript) | ||
| // before the character is inserted programmatically. | ||
| event.preventDefault() | ||
| if (chatInput.focusAndInsertText) { | ||
| chatInput.focusAndInsertText(intent.text) | ||
| return | ||
| } | ||
|
|
||
| chatInput.focusInput() | ||
| }) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Decides what a window-level keydown should do for the chat composer. | ||
| * | ||
| * Kept as a pure function so the routing rules — which are the whole contract of | ||
| * type-to-focus — can be tested without mounting a page or an editor. | ||
| */ | ||
|
|
||
| export type ComposerTypeToFocusIntent = | ||
| /** Leave the event alone: another handler or control owns it. */ | ||
| | { kind: 'ignore' } | ||
| /** Start the input method in the composer without inserting anything. */ | ||
| | { kind: 'focus-only' } | ||
| /** Focus the composer and insert the character that triggered the focus. */ | ||
| | { kind: 'focus-and-insert'; text: string } | ||
|
|
||
| export type ComposerTypeToFocusContext = { | ||
| /** False for read-only sessions, inert composers, and blocking interactions. */ | ||
| isEnabled: boolean | ||
| isEditableTarget: boolean | ||
| hasInteractiveFocus: boolean | ||
| } | ||
|
|
||
| /** C0 controls plus DEL: the non-printable single-code-unit keys. */ | ||
| function isControlCharacter(key: string): boolean { | ||
| const code = key.charCodeAt(0) | ||
| return code <= 0x1f || code === 0x7f | ||
| } | ||
|
|
||
| export function resolveComposerTypeToFocusIntent( | ||
| event: KeyboardEvent, | ||
| context: ComposerTypeToFocusContext | ||
| ): ComposerTypeToFocusIntent { | ||
| if (event.defaultPrevented) { | ||
| return { kind: 'ignore' } | ||
| } | ||
|
|
||
| // Shortcuts and AltGr-style chords keep their native behavior. A bare Shift | ||
| // still reports a printable `key` (e.g. 'A'), so it is intentionally allowed. | ||
| if (event.metaKey || event.ctrlKey || event.altKey) { | ||
| return { kind: 'ignore' } | ||
| } | ||
|
|
||
| // Ownership comes before intent: a composition already running in another | ||
| // field (the chat search box, a dialog input) must keep its keystrokes. | ||
| if (context.isEditableTarget || context.hasInteractiveFocus) { | ||
| return { kind: 'ignore' } | ||
| } | ||
|
|
||
| if (!context.isEnabled) { | ||
| return { kind: 'ignore' } | ||
| } | ||
|
|
||
| // Keystrokes whose text is produced later rather than by this key: input | ||
| // method compositions (isComposing / keyCode 229 / 'Process') and dead keys | ||
| // waiting for a following letter. Focus without inserting and without | ||
| // preventDefault, so the platform keeps its pending state and the *next* | ||
| // keystroke reaches an already focused editor and composes natively — which is | ||
| // what turns a dead key + 'e' into 'é' instead of a bare 'e'. | ||
| if ( | ||
| event.isComposing || | ||
| event.keyCode === 229 || | ||
| event.key === 'Process' || | ||
| event.key === 'Dead' | ||
| ) { | ||
| return { kind: 'focus-only' } | ||
| } | ||
|
|
||
| // Navigation and function keys, plus 'Unidentified'. Space is a single | ||
| // printable character and is deliberately included here. | ||
| if (event.key.length !== 1 || isControlCharacter(event.key)) { | ||
| return { kind: 'ignore' } | ||
| } | ||
|
|
||
| return { kind: 'focus-and-insert', text: event.key } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * Focus predicates for renderer-level keyboard routing. | ||
| * | ||
| * They exist so that features which intercept global keydown (type-to-focus, | ||
| * list scroll intent, session shortcuts) agree on what "the user is already | ||
| * typing somewhere" and "some control owns the keyboard" mean. | ||
| */ | ||
|
|
||
| /** Anything that natively consumes typed characters. */ | ||
| const EDITABLE_SELECTOR = [ | ||
| 'input', | ||
| 'textarea', | ||
| 'select', | ||
| // `contenteditable=""` and `plaintext-only` are editable too, so match every | ||
| // value except the explicit opt-out. ProseMirror's attribute literal is not | ||
| // part of our contract. | ||
| '[contenteditable]:not([contenteditable="false"])', | ||
| '[role="textbox"]' | ||
| ].join(', ') | ||
|
|
||
| /** | ||
| * Controls that own keyboard interaction while focused: activation keys, | ||
| * roving tab stops, and modal focus traps. A bare `[tabindex]` is deliberately | ||
| * absent — the message scroll container is `tabindex="0"` and must stay a | ||
| * valid type-to-focus surface. | ||
| */ | ||
| const INTERACTIVE_SELECTOR = [ | ||
| 'button', | ||
| 'a[href]', | ||
| 'summary', | ||
| '[role="button"]', | ||
| '[role="link"]', | ||
| '[role="menuitem"]', | ||
| '[role="menuitemcheckbox"]', | ||
| '[role="menuitemradio"]', | ||
| '[role="option"]', | ||
| '[role="tab"]', | ||
| '[role="checkbox"]', | ||
| '[role="radio"]', | ||
| '[role="switch"]', | ||
| '[role="slider"]', | ||
| '[role="combobox"]', | ||
| '[role="listbox"]', | ||
| '[role="menu"]', | ||
| '[role="menubar"]', | ||
| '[role="toolbar"]', | ||
| // reka-ui writes role but no aria-modal, so the modal test cannot require it. | ||
| '[role="dialog"]', | ||
| '[role="alertdialog"]', | ||
| '[data-slot="dialog-content"]', | ||
| '[data-slot="alert-dialog-content"]', | ||
| '[data-reka-popper-content-wrapper]', | ||
| '[data-radix-popper-content-wrapper]' | ||
| ].join(', ') | ||
|
|
||
| function resolveElement(target: EventTarget | null): Element | null { | ||
| if (target instanceof Element) { | ||
| return target | ||
| } | ||
|
|
||
| return document.activeElement | ||
| } | ||
|
|
||
| /** True when the target (or its ancestors) natively consumes typed characters. */ | ||
| export function isEditableKeyboardTarget(target: EventTarget | null): boolean { | ||
| return Boolean(resolveElement(target)?.closest(EDITABLE_SELECTOR)) | ||
| } | ||
|
|
||
| /** True when the target (or its ancestors) is a control that owns the keyboard. */ | ||
| export function hasInteractiveKeyboardFocus(target: EventTarget | null): boolean { | ||
| return Boolean(resolveElement(target)?.closest(INTERACTIVE_SELECTOR)) | ||
| } |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Classify printable characters by Unicode code point.
event.key.lengthmeasures UTF-16 code units. A printable supplementary character, such as an emoji, has length 2 and returnsignore.Count Unicode code points instead.
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents