Skip to content

The agent that grows with you

fix(desktop): write the clipboard through IPC first on Electron

emozillac085f77149a8
diff --git a/apps/desktop/src/lib/clipboard.test.ts b/apps/desktop/src/lib/clipboard.test.ts
index e2caae6e7d6..a48f9650f7c 100644
--- a/apps/desktop/src/lib/clipboard.test.ts
+++ b/apps/desktop/src/lib/clipboard.test.ts
@@ -20,7 +20,7 @@ afterEach(() => {
})
describe('installClipboardShim', () => {
- it('keeps a successful native write on the trusted path', async () => {
+ it('routes an Electron write through IPC first', async () => {
const nativeWrite = vi.fn().mockResolvedValue(undefined)
const ipcWrite = vi.fn().mockResolvedValue(true)
installClipboard(nativeWrite)
@@ -29,26 +29,53 @@ describe('installClipboardShim', () => {
installClipboardShim()
await navigator.clipboard.writeText('payload')
- expect(nativeWrite).toHaveBeenCalledWith('payload')
- expect(ipcWrite).not.toHaveBeenCalled()
+ expect(ipcWrite).toHaveBeenCalledWith('payload')
+ expect(nativeWrite).not.toHaveBeenCalled()
})
- it('uses Electron IPC only after the native write fails', async () => {
- const nativeWrite = vi.fn().mockRejectedValue(new Error('lost focus'))
- const ipcWrite = vi.fn().mockResolvedValue(true)
+ it('uses the native write only after the Electron IPC write fails', async () => {
+ const nativeWrite = vi.fn().mockResolvedValue(undefined)
+ const ipcWrite = vi.fn().mockRejectedValue(new Error('main process gone'))
installClipboard(nativeWrite)
desktopWindow.hermesDesktop = { writeClipboard: ipcWrite } as unknown as Window['hermesDesktop']
installClipboardShim()
await navigator.clipboard.writeText('payload')
- expect(nativeWrite).toHaveBeenCalledWith('payload')
expect(ipcWrite).toHaveBeenCalledWith('payload')
+ expect(nativeWrite).toHaveBeenCalledWith('payload')
})
})
describe('writeClipboardText', () => {
- it('attempts native and IPC once when an installed Electron shim fails', async () => {
+ it('writes through Electron IPC first, so a copy from an unfocused document still lands', async () => {
+ const nativeWrite = vi.fn().mockRejectedValue(new Error('Document is not focused'))
+ const ipcWrite = vi.fn().mockResolvedValue(true)
+ installClipboard(nativeWrite)
+ desktopWindow.hermesDesktop = { writeClipboard: ipcWrite } as unknown as Window['hermesDesktop']
+
+ await writeClipboardText('payload')
+
+ expect(ipcWrite).toHaveBeenCalledTimes(1)
+ expect(nativeWrite).not.toHaveBeenCalled()
+ })
+
+ it('writes a browser-hosted copy through the native clipboard once', async () => {
+ const nativeWrite = vi.fn().mockResolvedValue(undefined)
+ installClipboard(nativeWrite)
+ document.documentElement.dataset.hermesDesktopHost = 'browser'
+ desktopWindow.hermesDesktop = createBrowserClipboardBridge({
+ saveBuffer: async () => ''
+ }) as Window['hermesDesktop']
+ installClipboardShim()
+
+ await writeClipboardText('payload')
+
+ expect(nativeWrite).toHaveBeenCalledTimes(1)
+ expect(nativeWrite).toHaveBeenCalledWith('payload')
+ })
+
+ it('attempts IPC and native once when an installed Electron shim fails', async () => {
const nativeWrite = vi.fn().mockRejectedValue(new Error('lost focus'))
const bridgeWrite = vi.fn().mockResolvedValue(false)
installClipboard(nativeWrite)
diff --git a/apps/desktop/src/lib/clipboard.ts b/apps/desktop/src/lib/clipboard.ts
index f95cac59a77..7179b8499e7 100644
--- a/apps/desktop/src/lib/clipboard.ts
+++ b/apps/desktop/src/lib/clipboard.ts
@@ -3,6 +3,10 @@ import { isBrowserHostedDesktop } from '@/lib/platform'
// Primitives and CopyButton enter one policy, never nested fallback retries.
const nativeWrites = new WeakMap<Clipboard, Clipboard['writeText'] | undefined>()
+// Electron writes through IPC first: the main process writes the clipboard even after the document
+// loses focus (a portaled Radix menu), where the renderer's clipboard API refuses with "Write
+// permission denied". The native API is the fallback. The browser-hosted Desktop has only the native
+// API; its bridge's writeClipboard calls that same API.
export async function writeClipboardText(text: string) {
if (!text) {
return
@@ -13,28 +17,25 @@ export async function writeClipboardText(text: string) {
const native =
clipboard && (nativeWrites.has(clipboard) ? nativeWrites.get(clipboard) : clipboard.writeText?.bind(clipboard))
- // The browser bridge calls the same native API, not an independent fallback.
const ipc = isBrowserHostedDesktop() ? undefined : window.hermesDesktop?.writeClipboard
- if (native) {
+ if (ipc) {
try {
- await native(text)
-
- return
+ if (await ipc(text)) {
+ return
+ }
} catch (error) {
- if (!ipc) {
+ if (!native) {
throw error
}
}
}
- if (!ipc) {
- throw new Error('Clipboard API is unavailable')
+ if (!native) {
+ throw new Error(ipc ? 'Clipboard write is unavailable' : 'Clipboard API is unavailable')
}
- if (!(await ipc(text))) {
- throw new Error('Clipboard write is unavailable')
- }
+ await native(text)
}
export function installClipboardShim() {