Skip to content

Commit

Permalink
Enable all window restore on quit
Browse files Browse the repository at this point in the history
Part of #13305
  • Loading branch information
Tyriar committed Nov 4, 2016
1 parent 8ed9c07 commit 5bb2cb8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/vs/code/electron-main/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class LifecycleService implements ILifecycleService {
c(true); // veto
});

vscodeWindow.send('vscode:beforeUnload', { okChannel: oneTimeOkEvent, cancelChannel: oneTimeCancelEvent });
vscodeWindow.send('vscode:beforeUnload', { okChannelReply: oneTimeOkEvent, cancelChannelReply: oneTimeCancelEvent, quitRequested: this.quitRequested });
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/vs/platform/lifecycle/common/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleSe
export interface ShutdownEvent {

veto(value: boolean | TPromise<boolean>): void;
quitRequested: boolean;
}

/**
Expand All @@ -38,6 +39,12 @@ export interface ILifecycleService {
*/
willShutdown: boolean;

/**
* A flag indications if the application is in the process of quitting all windows. This will be
* set before the onWillShutdown event is fired and reverted to false afterwards.
*/
quitRequested: boolean;

/**
* Fired before shutdown happens. Allows listeners to veto against the
* shutdown.
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/services/backup/common/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IBackupService {
_serviceBrand: any;

isHotExitEnabled: boolean;
backupBeforeShutdown(dirtyToBackup: Uri[], textFileEditorModelManager: ITextFileEditorModelManager, confirmCallback: () => boolean | TPromise<boolean>): boolean | TPromise<boolean>;
backupBeforeShutdown(dirtyToBackup: Uri[], textFileEditorModelManager: ITextFileEditorModelManager, quitRequested: boolean, confirmCallback: () => boolean | TPromise<boolean>): boolean | TPromise<boolean>;
cleanupBackupsBeforeShutdown(): boolean | TPromise<boolean>;

doBackup(resource: Uri, content: string, immediate?: boolean): TPromise<void>;
Expand Down
7 changes: 4 additions & 3 deletions src/vs/workbench/services/backup/node/backupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ export class BackupService implements IBackupService {
return this.configuredHotExit && this.contextService.getWorkspace() && !platform.isMacintosh;
}

public backupBeforeShutdown(dirtyToBackup: Uri[], textFileEditorModelManager: ITextFileEditorModelManager, confirmCallback: () => boolean | TPromise<boolean>): boolean | TPromise<boolean> {
public backupBeforeShutdown(dirtyToBackup: Uri[], textFileEditorModelManager: ITextFileEditorModelManager, quitRequested: boolean, confirmCallback: () => boolean | TPromise<boolean>): boolean | TPromise<boolean> {
// If there are no dirty files, clean up and exit
if (dirtyToBackup.length === 0) {
return this.cleanupBackupsBeforeShutdown();
}

return this.backupFileService.getWorkspaceBackupPaths().then(workspaceBackupPaths => {
// Only remove the workspace from the backup service if it's not the last one or it's not dirty
if (workspaceBackupPaths.length > 1) {
// Only remove the workspace's backups if it's not the last one or it's not dirty. The one exception to this
// is if quit (all windows) was requested then all windows should be backed up and restored on next launch..
if (!quitRequested && workspaceBackupPaths.length > 1) {
return confirmCallback(); // confirm save
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class LifecycleService implements ILifecycleService {
private _onShutdown = new Emitter<void>();

private _willShutdown: boolean;
private _quitRequested: boolean;

constructor(
@IMessageService private messageService: IMessageService,
Expand All @@ -33,6 +34,10 @@ export class LifecycleService implements ILifecycleService {
return this._willShutdown;
}

public get quitRequested(): boolean {
return this._quitRequested;
}

public get onWillShutdown(): Event<ShutdownEvent> {
return this._onWillShutdown.event;
}
Expand All @@ -45,29 +50,32 @@ export class LifecycleService implements ILifecycleService {
const windowId = this.windowService.getWindowId();

// Main side indicates that window is about to unload, check for vetos
ipc.on('vscode:beforeUnload', (event, reply: { okChannel: string, cancelChannel: string }) => {
ipc.on('vscode:beforeUnload', (event, args: { okChannelReply: string, cancelChannelReply: string, quitRequested: boolean }) => {
this._willShutdown = true;
this._quitRequested = args.quitRequested;

// trigger onWillShutdown events and veto collecting
this.onBeforeUnload().done(veto => {
this.onBeforeUnload(args.quitRequested).done(veto => {
this._quitRequested = false;
if (veto) {
this._willShutdown = false; // reset this flag since the shutdown has been vetoed!
ipc.send(reply.cancelChannel, windowId);
ipc.send(args.cancelChannelReply, windowId);
} else {
this._onShutdown.fire();
ipc.send(reply.okChannel, windowId);
ipc.send(args.okChannelReply, windowId);
}
});
});
}

private onBeforeUnload(): TPromise<boolean> {
private onBeforeUnload(quitRequested: boolean): TPromise<boolean> {
const vetos: (boolean | TPromise<boolean>)[] = [];

this._onWillShutdown.fire({
veto(value) {
vetos.push(value);
}
},
quitRequested
});

if (vetos.length === 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/services/textfile/browser/textFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export abstract class TextFileService implements ITextFileService {
private registerListeners(): void {

// Lifecycle
this.lifecycleService.onWillShutdown(event => event.veto(this.beforeShutdown()));
this.lifecycleService.onWillShutdown(event => event.veto(this.beforeShutdown(event.quitRequested)));
this.lifecycleService.onShutdown(this.dispose, this);

// Configuration changes
Expand All @@ -113,9 +113,9 @@ export abstract class TextFileService implements ITextFileService {
this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorFocusChanged()));
}

private beforeShutdown(): boolean | TPromise<boolean> {
private beforeShutdown(quitRequested: boolean): boolean | TPromise<boolean> {
if (this.backupService.isHotExitEnabled) {
return this.backupService.backupBeforeShutdown(this.getDirty(), this.models, this.confirmBeforeShutdown.bind(this));
return this.backupService.backupBeforeShutdown(this.getDirty(), this.models, quitRequested, this.confirmBeforeShutdown.bind(this));
}

// Dirty files need treatment on shutdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ServiceAccessor {
class ShutdownEventImpl implements ShutdownEvent {

public value: boolean | TPromise<boolean>;
public quitRequested: boolean = false;

veto(value: boolean | TPromise<boolean>): void {
this.value = value;
Expand Down

0 comments on commit 5bb2cb8

Please sign in to comment.