From 1ae99cf1fbb92dcdcd85b6fd45c39f3534fa7213 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 15 Aug 2016 08:20:50 +0200 Subject: [PATCH] AutoSave when app looses focus (fixes #5565) --- src/vs/platform/files/common/files.ts | 3 +- .../parts/files/browser/files.contribution.ts | 4 +-- src/vs/workbench/parts/files/common/files.ts | 4 ++- .../parts/files/common/textFileServices.ts | 32 ++++++++++++++++--- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/vs/platform/files/common/files.ts b/src/vs/platform/files/common/files.ts index f536268e652..a4e0a1e12b5 100644 --- a/src/vs/platform/files/common/files.ts +++ b/src/vs/platform/files/common/files.ts @@ -460,7 +460,8 @@ export const MAX_FILE_SIZE = 50 * 1024 * 1024; export const AutoSaveConfiguration = { OFF: 'off', AFTER_DELAY: 'afterDelay', - ON_FOCUS_CHANGE: 'onFocusChange' + ON_FOCUS_CHANGE: 'onFocusChange', + ON_WINDOW_CHANGE: 'onWindowChange' }; export interface IFilesConfiguration { diff --git a/src/vs/workbench/parts/files/browser/files.contribution.ts b/src/vs/workbench/parts/files/browser/files.contribution.ts index 96a7d8af4d1..1463df5a2a3 100644 --- a/src/vs/workbench/parts/files/browser/files.contribution.ts +++ b/src/vs/workbench/parts/files/browser/files.contribution.ts @@ -214,9 +214,9 @@ configurationRegistry.registerConfiguration({ }, 'files.autoSave': { 'type': 'string', - 'enum': [AutoSaveConfiguration.OFF, AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE], + 'enum': [AutoSaveConfiguration.OFF, AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, , AutoSaveConfiguration.ON_WINDOW_CHANGE], 'default': AutoSaveConfiguration.OFF, - 'description': nls.localize('autoSave', "Controls auto save of dirty files. Accepted values: \"{0}\", \"{1}\", \"{2}\". If set to \"{3}\" you can configure the delay in \"files.autoSaveDelay\".", AutoSaveConfiguration.OFF, AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.AFTER_DELAY) + 'description': nls.localize('autoSave', "Controls auto save of dirty files. Accepted values: \"{0}\", \"{1}\", \"{2}\" (editor looses focus), \"{3}\" (window looses focus). If set to \"{4}\" you can configure the delay in \"files.autoSaveDelay\".", AutoSaveConfiguration.OFF, AutoSaveConfiguration.AFTER_DELAY, AutoSaveConfiguration.ON_FOCUS_CHANGE, AutoSaveConfiguration.ON_WINDOW_CHANGE, AutoSaveConfiguration.AFTER_DELAY) }, 'files.autoSaveDelay': { 'type': 'number', diff --git a/src/vs/workbench/parts/files/common/files.ts b/src/vs/workbench/parts/files/common/files.ts index 1d47bd96e8f..67098931fb1 100644 --- a/src/vs/workbench/parts/files/common/files.ts +++ b/src/vs/workbench/parts/files/common/files.ts @@ -239,13 +239,15 @@ export interface IResult { export interface IAutoSaveConfiguration { autoSaveDelay: number; autoSaveFocusChange: boolean; + autoSaveApplicationChange: boolean; } export enum AutoSaveMode { OFF, AFTER_SHORT_DELAY, AFTER_LONG_DELAY, - ON_FOCUS_CHANGE + ON_FOCUS_CHANGE, + ON_WINDOW_CHANGE } export interface IFileEditorDescriptor extends IEditorDescriptor { diff --git a/src/vs/workbench/parts/files/common/textFileServices.ts b/src/vs/workbench/parts/files/common/textFileServices.ts index f89ac960ff7..da59a13e4c0 100644 --- a/src/vs/workbench/parts/files/common/textFileServices.ts +++ b/src/vs/workbench/parts/files/common/textFileServices.ts @@ -38,6 +38,7 @@ export abstract class TextFileService implements ITextFileService { private configuredAutoSaveDelay: number; private configuredAutoSaveOnFocusChange: boolean; + private configuredAutoSaveOnWindowChange: boolean; constructor( @IWorkspaceContextService protected contextService: IWorkspaceContextService, @@ -74,12 +75,19 @@ export abstract class TextFileService implements ITextFileService { // Configuration changes this.listenerToUnbind.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config))); - // Editor focus change - window.addEventListener('blur', () => this.onEditorsChanged(), true); - this.listenerToUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged())); + // Application & Editor focus change + window.addEventListener('blur', () => this.onWindowFocusLost()); + window.addEventListener('blur', () => this.onEditorFocusChanged(), true); + this.listenerToUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorFocusChanged())); } - private onEditorsChanged(): void { + private onWindowFocusLost(): void { + if (this.configuredAutoSaveOnWindowChange && this.getDirty().length) { + this.saveAll().done(null, errors.onUnexpectedError); // save dirty files when we change focus in the editor area + } + } + + private onEditorFocusChanged(): void { if (this.configuredAutoSaveOnFocusChange && this.getDirty().length) { this.saveAll().done(null, errors.onUnexpectedError); // save dirty files when we change focus in the editor area } @@ -93,16 +101,25 @@ export abstract class TextFileService implements ITextFileService { case AutoSaveConfiguration.AFTER_DELAY: this.configuredAutoSaveDelay = configuration && configuration.files && configuration.files.autoSaveDelay; this.configuredAutoSaveOnFocusChange = false; + this.configuredAutoSaveOnWindowChange = false; break; case AutoSaveConfiguration.ON_FOCUS_CHANGE: this.configuredAutoSaveDelay = void 0; this.configuredAutoSaveOnFocusChange = true; + this.configuredAutoSaveOnWindowChange = false; + break; + + case AutoSaveConfiguration.ON_WINDOW_CHANGE: + this.configuredAutoSaveDelay = void 0; + this.configuredAutoSaveOnFocusChange = false; + this.configuredAutoSaveOnWindowChange = true; break; default: this.configuredAutoSaveDelay = void 0; this.configuredAutoSaveOnFocusChange = false; + this.configuredAutoSaveOnWindowChange = false; break; } @@ -231,6 +248,10 @@ export abstract class TextFileService implements ITextFileService { return AutoSaveMode.ON_FOCUS_CHANGE; } + if (this.configuredAutoSaveOnWindowChange) { + return AutoSaveMode.ON_WINDOW_CHANGE; + } + if (this.configuredAutoSaveDelay && this.configuredAutoSaveDelay > 0) { return this.configuredAutoSaveDelay <= 1000 ? AutoSaveMode.AFTER_SHORT_DELAY : AutoSaveMode.AFTER_LONG_DELAY; } @@ -241,7 +262,8 @@ export abstract class TextFileService implements ITextFileService { public getAutoSaveConfiguration(): IAutoSaveConfiguration { return { autoSaveDelay: this.configuredAutoSaveDelay && this.configuredAutoSaveDelay > 0 ? this.configuredAutoSaveDelay : void 0, - autoSaveFocusChange: this.configuredAutoSaveOnFocusChange + autoSaveFocusChange: this.configuredAutoSaveOnFocusChange, + autoSaveApplicationChange: this.configuredAutoSaveOnWindowChange }; }