diff --git a/src/vs/workbench/browser/layout.ts b/src/vs/workbench/browser/layout.ts index c7d77180475..9aed6958fcc 100644 --- a/src/vs/workbench/browser/layout.ts +++ b/src/vs/workbench/browser/layout.ts @@ -121,7 +121,7 @@ export class WorkbenchLayout implements IVerticalSashLayoutProvider, IHorizontal this.panelHeight = this.storageService.getInteger(WorkbenchLayout.sashYHeightSettingsKey, StorageScope.GLOBAL, 0); this.toUnbind.push(themeService.onDidThemeChange(_ => this.relayout())); - this.toUnbind.push(eventService.addListener2(EventType.EDITOR_INPUT_CHANGING, (e: EditorEvent) => this.onEditorInputChanging(e))); + this.toUnbind.push(eventService.addListener2(EventType.EDITOR_INPUT_CHANGING, (e: EditorEvent) => this.onEditorInputChanging())); this.registerSashListeners(); } @@ -243,7 +243,7 @@ export class WorkbenchLayout implements IVerticalSashLayoutProvider, IHorizontal }); } - private onEditorInputChanging(e: EditorEvent): void { + private onEditorInputChanging(): void { // Make sure that we layout properly in case we detect that the sidebar is large enought to cause // multiple opened editors to go below minimal size. The fix is to trigger a layout for any editor diff --git a/src/vs/workbench/browser/parts/editor/baseEditor.ts b/src/vs/workbench/browser/parts/editor/baseEditor.ts index 289254d05de..1937f4e5d2a 100644 --- a/src/vs/workbench/browser/parts/editor/baseEditor.ts +++ b/src/vs/workbench/browser/parts/editor/baseEditor.ts @@ -9,7 +9,7 @@ import {Action, IAction} from 'vs/base/common/actions'; import {ActionBarContributor} from 'vs/workbench/browser/actionBarRegistry'; import types = require('vs/base/common/types'); import {Builder} from 'vs/base/browser/builder'; -import {EventType, EditorEvent} from 'vs/workbench/common/events'; +import {EventType} from 'vs/workbench/common/events'; import {Registry} from 'vs/platform/platform'; import {Panel} from 'vs/workbench/browser/panel'; import {EditorInput, IFileEditorInput, EditorOptions} from 'vs/workbench/common/editor'; @@ -123,7 +123,7 @@ export abstract class BaseEditor extends Panel implements IEditor { */ public changePosition(position: Position): void { this._position = position; - this.emit(EventType.EDITOR_POSITION_CHANGED, new EditorEvent(this, this.input, this.options, this.position)); + this.emit(EventType.EDITOR_POSITION_CHANGED, this); } /** diff --git a/src/vs/workbench/browser/parts/editor/editorPart.ts b/src/vs/workbench/browser/parts/editor/editorPart.ts index 77a1254bfdc..046a3366522 100644 --- a/src/vs/workbench/browser/parts/editor/editorPart.ts +++ b/src/vs/workbench/browser/parts/editor/editorPart.ts @@ -21,7 +21,7 @@ import errors = require('vs/base/common/errors'); import {Scope as MementoScope} from 'vs/workbench/common/memento'; import {Scope} from 'vs/workbench/browser/actionBarRegistry'; import {Part} from 'vs/workbench/browser/part'; -import {EventType as WorkbenchEventType, EditorEvent} from 'vs/workbench/common/events'; +import {EventType as WorkbenchEventType, EditorEvent, EditorInputEvent} from 'vs/workbench/common/events'; import {IEditorRegistry, Extensions as EditorExtensions, BaseEditor, EditorDescriptor} from 'vs/workbench/browser/parts/editor/baseEditor'; import {EditorInput, EditorOptions, TextEditorOptions, ConfirmResult} from 'vs/workbench/common/editor'; import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor'; @@ -163,7 +163,7 @@ export class EditorPart extends Part implements IEditorPart { } // Emit early open event to allow for veto - let event = new EditorEvent(null, input, options, position); + let event = new EditorInputEvent(input); this.emit(WorkbenchEventType.EDITOR_INPUT_OPENING, event); if (event.isPrevented()) { return TPromise.as(null); @@ -263,7 +263,7 @@ export class EditorPart extends Part implements IEditorPart { this.sideBySideControl.layout(position); // Emit Editor-Opened Event - this.emit(WorkbenchEventType.EDITOR_OPENED, new EditorEvent(editor, input, options, position)); + this.emit(WorkbenchEventType.EDITOR_OPENED, editor); timerEvent.stop(); @@ -452,7 +452,7 @@ export class EditorPart extends Part implements IEditorPart { this.sideBySideControl.updateProgress(position, ProgressState.DONE); // Event - this.emit(WorkbenchEventType.EDITOR_SET_INPUT_ERROR, new EditorEvent(editor, input, options, position)); + this.emit(WorkbenchEventType.EDITOR_SET_INPUT_ERROR, new EditorInputEvent(input)); // Recover by closing the active editor (if the input is still the active one) if (group.activeEditor === input) { @@ -570,7 +570,7 @@ export class EditorPart extends Part implements IEditorPart { this.sideBySideControl.clearTitleArea(position); // Emit Editor Closed Event - this.emit(WorkbenchEventType.EDITOR_CLOSED, new EditorEvent(editor, null, null, position)); + this.emit(WorkbenchEventType.EDITOR_CLOSED, editor); } public closeAllEditors(except?: Position): TPromise { @@ -812,8 +812,8 @@ export class EditorPart extends Part implements IEditorPart { // Emit as editor input change event so that clients get aware of new active editor let activeEditor = this.sideBySideControl.getActiveEditor(); if (activeEditor) { - this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGING, new EditorEvent(activeEditor, activeEditor.input, null, activeEditor.position)); - this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(activeEditor, activeEditor.input, null, activeEditor.position)); + this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGING, new EditorEvent(activeEditor)); + this.emit(WorkbenchEventType.EDITOR_INPUT_CHANGED, new EditorEvent(activeEditor)); } // Update Title Area diff --git a/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts b/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts index e8d0b5e6f5f..ec5fd102e48 100644 --- a/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts +++ b/src/vs/workbench/browser/parts/quickopen/quickOpenController.ts @@ -20,7 +20,7 @@ import {ContributableActionProvider} from 'vs/workbench/browser/actionBarRegistr import {ITree, IElementCallback} from 'vs/base/parts/tree/browser/tree'; import {Registry} from 'vs/platform/platform'; import {WorkbenchComponent} from 'vs/workbench/common/component'; -import {EditorEvent, EventType} from 'vs/workbench/common/events'; +import {EditorEvent, EditorInputEvent, EventType} from 'vs/workbench/common/events'; import Event, {Emitter} from 'vs/base/common/event'; import {Identifiers} from 'vs/workbench/common/constants'; import {IEditorInput} from 'vs/platform/editor/common/editor'; @@ -125,7 +125,7 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe // Listen on Editor Input Changes to show in MRU List this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_INPUT_CHANGING, (e: EditorEvent) => this.onEditorInputChanging(e))); - this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_SET_INPUT_ERROR, (e: EditorEvent) => this.onEditorInputSetError(e))); + this.toUnbind.push(this.eventService.addListener2(EventType.EDITOR_SET_INPUT_ERROR, (e: EditorInputEvent) => this.onEditorInputSetError(e))); // Editor History Model this.editorHistoryModel = new EditorHistoryModel(this.editorService, this.instantiationService, this.contextService); @@ -155,10 +155,8 @@ export class QuickOpenController extends WorkbenchComponent implements IQuickOpe } } - private onEditorInputSetError(e: EditorEvent): void { - if (e.editorInput) { - this.removeEditorHistoryEntry(e.editorInput); // make sure this input does not show up in history if it failed to open - } + private onEditorInputSetError(e: EditorInputEvent): void { + this.removeEditorHistoryEntry(e.editorInput); // make sure this input does not show up in history if it failed to open } public getEditorHistory(): IEditorInput[] { diff --git a/src/vs/workbench/common/events.ts b/src/vs/workbench/common/events.ts index 250e9f2a7f3..27aa32bc206 100644 --- a/src/vs/workbench/common/events.ts +++ b/src/vs/workbench/common/events.ts @@ -6,8 +6,7 @@ import URI from 'vs/base/common/uri'; import {Event} from 'vs/base/common/events'; -import {IEditor, IEditorInput} from 'vs/platform/editor/common/editor'; -import {EditorOptions} from 'vs/workbench/common/editor'; +import {IEditor, IEditorInput, IEditorOptions} from 'vs/platform/editor/common/editor'; import {Position} from 'vs/platform/editor/common/editor'; /** @@ -98,26 +97,18 @@ export class EventType { static WORKBENCH_OPTIONS_CHANGED = 'workbenchOptionsChanged'; } -/** - * Editor events are being emitted when the editor input changes, shows, is being saved or when the editor content changes. - */ -export class EditorEvent extends Event { - public editor: IEditor; - public editorId: string; - public editorInput: IEditorInput; - public editorOptions: EditorOptions; - public position: Position; - +export class EditorInputEvent extends Event { + private _editorInput: IEditorInput; private prevented: boolean; - constructor(editor: IEditor, editorInput: IEditorInput, editorOptions: EditorOptions, position: Position, originalEvent?: any) { - super(originalEvent); + constructor(editorInput: IEditorInput) { + super(null); - this.editor = editor; - this.editorId = editor ? editor.getId() : void 0; - this.editorInput = editorInput; - this.editorOptions = editorOptions; - this.position = position; + this._editorInput = editorInput; + } + + public get editorInput(): IEditorInput { + return this._editorInput; } public prevent(): void { @@ -129,6 +120,47 @@ export class EditorEvent extends Event { } } +/** + * Editor events are being emitted when the editor input changes, shows, is being saved or when the editor content changes. + */ +export class EditorEvent extends Event { + private _editor: IEditor; + private _editorId: string; + private _editorInput: IEditorInput; + private _editorOptions: IEditorOptions; + private _position: Position; + + constructor(editor: IEditor, editorInput = editor.input, editorOptions = editor.options, position = editor.position, originalEvent?: any) { + super(originalEvent); + + this._editor = editor; + this._editorId = editor ? editor.getId() : void 0; + this._editorInput = editorInput; + this._editorOptions = editorOptions; + this._position = position; + } + + public get editor(): IEditor { + return this._editor; + } + + public get editorId(): string { + return this._editorId; + } + + public get editorInput(): IEditorInput { + return this._editorInput; + } + + public get editorOptions(): IEditorOptions { + return this._editorOptions; + } + + public get position(): Position { + return this._position; + } +} + /** * Option change events are send when the options in the running instance change. */ diff --git a/src/vs/workbench/parts/files/browser/fileActions.ts b/src/vs/workbench/parts/files/browser/fileActions.ts index 88438366d25..67272a8004f 100644 --- a/src/vs/workbench/parts/files/browser/fileActions.ts +++ b/src/vs/workbench/parts/files/browser/fileActions.ts @@ -24,7 +24,7 @@ import {Action, IAction} from 'vs/base/common/actions'; import {MessageType, IInputValidator} from 'vs/base/browser/ui/inputbox/inputBox'; import {ITree, IHighlightEvent} from 'vs/base/parts/tree/browser/tree'; import {dispose, IDisposable} from 'vs/base/common/lifecycle'; -import {EventType as WorkbenchEventType, EditorEvent} from 'vs/workbench/common/events'; +import {EventType as WorkbenchEventType, EditorInputEvent} from 'vs/workbench/common/events'; import {LocalFileChangeEvent, VIEWLET_ID, ITextFileService, TextFileChangeEvent, EventType as FileEventType} from 'vs/workbench/parts/files/common/files'; import {IFileService, IFileStat, IImportResult} from 'vs/platform/files/common/files'; import {DiffEditorInput, toDiffLabel} from 'vs/workbench/common/editor/diffEditorInput'; @@ -1227,7 +1227,7 @@ export class GlobalCompareResourcesAction extends Action { globalResourceToCompare = fileInput.getResource(); // Listen for next editor to open - let unbind = this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_OPENING, (e: EditorEvent) => { + let unbind = this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_OPENING, (e: EditorInputEvent) => { unbind.dispose(); // listen once let otherFileInput = asFileEditorInput(e.editorInput); diff --git a/src/vs/workbench/services/progress/browser/progressService.ts b/src/vs/workbench/services/progress/browser/progressService.ts index 4feaca0399e..115f5e53484 100644 --- a/src/vs/workbench/services/progress/browser/progressService.ts +++ b/src/vs/workbench/services/progress/browser/progressService.ts @@ -7,9 +7,10 @@ import {TPromise} from 'vs/base/common/winjs.base'; import types = require('vs/base/common/types'); import {ProgressBar} from 'vs/base/browser/ui/progressbar/progressbar'; -import {EditorEvent, EventType, CompositeEvent} from 'vs/workbench/common/events'; +import {EventType, CompositeEvent} from 'vs/workbench/common/events'; import {IEventService} from 'vs/platform/event/common/event'; import {IProgressService, IProgressRunner} from 'vs/platform/progress/common/progress'; +import {IEditor} from 'vs/platform/editor/common/editor'; interface ProgressState { infinite?: boolean; @@ -35,14 +36,14 @@ export abstract class ScopedService { } public registerListeners(): void { - this.eventService.addListener2(EventType.EDITOR_CLOSED, (e: EditorEvent) => { - if (e.editorId === this.scopeId) { + this.eventService.addListener2(EventType.EDITOR_CLOSED, (editor: IEditor) => { + if (editor.getId() === this.scopeId) { this.onScopeDeactivated(); } }); - this.eventService.addListener2(EventType.EDITOR_OPENED, (e: EditorEvent) => { - if (e.editorId === this.scopeId) { + this.eventService.addListener2(EventType.EDITOR_OPENED, (editor: IEditor) => { + if (editor.getId() === this.scopeId) { this.onScopeActivated(); } }); diff --git a/src/vs/workbench/test/browser/services.test.ts b/src/vs/workbench/test/browser/services.test.ts index 79b1754adc6..a59696a13cd 100644 --- a/src/vs/workbench/test/browser/services.test.ts +++ b/src/vs/workbench/test/browser/services.test.ts @@ -426,13 +426,13 @@ suite('Workbench UI Services', () => { let service = new TestScopedService(eventService); assert(!service.isActive); - eventService.emit(EventType.EDITOR_OPENED, { editorId: 'other.test.scopeId' }); + eventService.emit(EventType.EDITOR_OPENED, { getId: () => 'other.test.scopeId' }); assert(!service.isActive); - eventService.emit(EventType.EDITOR_OPENED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_OPENED, { getId: () => 'test.scopeId' }); assert(service.isActive); - eventService.emit(EventType.EDITOR_CLOSED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_CLOSED, { getId: () => 'test.scopeId' }); assert(!service.isActive); eventService.emit(EventType.COMPOSITE_OPENED, { compositeId: 'test.scopeId' }); @@ -465,19 +465,19 @@ suite('Workbench UI Services', () => { assert.strictEqual(true, testProgressBar.fDone); // Inactive: Show (Infinite) - eventService.emit(EventType.EDITOR_CLOSED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_CLOSED, { getId: () => 'test.scopeId' }); service.show(true); assert.strictEqual(false, !!testProgressBar.fInfinite); - eventService.emit(EventType.EDITOR_OPENED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_OPENED, { getId: () => 'test.scopeId' }); assert.strictEqual(true, testProgressBar.fInfinite); // Inactive: Show (Total / Worked) - eventService.emit(EventType.EDITOR_CLOSED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_CLOSED, { getId: () => 'test.scopeId' }); fn = service.show(100); fn.total(80); fn.worked(20); assert.strictEqual(false, !!testProgressBar.fTotal); - eventService.emit(EventType.EDITOR_OPENED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_OPENED, { getId: () => 'test.scopeId' }); assert.strictEqual(20, testProgressBar.fWorked); assert.strictEqual(80, testProgressBar.fTotal); @@ -486,12 +486,12 @@ suite('Workbench UI Services', () => { service.showWhile(p).then(() => { assert.strictEqual(true, testProgressBar.fDone); - eventService.emit(EventType.EDITOR_CLOSED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_CLOSED, { getId: () => 'test.scopeId' }); p = TPromise.as(null); service.showWhile(p).then(() => { assert.strictEqual(true, testProgressBar.fDone); - eventService.emit(EventType.EDITOR_OPENED, { editorId: 'test.scopeId' }); + eventService.emit(EventType.EDITOR_OPENED, { getId: () => 'test.scopeId' }); assert.strictEqual(true, testProgressBar.fDone); }); });