From 2750a196017aeb499a009cd8898de90d43fbff46 Mon Sep 17 00:00:00 2001 From: moran Date: Wed, 2 Sep 2026 17:34:02 +0800 Subject: [PATCH] feat(webui): newline instead of send on touch keyboards A plain Enter in the composer now inserts a newline on coarse-pointer devices; sending stays on the send button. The slash/mention pickers still select with Enter, and precision-pointer (desktop) behavior is unchanged. Adds enterKeyHint to label the soft keyboard action and enables interactive-widget=resizes-content in the viewport meta so Android keyboards resize the layout viewport. --- .../src/components/thread/ThreadComposer.tsx | 17 ++++ webui/src/tests/thread-composer.test.tsx | 94 ++++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index a09b77b9e..b5c484c6d 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -971,6 +971,9 @@ export function ThreadComposer({ const [recentSlashCommands, setRecentSlashCommands] = useState(() => readSlashRecents()); const [queuedPrompts, setQueuedPrompts] = useState([]); const hasTouchPrimaryPointer = useMediaQuery("(hover: none) and (pointer: coarse)"); + // Wider than hasTouchPrimaryPointer on purpose: tablets with a physical + // keyboard still count as touch for the Enter key behavior below. + const hasCoarsePointer = useMediaQuery("(pointer: coarse)"); const textareaRef = useRef(null); const mentionOverlayRef = useRef(null); const formRef = useRef(null); @@ -2181,6 +2184,19 @@ export function ThreadComposer({ return; } } + // Touch keyboards: a plain Enter inserts a newline; sending stays on the + // send button. The select-on-Enter menu branches above take precedence. + if ( + e.key === "Enter" + && !e.shiftKey + && !e.altKey + && !e.ctrlKey + && !e.metaKey + && !e.nativeEvent.isComposing + && hasCoarsePointer + ) { + return; + } if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); if (canQueueGuidance) { @@ -2533,6 +2549,7 @@ export function ThreadComposer({ }} onPaste={onPaste} rows={1} + enterKeyHint={hasCoarsePointer ? "enter" : undefined} placeholder={sessionDragPreview ? "" : resolvedPlaceholder} disabled={interactionDisabled} aria-label={inputAriaLabel ?? t("thread.composer.inputAria")} diff --git a/webui/src/tests/thread-composer.test.tsx b/webui/src/tests/thread-composer.test.tsx index d362b58b4..90f8dcdb7 100644 --- a/webui/src/tests/thread-composer.test.tsx +++ b/webui/src/tests/thread-composer.test.tsx @@ -1,4 +1,4 @@ -import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import { act, createEvent, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; @@ -347,6 +347,17 @@ function renderPresetComposer( }; } +function stubCoarsePointer(): void { + vi.stubGlobal("matchMedia", vi.fn((query: string) => ({ + matches: query.includes("pointer: coarse"), + media: query, + onchange: null, + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + }))); +} + describe("ThreadComposer", () => { it("locks an async send and keeps the draft when it is rejected", async () => { let resolveSend!: (accepted: boolean) => void; @@ -407,6 +418,87 @@ describe("ThreadComposer", () => { expect(input).not.toHaveFocus(); }); + it("inserts a newline instead of sending on Enter on coarse-pointer devices", () => { + stubCoarsePointer(); + const onSend = vi.fn(); + render( + , + ); + + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "line one" } }); + const keyEvent = createEvent.keyDown(input, { key: "Enter" }); + fireEvent(input, keyEvent); + + expect(keyEvent.defaultPrevented).toBe(false); + expect(onSend).not.toHaveBeenCalled(); + }); + + it("keeps sending on Enter on precision-pointer devices", () => { + const onSend = vi.fn(); + render( + , + ); + + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "desktop message" } }); + fireEvent.keyDown(input, { key: "Enter" }); + + expect(onSend).toHaveBeenCalledWith("desktop message", undefined, undefined); + }); + + it("still selects a slash command with Enter on coarse-pointer devices", () => { + stubCoarsePointer(); + const onSend = vi.fn(); + render( + , + ); + + const input = screen.getByLabelText("Message input"); + fireEvent.change(input, { target: { value: "/" } }); + expect(screen.getByRole("option", { name: /\/history/i })).toHaveAttribute( + "aria-selected", + "true", + ); + fireEvent.keyDown(input, { key: "Enter" }); + + expect(input).toHaveValue("/history "); + expect(onSend).not.toHaveBeenCalled(); + }); + + it("labels the keyboard action as enter on coarse-pointer devices", () => { + stubCoarsePointer(); + render( + , + ); + + expect(screen.getByLabelText("Message input")).toHaveAttribute("enterkeyhint", "enter"); + }); + + it("does not set enterkeyhint on precision-pointer devices", () => { + render( + , + ); + + expect(screen.getByLabelText("Message input")).not.toHaveAttribute("enterkeyhint"); + }); + it("focuses and sends a removable quoted answer excerpt", async () => { const onSend = vi.fn(); const onQuotedContextChange = vi.fn();