Skip to content

The agent that grows with you

perf(desktop): pause decorative animations when unfocused

AlexDev_471272103320
diff --git a/apps/desktop/src/lib/renderer-loop-pause.test.ts b/apps/desktop/src/lib/renderer-loop-pause.test.ts
new file mode 100644
index 00000000000..c6fe0e91de3
--- /dev/null
+++ b/apps/desktop/src/lib/renderer-loop-pause.test.ts
@@ -0,0 +1,34 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import {
+ installRendererAnimationPauseState,
+ RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE
+} from './renderer-loop-pause'
+
+describe('installRendererAnimationPauseState', () => {
+ afterEach(() => {
+ document.documentElement.removeAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)
+ vi.restoreAllMocks()
+ })
+
+ it('pauses on blur, resumes on focus, and cleans up its root state', () => {
+ let focused = true
+ vi.spyOn(document, 'hasFocus').mockImplementation(() => focused)
+
+ const dispose = installRendererAnimationPauseState()
+ expect(document.documentElement.hasAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)).toBe(false)
+
+ focused = false
+ window.dispatchEvent(new Event('blur'))
+ expect(document.documentElement.hasAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)).toBe(true)
+
+ focused = true
+ window.dispatchEvent(new Event('focus'))
+ expect(document.documentElement.hasAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)).toBe(false)
+
+ focused = false
+ window.dispatchEvent(new Event('blur'))
+ dispose()
+ expect(document.documentElement.hasAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/lib/renderer-loop-pause.ts b/apps/desktop/src/lib/renderer-loop-pause.ts
index 88b9e3559bc..55fb19b1c78 100644
--- a/apps/desktop/src/lib/renderer-loop-pause.ts
+++ b/apps/desktop/src/lib/renderer-loop-pause.ts
@@ -3,6 +3,8 @@ interface WindowStatePayload {
isVisible?: boolean
}
+export const RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE = 'data-renderer-animations-paused'
+
export function createRendererLoopPauseController(onChange: () => void, { pauseWhenUnfocused = true } = {}) {
let windowPaused = false
let windowFocused = document.hasFocus()
@@ -48,3 +50,23 @@ export function createRendererLoopPauseController(onChange: () => void, { pauseW
isPaused: () => document.visibilityState === 'hidden' || (pauseWhenUnfocused && !windowFocused) || windowPaused
}
}
+
+/**
+ * Mirrors the main window's observability onto :root so continuous decorative
+ * CSS animations can sleep with the JS renderer loops. The caller owns the
+ * returned cleanup; overlay windows intentionally do not install this state.
+ */
+export function installRendererAnimationPauseState(): () => void {
+ const root = document.documentElement
+ let controller: ReturnType<typeof createRendererLoopPauseController>
+
+ const sync = () => root.toggleAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE, controller.isPaused())
+
+ controller = createRendererLoopPauseController(sync)
+ sync()
+
+ return () => {
+ controller.dispose()
+ root.removeAttribute(RENDERER_ANIMATIONS_PAUSED_ATTRIBUTE)
+ }
+}
diff --git a/apps/desktop/src/main.tsx b/apps/desktop/src/main.tsx
index b2ef156a436..39e21ce7434 100644
--- a/apps/desktop/src/main.tsx
+++ b/apps/desktop/src/main.tsx
@@ -25,6 +25,7 @@ import { RootTooltipProvider } from './components/ui/tooltip'
import { I18nProvider } from './i18n'
import { installClipboardShim } from './lib/clipboard'
import { queryClient } from './lib/query-client'
+import { installRendererAnimationPauseState } from './lib/renderer-loop-pause'
import { ThemeProvider } from './themes/context'
installClipboardShim()
@@ -50,6 +51,11 @@ if (winParam === 'overlay') {
} else if (winParam === 'wake') {
void import('./app/wake-indicator/wake-indicator-root').then(({ mountWakeIndicator }) => mountWakeIndicator())
} else {
+ // CSS animations do not inherit Chromium's JS-loop pause policy. Mirror the
+ // main window's focus/visibility state to :root so decorative infinite
+ // animations stop producing frames when nobody can see them.
+ installRendererAnimationPauseState()
+
createRoot(document.getElementById('root')!).render(
<StrictMode>
<RootErrorBoundary>
diff --git a/apps/desktop/src/styles.css b/apps/desktop/src/styles.css
index 6bd413648fd..16a6da217b9 100644
--- a/apps/desktop/src/styles.css
+++ b/apps/desktop/src/styles.css
@@ -28,6 +28,27 @@
}
}
+/* Continuous decorative animations otherwise keep Chromium's renderer awake
+ behind another app. main.tsx owns this attribute only for the primary
+ window; wake/pet overlays retain their purpose-built visibility behavior. */
+:root[data-renderer-animations-paused] :is(
+ .shimmer,
+ .quest-glow,
+ .pet-egg,
+ .pet-egg__glow,
+ .pet-egg-shadow,
+ .pet-wobble,
+ .progress-slide,
+ .kanban-arc
+ ),
+:root[data-renderer-animations-paused] .arc-border::before,
+:root[data-renderer-animations-paused]
+ [data-slot='aui_assistant-message-content']
+ .aui-md
+ [data-slot='code-card'][data-streaming='true'] {
+ animation-play-state: paused !important;
+}
+
/* Sidebar sections: tall viewports give each its own scroller; compact ones
(this variant) flatten everything into one shared scroll. See ChatSidebar. */
@custom-variant compact (@media (max-height: 768px));