@@ -674,6 +674,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
674674 const frameCaptureSessionsRef = yield * SynchronizedRef . make <
675675 ReadonlyMap < string , FrameCaptureSession >
676676 > ( new Map ( ) ) ;
677+ // capturePage calls in flight. Like frame capture sessions, each keeps the main window unthrottled.
678+ const paintingCapturesRef = yield * Ref . make ( 0 ) ;
677679 const pictureInPictureSessionsRef = yield * SynchronizedRef . make <
678680 ReadonlyMap < string , PictureInPictureSession >
679681 > ( new Map ( ) ) ;
@@ -735,12 +737,19 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
735737 yield * requireCurrentGuest ;
736738 return image ;
737739 } ) ;
738- return yield * capture . pipe (
739- Effect . retry ( {
740- times : CAPTURE_PAGE_RETRY_ATTEMPTS - 1 ,
741- schedule : Schedule . spaced ( CAPTURE_PAGE_RETRY_DELAY_MS ) ,
742- while : isPreviewOperationError ,
743- } ) ,
740+ // capturePage waits for a window frame that contains the guest. A throttled main window
741+ // that is covered, minimized, or on another Space draws none, so the capture never settles.
742+ return yield * Effect . acquireUseRelease (
743+ holdMainWindowPainting ,
744+ ( ) =>
745+ capture . pipe (
746+ Effect . retry ( {
747+ times : CAPTURE_PAGE_RETRY_ATTEMPTS - 1 ,
748+ schedule : Schedule . spaced ( CAPTURE_PAGE_RETRY_DELAY_MS ) ,
749+ while : isPreviewOperationError ,
750+ } ) ,
751+ ) ,
752+ ( ) => releaseMainWindowPainting ,
744753 ) ;
745754 } ) ;
746755 const currentIso = DateTime . now . pipe ( Effect . map ( DateTime . formatIso ) ) ;
@@ -795,6 +804,34 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
795804 if ( Option . isNone ( mainWindow ) ) return ;
796805 yield * setWindowBackgroundThrottling ( mainWindow . value , enabled ) ;
797806 } ) ;
807+ /** Re-throttles the main window once no frame capture session or capturePage call needs frames. */
808+ const throttleMainWindowWhenIdle = Effect . fnUntraced ( function * ( remainingSessions : number ) {
809+ if ( remainingSessions > 0 || ( yield * Ref . get ( paintingCapturesRef ) ) > 0 ) return ;
810+ yield * setFrameCaptureBackgroundThrottling ( true ) . pipe (
811+ Effect . retry ( { times : 2 } ) ,
812+ Effect . catch ( ( error ) =>
813+ Effect . logWarning ( "Failed to restore preview frame capture throttling." , { error } ) ,
814+ ) ,
815+ ) ;
816+ } ) ;
817+ // Both run under the frame capture lock so a session that starts or stops mid-capture sees the count.
818+ const holdMainWindowPainting = SynchronizedRef . modifyEffect ( frameCaptureSessionsRef , ( sessions ) =>
819+ Effect . gen ( function * ( ) {
820+ const held = yield * Ref . getAndUpdate ( paintingCapturesRef , ( count ) => count + 1 ) ;
821+ if ( held === 0 && sessions . size === 0 ) {
822+ yield * setFrameCaptureBackgroundThrottling ( false ) . pipe ( Effect . ignore ) ;
823+ }
824+ return [ undefined , sessions ] as const ;
825+ } ) ,
826+ ) ;
827+ const releaseMainWindowPainting = SynchronizedRef . modifyEffect (
828+ frameCaptureSessionsRef ,
829+ ( sessions ) =>
830+ Ref . update ( paintingCapturesRef , ( count ) => count - 1 ) . pipe (
831+ Effect . andThen ( throttleMainWindowWhenIdle ( sessions . size ) ) ,
832+ Effect . as ( [ undefined , sessions ] as const ) ,
833+ ) ,
834+ ) ;
798835 const setFrameCaptureWebContentsBackgroundThrottling = Effect . fnUntraced ( function * (
799836 wc : Electron . WebContents ,
800837 enabled : boolean ,
@@ -909,14 +946,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
909946 yield * restoreFrameCaptureWebContentsBackgroundThrottling (
910947 current . unthrottledWebContentsIds ,
911948 ) ;
912- if ( remainingSessions . size === 0 ) {
913- yield * setFrameCaptureBackgroundThrottling ( true ) . pipe (
914- Effect . retry ( { times : 2 } ) ,
915- Effect . catch ( ( error ) =>
916- Effect . logWarning ( "Failed to restore preview frame capture throttling." , { error } ) ,
917- ) ,
918- ) ;
919- }
949+ yield * throttleMainWindowWhenIdle ( remainingSessions . size ) ;
920950 return [ current . scope , remainingSessions ] as const ;
921951 } ) ,
922952 ) . pipe (
@@ -2079,7 +2109,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20792109 }
20802110 yield * SynchronizedRef . modifyEffect ( frameCaptureSessionsRef , ( sessions ) =>
20812111 Effect . gen ( function * ( ) {
2082- if ( sessions . size > 0 ) {
2112+ if ( sessions . size > 0 || ( yield * Ref . get ( paintingCapturesRef ) ) > 0 ) {
20832113 yield * setWindowBackgroundThrottling ( window , false ) ;
20842114 }
20852115 yield * Ref . set ( mainWindowRef , Option . some ( window ) ) ;
@@ -3090,11 +3120,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
30903120 yield * setFrameCaptureBackgroundThrottling ( false ) ;
30913121 }
30923122 yield * setFrameCaptureWebContentsBackgroundThrottling ( wc , false ) . pipe (
3093- Effect . onError ( ( ) =>
3094- sessions . size === 0
3095- ? setFrameCaptureBackgroundThrottling ( true ) . pipe ( Effect . ignore )
3096- : Effect . void ,
3097- ) ,
3123+ Effect . onError ( ( ) => throttleMainWindowWhenIdle ( sessions . size ) ) ,
30983124 ) ;
30993125 const scope =
31003126 consumer === "picture-in-picture" ? yield * Scope . fork ( parentScope , "sequential" ) : null ;
0 commit comments