Skip to content

The agent that grows with you

fix(desktop): keep preview input alive when the window is in the background

xxxigm6e4445cbf724
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index bed75167ced..778699aa789 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -462,6 +462,7 @@ import { capturePreviewContents } from './preview-capture'
import { onPreviewWatchOwnerDestroyed, sendPreviewFileChangedToOwner } from './preview-file-watch'
import { hasClosePreviewFlag, previewGuestInputAction } from './preview-guest-escape'
import { commandFocusedGuest, notePreviewGuestHidden } from './preview-guest-offscreen'
+import { applyPreviewGuestAttachPrefs } from './preview-guest-throttle'
import { PreviewReachRegistry } from './preview-reach'
import { previewHttpUrlTarget } from './preview-url-target'
import {
@@ -13652,11 +13653,7 @@ function installPreviewGuestPreload() {
}
contents.on('will-attach-webview', (_attachEvent, webPreferences, params) => {
- if (params.partition !== 'persist:hermes-preview') {
- return
- }
-
- webPreferences.preload = PREVIEW_GUEST_PRELOAD_PATH
+ applyPreviewGuestAttachPrefs(webPreferences, params.partition, PREVIEW_GUEST_PRELOAD_PATH)
})
})
}
diff --git a/apps/desktop/electron/preview-guest-throttle.test.ts b/apps/desktop/electron/preview-guest-throttle.test.ts
new file mode 100644
index 00000000000..63eb1b9e79a
--- /dev/null
+++ b/apps/desktop/electron/preview-guest-throttle.test.ts
@@ -0,0 +1,22 @@
+import { describe, expect, it } from 'vitest'
+
+import { applyPreviewGuestAttachPrefs, PREVIEW_GUEST_PARTITION } from './preview-guest-throttle'
+
+describe('applyPreviewGuestAttachPrefs', () => {
+ it('keeps the preview guest animating while the Desktop window is in the background', () => {
+ const webPreferences: { backgroundThrottling?: boolean; preload?: string } = {}
+
+ applyPreviewGuestAttachPrefs(webPreferences, PREVIEW_GUEST_PARTITION, '/preload.js')
+
+ expect(webPreferences.backgroundThrottling).toBe(false)
+ expect(webPreferences.preload).toBe('/preload.js')
+ })
+
+ it('leaves every other webview alone', () => {
+ const webPreferences: { backgroundThrottling?: boolean; preload?: string } = {}
+
+ applyPreviewGuestAttachPrefs(webPreferences, 'persist:other', '/preload.js')
+
+ expect(webPreferences).toEqual({})
+ })
+})
diff --git a/apps/desktop/electron/preview-guest-throttle.ts b/apps/desktop/electron/preview-guest-throttle.ts
new file mode 100644
index 00000000000..01c21b354b6
--- /dev/null
+++ b/apps/desktop/electron/preview-guest-throttle.ts
@@ -0,0 +1,27 @@
+/** Partition the preview pane's `<webview>` always carries. */
+export const PREVIEW_GUEST_PARTITION = 'persist:hermes-preview'
+
+export interface PreviewGuestAttachPrefs {
+ backgroundThrottling?: boolean
+ preload?: string
+}
+
+/**
+ * `drive_preview` aims the pointer only after one animation frame inside the
+ * guest. Chromium does not fire that frame while the guest is
+ * background-throttled, so a click or keystroke never leaves once the Desktop
+ * window is not in front. This guest is the page being driven, not a Hermes
+ * chat renderer — those stay on the stream-throttle dial.
+ */
+export function applyPreviewGuestAttachPrefs(
+ webPreferences: PreviewGuestAttachPrefs,
+ partition: string | undefined,
+ preloadPath: string
+): void {
+ if (partition !== PREVIEW_GUEST_PARTITION) {
+ return
+ }
+
+ webPreferences.preload = preloadPath
+ webPreferences.backgroundThrottling = false
+}