support auto save on focus lost for all editors (#84672)

This commit is contained in:
Benjamin Pasero
2019-11-21 07:14:26 +01:00
parent d9c4923f98
commit a825aaaa90
5 changed files with 31 additions and 35 deletions

View File

@@ -31,11 +31,11 @@ import { find } from 'vs/base/common/arrays';
*/
export abstract class Composite extends Component implements IComposite {
private readonly _onTitleAreaUpdate: Emitter<void> = this._register(new Emitter<void>());
readonly onTitleAreaUpdate: Event<void> = this._onTitleAreaUpdate.event;
private readonly _onTitleAreaUpdate = this._register(new Emitter<void>());
readonly onTitleAreaUpdate = this._onTitleAreaUpdate.event;
private readonly _onDidChangeVisibility: Emitter<boolean> = this._register(new Emitter<boolean>());
readonly onDidChangeVisibility: Event<boolean> = this._onDidChangeVisibility.event;
private readonly _onDidChangeVisibility = this._register(new Emitter<boolean>());
readonly onDidChangeVisibility = this._onDidChangeVisibility.event;
private _onDidFocus: Emitter<void> | undefined;
get onDidFocus(): Event<void> {
@@ -250,11 +250,11 @@ export abstract class CompositeDescriptor<T extends Composite> {
export abstract class CompositeRegistry<T extends Composite> extends Disposable {
private readonly _onDidRegister: Emitter<CompositeDescriptor<T>> = this._register(new Emitter<CompositeDescriptor<T>>());
get onDidRegister(): Event<CompositeDescriptor<T>> { return this._onDidRegister.event; }
private readonly _onDidRegister = this._register(new Emitter<CompositeDescriptor<T>>());
readonly onDidRegister = this._onDidRegister.event;
private readonly _onDidDeregister: Emitter<CompositeDescriptor<T>> = this._register(new Emitter<CompositeDescriptor<T>>());
get onDidDeregister(): Event<CompositeDescriptor<T>> { return this._onDidDeregister.event; }
private readonly _onDidDeregister = this._register(new Emitter<CompositeDescriptor<T>>());
readonly onDidDeregister = this._onDidDeregister.event;
private composites: CompositeDescriptor<T>[] = [];

View File

@@ -9,7 +9,6 @@ import { IFilesConfigurationService, AutoSaveMode } from 'vs/workbench/services/
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { SaveReason, IEditorIdentifier, IEditorInput, GroupIdentifier } from 'vs/workbench/common/editor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ICodeEditor, isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { withNullAsUndefined } from 'vs/base/common/types';
@@ -57,19 +56,11 @@ export class EditorAutoSave extends Disposable implements IWorkbenchContribution
this.lastActiveEditorControlDisposable.clear();
// Listen to focus changes on control for auto save
if (activeGroup && activeEditor) {
const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
const controlsToObserve: ICodeEditor[] = [];
if (isCodeEditor(activeTextEditorWidget)) {
controlsToObserve.push(activeTextEditorWidget);
} else if (isDiffEditor(activeTextEditorWidget)) {
controlsToObserve.push(activeTextEditorWidget.getOriginalEditor(), activeTextEditorWidget.getModifiedEditor());
}
controlsToObserve.forEach(control => this.lastActiveEditorControlDisposable.add(control.onDidBlurEditorWidget(() => {
const activeEditorControl = this.editorService.activeControl;
if (activeEditor && activeEditorControl) {
this.lastActiveEditorControlDisposable.add(activeEditorControl.onDidBlur(() => {
this.maybeTriggerAutoSave(SaveReason.FOCUS_CHANGE, { groupId: activeGroup.id, editor: activeEditor });
})));
}));
}
}
@@ -78,10 +69,9 @@ export class EditorAutoSave extends Disposable implements IWorkbenchContribution
return; // no auto save for readonly or untitled editors
}
// Determine if we need to save all. In case of a window focus change we also save if 
// auto save mode is configured to be ON_FOCUS_CHANGE (editor focus change)
const mode = this.filesConfigurationService.getAutoSaveMode();
// Determine if we need to save all. In case of a window focus change we also save if auto save mode
// is configured to be ON_FOCUS_CHANGE (editor focus change)
if (
(reason === SaveReason.WINDOW_CHANGE && (mode === AutoSaveMode.ON_FOCUS_CHANGE || mode === AutoSaveMode.ON_WINDOW_CHANGE)) ||
(reason === SaveReason.FOCUS_CHANGE && mode === AutoSaveMode.ON_FOCUS_CHANGE)

View File

@@ -4,9 +4,20 @@
*--------------------------------------------------------------------------------------------*/
import { IAction, IActionViewItem } from 'vs/base/common/actions';
import { Event } from 'vs/base/common/event';
export interface IComposite {
/**
* An event when the composite gained focus.
*/
readonly onDidFocus: Event<void>;
/**
* An event when the composite lost focus.
*/
readonly onDidBlur: Event<void>;
/**
* Returns the unique identifier of this composite.
*/

View File

@@ -23,6 +23,7 @@ import { coalesce, firstOrDefault } from 'vs/base/common/arrays';
import { ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { isEqual } from 'vs/base/common/resources';
import { IPanel } from 'vs/workbench/common/panel';
export const DirtyWorkingCopiesContext = new RawContextKey<boolean>('dirtyWorkingCopies', false);
export const ActiveEditorContext = new RawContextKey<string | null>('activeEditor', null);
@@ -54,7 +55,7 @@ export const TEXT_DIFF_EDITOR_ID = 'workbench.editors.textDiffEditor';
*/
export const BINARY_DIFF_EDITOR_ID = 'workbench.editors.binaryResourceDiffEditor';
export interface IEditor {
export interface IEditor extends IPanel {
/**
* The assigned input of this editor.
@@ -96,21 +97,11 @@ export interface IEditor {
*/
readonly onDidSizeConstraintsChange: Event<{ width: number; height: number; } | undefined>;
/**
* Returns the unique identifier of this editor.
*/
getId(): string;
/**
* Returns the underlying control of this editor.
*/
getControl(): IEditorControl | undefined;
/**
* Asks the underlying control to focus.
*/
focus(): void;
/**
* Finds out if this editor is visible or not.
*/

View File

@@ -11,11 +11,15 @@ import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IViewlet } from 'vs/workbench/common/viewlet';
import { TestViewletService, TestPanelService } from 'vs/workbench/test/workbenchTestServices';
import { Event } from 'vs/base/common/event';
class TestViewlet implements IViewlet {
constructor(private id: string) { }
readonly onDidBlur = Event.None;
readonly onDidFocus = Event.None;
getId(): string { return this.id; }
getTitle(): string { return this.id; }
getActions(): IAction[] { return []; }