Skip to content

The agent that grows with you

fix(desktop): pause inactive diffusion rendering

Jakub Wolniewiczc776bdd244ba
diff --git a/apps/desktop/src/components/chat/image-generation-placeholder.test.tsx b/apps/desktop/src/components/chat/image-generation-placeholder.test.tsx
new file mode 100644
index 00000000000..e65981add80
--- /dev/null
+++ b/apps/desktop/src/components/chat/image-generation-placeholder.test.tsx
@@ -0,0 +1,119 @@
+import { act } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { DiffusionCanvas } from './image-generation-placeholder'
+
+let root: Root | null = null
+let container: HTMLDivElement | null = null
+let windowStateCallback: ((payload: { isMinimized?: boolean; isVisible?: boolean }) => void) | null = null
+
+function render() {
+ container = document.createElement('div')
+ document.body.append(container)
+ root = createRoot(container)
+
+ act(() => {
+ root!.render(<DiffusionCanvas />)
+ })
+}
+
+function cleanup() {
+ if (root) {
+ act(() => {
+ root!.unmount()
+ })
+ }
+
+ container?.remove()
+ root = null
+ container = null
+}
+
+function installRaf() {
+ let nextId = 1
+ const frames = new Map<number, FrameRequestCallback>()
+
+ const request = vi.fn((callback: FrameRequestCallback) => {
+ const id = nextId++
+ frames.set(id, callback)
+
+ return id
+ })
+
+ const cancel = vi.fn((id: number) => {
+ frames.delete(id)
+ })
+
+ Object.defineProperty(window, 'requestAnimationFrame', { configurable: true, value: request })
+ Object.defineProperty(window, 'cancelAnimationFrame', { configurable: true, value: cancel })
+
+ return {
+ pending: () => frames.size,
+ request
+ }
+}
+
+function installWindowStateBridge() {
+ windowStateCallback = null
+ Object.defineProperty(window, 'hermesDesktop', {
+ configurable: true,
+ value: {
+ onWindowStateChanged: vi.fn((callback: typeof windowStateCallback) => {
+ windowStateCallback = callback
+
+ return () => {
+ if (windowStateCallback === callback) {
+ windowStateCallback = null
+ }
+ }
+ })
+ }
+ })
+}
+
+describe('DiffusionCanvas scheduling', () => {
+ beforeEach(() => {
+ ;(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+ vi.spyOn(document, 'hasFocus').mockReturnValue(true)
+ installWindowStateBridge()
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({
+ setTransform: vi.fn()
+ } as unknown as CanvasRenderingContext2D)
+ })
+
+ afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+ delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
+ })
+
+ it('cancels its loop while inactive and resumes only when the window is observable', () => {
+ const raf = installRaf()
+
+ render()
+ expect(raf.request).toHaveBeenCalledTimes(1)
+ expect(raf.pending()).toBe(1)
+
+ act(() => {
+ window.dispatchEvent(new Event('blur'))
+ })
+ expect(raf.pending()).toBe(0)
+
+ act(() => {
+ window.dispatchEvent(new Event('focus'))
+ })
+ expect(raf.pending()).toBe(1)
+
+ act(() => {
+ windowStateCallback?.({ isMinimized: true, isVisible: false })
+ })
+ expect(raf.pending()).toBe(0)
+
+ cleanup()
+ act(() => {
+ window.dispatchEvent(new Event('focus'))
+ })
+ expect(raf.pending()).toBe(0)
+ })
+})
diff --git a/apps/desktop/src/components/chat/image-generation-placeholder.tsx b/apps/desktop/src/components/chat/image-generation-placeholder.tsx
index 3f0e5a16c6a..51742687a54 100644
--- a/apps/desktop/src/components/chat/image-generation-placeholder.tsx
+++ b/apps/desktop/src/components/chat/image-generation-placeholder.tsx
@@ -2,6 +2,7 @@ import { type FC, useCallback, useEffect, useRef } from 'react'
import { useResizeObserver } from '@/hooks/use-resize-observer'
import { onThemeRepaint } from '@/hooks/use-theme-epoch'
+import { createRendererLoopPauseController } from '@/lib/renderer-loop-pause'
type Rgb = { r: number; g: number; b: number }
@@ -299,15 +300,50 @@ export const DiffusionCanvas: FC = () => {
sizeRef.current = fitCanvas(canvas, ctx)
- let frame = requestAnimationFrame(function draw(now) {
+ let frame = 0
+ let stopped = false
+ let pauseController: ReturnType<typeof createRendererLoopPauseController> | null = null
+
+ const cancelFrame = () => {
+ if (frame !== 0) {
+ window.cancelAnimationFrame(frame)
+ frame = 0
+ }
+ }
+
+ const scheduleFrame = () => {
+ if (stopped || pauseController?.isPaused() || frame !== 0) {
+ return
+ }
+
+ frame = window.requestAnimationFrame(draw)
+ }
+
+ const draw = (now: number) => {
+ frame = 0
+
+ if (stopped || pauseController?.isPaused()) {
+ return
+ }
+
const { width, height } = sizeRef.current
ctx.clearRect(0, 0, width, height)
drawAsciiDiffusion(ctx, themeRef.current, width, height, now / 1000)
- frame = requestAnimationFrame(draw)
- })
+ scheduleFrame()
+ }
+
+ const handleVisibilityChange = () => {
+ cancelFrame()
+ scheduleFrame()
+ }
+
+ pauseController = createRendererLoopPauseController(handleVisibilityChange)
+ scheduleFrame()
return () => {
- cancelAnimationFrame(frame)
+ stopped = true
+ cancelFrame()
+ pauseController.dispose()
}
}, [])