The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): route native browser gestures to the in-app browser
Brooklyn Nicholsonccabff89766f
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 24fe3089140..77ab9d313d7 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -26,7 +26,8 @@ import {screen,session,shell,- systemPreferences+ systemPreferences,+ webContents as electronWebContents} from 'electron'import nodePty from 'node-pty'@@ -5837,6 +5838,102 @@ function sendClosePreviewRequested() {webContents.send('hermes:close-preview-requested')}+/**+ * Run a browser gesture on the guest page the user is actually in, if any.+ *+ * A `<webview>` guest is its own out-of-process webContents: pointer and focus+ * events inside the page never reach the host document, so NOTHING in the+ * renderer — not `document.activeElement`, not the layout tree's hover/focus+ * ladder — can see that the user is in there. Main can: Electron tracks the+ * focused webContents across processes, which is the definition of a runtime+ * fact it owns.+ *+ * Returns false when focus is in the app's own chrome, where the renderer is+ * the one that knows which pane is active.+ */+function commandFocusedGuest(command: 'back' | 'forward' | 'reload'): boolean {+ const focused = electronWebContents.getFocusedWebContents()++ if (!focused || focused.isDestroyed() || focused.getType() !== 'webview') {+ return false+ }++ const history = focused.navigationHistory++ if (command === 'reload') {+ focused.reload()+ } else if (command === 'back') {+ if (!history.canGoBack()) {+ return true+ }++ history.goBack()+ } else {+ if (!history.canGoForward()) {+ return true+ }++ history.goForward()+ }++ return true+}++/**+ * Ask the renderer to run a browser-navigation gesture on its focused preview+ * pane. `reload` also has an app-level fallback (reload the window); `back` and+ * `forward` mean nothing outside the browser, so the renderer just ignores them.+ */+function sendPreviewNavCommand(command: 'back' | 'forward' | 'reload') {+ // The user is inside the page itself — main is the only party that can see+ // that, so act here and never round-trip.+ if (commandFocusedGuest(command)) {+ return+ }++ if (!mainWindow || mainWindow.isDestroyed()) {+ return+ }++ const { webContents } = mainWindow++ if (!webContents || webContents.isDestroyed()) {+ return+ }++ webContents.send('hermes:preview-nav', command)+}++/**+ * The native back/forward gestures, which never reach the renderer on their own.+ *+ * - macOS: a two/three-finger swipe. Chromium's own overscroll navigation is+ * off in an Electron window, so the OS gesture surfaces as this event and+ * nothing consumes it. Requires "Swipe between pages" in System Settings.+ * - Windows/Linux: the dedicated back/forward buttons on a mouse, delivered as+ * `WM_APPCOMMAND`.+ */+function installBrowserNavGestures(window) {+ window.on('swipe', (_event, direction) => {+ if (direction === 'left' || direction === 'right') {+ // Swipe LEFT moves the page left, revealing what's behind it — that's+ // back. Matches Safari, Chrome, and Finder.+ sendPreviewNavCommand(direction === 'left' ? 'back' : 'forward')+ }+ })++ window.on('app-command', (event, command) => {+ if (command !== 'browser-backward' && command !== 'browser-forward') {+ return+ }++ // Claim it either way: unhandled, Chromium walks the HOST document's+ // history, which would navigate the app shell itself.+ event.preventDefault()+ sendPreviewNavCommand(command === 'browser-backward' ? 'back' : 'forward')+ })+}+function sendOpenFolderRequested() {if (!mainWindow || mainWindow.isDestroyed()) {return@@ -6037,7 +6134,15 @@ function buildApplicationMenu() {template.push({label: 'View',submenu: [- { role: 'reload' },+ // Not `role: 'reload'`: that hard-reloads the RENDERER (every pane, the+ // whole shell) and a focused in-app browser needs ⌘R to mean "reload+ // this page", the way it does in every other browser. ⇧⌘R+ // (`forceReload`) below stays the unconditional escape hatch.+ {+ accelerator: 'CommandOrControl+R',+ label: 'Reload',+ click: () => sendPreviewNavCommand('reload')+ },{ role: 'forceReload' },{label: 'Toggle Developer Tools',@@ -10576,6 +10681,7 @@ async function startHermes() {function wireCommonWindowHandlers(win, { zoom = true }: { zoom?: boolean } = {}) {installPreviewShortcut(win)installDevToolsShortcut(win)+ installBrowserNavGestures(win)// Claim Ctrl/Cmd+F in the main process — on Pop!_OS / GNOME-based Linux// distros the Ctrl+F keydown does not reach the renderer's `view.findInPage`diff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.tsindex f573ca24d4f..607e88ce2a7 100644--- a/apps/desktop/electron/preload.ts+++ b/apps/desktop/electron/preload.ts@@ -297,6 +297,12 @@ contextBridge.exposeInMainWorld('hermesDesktop', {return () => ipcRenderer.removeListener('hermes:close-preview-requested', listener)},+ onPreviewNav: callback => {+ const listener = (_event, command) => callback(command)+ ipcRenderer.on('hermes:preview-nav', listener)++ return () => ipcRenderer.removeListener('hermes:preview-nav', listener)+ },onOpenFolderRequested: callback => {const listener = () => callback()ipcRenderer.on('hermes:open-folder-requested', listener)diff --git a/apps/desktop/src/app/chat/right-rail/preview-nav.test.ts b/apps/desktop/src/app/chat/right-rail/preview-nav.test.tsnew file mode 100644index 00000000000..d19f5b2592f--- /dev/null+++ b/apps/desktop/src/app/chat/right-rail/preview-nav.test.ts@@ -0,0 +1,105 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++import { commandFocusedPreview, PREVIEW_BROWSER_ATTR, registerPreviewNav } from './preview-nav'++const nav = () => ({ back: vi.fn(), forward: vi.fn(), reload: vi.fn() })++/** A focusable stand-in for a browser pane, marked the way PreviewPane marks it. */+function mountPane(tabId: string) {+ const host = document.createElement('div')++ host.setAttribute(PREVIEW_BROWSER_ATTR, tabId)+ host.tabIndex = -1+ document.body.append(host)++ return host+}++afterEach(() => {+ document.body.replaceChildren()+})++// This registry answers only for focus in Hermes' OWN chrome. A gesture made+// inside the guest page never reaches this renderer at all — main handles that+// against the focused webContents (see `commandFocusedGuest` in main.ts).+describe('commandFocusedPreview', () => {+ it('reports no handler when focus is elsewhere, so ⌘R falls back to the window', () => {+ const handle = nav()++ registerPreviewNav('url:browser', handle)++ expect(commandFocusedPreview('reload')).toBe(false)+ expect(handle.reload).not.toHaveBeenCalled()+ })++ it.each(['back', 'forward', 'reload'] as const)('routes %s to the focused pane', command => {+ const handle = nav()+ const host = mountPane('url:browser')++ registerPreviewNav('url:browser', handle)+ host.focus()++ expect(commandFocusedPreview(command)).toBe(true)+ expect(handle[command]).toHaveBeenCalledOnce()+ })++ // The common chrome case: focus is in the address field, not the page.+ it('answers for focus nested inside the pane', () => {+ const handle = nav()+ const host = mountPane('url:browser')+ const input = document.createElement('input')++ host.append(input)+ registerPreviewNav('url:browser', handle)+ input.focus()++ expect(commandFocusedPreview('reload')).toBe(true)+ expect(handle.reload).toHaveBeenCalledOnce()+ })++ it('commands only the focused pane when several are open', () => {+ const first = nav()+ const second = nav()++ mountPane('url:one')+ const secondHost = mountPane('url:two')++ registerPreviewNav('url:one', first)+ registerPreviewNav('url:two', second)+ secondHost.focus()++ expect(commandFocusedPreview('back')).toBe(true)+ expect(second.back).toHaveBeenCalledOnce()+ expect(first.back).not.toHaveBeenCalled()+ })++ // A parked/unmounted pane must not answer for a tab that is no longer live —+ // the gesture has to fall through instead of hitting a dead handle.+ it('stops answering once the pane unregisters', () => {+ const handle = nav()+ const host = mountPane('url:browser')+ const unregister = registerPreviewNav('url:browser', handle)++ host.focus()+ unregister()++ expect(commandFocusedPreview('reload')).toBe(false)+ expect(handle.reload).not.toHaveBeenCalled()+ })++ it('leaves a later registration in place when an older one unregisters', () => {+ const stale = nav()+ const live = nav()+ const host = mountPane('url:browser')++ const unregisterStale = registerPreviewNav('url:browser', stale)++ registerPreviewNav('url:browser', live)+ unregisterStale()+ host.focus()++ expect(commandFocusedPreview('reload')).toBe(true)+ expect(live.reload).toHaveBeenCalledOnce()+ expect(stale.reload).not.toHaveBeenCalled()+ })+})diff --git a/apps/desktop/src/app/chat/right-rail/preview-nav.ts b/apps/desktop/src/app/chat/right-rail/preview-nav.tsnew file mode 100644index 00000000000..954c1a5e9b5--- /dev/null+++ b/apps/desktop/src/app/chat/right-rail/preview-nav.ts@@ -0,0 +1,43 @@+/**+ * Browser gestures that land on the app's own chrome — ⌘R with the address bar+ * focused, a mouse's back/forward buttons over the pane's frame.+ *+ * The interesting case is handled elsewhere: when the user is INSIDE the guest+ * page, main acts on the focused webContents directly (see+ * `commandFocusedGuest`), because a webview guest is out-of-process and nothing+ * in this renderer can see it. This registry only covers the other half — focus+ * sitting in Hermes' own DOM, where `activeElement` is authoritative.+ */++/** Marks a live browser pane so a gesture can find the one holding focus. */+export const PREVIEW_BROWSER_ATTR = 'data-preview-browser'++export interface PreviewNavHandle {+ back: () => void+ forward: () => void+ reload: () => void+}++const handles = new Map<string, PreviewNavHandle>()++/** Register a live browser pane's commands; returns an idempotent remove. */+export function registerPreviewNav(tabId: string, handle: PreviewNavHandle): () => void {+ handles.set(tabId, handle)++ return () => {+ if (handles.get(tabId) === handle) {+ handles.delete(tabId)+ }+ }+}++/** Run `command` on the browser pane holding DOM focus. False = focus is+ * elsewhere in the app, so the caller falls back to the app-level meaning. */+export function commandFocusedPreview(command: keyof PreviewNavHandle): boolean {+ const host = document.activeElement?.closest(`[${PREVIEW_BROWSER_ATTR}]`)+ const nav = host ? handles.get(host.getAttribute(PREVIEW_BROWSER_ATTR) || '') : undefined++ nav?.[command]()++ return Boolean(nav)+}diff --git a/apps/desktop/src/app/command-palette/index.tsx b/apps/desktop/src/app/command-palette/index.tsxindex 46986201370..b76862d5167 100644--- a/apps/desktop/src/app/command-palette/index.tsx+++ b/apps/desktop/src/app/command-palette/index.tsx@@ -895,6 +895,13 @@ function CommandPaletteBody({ onExited }: { onExited: () => void }) {keywords: ['update', 'upgrade', 'hermes', 'version', 'system', 'restart'],label: cc.updateHermes,run: () => requestActiveUpdate()+ },+ {+ icon: RefreshCw,+ id: 'cc-reload-window',+ keywords: ['reload', 'window', 'refresh', 'restart', 'ui', 'stuck'],+ label: cc.reloadWindow,+ run: () => window.location.reload()}]},diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.tsindex c2ebf579c0d..c5dff9df42b 100644--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts@@ -1,6 +1,7 @@import { useEffect, useRef } from 'react'import { closeActiveTab } from '@/app/chat/close-tab'+import { commandFocusedPreview } from '@/app/chat/right-rail/preview-nav'import { openSession } from '@/app/open-session'import { storedSessionIdForNotification } from '@/lib/session-ids'import { requestMcpInstallFromDeepLink } from '@/store/mcp-deeplink-install'@@ -242,6 +243,20 @@ export function useDesktopIntegrations({return () => unsubscribe?.()}, [navigate])+ // Native browser gestures (⌘R, a mouse's back/forward buttons, a trackpad+ // swipe) that landed on the app's own chrome rather than inside a page — main+ // answers those against the focused guest and never asks. Only ⌘R has an+ // app-level meaning to fall back to; an unfocused swipe is a no-op.+ useEffect(() => {+ const unsubscribe = window.hermesDesktop?.onPreviewNav?.(command => {+ if (!commandFocusedPreview(command) && command === 'reload') {+ window.location.reload()+ }+ })++ return () => unsubscribe?.()+ }, [])+// File > Open Folder… — same open-folder-as-project upsert as the ⌘O keybind.useEffect(() => {const unsubscribe = window.hermesDesktop?.onOpenFolderRequested?.(() => void openFolderAsProject())diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.tsindex 1c0446082bd..b0f71e854fc 100644--- a/apps/desktop/src/global.d.ts+++ b/apps/desktop/src/global.d.ts@@ -351,6 +351,7 @@ declare global {write: (id: string, data: string) => Promise<boolean>}onClosePreviewRequested?: (callback: () => void) => () => void+ onPreviewNav?: (callback: (command: 'back' | 'forward' | 'reload') => void) => () => voidonOpenFolderRequested?: (callback: () => void) => () => voidonOpenUpdatesRequested?: (callback: () => void) => () => voidonDeepLink?: (