@@ -628,31 +628,34 @@ else {
628628 return focusDocumentWindow ( target ) ;
629629 }
630630 const openingDocuments = new Map ( ) ;
631- async function openDocumentWindow ( document , preferredWindow , restoreView ) {
631+ async function openDocumentWindow ( document , preferredWindow , restoreView , activate = true ) {
632632 const started = performance . now ( ) ;
633633 try {
634- return await deliverDocumentWindow ( document , preferredWindow , restoreView ) ;
634+ return await deliverDocumentWindow ( document , preferredWindow , restoreView , activate ) ;
635635 } finally {
636636 developerOperationMetrics . fileOpen ( performance . now ( ) - started ) ;
637637 }
638638 }
639- async function deliverDocumentWindow ( document , preferredWindow , restoreView ) {
639+ async function deliverDocumentWindow ( document , preferredWindow , restoreView , activate ) {
640640 const identity = typeof document === 'string' ? documentIdentity ( document ) : null ;
641641 const existing = findDocumentWindow ( identity ) ;
642- if ( existing ) return focusDocumentWindow ( existing ) ;
643- if ( identity && openingDocuments . has ( identity ) )
644- return focusDocumentWindow ( await openingDocuments . get ( identity ) ) ;
642+ if ( existing ) return activate ? focusDocumentWindow ( existing ) : existing ;
643+ if ( identity && openingDocuments . has ( identity ) ) {
644+ const target = await openingDocuments . get ( identity ) ;
645+ return activate ? focusDocumentWindow ( target ) : target ;
646+ }
645647 const operation = ( async ( ) => {
646648 const candidates = [ preferredWindow , focusedWindow ( ) , ...windows . keys ( ) ] ;
647649 const target = candidates . find ( isBlankStartPage ) ;
648- if ( ! target ) return createWindow ( document , restoreView ) ;
650+ if ( ! target ) return createWindow ( document , restoreView , null , 'general' , activate ) ;
649651 const state = windows . get ( target ) ;
650652 state . documentIdentity = identity ;
653+ state . restoreView = restoreView ;
651654 state . documents . push ( document ) ;
652655 state . unkeyedAnnotationSource = annotationSourceForDocument ( document ) ;
653656 state . performance . hasDocument = true ;
654657 target . webContents . send ( 'documents:available' ) ;
655- return focusDocumentWindow ( target ) ;
658+ return activate ? focusDocumentWindow ( target ) : target ;
656659 } ) ( ) ;
657660 if ( identity ) openingDocuments . set ( identity , operation ) ;
658661 try {
@@ -666,6 +669,7 @@ else {
666669 restoreView ,
667670 settingsOwner = null ,
668671 settingsSection = 'general' ,
672+ activate = true ,
669673 ) {
670674 let window ;
671675 const backend = settingsOwner
@@ -912,7 +916,7 @@ else {
912916 const showLoadedWindow = ( ) => {
913917 hideNativeMenuBar ( window ) ;
914918 if ( ! backgroundRenderSmoke && ! window . isVisible ( ) ) {
915- if ( process . argv . includes ( '--background' ) ) window . showInactive ( ) ;
919+ if ( ! activate || process . argv . includes ( '--background' ) ) window . showInactive ( ) ;
916920 else window . show ( ) ;
917921 }
918922 updatePerformanceSampling ( window ) ;
@@ -2337,22 +2341,32 @@ else {
23372341 trustedWindow ( event ) ;
23382342 await createWindow ( ) ;
23392343 } ) ;
2344+ // Drain external launch requests before restoring the previous session. Requests
2345+ // arriving while a backend starts also take priority over the next restore.
2346+ let externalLaunch = false ;
2347+ const openLaunchFiles = async ( ) => {
2348+ if ( ciLaunchCheck || ! pendingFiles . length ) return ;
2349+ externalLaunch = true ;
2350+ await deliverPendingFiles ( ) ;
2351+ } ;
2352+ await openLaunchFiles ( ) ;
23402353 if ( ! smoke && ! ciLaunchCheck && preferences . load ( ) . restoreDocuments ) {
23412354 for ( const document of documentSession . restore ( ) ) {
2355+ await openLaunchFiles ( ) ;
23422356 try {
23432357 await validateSystemPDF ( document . path ) ;
23442358 } catch {
23452359 continue ;
23462360 }
2347- if ( ! pendingFiles . includes ( document . path ) )
2348- await openDocumentWindow ( document . path , null , document . view ) ;
2361+ await openLaunchFiles ( ) ;
2362+ const firstRestore = windows . size === 0 ;
2363+ const restored = await openDocumentWindow ( document . path , null , document . view , false ) ;
2364+ await openLaunchFiles ( ) ;
2365+ if ( firstRestore && ! externalLaunch ) focusDocumentWindow ( restored ) ;
23492366 }
23502367 }
2351- window =
2352- [ ...windows . keys ( ) ] [ 0 ] ||
2353- ( ! ciLaunchCheck && pendingFiles . length
2354- ? await openDocumentWindow ( pendingFiles . shift ( ) )
2355- : await createWindow ( ) ) ;
2368+ await openLaunchFiles ( ) ;
2369+ window = [ ...windows . keys ( ) ] [ 0 ] || ( await createWindow ( ) ) ;
23562370 backend = windows . get ( window ) . backend ;
23572371 documentsReady = true ;
23582372 if ( ! ciLaunchCheck ) await deliverPendingFiles ( ) ;
0 commit comments