@@ -14,13 +14,17 @@ import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-
1414import { FrontendApplicationConfig } from '@theia/application-package/lib/application-props' ;
1515import {
1616 ElectronMainApplication as TheiaElectronMainApplication ,
17+ ElectronMainExecutionParams ,
1718 TheiaBrowserWindowOptions ,
1819} from '@theia/core/lib/electron-main/electron-main-application' ;
1920import { SplashServiceImpl } from '../splash/splash-service-impl' ;
2021import { ipcMain } from '@theia/core/shared/electron' ;
22+ import { URI } from '@theia/core/shared/vscode-uri' ;
2123
2224app . commandLine . appendSwitch ( 'disable-http-cache' ) ;
2325
26+ const WORKSPACES = 'workspaces' ;
27+
2428@injectable ( )
2529export class ElectronMainApplication extends TheiaElectronMainApplication {
2630 protected _windows : BrowserWindow [ ] = [ ] ;
@@ -36,6 +40,17 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
3640 return super . start ( config ) ;
3741 }
3842
43+ protected async launch ( params : ElectronMainExecutionParams ) : Promise < void > {
44+ const workspaces : string [ ] | undefined = this . electronStore . get ( WORKSPACES ) ;
45+ if ( workspaces && workspaces . length > 0 ) {
46+ // Setting `secondInstance=true` allows us to pass workspace URIs to the frontend
47+ Object . assign ( params , { secondInstance : true } ) ;
48+ await Promise . all ( workspaces . map ( file => this . handleMainCommand ( params , { file } ) ) ) ;
49+ } else {
50+ super . launch ( params ) ;
51+ }
52+ }
53+
3954 protected getTitleBarStyle ( ) : 'native' | 'custom' {
4055 return 'native' ;
4156 }
@@ -148,6 +163,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
148163 }
149164 }
150165 } ) ;
166+ this . attachClosedWorkspace ( electronWindow ) ;
151167 this . attachReadyToShow ( electronWindow ) ;
152168 this . attachSaveWindowState ( electronWindow ) ;
153169 this . attachGlobalShortcuts ( electronWindow ) ;
@@ -218,6 +234,33 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
218234 }
219235 }
220236
237+ protected closedWorkspaces : { workspace : string , time : number } [ ] = [ ] ;
238+
239+ protected attachClosedWorkspace ( window : BrowserWindow ) : void {
240+ // Since the `before-quit` event is only fired when closing the *last* window
241+ // We need to keep track of recently closed windows/workspaces manually
242+ window . on ( 'close' , ( ) => {
243+ const url = window . webContents . getURL ( ) ;
244+ const workspace = URI . parse ( url ) . fragment ;
245+ if ( workspace ) {
246+ const workspaceUri = URI . file ( workspace ) ;
247+ this . closedWorkspaces . push ( {
248+ workspace : workspaceUri . fsPath ,
249+ time : Date . now ( )
250+ } )
251+ }
252+ } ) ;
253+ }
254+
255+ protected onWillQuit ( event : Electron . Event ) : void {
256+ // Only add workspaces which were closed within the last second (1000 milliseconds)
257+ const threshold = Date . now ( ) - 1000 ;
258+ const workspaces = this . closedWorkspaces . filter ( e => e . time > threshold ) . map ( e => e . workspace ) . sort ( ) ;
259+ this . electronStore . set ( WORKSPACES , workspaces ) ;
260+
261+ super . onWillQuit ( event ) ;
262+ }
263+
221264 get windows ( ) : BrowserWindow [ ] {
222265 return this . _windows . slice ( ) ;
223266 }
0 commit comments