@@ -14,13 +14,17 @@ import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-
14
14
import { FrontendApplicationConfig } from '@theia/application-package/lib/application-props' ;
15
15
import {
16
16
ElectronMainApplication as TheiaElectronMainApplication ,
17
+ ElectronMainExecutionParams ,
17
18
TheiaBrowserWindowOptions ,
18
19
} from '@theia/core/lib/electron-main/electron-main-application' ;
19
20
import { SplashServiceImpl } from '../splash/splash-service-impl' ;
20
21
import { ipcMain } from '@theia/core/shared/electron' ;
22
+ import { URI } from '@theia/core/shared/vscode-uri' ;
21
23
22
24
app . commandLine . appendSwitch ( 'disable-http-cache' ) ;
23
25
26
+ const WORKSPACES = 'workspaces' ;
27
+
24
28
@injectable ( )
25
29
export class ElectronMainApplication extends TheiaElectronMainApplication {
26
30
protected _windows : BrowserWindow [ ] = [ ] ;
@@ -36,6 +40,17 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
36
40
return super . start ( config ) ;
37
41
}
38
42
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
+
39
54
protected getTitleBarStyle ( ) : 'native' | 'custom' {
40
55
return 'native' ;
41
56
}
@@ -148,6 +163,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
148
163
}
149
164
}
150
165
} ) ;
166
+ this . attachClosedWorkspace ( electronWindow ) ;
151
167
this . attachReadyToShow ( electronWindow ) ;
152
168
this . attachSaveWindowState ( electronWindow ) ;
153
169
this . attachGlobalShortcuts ( electronWindow ) ;
@@ -218,6 +234,33 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
218
234
}
219
235
}
220
236
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
+
221
264
get windows ( ) : BrowserWindow [ ] {
222
265
return this . _windows . slice ( ) ;
223
266
}
0 commit comments