Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(chat): preserve selection actions through touch gestures (#2249)
Call White49f4862ae32a
diff --git a/docs/plans/mobile-selection-toolbar.md b/docs/plans/mobile-selection-toolbar.mdnew file mode 100644index 000000000..4f4ba514d--- /dev/null+++ b/docs/plans/mobile-selection-toolbar.md@@ -0,0 +1,49 @@+# Mobile selection toolbar gesture lifetime++## Contract++A primary pointer press on a chat selection action owns the selected text until+that gesture finishes. Selection collapse or transcript scrolling during the+press must not remove the action before release. Quote, Ask in a new session,+and Copy execute exactly once when the same pointer releases inside its button.+Cancellation, lost capture, and release outside the button execute nothing and+restore normal selection tracking. Keyboard activation remains available.++This change belongs to `SelectionQuoteToolbar`; it does not change composer,+fork, clipboard permission, or PWA installation contracts.++Dependencies: none. No existing scenario-catalog ID covers this interaction;+the toolbar component regression is its primary automated contract.++## Validation++- Component regression: an actual DOM selection collapses between pointer down+ and pointer up; the debounce elapses; each action still receives the original+ non-ASCII text exactly once. Existing tests do not exercise this toolbar's+ pointer/selection interaction.+- Cancellation, capture loss, outside release, non-primary input, and ordinary+ selection dismissal cover gesture ownership boundaries.+- Hermetic browser checks exercise native pointer capture and touch activation+ in Chromium and WebKit. WebKit touch emulation does not establish the exact+ event ordering of an iPhone's native selection menu.+- Focused tests, changed-file lint, production UI build, exact-head Codex review,+ and required CI.++## Local results++- The pre-fix component failed the three selection-collapse action cases.+- All 75 tests across the toolbar, archived-chat behavior, and composer shortcuts+ pass after the fix.+- Chromium and WebKit each passed native touch taps for all three actions,+ native mouse pointer capture across selection collapse, and captured release+ outside the button. The probe served the actual component through Vite on+ loopback in disposable browser contexts; clipboard writes were intercepted to+ avoid changing the host clipboard. It reported no page errors.+- No iPhone hardware or native iOS selection menu was exercised.++## Known by design++- The operating system's selection menu remains available.+- Toolbar placement and transcript scroll behavior outside an active button+ gesture remain unchanged.+- No production update, service restart, or merge is part of this change.diff --git a/ui/src/components/workbench/SelectionQuoteToolbar.test.tsx b/ui/src/components/workbench/SelectionQuoteToolbar.test.tsxnew file mode 100644index 000000000..1dad80df5--- /dev/null+++ b/ui/src/components/workbench/SelectionQuoteToolbar.test.tsx@@ -0,0 +1,171 @@+// @vitest-environment jsdom+import { createRef } from 'react';+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { SelectionQuoteToolbar } from './SelectionQuoteToolbar';++vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));++const TEXT = 'Selected 中文消息 🌱';+const quote = vi.fn();+const ask = vi.fn();+const writeText = vi.fn().mockResolvedValue(undefined);+const rect = { top: 200, bottom: 236, left: 40, right: 340, width: 300, height: 36 };++// jsdom has selections but no layout, pointer capture, or PointerEvent. Only+// those browser boundaries are supplied; selection tracking and action dispatch+// come from the real component. Before the fix, clearing the selection during a+// press unmounts the action and none of its consumers receive the selected text.+class TestPointerEvent extends MouseEvent {+ pointerId: number;+ isPrimary: boolean;+ pointerType: string;++ constructor(type: string, init: PointerEventInit = {}) {+ super(type, init);+ this.pointerId = init.pointerId ?? 1;+ this.isPrimary = init.isPrimary ?? true;+ this.pointerType = init.pointerType ?? 'touch';+ }+}++beforeEach(() => {+ vi.useFakeTimers();+ vi.clearAllMocks();+ vi.stubGlobal('PointerEvent', TestPointerEvent);+ vi.stubGlobal('matchMedia', () => ({ matches: true }));+ Object.defineProperty(Range.prototype, 'getBoundingClientRect', {+ configurable: true,+ value: () => rect,+ });+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(rect as DOMRect);+ Object.defineProperty(HTMLElement.prototype, 'setPointerCapture', {+ configurable: true,+ value: vi.fn(),+ });+ Object.defineProperty(navigator, 'clipboard', {+ configurable: true,+ value: { writeText },+ });+});++afterEach(() => {+ cleanup();+ window.getSelection()?.removeAllRanges();+ vi.clearAllTimers();+ vi.useRealTimers();+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+ Reflect.deleteProperty(Range.prototype, 'getBoundingClientRect');+ Reflect.deleteProperty(HTMLElement.prototype, 'setPointerCapture');+ Reflect.deleteProperty(navigator, 'clipboard');+});++function mountToolbar() {+ const containerRef = createRef<HTMLDivElement>();+ render(+ <>+ <div ref={containerRef}>{TEXT}</div>+ <SelectionQuoteToolbar containerRef={containerRef} onQuote={quote} onAskInNew={ask} />+ </>,+ );+ const range = document.createRange();+ range.selectNodeContents(containerRef.current!);+ window.getSelection()!.addRange(range);+ fireEvent(document, new Event('selectionchange'));+ act(() => vi.advanceTimersByTime(150));+ return containerRef.current!;+}++function collapseSelection() {+ window.getSelection()!.removeAllRanges();+ fireEvent(document, new Event('selectionchange'));+ act(() => vi.advanceTimersByTime(200));+}++const press = { pointerId: 1, isPrimary: true, pointerType: 'touch', button: 0, clientX: 100, clientY: 220 };++describe('chat selection action gesture lifetime', () => {+ it.each([+ ['quote', quote],+ ['askInNew', ask],+ ['copy', writeText],+ ] as const)('keeps %s alive until release after selection collapse and scrolling', async (action, consumer) => {+ const container = mountToolbar();+ const button = screen.getByRole('button', { name: `chat.selection.${action}` });+ fireEvent.pointerDown(button, press);+ expect(consumer).not.toHaveBeenCalled();++ collapseSelection();+ fireEvent.scroll(container);+ act(() => vi.advanceTimersByTime(200));+ expect(screen.getByRole('toolbar').contains(button)).toBe(true);++ await act(async () => fireEvent.pointerUp(button, press));+ // Browsers may follow pointerup with click. It must not run the action twice.+ fireEvent.click(button, { detail: 1 });+ expect(consumer).toHaveBeenCalledExactlyOnceWith(TEXT);+ });++ it.each(['pointerCancel', 'lostPointerCapture'] as const)(+ '%s ends ownership without activating',+ (event) => {+ mountToolbar();+ const button = screen.getByRole('button', { name: 'chat.selection.quote' });+ fireEvent.pointerDown(button, press);+ collapseSelection();+ expect(screen.getByRole('toolbar').contains(button)).toBe(true);+ fireEvent[event](button, press);+ expect(screen.queryByRole('toolbar')).toBeNull();+ expect(quote).not.toHaveBeenCalled();+ },+ );++ it('does not activate when a captured pointer releases outside the button', () => {+ mountToolbar();+ const button = screen.getByRole('button', { name: 'chat.selection.quote' });+ fireEvent.pointerDown(button, press);+ collapseSelection();+ expect(screen.getByRole('toolbar').contains(button)).toBe(true);+ fireEvent.pointerUp(button, { ...press, clientX: 500 });+ expect(quote).not.toHaveBeenCalled();+ expect(screen.queryByRole('toolbar')).toBeNull();+ });++ it('ignores another pointer while the primary press owns the selection', () => {+ mountToolbar();+ const button = screen.getByRole('button', { name: 'chat.selection.quote' });+ fireEvent.pointerDown(button, press);+ collapseSelection();+ fireEvent.pointerDown(button, { ...press, pointerId: 2, isPrimary: false });+ fireEvent.pointerUp(button, { ...press, pointerId: 2, isPrimary: false });+ expect(quote).not.toHaveBeenCalled();+ fireEvent.pointerUp(button, press);+ expect(quote).toHaveBeenCalledExactlyOnceWith(TEXT);+ });++ it('does not treat a release without a primary press as activation', () => {+ mountToolbar();+ const button = screen.getByRole('button', { name: 'chat.selection.quote' });+ fireEvent.pointerDown(button, { ...press, button: 2 });+ fireEvent.pointerUp(button, { ...press, button: 2 });+ expect(quote).not.toHaveBeenCalled();+ collapseSelection();+ expect(screen.queryByRole('toolbar')).toBeNull();+ });++ it.each(['Enter', ' '])('preserves %s keyboard activation', (key) => {+ mountToolbar();+ fireEvent.keyDown(screen.getByRole('button', { name: 'chat.selection.quote' }), { key });+ expect(quote).toHaveBeenCalledExactlyOnceWith(TEXT);+ });++ it('dismisses a cleared selection when no action is being pressed', () => {+ mountToolbar();+ collapseSelection();+ expect(screen.queryByRole('toolbar')).toBeNull();+ });+});diff --git a/ui/src/components/workbench/SelectionQuoteToolbar.tsx b/ui/src/components/workbench/SelectionQuoteToolbar.tsxindex 1f1d783b0..683685bbc 100644--- a/ui/src/components/workbench/SelectionQuoteToolbar.tsx+++ b/ui/src/components/workbench/SelectionQuoteToolbar.tsx@@ -1,4 +1,4 @@-import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';+import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';import { createPortal } from 'react-dom';import { useTranslation } from 'react-i18next';import { Check, Copy, GitFork, TextQuote } from 'lucide-react';@@ -34,6 +34,7 @@ export const SelectionQuoteToolbar: React.FC<{const [sel, setSel] = useState<SelectionState | null>(null);const [copied, setCopied] = useState(false);const toolbarRef = useRef<HTMLDivElement>(null);+ const activePointerId = useRef<number | null>(null);const [width, setWidth] = useState(0);// Touch (coarse pointer — phones AND tablets/iPads) is where the OS selection// menu coexists; it drives the stagger-positioning + the touch-only Copy.@@ -41,29 +42,35 @@ export const SelectionQuoteToolbar: React.FC<{() => typeof window !== 'undefined' && !!window.matchMedia?.('(pointer: coarse)').matches,);+ const recompute = useCallback(() => {+ // The press owns this snapshot until release/cancellation. Touch selection+ // changes can otherwise remove the button before its pointerup is delivered.+ if (activePointerId.current !== null) return;+ const container = containerRef.current;+ if (!container) return;+ const selection = window.getSelection();+ if (!selection || selection.isCollapsed || selection.rangeCount === 0) {+ setSel(null);+ return;+ }+ const text = selection.toString().trim();+ const range = selection.getRangeAt(0);+ if (!text || !container.contains(range.commonAncestorContainer)) {+ setSel(null);+ return;+ }+ const rect = range.getBoundingClientRect();+ if (!rect.width && !rect.height) {+ setSel(null);+ return;+ }+ setSel({ text, top: rect.top, bottom: rect.bottom, left: rect.left + rect.width / 2 });+ }, [containerRef]);+useEffect(() => {const container = containerRef.current;if (!container) return;let timer = 0;- const recompute = () => {- const selection = window.getSelection();- if (!selection || selection.isCollapsed || selection.rangeCount === 0) {- setSel(null);- return;- }- const text = selection.toString().trim();- const range = selection.getRangeAt(0);- if (!text || !container.contains(range.commonAncestorContainer)) {- setSel(null);- return;- }- const rect = range.getBoundingClientRect();- if (!rect.width && !rect.height) {- setSel(null);- return;- }- setSel({ text, top: rect.top, bottom: rect.bottom, left: rect.left + rect.width / 2 });- };// Debounce so the toolbar appears when the selection settles, not on every// intermediate range while dragging the selection / handles.const onSelectionChange = () => {@@ -74,6 +81,7 @@ export const SelectionQuoteToolbar: React.FC<{// at the new position once it settles. The toolbar persists as long as the// selection exists, so the user can scroll to move it clear of the OS menu.const onScroll = () => {+ if (activePointerId.current !== null) return;setSel(null);window.clearTimeout(timer);timer = window.setTimeout(recompute, 150);@@ -85,7 +93,7 @@ export const SelectionQuoteToolbar: React.FC<{document.removeEventListener('selectionchange', onSelectionChange);container.removeEventListener('scroll', onScroll);};- }, [containerRef]);+ }, [containerRef, recompute]);// Measure the rendered toolbar so we can clamp it on-screen by its real width// (label widths vary by locale + which actions are shown).@@ -99,6 +107,7 @@ export const SelectionQuoteToolbar: React.FC<{if (!onQuote && !onAskInNew && !isTouch) return null;const dismiss = () => {+ activePointerId.current = null;window.getSelection()?.removeAllRanges();setSel(null);setCopied(false);@@ -121,20 +130,43 @@ export const SelectionQuoteToolbar: React.FC<{});};- // Activate on pointerup (mouse + touch) and Enter/Space (keyboard). The- // pointerdown preventDefault keeps the text selection alive (and on touch- // cancels the synthetic click we don't use), so onClick is intentionally- // avoided — it wouldn't fire on touch yet would double-fire on mouse.- const activate = (run: () => void) => ({- onPointerDown: (e: React.PointerEvent) => e.preventDefault(),- onPointerUp: () => run(),- onKeyDown: (e: React.KeyboardEvent) => {- if (e.key === 'Enter' || e.key === ' ') {- e.preventDefault();- run();- }+ const cancelPress = (e: React.PointerEvent) => {+ if (activePointerId.current !== e.pointerId) return;+ activePointerId.current = null;+ recompute();+ };++ // Capture the pointer so release/cancellation still arrives after it moves+ // away. Release must land inside the original button to activate; scrolling+ // the toolbar can cancel the gesture without accidentally quoting or forking.+ // preventDefault avoids a mouse press moving focus/selection. It does not+ // replace the explicit snapshot lifetime above.+ const pressHandlers = {+ onPointerDown: (e: React.PointerEvent<HTMLButtonElement>) => {+ if (!e.isPrimary || e.button !== 0 || activePointerId.current !== null) return;+ e.preventDefault();+ activePointerId.current = e.pointerId;+ e.currentTarget.setPointerCapture(e.pointerId);},- });+ onPointerCancel: cancelPress,+ onLostPointerCapture: cancelPress,+ };+ const releasePress = (e: React.PointerEvent<HTMLButtonElement>, run: () => void) => {+ if (activePointerId.current !== e.pointerId) return;+ activePointerId.current = null;+ const rect = e.currentTarget.getBoundingClientRect();+ if (e.clientX >= rect.left && e.clientX <= rect.right+ && e.clientY >= rect.top && e.clientY <= rect.bottom) {+ run();+ }+ recompute();+ };+ const activateWithKey = (e: React.KeyboardEvent, run: () => void) => {+ if (e.key === 'Enter' || e.key === ' ') {+ e.preventDefault();+ run();+ }+ };const roomAbove = sel.top > TOOLBAR_H + GAP + EDGE;const roomBelow = window.innerHeight - sel.bottom > TOOLBAR_H + GAP + EDGE;@@ -172,7 +204,13 @@ export const SelectionQuoteToolbar: React.FC<{{/* Separators sit BEFORE each item after the first, so a hidden actionnever leaves a dangling divider at the edge of the bar. */}{onQuote && (- <Button variant="ghost" className={itemClass} {...activate(runQuote)}>+ <Button+ variant="ghost"+ className={itemClass}+ {...pressHandlers}+ onPointerUp={(e) => releasePress(e, runQuote)}+ onKeyDown={(e) => activateWithKey(e, runQuote)}+ ><TextQuote className="size-3.5 text-muted" />{t('chat.selection.quote')}</Button>@@ -180,7 +218,13 @@ export const SelectionQuoteToolbar: React.FC<{{onAskInNew && (<>{onQuote && <span className="h-5 w-px bg-border" />}- <Button variant="ghost" className={itemClass} {...activate(runAsk)}>+ <Button+ variant="ghost"+ className={itemClass}+ {...pressHandlers}+ onPointerUp={(e) => releasePress(e, runAsk)}+ onKeyDown={(e) => activateWithKey(e, runAsk)}+ ><GitFork className="size-3.5 text-muted" />{t('chat.selection.askInNew')}</Button>@@ -189,7 +233,13 @@ export const SelectionQuoteToolbar: React.FC<{{isTouch && (<>{(onQuote || onAskInNew) && <span className="h-5 w-px bg-border" />}- <Button variant="ghost" className={itemClass} {...activate(runCopy)}>+ <Button+ variant="ghost"+ className={itemClass}+ {...pressHandlers}+ onPointerUp={(e) => releasePress(e, runCopy)}+ onKeyDown={(e) => activateWithKey(e, runCopy)}+ >{copied ? <Check className="size-3.5 text-mint-ink" /> : <Copy className="size-3.5 text-muted" />}{t('chat.selection.copy')}</Button>