The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): touch handling stays in the browser tab, and a mouse right-click always reacts
emozillaf84699c8da2e
diff --git a/apps/desktop/src/app/chat/composer/floating-target-selection-guard.test.ts b/apps/desktop/src/app/chat/composer/floating-target-selection-guard.test.tsindex 830db105938..258448f8033 100644--- a/apps/desktop/src/app/chat/composer/floating-target-selection-guard.test.ts+++ b/apps/desktop/src/app/chat/composer/floating-target-selection-guard.test.ts@@ -52,6 +52,7 @@ describe('floating composer focus-follow vs transcript selection', () => {unregister?.()unregister = undefineddocument.body.innerHTML = ''+ delete document.documentElement.dataset.hermesDesktopHostwindow.getSelection()?.removeAllRanges()})@@ -89,6 +90,8 @@ describe('floating composer focus-follow vs transcript selection', () => {})it.each(['touch', 'pen'])('keeps %s transcript gestures from focusing the composer', pointerType => {+ // Touch handling is the browser-hosted Desktop's.+ document.documentElement.dataset.hermesDesktopHost = 'browser'const { button, editor } = mount()unregister = registerFloatingComposer('surface-1', { groupId: 'g1', target: 'main' })diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsxindex 814e5e2537c..a05e24e1068 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx@@ -825,6 +825,8 @@ describe('useComposerDraft — touch never re-enters the editor on its own', ()}beforeEach(() => {+ // Touch handling is the browser-hosted Desktop's.+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: true }))})@@ -834,6 +836,7 @@ describe('useComposerDraft — touch never re-enters the editor on its own', ()clearSessionDraft('touch-clear')mainComposerScope.clear()markActiveComposer('main')+ delete window.document.documentElement.dataset.hermesDesktopHostvi.unstubAllGlobals()})diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsxindex 0bb73681704..5b5dbd4989e 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx@@ -1,6 +1,6 @@import { act, cleanup, renderHook, waitFor } from '@testing-library/react'import { type Dispatch, type PropsWithChildren, type SetStateAction, useLayoutEffect, useState } from 'react'-import { afterEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import * as sessionOwnerUtils from '@/app/session/hooks/use-session-actions/utils'import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'@@ -597,9 +597,15 @@ describe('useComposerSubmit busy-turn routing', () => {})describe('useComposerSubmit keyboard after send', () => {+ beforeEach(() => {+ // Touch handling is the browser-hosted Desktop's.+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'+ })+afterEach(() => {cleanup()window.document.body.replaceChildren()+ delete window.document.documentElement.dataset.hermesDesktopHostvi.unstubAllGlobals()})diff --git a/apps/desktop/src/components/assistant-ui/thread/double-click-reaction.test.tsx b/apps/desktop/src/components/assistant-ui/thread/double-click-reaction.test.tsxindex 15b813881b4..7446b9d7fce 100644--- a/apps/desktop/src/components/assistant-ui/thread/double-click-reaction.test.tsx+++ b/apps/desktop/src/components/assistant-ui/thread/double-click-reaction.test.tsx@@ -43,6 +43,7 @@ beforeEach(() => {afterEach(() => {cleanup()+ delete window.document.documentElement.dataset.hermesDesktopHostvi.restoreAllMocks()vi.unstubAllGlobals()})@@ -70,6 +71,8 @@ describe('isTapbackDoubleClick', () => {describe('double-click to heart an assistant message', () => {it('leaves touch double-tap selection intact instead of claiming it as a reaction', async () => {+ // Touch handling is the browser-hosted Desktop's.+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'vi.stubGlobal('matchMedia',vi.fn((query: string) => ({diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-pointer.test.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message-pointer.test.tsxnew file mode 100644index 00000000000..f17f4b80da2--- /dev/null+++ b/apps/desktop/src/components/assistant-ui/thread/user-message-pointer.test.tsx@@ -0,0 +1,88 @@+import { AssistantRuntimeProvider, type ThreadMessage, useExternalStoreRuntime } from '@assistant-ui/react'+import { cleanup, fireEvent, render, screen } from '@testing-library/react'+import { afterEach, beforeEach, describe, expect, it } from 'vitest'++import { $touchPointer } from '@/lib/touch-interaction'+import { $reactionsEnabled } from '@/store/reactions-enabled'++import { assistantMessage, stubThreadEnvironment, stubThreadViewportSize, userMessage } from '../test-utils'++import { Thread } from '.'++stubThreadEnvironment()+stubThreadViewportSize()++function Harness() {+ const runtime = useExternalStoreRuntime<ThreadMessage>({+ messages: [userMessage(), assistantMessage()],+ isRunning: false,+ onNew: async () => {}+ })++ return (+ <AssistantRuntimeProvider runtime={runtime}>+ <Thread />+ </AssistantRuntimeProvider>+ )+}++/** A contextmenu event as Chromium sends it: a PointerEvent that says which pointer raised it. */+function contextMenu(target: Element, pointerType: 'mouse' | 'pen' | 'touch'): MouseEvent {+ const event = new MouseEvent('contextmenu', { bubbles: true, button: 2, cancelable: true })+ Object.defineProperty(event, 'pointerType', { value: pointerType })+ fireEvent(target, event)++ return event+}++beforeEach(() => {+ $reactionsEnabled.set(true)+})++afterEach(() => {+ cleanup()+ $reactionsEnabled.set(false)+ $touchPointer.set(false)+ delete window.document.documentElement.dataset.hermesDesktopHost+})++describe('user message reactions by pointer', () => {+ // A browser tab on a touch surface leaves a long press to the platform (selection handles and+ // Copy), but a mouse plugged into that device right-clicks the way it does anywhere else: the+ // reaction picker opens.+ it('opens the reaction picker on a mouse right-click in a touch-layout browser tab', async () => {+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'+ $touchPointer.set(true)+ render(<Harness />)++ const event = contextMenu(await screen.findByText('edit me please'), 'mouse')++ expect(event.defaultPrevented).toBe(true)+ expect(await screen.findByRole('button', { name: '👍' })).toBeTruthy()+ })++ it('leaves a touch long-press in a touch-layout browser tab to the platform', async () => {+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'+ $touchPointer.set(true)+ render(<Harness />)++ const event = contextMenu(await screen.findByText('edit me please'), 'touch')++ expect(event.defaultPrevented).toBe(false)+ expect(screen.queryByRole('button', { name: '👍' })).toBeNull()+ })++ // Native Desktop has no touch handling: a long press on a touchscreen raises the picker as a+ // right-click does, as it always has.+ it.each(['mouse', 'touch'] as const)(+ 'opens the picker for a %s context menu in native Desktop',+ async pointerType => {+ render(<Harness />)++ const event = contextMenu(await screen.findByText('edit me please'), pointerType)++ expect(event.defaultPrevented).toBe(true)+ expect(await screen.findByRole('button', { name: '👍' })).toBeTruthy()+ }+ )+})diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message.tsxindex ec5de7415be..0feeb17411e 100644--- a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx+++ b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx@@ -25,7 +25,7 @@ import { useI18n } from '@/i18n'import { triggerHaptic } from '@/lib/haptics'import { StopFilled } from '@/lib/icons'import { LruCache } from '@/lib/lru-cache'-import { $touchPointer } from '@/lib/touch-interaction'+import { $touchPointer, isTouchInteraction } from '@/lib/touch-interaction'import { cn } from '@/lib/utils'import { $gateway } from '@/store/gateway'import { notifyThreadEditOpen } from '@/store/thread-scroll'@@ -570,13 +570,14 @@ export const UserMessage: FC<{// menu, and this handler's selection guard keeps ⌘C flows.data-context-menu-skip=""onContextMenu={- // Right-click is the desktop reaction gesture. Touch surfaces- // leave long-press entirely to the platform so selection- // handles and Copy are never replaced by the picker.+ // Right-click is the desktop reaction gesture. A touch long-press+ // in the browser-hosted Desktop stays the platform's, so selection+ // handles and Copy are never replaced by the picker; a mouse+ // right-click on the same device still opens it.readOnly || !reactionsEnabled? undefined: event => {- if (hasTextSelection() || touch) {+ if (hasTextSelection() || isTouchInteraction(event.nativeEvent)) {return}diff --git a/apps/desktop/src/lib/touch-interaction.test.ts b/apps/desktop/src/lib/touch-interaction.test.tsindex 18c675bf459..5e7cb0bbbd0 100644--- a/apps/desktop/src/lib/touch-interaction.test.ts+++ b/apps/desktop/src/lib/touch-interaction.test.ts@@ -1,12 +1,22 @@-import { afterEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { isTouchInteraction } from './touch-interaction'-afterEach(() => vi.unstubAllGlobals())+const coarsePointer = (matches: boolean) =>+ vi.fn().mockReturnValue({ addEventListener: vi.fn(), matches, removeEventListener: vi.fn() })++afterEach(() => {+ vi.unstubAllGlobals()+ delete document.documentElement.dataset.hermesDesktopHost+})++describe('touch input ownership in the browser-hosted Desktop', () => {+ beforeEach(() => {+ document.documentElement.dataset.hermesDesktopHost = 'browser'+ })-describe('touch input ownership', () => {it('uses coarse capability only when the event has no input evidence', () => {- vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: true }))+ vi.stubGlobal('matchMedia', coarsePointer(true))expect(isTouchInteraction(new Event('contextmenu'))).toBe(true)expect(isTouchInteraction(Object.assign(new Event('contextmenu'), { pointerType: 'mouse' }))).toBe(false)expect(@@ -15,7 +25,7 @@ describe('touch input ownership', () => {})it('recognizes touch and pen on a hybrid device', () => {- vi.stubGlobal('matchMedia', vi.fn().mockReturnValue({ matches: false }))+ vi.stubGlobal('matchMedia', coarsePointer(false))for (const pointerType of ['touch', 'pen']) {expect(isTouchInteraction(Object.assign(new Event('contextmenu'), { pointerType }))).toBe(true)@@ -25,4 +35,37 @@ describe('touch input ownership', () => {isTouchInteraction(Object.assign(new Event('dblclick'), { sourceCapabilities: { firesTouchEvents: true } }))).toBe(true)})++ it('turns the touch layout on for a coarse-pointer tab', async () => {+ vi.stubGlobal('matchMedia', coarsePointer(true))+ vi.resetModules()++ const { $touchPointer } = await import('./touch-interaction')++ expect($touchPointer.get()).toBe(true)+ })+})++// Touch handling belongs to the browser-hosted Desktop. Native Desktop on a touchscreen laptop or+// a tablet (a coarse pointer) keeps the pointer behaviour and layout it has everywhere else.+describe('native Desktop on a coarse-pointer device', () => {+ it('never reads input as touch, with or without input evidence', () => {+ vi.stubGlobal('matchMedia', coarsePointer(true))++ expect(isTouchInteraction()).toBe(false)+ expect(isTouchInteraction(new Event('contextmenu'))).toBe(false)++ for (const pointerType of ['touch', 'pen']) {+ expect(isTouchInteraction(Object.assign(new Event('contextmenu'), { pointerType }))).toBe(false)+ }+ })++ it('keeps the touch layout off', async () => {+ vi.stubGlobal('matchMedia', coarsePointer(true))+ vi.resetModules()++ const { $touchPointer } = await import('./touch-interaction')++ expect($touchPointer.get()).toBe(false)+ })})diff --git a/apps/desktop/src/lib/touch-interaction.ts b/apps/desktop/src/lib/touch-interaction.tsindex 255e0fa1705..70ed917cc82 100644--- a/apps/desktop/src/lib/touch-interaction.ts+++ b/apps/desktop/src/lib/touch-interaction.ts@@ -10,10 +10,11 @@ export const TOUCH_POINTER_QUERY = '(hover: none), (pointer: coarse)'// `matchMedia`.const touchPointerQuery = typeof window !== 'undefined' ? window.matchMedia?.(TOUCH_POINTER_QUERY) : undefined-/** Coarse-pointer / no-hover surface, the same query the touch CSS keys on. */-export const $touchPointer = atom(Boolean(touchPointerQuery?.matches))+/** The browser-hosted Desktop's touch layout: a coarse-pointer / no-hover surface, the same query+ * the touch CSS keys on. Native Desktop keeps its hover layout on any pointer. */+export const $touchPointer = atom(isBrowserHostedDesktop() && Boolean(touchPointerQuery?.matches))-touchPointerQuery?.addEventListener('change', event => $touchPointer.set(event.matches))+touchPointerQuery?.addEventListener('change', event => $touchPointer.set(isBrowserHostedDesktop() && event.matches))const BROWSER_OWNED_TOUCH_TARGET ='[data-slot="aui_user-message-root"], [data-slot="aui_assistant-message-content"], [data-slot="aui_system-message-root"], [data-selectable-text="true"], input, textarea, [contenteditable]:not([contenteditable="false"]), a[href], img, video, audio'@@ -28,8 +29,14 @@ export function isBrowserOwnedTouch(event: Event): boolean {}/** Context-menu/double-click events are not PointerEvents in every browser.- * Prefer actual input evidence, then WebKit's coarse-pointer capability. */+ * Prefer actual input evidence, then WebKit's coarse-pointer capability.+ * Touch handling is the browser-hosted Desktop's: native Desktop treats every+ * pointer, a touchscreen's included, as it always has. */export function isTouchInteraction(event?: Event): boolean {+ if (!isBrowserHostedDesktop()) {+ return false+ }+const input = event as(Event & { pointerType?: string; sourceCapabilities?: { firesTouchEvents?: boolean } }) | undefineddiff --git a/apps/desktop/src/styles.css b/apps/desktop/src/styles.cssindex 06a7d94eee5..9456303a121 100644--- a/apps/desktop/src/styles.css+++ b/apps/desktop/src/styles.css@@ -848,9 +848,10 @@/* Touch surfaces need a real finger-sized control lane. Keep desktop'scompact 24/26px rhythm, but promote the same composer actions to the 44pxminimum recommended for coarse pointers without adding a second- mobile-only component. */+ mobile-only component. The touch layout is the browser-hosted Desktop's;+ native Desktop keeps its compact rhythm on a coarse pointer too. */@media (hover: none), (pointer: coarse) {- :root {+ html[data-hermes-desktop-host='browser'] {--composer-control-size: 2.75rem;--composer-control-primary-size: 2.75rem;--composer-voice-menu-size: var(--composer-control-size);@@ -2564,38 +2565,39 @@ button[data-slot='aui_msg-reactions'] svg {/* Hover reveals are useful on desktop, but a coarse pointer needs a stableaction lane. Keep the rich desktop row intact and expose a compact Copy +More pair on touch surfaces; the buttons themselves get the 44px hit box- while the glyphs retain the existing quiet 14px scale. */+ while the glyphs retain the existing quiet 14px scale. Only the+ browser-hosted Desktop has the touch layout. */.aui-message-actions-touch {display: none;}@media (hover: none), (pointer: coarse) {- [data-slot='aui_msg-actions'] {+ html[data-hermes-desktop-host='browser'] [data-slot='aui_msg-actions'] {opacity: 1;pointer-events: auto;}- [data-slot='aui_msg-actions'] .aui-message-actions-desktop {+ html[data-hermes-desktop-host='browser'] [data-slot='aui_msg-actions'] .aui-message-actions-desktop {display: none;}- [data-slot='aui_msg-actions'] .aui-message-actions-touch {+ html[data-hermes-desktop-host='browser'] [data-slot='aui_msg-actions'] .aui-message-actions-touch {display: flex;}- [data-slot='aui_msg-actions'] .aui-message-actions-touch button {+ html[data-hermes-desktop-host='browser'] [data-slot='aui_msg-actions'] .aui-message-actions-touch button {min-width: 2.75rem;min-height: 2.75rem;}- button[data-slot='aui_msg-reactions'] {+ html[data-hermes-desktop-host='browser'] button[data-slot='aui_msg-reactions'] {min-width: 2.75rem;min-height: 2.75rem;opacity: 0.5;pointer-events: auto;}- button[data-slot='aui_msg-reactions'][data-reacted] {+ html[data-hermes-desktop-host='browser'] button[data-slot='aui_msg-reactions'][data-reacted] {opacity: 1;}}diff --git a/apps/desktop/src/touch-styles.test.ts b/apps/desktop/src/touch-styles.test.tsnew file mode 100644index 00000000000..0a12aa823c2--- /dev/null+++ b/apps/desktop/src/touch-styles.test.ts@@ -0,0 +1,84 @@+// @vitest-environment node+import { readFileSync } from 'node:fs'+import { dirname, join } from 'node:path'+import { fileURLToPath } from 'node:url'++import { describe, expect, it } from 'vitest'++const STYLES = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'styles.css'), 'utf8').replace(+ /\/\*[\s\S]*?\*\//g,+ ''+)++const COARSE_POINTER_MEDIA = /@media\s*\(\s*hover\s*:\s*none\s*\)\s*,\s*\(\s*pointer\s*:\s*coarse\s*\)\s*\{/g+const BROWSER_HOST = "html[data-hermes-desktop-host='browser']"++/** The text between the brace at `open` and its matching close. */+function blockBody(css: string, open: number): string {+ let depth = 0++ for (let index = open; index < css.length; index += 1) {+ if (css[index] === '{') {+ depth += 1+ } else if (css[index] === '}' && (depth -= 1) === 0) {+ return css.slice(open + 1, index)+ }+ }++ throw new Error('Unbalanced block in styles.css')+}++/** Every selector of the block's own rules, split on commas outside parentheses. */+function ruleSelectors(body: string): string[] {+ const selectors: string[] = []+ let depth = 0+ let prelude = ''++ for (const char of body) {+ if (char === '{') {+ if (depth === 0) {+ let parens = 0+ let current = ''++ for (const part of prelude) {+ parens += part === '(' ? 1 : part === ')' ? -1 : 0++ if (part === ',' && parens === 0) {+ selectors.push(current.trim())+ current = ''+ } else {+ current += part+ }+ }++ selectors.push(current.trim())+ prelude = ''+ }++ depth += 1+ } else if (char === '}') {+ depth -= 1+ } else if (depth === 0) {+ prelude += char+ }+ }++ return selectors+}++describe('touch styles', () => {+ // Finger-sized controls and always-visible message actions are the browser-hosted Desktop's+ // touch layout. Native Desktop on a coarse-pointer device keeps its compact hover layout, so+ // every rule under the coarse-pointer query is scoped to the browser host.+ it('applies coarse-pointer rules only in the browser-hosted Desktop', () => {+ const blocks = [...STYLES.matchAll(COARSE_POINTER_MEDIA)].map(match =>+ blockBody(STYLES, match.index + match[0].length - 1)+ )++ expect(blocks.length).toBeGreaterThan(0)++ for (const selector of blocks.flatMap(ruleSelectors)) {+ expect(selector.startsWith(BROWSER_HOST), selector).toBe(true)+ }+ })+})