Skip to content

Commit 7371cfd

Browse files
committed
add ILifecycleService#startupKind
1 parent 3d1a0cc commit 7371cfd

5 files changed

Lines changed: 64 additions & 26 deletions

File tree

src/vs/code/electron-main/lifecycle.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
1717
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
1818

1919
export enum UnloadReason {
20-
CLOSE,
21-
QUIT,
22-
RELOAD,
23-
LOAD
20+
CLOSE = 1,
21+
QUIT = 2,
22+
RELOAD = 3,
23+
LOAD = 4
2424
}
2525

2626
export interface ILifecycleService {
@@ -264,4 +264,4 @@ export class LifecycleService implements ILifecycleService {
264264
public isQuitRequested(): boolean {
265265
return !!this.quitRequested;
266266
}
267-
}
267+
}

src/vs/platform/lifecycle/common/lifecycle.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ export interface ShutdownEvent {
2626
export enum ShutdownReason {
2727

2828
/** Window is closed */
29-
CLOSE,
29+
CLOSE = 1,
3030

3131
/** Application is quit */
32-
QUIT,
32+
QUIT = 2,
3333

3434
/** Window is reloaded */
35-
RELOAD,
35+
RELOAD = 3,
3636

3737
/** Other configuration loaded into window */
38-
LOAD
38+
LOAD = 4
39+
}
40+
41+
export enum StartupKind {
42+
NewWindow = 1,
43+
ReloadedWindow = 3,
44+
ReopenedWindow = 4,
3945
}
4046

4147
/**
@@ -46,30 +52,36 @@ export interface ILifecycleService {
4652

4753
_serviceBrand: any;
4854

55+
/**
56+
* Value indicates how this window got loaded.
57+
*/
58+
readonly startupKind: StartupKind;
59+
4960
/**
5061
* A flag indicating if the application is in the process of shutting down. This will be true
5162
* before the onWillShutdown event is fired and false if the shutdown is being vetoed.
5263
*/
53-
willShutdown: boolean;
64+
readonly willShutdown: boolean;
5465

5566
/**
5667
* Fired before shutdown happens. Allows listeners to veto against the
5768
* shutdown.
5869
*/
59-
onWillShutdown: Event<ShutdownEvent>;
70+
readonly onWillShutdown: Event<ShutdownEvent>;
6071

6172
/**
6273
* Fired when no client is preventing the shutdown from happening. Can be used to dispose heavy resources
6374
* like running processes. Can also be used to save UI state to storage.
6475
*
6576
* The event carries a shutdown reason that indicates how the shutdown was triggered.
6677
*/
67-
onShutdown: Event<ShutdownReason>;
78+
readonly onShutdown: Event<ShutdownReason>;
6879
}
6980

7081
export const NullLifecycleService: ILifecycleService = {
7182
_serviceBrand: null,
83+
startupKind: StartupKind.NewWindow,
7284
willShutdown: false,
7385
onWillShutdown: () => ({ dispose() { } }),
7486
onShutdown: (reason) => ({ dispose() { } })
75-
};
87+
};

src/vs/workbench/electron-browser/shell.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class WorkbenchShell {
132132
private windowIPCService: IWindowIPCService;
133133
private timerService: ITimerService;
134134
private themeService: WorkbenchThemeService;
135+
private lifecycleService: ILifecycleService;
135136

136137
private container: HTMLElement;
137138
private toUnbind: IDisposable[];
@@ -237,7 +238,8 @@ export class WorkbenchShell {
237238
experiments: this.telemetryService.getExperiments(),
238239
pinnedViewlets: info.pinnedViewlets,
239240
restoredViewlet: info.restoredViewlet,
240-
restoredEditors: info.restoredEditors.length
241+
restoredEditors: info.restoredEditors.length,
242+
startupKind: this.lifecycleService.startupKind
241243
});
242244

243245
// Telemetry: startup metrics
@@ -357,6 +359,7 @@ export class WorkbenchShell {
357359
this.toUnbind.push(lifecycleService.onShutdown(reason => saveFontInfo(this.storageService)));
358360
serviceCollection.set(ILifecycleService, lifecycleService);
359361
disposables.add(lifecycleTelemetry(this.telemetryService, lifecycleService));
362+
this.lifecycleService = lifecycleService;
360363

361364
const extensionManagementChannel = getDelayedChannel<IExtensionManagementChannel>(sharedProcess.then(c => c.getChannel('extensions')));
362365
serviceCollection.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementChannelClient, extensionManagementChannel));

src/vs/workbench/services/lifecycle/electron-browser/lifecycleService.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,46 @@
77
import { TPromise } from 'vs/base/common/winjs.base';
88
import Severity from 'vs/base/common/severity';
99
import { toErrorMessage } from 'vs/base/common/errorMessage';
10-
import { ILifecycleService, ShutdownEvent, ShutdownReason } from 'vs/platform/lifecycle/common/lifecycle';
10+
import { ILifecycleService, ShutdownEvent, ShutdownReason, StartupKind } from 'vs/platform/lifecycle/common/lifecycle';
1111
import { IMessageService } from 'vs/platform/message/common/message';
12+
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
1213
import { IWindowIPCService } from 'vs/workbench/services/window/electron-browser/windowService';
1314
import { ipcRenderer as ipc } from 'electron';
1415
import Event, { Emitter } from 'vs/base/common/event';
1516

1617
export class LifecycleService implements ILifecycleService {
1718

19+
private static readonly _lastShutdownReasonKey = 'lifecyle.lastShutdownReason';
20+
1821
public _serviceBrand: any;
1922

20-
private _onWillShutdown = new Emitter<ShutdownEvent>();
21-
private _onShutdown = new Emitter<ShutdownReason>();
23+
private readonly _onWillShutdown = new Emitter<ShutdownEvent>();
24+
private readonly _onShutdown = new Emitter<ShutdownReason>();
25+
private readonly _startupKind: StartupKind;
2226

2327
private _willShutdown: boolean;
2428

2529
constructor(
26-
@IMessageService private messageService: IMessageService,
27-
@IWindowIPCService private windowService: IWindowIPCService
30+
@IMessageService private _messageService: IMessageService,
31+
@IWindowIPCService private _windowService: IWindowIPCService,
32+
@IStorageService private _storageService: IStorageService
2833
) {
29-
this.registerListeners();
34+
this._registerListeners();
35+
36+
const lastShutdownReason = this._storageService.getInteger(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
37+
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
38+
if (lastShutdownReason === ShutdownReason.RELOAD) {
39+
this._startupKind = StartupKind.ReloadedWindow;
40+
} else if (lastShutdownReason === ShutdownReason.LOAD) {
41+
this._startupKind = StartupKind.ReopenedWindow;
42+
} else {
43+
this._startupKind = StartupKind.NewWindow;
44+
}
45+
console.log(this.startupKind);
46+
}
47+
48+
public get startupKind(): StartupKind {
49+
return this._startupKind;
3050
}
3151

3252
public get willShutdown(): boolean {
@@ -41,16 +61,18 @@ export class LifecycleService implements ILifecycleService {
4161
return this._onShutdown.event;
4262
}
4363

44-
private registerListeners(): void {
45-
const windowId = this.windowService.getWindowId();
64+
private _registerListeners(): void {
65+
const windowId = this._windowService.getWindowId();
4666

4767
// Main side indicates that window is about to unload, check for vetos
4868
ipc.on('vscode:beforeUnload', (event, reply: { okChannel: string, cancelChannel: string, reason: ShutdownReason }) => {
4969
this._willShutdown = true;
70+
this._storageService.store(LifecycleService._lastShutdownReasonKey, JSON.stringify(reply.reason), StorageScope.WORKSPACE);
5071

5172
// trigger onWillShutdown events and veto collecting
5273
this.onBeforeUnload(reply.reason).done(veto => {
5374
if (veto) {
75+
this._storageService.remove(LifecycleService._lastShutdownReasonKey, StorageScope.WORKSPACE);
5476
this._willShutdown = false; // reset this flag since the shutdown has been vetoed!
5577
ipc.send(reply.cancelChannel, windowId);
5678
} else {
@@ -92,11 +114,11 @@ export class LifecycleService implements ILifecycleService {
92114
}
93115
}, err => {
94116
// error, treated like a veto, done
95-
this.messageService.show(Severity.Error, toErrorMessage(err));
117+
this._messageService.show(Severity.Error, toErrorMessage(err));
96118
lazyValue = true;
97119
}));
98120
}
99121
}
100122
return TPromise.join(promises).then(() => lazyValue);
101123
}
102-
}
124+
}

src/vs/workbench/test/workbenchTestServices.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { IEditorInput, IEditorOptions, Position, Direction, IEditor, IResourceIn
2828
import { IUntitledEditorService, UntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
2929
import { IMessageService, IConfirmation } from 'vs/platform/message/common/message';
3030
import { IWorkspace, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
31-
import { ILifecycleService, ShutdownEvent, ShutdownReason } from 'vs/platform/lifecycle/common/lifecycle';
31+
import { ILifecycleService, ShutdownEvent, ShutdownReason, StartupKind } from 'vs/platform/lifecycle/common/lifecycle';
3232
import { EditorStacksModel } from 'vs/workbench/common/editor/editorStacksModel';
3333
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
3434
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
@@ -855,6 +855,7 @@ export class TestLifecycleService implements ILifecycleService {
855855
public _serviceBrand: any;
856856

857857
public willShutdown: boolean;
858+
public startupKind: StartupKind;
858859

859860
private _onWillShutdown = new Emitter<ShutdownEvent>();
860861
private _onShutdown = new Emitter<ShutdownReason>();
@@ -1023,4 +1024,4 @@ export class TestThemeService implements IThemeService {
10231024
onThemeChange(participant: IThemingParticipant): IDisposable {
10241025
return { dispose: () => { } };
10251026
}
1026-
}
1027+
}

0 commit comments

Comments
 (0)