AutoSave when app looses focus (fixes #5565)

This commit is contained in:
Benjamin Pasero 2016-08-15 08:20:50 +02:00
parent 2e54e84905
commit 1ae99cf1fb
4 changed files with 34 additions and 9 deletions

View file

@ -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 {

View file

@ -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',

View file

@ -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 {

View file

@ -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
};
}