Skip to content

The agent that grows with you

fix(desktop): keep pets and starmap animated without focus

brooklyn!9e8c1569b6d6
diff --git a/apps/desktop/src/app/starmap/star-map.tsx b/apps/desktop/src/app/starmap/star-map.tsx
index 2a23bc2ef5b..73b070e159f 100644
--- a/apps/desktop/src/app/starmap/star-map.tsx
+++ b/apps/desktop/src/app/starmap/star-map.tsx
@@ -3,6 +3,7 @@ import { atom, type WritableAtom } from 'nanostores'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useThemeEpoch } from '@/hooks/use-theme-epoch'
+import { createRendererLoopPauseController } from '@/lib/renderer-loop-pause'
import { createDoubleTapDetector, isSmartZoomWheel } from '@/lib/trackpad-gestures'
import type { StarmapGraph } from '@/types/hermes'
@@ -496,7 +497,7 @@ export function StarMap({
}, [invalidate, themeEpoch])
// Render loop. The core scramble animates continuously, so the loop runs while
- // the window is focused — but each frame is cheap (live scramble + a blit of the
+ // the window is visible — but each frame is cheap (live scramble + a blit of the
// cached static layer). The expensive scene only re-renders when invalidate()
// marks it dirty. Capped to ~30fps; interaction (force) bypasses the cap.
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
@@ -506,16 +507,8 @@ export function StarMap({
let lastAnimTs = 0
let force = true
- // The scramble keeps the loop perpetually "animating", so a fully-built,
- // untouched map still repaints 30×/s for as long as the panel is open. That's
- // wasted CPU/GPU (WindowServer compositing) when the window isn't even the one
- // you're looking at. Freeze the loop while the window is hidden or unfocused;
- // a frozen core next to other work is fine, and it resumes instantly on focus.
- const isPaused = () =>
- (typeof document !== 'undefined' && document.hidden) ||
- (typeof document.hasFocus === 'function' && !document.hasFocus())
-
- let paused = isPaused()
+ let paused = false
+ let pauseController: ReturnType<typeof createRendererLoopPauseController>
const schedule = () => {
if (!paused && !raf) {
@@ -641,10 +634,10 @@ export function StarMap({
schedule()
}
- // Suspend the loop when the window drops out of view/focus; wake + force a
+ // Suspend the loop when the window drops out of view; wake + force a
// fresh frame the moment it returns so the resume is seamless.
const onActivity = () => {
- const next = isPaused()
+ const next = pauseController.isPaused()
if (next === paused) {
return
@@ -664,17 +657,14 @@ export function StarMap({
}
}
- document.addEventListener('visibilitychange', onActivity)
- window.addEventListener('blur', onActivity)
- window.addEventListener('focus', onActivity)
+ pauseController = createRendererLoopPauseController(onActivity)
+ paused = pauseController.isPaused()
schedule()
return () => {
cancelAnimationFrame(raf)
- document.removeEventListener('visibilitychange', onActivity)
- window.removeEventListener('blur', onActivity)
- window.removeEventListener('focus', onActivity)
+ pauseController.dispose()
invalidateRef.current = () => {}
}
diff --git a/apps/desktop/src/components/pet/pet-sprite.test.tsx b/apps/desktop/src/components/pet/pet-sprite.test.tsx
index 719269bf18f..5840201f54f 100644
--- a/apps/desktop/src/components/pet/pet-sprite.test.tsx
+++ b/apps/desktop/src/components/pet/pet-sprite.test.tsx
@@ -172,13 +172,13 @@ describe('PetSprite RAF scheduling', () => {
expect(raf.request).toHaveBeenCalledTimes(2)
})
- it('suspends while unfocused, resumes on focus, and leaves no work after unmount', () => {
+ it('keeps animating while unfocused and leaves no work after unmount', () => {
const raf = installRaf()
mount.render(<PetSprite info={INFO} />)
act(() => window.dispatchEvent(new Event('blur')))
- expect(raf.pending()).toBe(0)
+ expect(raf.pending()).toBe(1)
act(() => window.dispatchEvent(new Event('focus')))
expect(raf.pending()).toBe(1)
diff --git a/apps/desktop/src/components/pet/pet-sprite.tsx b/apps/desktop/src/components/pet/pet-sprite.tsx
index 350a52752df..cd3436e3cc2 100644
--- a/apps/desktop/src/components/pet/pet-sprite.tsx
+++ b/apps/desktop/src/components/pet/pet-sprite.tsx
@@ -133,7 +133,7 @@ export function roamWalkRow(dir: -1 | 0 | 1, stateRows?: string[]): { row?: stri
interface PetSpriteProps {
info: PetInfo
- /** Keep animating in a deliberately non-activating visible window, such as the pop-out pet overlay. */
+ /** Opt in to pausing on blur; visible pets animate by default. */
pauseWhenUnfocused?: boolean
/** On-screen scale multiplier applied on top of the pet's native scale. */
zoom?: number
@@ -158,7 +158,7 @@ interface PetSpriteProps {
* with `memo`, this component effectively never re-renders after mount until
* the pet itself changes.
*/
-function PetSpriteImpl({ info, zoom = 1, stateOverride, rowOverride, pauseWhenUnfocused = true }: PetSpriteProps) {
+function PetSpriteImpl({ info, zoom = 1, stateOverride, rowOverride, pauseWhenUnfocused = false }: PetSpriteProps) {
const canvasRef = useRef<HTMLCanvasElement | null>(null)
const stateRef = useRef<PetState>($petState.get())
const overrideRef = useRef<PetState | undefined>(stateOverride)