diff --git a/packages/app/e2e/browser/composer-focus-hint.spec.ts b/packages/app/e2e/browser/composer-focus-hint.spec.ts new file mode 100644 index 000000000..668e85558 --- /dev/null +++ b/packages/app/e2e/browser/composer-focus-hint.spec.ts @@ -0,0 +1,143 @@ +import type { Locator } from "@playwright/test"; +import { expect, test, type Page } from "../support/fixtures"; +import { openAgentRoute, seedMockAgentWorkspace } from "../support/helpers/mock-agent"; + +const APP_SETTINGS_KEY = "@paseo:app-settings"; + +async function useLanguage(page: Page, language: string): Promise { + await page.addInitScript( + ({ key, language: value }) => { + localStorage.setItem(key, JSON.stringify({ language: value })); + }, + { key: APP_SETTINGS_KEY, language }, + ); +} + +/** + * Lays the placeholder out the way the textarea does, in a mirror element with + * the same box and font, and reports whether any of its lines reach the hint. + */ +async function placeholderOverlaps(input: Locator, hint: Locator): Promise { + const hintBox = await hint.boundingBox(); + if (!hintBox) throw new Error("Focus hint has no layout box"); + return input.evaluate((element, box) => { + const textarea = element as HTMLTextAreaElement; + const style = getComputedStyle(textarea); + const rect = textarea.getBoundingClientRect(); + const mirror = document.createElement("div"); + for (const property of [ + "boxSizing", + "paddingTop", + "paddingRight", + "paddingBottom", + "paddingLeft", + "borderTopWidth", + "borderRightWidth", + "borderBottomWidth", + "borderLeftWidth", + "fontFamily", + "fontSize", + "fontWeight", + "letterSpacing", + "lineHeight", + "textIndent", + "wordSpacing", + ] as const) { + mirror.style[property] = style[property]; + } + Object.assign(mirror.style, { + position: "fixed", + left: `${rect.left}px`, + top: `${rect.top}px`, + width: `${rect.width}px`, + borderStyle: "solid", + borderColor: "transparent", + whiteSpace: "pre-wrap", + overflowWrap: "break-word", + visibility: "hidden", + }); + const text = document.createTextNode(textarea.placeholder); + mirror.appendChild(text); + document.body.appendChild(mirror); + const range = document.createRange(); + range.selectNodeContents(text); + const lines = Array.from(range.getClientRects()); + mirror.remove(); + return lines.some( + (line) => + line.right > box.x && + line.left < box.x + box.width && + line.bottom > box.y && + line.top < box.y + box.height, + ); + }, hintBox); +} + +async function recordFirstVisibleHint(page: Page): Promise { + await page.addInitScript(() => { + function sample() { + const root = document.querySelector('[data-testid="message-input-root"]'); + const input = root?.querySelector("textarea"); + const hint = Array.from(root?.querySelectorAll("div") ?? []).find( + (element) => !element.childElementCount && element.textContent?.endsWith("pour saisir"), + ); + if (!input || !hint || Number(getComputedStyle(hint).opacity) === 0) { + requestAnimationFrame(sample); + return; + } + + const inputRight = input.getBoundingClientRect().right; + const textRight = inputRight - parseFloat(getComputedStyle(input).paddingRight); + const hintLeft = hint.getBoundingClientRect().left; + Reflect.set(window, "__firstVisibleHintHasSpace", textRight <= hintLeft + 1); + } + requestAnimationFrame(sample); + }); +} + +async function expectFirstVisibleHintHasSpace(page: Page): Promise { + await expect + .poll(() => page.evaluate(() => Reflect.get(window, "__firstVisibleHintHasSpace"))) + .toBe(true); +} + +const FRENCH_LAYOUTS = [ + { name: "phone width", width: 390, placeholder: "Message, @fichiers, /commandes" }, + { + name: "desktop width", + width: 1024, + placeholder: + "Écrivez à l’agent, mentionnez des @fichiers ou utilisez les /commandes et /skills", + }, +]; + +for (const layout of FRENCH_LAYOUTS) { + test.describe(`French composer at ${layout.name}`, () => { + test.use({ viewport: { width: layout.width, height: 844 } }); + + test("the focus hint does not cover the placeholder", async ({ page }) => { + const agent = await seedMockAgentWorkspace({ + repoPrefix: "composer-focus-hint-", + title: "Composer focus hint", + }); + + try { + await useLanguage(page, "fr"); + await recordFirstVisibleHint(page); + await openAgentRoute(page, agent); + const input = page.getByRole("textbox", { name: "Écrire à l’agent…" }).first(); + await expect(input).toBeVisible(); + await expect(input).toHaveAttribute("placeholder", layout.placeholder); + await input.blur(); + + const hint = page.getByText(/pour saisir$/).first(); + await expect(hint).toBeVisible(); + // The input takes the hint's width on the render after the hint lays out. + await expect.poll(() => placeholderOverlaps(input, hint)).toBe(false); + await expectFirstVisibleHintHasSpace(page); + } finally { + await agent.cleanup(); + } + }); + }); +} diff --git a/packages/app/e2e/browser/composer-whitespace.spec.ts b/packages/app/e2e/browser/composer-whitespace.spec.ts index 9ad03112d..0be3218ab 100644 --- a/packages/app/e2e/browser/composer-whitespace.spec.ts +++ b/packages/app/e2e/browser/composer-whitespace.spec.ts @@ -18,6 +18,7 @@ import { expectComposerFullscreenControls, sendComposerWithButton, queueComposerWithButton, + fillComposerDraft, configureComposerQueueSend, activateRetainedComposerTab, switchComposerTabWithKeyboard, @@ -450,6 +451,32 @@ test("fullscreen Send keeps the configured queue behavior while an agent runs", } }); +test("editing a queued draft restores its original height with the focus hint visible", async ({ + page, +}) => { + await configureComposerQueueSend(page); + await resizeComposerViewport(page, "portrait"); + const agent = await startRunningMockAgent(page, { + prefix: "composer-queued-draft-height-", + model: "one-minute-stream", + prompt: "Stay running while a draft is queued.", + }); + try { + const draft = "Please review the latest changes.\nPlease review the latest changes."; + await fillComposerDraft(page, draft); + const originalHeight = await composerHeight(page); + await queueComposerWithButton(page); + await expect(composerLocator(page)).toHaveValue(""); + await composerLocator(page).blur(); + await expect(page.getByText(/ to focus$/)).toBeVisible(); + await page.getByRole("button", { name: "Edit queued message", exact: true }).click(); + await expect(composerLocator(page)).toHaveValue(draft); + await expectComposerHeight(page, originalHeight); + } finally { + await agent.cleanup(); + } +}); + test("image paste stays attached to the active editor across fullscreen entry and exit", async ({ page, }) => { diff --git a/packages/app/src/composer/input/height.web.ts b/packages/app/src/composer/input/height.web.ts index 6b8c1ec56..891bb41ca 100644 --- a/packages/app/src/composer/input/height.web.ts +++ b/packages/app/src/composer/input/height.web.ts @@ -117,10 +117,11 @@ export function useComposerHeight({ useEffect(() => { const source = textareaRef.current; if (!source || typeof ResizeObserver === "undefined") return; - let previousWidth = source.clientWidth; - const observer = new ResizeObserver(() => { - const nextWidth = source.clientWidth; - if (Math.abs(nextWidth - previousWidth) < 1) return; + let previousWidth: number | undefined; + const observer = new ResizeObserver(([entry]) => { + // Hint padding changes the width text wraps at even when clientWidth stays fixed. + const nextWidth = entry.contentRect.width; + if (nextWidth === previousWidth) return; previousWidth = nextWidth; measure(paramsRef.current.getText()); }); diff --git a/packages/app/src/composer/input/input.tsx b/packages/app/src/composer/input/input.tsx index d154f753e..fc0dbe872 100644 --- a/packages/app/src/composer/input/input.tsx +++ b/packages/app/src/composer/input/input.tsx @@ -677,24 +677,6 @@ function MessageInputOverlay({ return null; } -function FocusHint({ - visible, - focusInputKeys, - label, -}: { - visible: boolean; - focusInputKeys: ShortcutChord | null | undefined; - label: string; -}) { - const { isActiveComposer } = useComposerKeyboardScope(); - if (!isActiveComposer || !visible || !focusInputKeys || !label.trim()) return null; - return ( - - {label} - - ); -} - interface ComposerTextSurfaceProps { readOnly: boolean; value: string; @@ -745,6 +727,32 @@ function ComposerTextSurface(props: ComposerTextSurfaceProps): React.ReactElemen onRenderedLinesChange(event.nativeEvent.lines.length), [onRenderedLinesChange], ); + const { isActiveComposer } = useComposerKeyboardScope(); + const { focusHintLabel, focusHintVisible, focusInputKeys } = props; + const [focusHintLayout, setFocusHintLayout] = useState<{ label: string; width: number }>(); + const handleFocusHintLayout = useCallback( + (event: LayoutChangeEvent) => { + setFocusHintLayout({ label: focusHintLabel, width: event.nativeEvent.layout.width }); + }, + [focusHintLabel], + ); + const shouldShowFocusHint = + isActiveComposer && + focusHintVisible && + Boolean(focusInputKeys) && + focusHintLabel.trim().length > 0; + const focusHintWidth = + shouldShowFocusHint && focusHintLayout?.label === focusHintLabel + ? focusHintLayout.width + : undefined; + // Make room for the hint in the same render that reveals it. + const textInputStyle = useMemo( + () => + focusHintWidth === undefined + ? props.textInputStyle + : [props.textInputStyle, { paddingRight: focusHintWidth }], + [focusHintWidth, props.textInputStyle], + ); if (props.readOnly) { return ( @@ -775,7 +783,7 @@ function ComposerTextSurface(props: ComposerTextSurfaceProps): React.ReactElemen accessibilityLabel={props.accessibilityLabel} onFocus={props.onFocus} onBlur={props.onBlur} - style={props.textInputStyle} + style={textInputStyle} multiline scrollEnabled={props.scrollEnabled} editable={props.editable} @@ -785,11 +793,16 @@ function ComposerTextSurface(props: ComposerTextSurfaceProps): React.ReactElemen onPasteError={props.onPasteError} autoFocus={props.autoFocus} /> - + {shouldShowFocusHint ? ( + + {focusHintLabel} + + ) : null} ); } @@ -2460,10 +2473,12 @@ const styles = StyleSheet.create((theme: Theme) => ({ position: "absolute", top: 0, right: 0, + paddingLeft: theme.spacing[2], fontSize: theme.fontSize.sm, color: theme.colors.foregroundMuted, opacity: 0.5, }, + unmeasuredFocusHint: { opacity: 0 }, textInput: { // Preserve the controls when an ancestor constrains an overlong draft. flexShrink: 1,