Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MutableDisposable for managing editorStatus items #75378

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 31 additions & 63 deletions src/vs/workbench/browser/parts/editor/editorStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Action } from 'vs/base/common/actions';
import { Language } from 'vs/base/common/platform';
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
import { IFileEditorInput, EncodingMode, IEncodingSupport, toResource, SideBySideEditorInput, IEditor as IBaseEditor, IEditorInput, SideBySideEditor, IModeSupport } from 'vs/workbench/common/editor';
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
import { IDisposable, dispose, Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IEditorAction } from 'vs/editor/common/editorCommon';
import { EndOfLineSequence } from 'vs/editor/common/model';
Expand Down Expand Up @@ -276,14 +276,14 @@ const nlsEOLLF = nls.localize('endOfLineLineFeed', "LF");
const nlsEOLCRLF = nls.localize('endOfLineCarriageReturnLineFeed', "CRLF");

export class EditorStatus extends Disposable implements IWorkbenchContribution {
private tabFocusModeElement?: IStatusbarEntryAccessor;
private screenRedearModeElement?: IStatusbarEntryAccessor;
private indentationElement?: IStatusbarEntryAccessor;
private selectionElement?: IStatusbarEntryAccessor;
private encodingElement?: IStatusbarEntryAccessor;
private eolElement?: IStatusbarEntryAccessor;
private modeElement?: IStatusbarEntryAccessor;
private metadataElement?: IStatusbarEntryAccessor;
private readonly tabFocusModeElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly screenRedearModeElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly indentationElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly selectionElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly encodingElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly eolElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly modeElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
private readonly metadataElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());

private readonly state = new State();
private readonly activeEditorListeners: IDisposable[] = [];
Expand Down Expand Up @@ -382,45 +382,35 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {

private updateTabFocusModeElement(visible: boolean): void {
if (visible) {
if (!this.tabFocusModeElement) {
this.tabFocusModeElement = this.statusbarService.addEntry({
if (!this.tabFocusModeElement.value) {
this.tabFocusModeElement.value = this.statusbarService.addEntry({
text: nls.localize('tabFocusModeEnabled', "Tab Moves Focus"),
tooltip: nls.localize('disableTabMode', "Disable Accessibility Mode"),
command: 'editor.action.toggleTabFocusMode'
}, 'status.editor.tabFocusMode', nls.localize('status.editor.tabFocusMode', "Accessibility Mode"), StatusbarAlignment.RIGHT, 100.7);
}
} else {
if (this.tabFocusModeElement) {
this.tabFocusModeElement.dispose();
this.tabFocusModeElement = undefined;
}
this.tabFocusModeElement.clear();
}
}

private updateScreenReaderModeElement(visible: boolean): void {
if (visible) {
if (!this.screenRedearModeElement) {
this.screenRedearModeElement = this.statusbarService.addEntry({
if (!this.screenRedearModeElement.value) {
this.screenRedearModeElement.value = this.statusbarService.addEntry({
text: nls.localize('screenReaderDetected', "Screen Reader Optimized"),
tooltip: nls.localize('screenReaderDetectedExtra', "If you are not using a Screen Reader, please change the setting `editor.accessibilitySupport` to \"off\"."),
command: 'showEditorScreenReaderNotification'
}, 'status.editor.screenReaderMode', nls.localize('status.editor.screenReaderMode', "Screen Reader Mode"), StatusbarAlignment.RIGHT, 100.6);
}
} else {
if (this.screenRedearModeElement) {
this.screenRedearModeElement.dispose();
this.screenRedearModeElement = undefined;
}
this.screenRedearModeElement.clear();
}
}

private updateSelectionElement(text: string | undefined): void {
if (!text) {
if (this.selectionElement) {
dispose(this.selectionElement);
this.selectionElement = undefined;
}

this.selectionElement.clear();
return;
}

Expand All @@ -430,16 +420,12 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
command: 'workbench.action.gotoLine'
};

this.selectionElement = this.updateElement(this.selectionElement, props, 'status.editor.selection', nls.localize('status.editor.selection', "Editor Selection"), StatusbarAlignment.RIGHT, 100.5);
this.updateElement(this.selectionElement, props, 'status.editor.selection', nls.localize('status.editor.selection', "Editor Selection"), StatusbarAlignment.RIGHT, 100.5);
}

private updateIndentationElement(text: string | undefined): void {
if (!text) {
if (this.indentationElement) {
dispose(this.indentationElement);
this.indentationElement = undefined;
}

this.indentationElement.clear();
return;
}

Expand All @@ -449,16 +435,12 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
command: 'changeEditorIndentation'
};

this.indentationElement = this.updateElement(this.indentationElement, props, 'status.editor.indentation', nls.localize('status.editor.indentation', "Editor Indentation"), StatusbarAlignment.RIGHT, 100.4);
this.updateElement(this.indentationElement, props, 'status.editor.indentation', nls.localize('status.editor.indentation', "Editor Indentation"), StatusbarAlignment.RIGHT, 100.4);
}

private updateEncodingElement(text: string | undefined): void {
if (!text) {
if (this.encodingElement) {
dispose(this.encodingElement);
this.encodingElement = undefined;
}

this.encodingElement.clear();
return;
}

Expand All @@ -468,16 +450,12 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
command: 'workbench.action.editor.changeEncoding'
};

this.encodingElement = this.updateElement(this.encodingElement, props, 'status.editor.encoding', nls.localize('status.editor.encoding', "Editor Encoding"), StatusbarAlignment.RIGHT, 100.3);
this.updateElement(this.encodingElement, props, 'status.editor.encoding', nls.localize('status.editor.encoding', "Editor Encoding"), StatusbarAlignment.RIGHT, 100.3);
}

private updateEOLElement(text: string | undefined): void {
if (!text) {
if (this.eolElement) {
dispose(this.eolElement);
this.eolElement = undefined;
}

this.eolElement.clear();
return;
}

Expand All @@ -487,16 +465,12 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
command: 'workbench.action.editor.changeEOL'
};

this.eolElement = this.updateElement(this.eolElement, props, 'status.editor.eol', nls.localize('status.editor.eol', "Editor End of Line"), StatusbarAlignment.RIGHT, 100.2);
this.updateElement(this.eolElement, props, 'status.editor.eol', nls.localize('status.editor.eol', "Editor End of Line"), StatusbarAlignment.RIGHT, 100.2);
}

private updateModeElement(text: string | undefined): void {
if (!text) {
if (this.modeElement) {
dispose(this.modeElement);
this.modeElement = undefined;
}

this.modeElement.clear();
return;
}

Expand All @@ -506,16 +480,12 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
command: 'workbench.action.editor.changeLanguageMode'
};

this.modeElement = this.updateElement(this.modeElement, props, 'status.editor.mode', nls.localize('status.editor.mode', "Editor Language"), StatusbarAlignment.RIGHT, 100.1);
this.updateElement(this.modeElement, props, 'status.editor.mode', nls.localize('status.editor.mode', "Editor Language"), StatusbarAlignment.RIGHT, 100.1);
}

private updateMetadataElement(text: string | undefined): void {
if (!text) {
if (this.metadataElement) {
dispose(this.metadataElement);
this.metadataElement = undefined;
}

this.metadataElement.clear();
return;
}

Expand All @@ -524,17 +494,15 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
tooltip: nls.localize('fileInfo', "File Information")
};

this.metadataElement = this.updateElement(this.metadataElement, props, 'status.editor.info', nls.localize('status.editor.info', "File Information"), StatusbarAlignment.RIGHT, 100);
this.updateElement(this.metadataElement, props, 'status.editor.info', nls.localize('status.editor.info', "File Information"), StatusbarAlignment.RIGHT, 100);
}

private updateElement(element: IStatusbarEntryAccessor | undefined, props: IStatusbarEntry, id: string, name: string, alignment: StatusbarAlignment, priority: number): IStatusbarEntryAccessor | undefined {
if (!element) {
element = this.statusbarService.addEntry(props, id, name, alignment, priority);
private updateElement(element: MutableDisposable<IStatusbarEntryAccessor>, props: IStatusbarEntry, id: string, name: string, alignment: StatusbarAlignment, priority: number) {
if (!element.value) {
element.value = this.statusbarService.addEntry(props, id, name, alignment, priority);
} else {
element.update(props);
element.value.update(props);
}

return element;
}

private updateState(update: StateDelta): void {
Expand Down