From 305f0fbbc29be0bb4661113d27730b1c0764931e Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 3 Mar 2017 10:11:04 +0100 Subject: [PATCH] editorInput.getTitle() for #16623 --- src/vs/base/common/labels.ts | 8 ++++ src/vs/platform/editor/common/editor.ts | 11 ++++++ .../parts/editor/noTabsTitleControl.ts | 9 +++-- .../browser/parts/editor/tabsTitleControl.ts | 10 ++--- .../browser/parts/titlebar/titlebarPart.ts | 21 +++------- src/vs/workbench/common/editor.ts | 10 +++-- .../files/common/editors/fileEditorInput.ts | 39 ++++++++++++------- .../test/browser/fileEditorInput.test.ts | 3 +- 8 files changed, 68 insertions(+), 43 deletions(-) diff --git a/src/vs/base/common/labels.ts b/src/vs/base/common/labels.ts index fd2fd80ba7f..e9280c65e09 100644 --- a/src/vs/base/common/labels.ts +++ b/src/vs/base/common/labels.ts @@ -76,6 +76,14 @@ function getPath(arg1: URI | string | IWorkspaceProvider): string { return (arg1).fsPath; } +export function tildify(path: string, userHome: string): string { + if (path && (platform.isMacintosh || platform.isLinux) && path.indexOf(userHome) === 0) { + path = `~${path.substr(userHome.length)}`; + } + + return path; +} + /** * Shortens the paths but keeps them easy to distinguish. * Replaces not important parts with ellipsis. diff --git a/src/vs/platform/editor/common/editor.ts b/src/vs/platform/editor/common/editor.ts index dcda3433ac6..237ef662803 100644 --- a/src/vs/platform/editor/common/editor.ts +++ b/src/vs/platform/editor/common/editor.ts @@ -159,6 +159,12 @@ export enum Direction { RIGHT } +export enum Verbosity { + SHORT, + MEDIUM, + LONG +} + export interface IEditorInput extends IDisposable { onDispose: Event; @@ -173,6 +179,11 @@ export interface IEditorInput extends IDisposable { */ getDescription(verbose?: boolean): string; + /** + * Returns the display title of this input. + */ + getTitle(verbosity?: Verbosity): string; + /** * Resolves the input. */ diff --git a/src/vs/workbench/browser/parts/editor/noTabsTitleControl.ts b/src/vs/workbench/browser/parts/editor/noTabsTitleControl.ts index d0ba9cd0a56..5cb3a53b0c4 100644 --- a/src/vs/workbench/browser/parts/editor/noTabsTitleControl.ts +++ b/src/vs/workbench/browser/parts/editor/noTabsTitleControl.ts @@ -11,6 +11,7 @@ import { IEditorGroup, toResource } from 'vs/workbench/common/editor'; import DOM = require('vs/base/browser/dom'); import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl'; import { EditorLabel } from 'vs/workbench/browser/labels'; +import { Verbosity } from 'vs/platform/editor/common/editor'; export class NoTabsTitleControl extends TitleControl { private titleContainer: HTMLElement; @@ -119,12 +120,12 @@ export class NoTabsTitleControl extends TitleControl { const resource = toResource(editor, { supportSideBySide: true }); const name = editor.getName() || ''; const description = isActive ? (editor.getDescription() || '') : ''; - let verboseDescription = editor.getDescription(true) || ''; - if (description === verboseDescription) { - verboseDescription = ''; // dont repeat what is already shown + let title = editor.getTitle(Verbosity.LONG); + if (description === title) { + title = ''; // dont repeat what is already shown } - this.editorLabel.setLabel({ name, description, resource }, { title: verboseDescription, italic: !isPinned, extraClasses: ['title-label'] }); + this.editorLabel.setLabel({ name, description, resource }, { title, italic: !isPinned, extraClasses: ['title-label'] }); // Update Editor Actions Toolbar this.updateEditorActionsToolbar(); diff --git a/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts b/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts index 078d2270b4a..a6c54f90a83 100644 --- a/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts +++ b/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts @@ -14,7 +14,7 @@ import { isMacintosh } from 'vs/base/common/platform'; import { MIME_BINARY } from 'vs/base/common/mime'; import { shorten } from 'vs/base/common/labels'; import { ActionRunner, IAction } from 'vs/base/common/actions'; -import { Position, IEditorInput } from 'vs/platform/editor/common/editor'; +import { Position, IEditorInput, Verbosity } from 'vs/platform/editor/common/editor'; import { IEditorGroup, toResource } from 'vs/workbench/common/editor'; import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent'; import { KeyCode } from 'vs/base/common/keyCodes'; @@ -44,7 +44,7 @@ interface IEditorInputLabel { name: string; hasAmbiguousName?: boolean; description?: string; - verboseDescription?: string; + title?: string; } export class TabsTitleControl extends TitleControl { @@ -211,11 +211,11 @@ export class TabsTitleControl extends TitleControl { const label = labels[index]; const name = label.name; const description = label.hasAmbiguousName && label.description ? label.description : ''; - const verboseDescription = label.verboseDescription || ''; + const title = label.title || ''; // Container tabContainer.setAttribute('aria-label', `${name}, tab`); - tabContainer.title = verboseDescription; + tabContainer.title = title; ['off', 'left'].forEach(option => { const domAction = this.tabOptions.tabCloseButton === option ? DOM.addClass : DOM.removeClass; domAction(tabContainer, `close-button-${option}`); @@ -264,7 +264,7 @@ export class TabsTitleControl extends TitleControl { editor, name: editor.getName(), description, - verboseDescription: editor.getDescription(true) + title: editor.getTitle(Verbosity.LONG) }; labels.push(item); diff --git a/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts b/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts index cd3a1bf23e7..84dac4bae22 100644 --- a/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts +++ b/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts @@ -23,12 +23,12 @@ import { IIntegrityService } from 'vs/platform/integrity/common/integrity'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService'; import { IDisposable, dispose } from 'vs/base/common/lifecycle'; -import { isMacintosh, isLinux } from 'vs/base/common/platform'; import nls = require('vs/nls'); import * as labels from 'vs/base/common/labels'; -import { EditorInput, toResource } from 'vs/workbench/common/editor'; +import { EditorInput } from 'vs/workbench/common/editor'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; +import { Verbosity } from 'vs/platform/editor/common/editor'; export class TitlebarPart extends Part implements ITitleService { @@ -66,7 +66,7 @@ export class TitlebarPart extends Part implements ITitleService { this.isPure = true; this.activeEditorListeners = []; - this.workspacePath = contextService.hasWorkspace() ? this.tildify(labels.getPathLabel(contextService.getWorkspace().resource)) : ''; + this.workspacePath = contextService.hasWorkspace() ? labels.tildify(labels.getPathLabel(contextService.getWorkspace().resource), environmentService.userHome) : ''; this.init(); @@ -163,12 +163,11 @@ export class TitlebarPart extends Part implements ITitleService { private doGetWindowTitle(): string { const input = this.editorService.getActiveEditorInput(); const workspace = this.contextService.getWorkspace(); - const file = toResource(input, { filter: 'file' }); // Variables - const activeEditorShort = input ? input.getName() : ''; - const activeEditorMedium = file ? labels.getPathLabel(file, this.contextService) : activeEditorShort; - const activeEditorLong = file ? this.tildify(labels.getPathLabel(file)) : activeEditorMedium; + const activeEditorShort = input ? input.getTitle(Verbosity.SHORT) : ''; + const activeEditorMedium = input ? input.getTitle(Verbosity.MEDIUM) : activeEditorShort; + const activeEditorLong = input ? input.getTitle(Verbosity.LONG) : activeEditorMedium; const rootName = workspace ? workspace.name : ''; const rootPath = workspace ? this.workspacePath : ''; const dirty = input && input.isDirty() ? TitlebarPart.TITLE_DIRTY : ''; @@ -187,14 +186,6 @@ export class TitlebarPart extends Part implements ITitleService { }); } - private tildify(path: string): string { - if (path && (isMacintosh || isLinux) && path.indexOf(this.environmentService.userHome) === 0) { - path = `~${path.substr(this.environmentService.userHome.length)}`; - } - - return path; - } - public createContentArea(parent: Builder): Builder { this.titleContainer = $(parent); diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index fd7383066a1..ca97f0cb802 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -11,7 +11,7 @@ import types = require('vs/base/common/types'); import URI from 'vs/base/common/uri'; import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle'; import { IEditor, ICommonCodeEditor, IEditorViewState, IEditorOptions as ICodeEditorOptions, IModel } from 'vs/editor/common/editorCommon'; -import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, Position } from 'vs/platform/editor/common/editor'; +import { IEditorInput, IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceInput, Position, Verbosity } from 'vs/platform/editor/common/editor'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService'; import { SyncDescriptor, AsyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; @@ -179,13 +179,15 @@ export abstract class EditorInput implements IEditorInput { /** * Returns the description of this input that can be shown to the user. Examples include showing the description of * the input above the editor area to the side of the name of the input. - * - * @param verbose controls if the description should be short or can contain additional details. */ - public getDescription(verbose?: boolean): string { + public getDescription(): string { return null; } + public getTitle(verbosity?: Verbosity): string { + return this.getName(); + } + /** * Returns the unique type identifier of this input. */ diff --git a/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts b/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts index a9b5b4215cd..e35c377f141 100644 --- a/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts +++ b/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts @@ -18,6 +18,8 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IDisposable, dispose } from 'vs/base/common/lifecycle'; import { telemetryURIDescriptor } from 'vs/platform/telemetry/common/telemetryUtils'; +import { Verbosity } from 'vs/platform/editor/common/editor'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; /** * A file editor input is the input type for the file editor of file system resources. @@ -29,7 +31,10 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput { private name: string; private description: string; - private verboseDescription: string; + + private shortTitle: string; + private mediumTitle: string; + private longTitle: string; private toUnbind: IDisposable[]; @@ -41,7 +46,8 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput { preferredEncoding: string, @IInstantiationService private instantiationService: IInstantiationService, @IWorkspaceContextService private contextService: IWorkspaceContextService, - @ITextFileService private textFileService: ITextFileService + @ITextFileService private textFileService: ITextFileService, + @IEnvironmentService private environmentService: IEnvironmentService ) { super(); @@ -76,7 +82,9 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput { // Reset resource dependent properties this.name = null; this.description = null; - this.verboseDescription = null; + this.shortTitle = null; + this.mediumTitle = null; + this.longTitle = null; } public getResource(): URI { @@ -125,20 +133,23 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput { return this.name; } - public getDescription(verbose?: boolean): string { - if (!verbose) { - if (!this.description) { - this.description = labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService); - } - - return this.description; + public getDescription(): string { + if (!this.description) { + this.description = labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService); } - if (!this.verboseDescription) { - this.verboseDescription = labels.getPathLabel(this.resource.fsPath); - } + return this.description; + } - return this.verboseDescription; + public getTitle(verbosity: Verbosity): string { + switch (verbosity) { + case Verbosity.SHORT: + return this.shortTitle ? this.shortTitle : (this.shortTitle = this.getName()); + case Verbosity.MEDIUM: + return this.mediumTitle ? this.mediumTitle : (this.mediumTitle = labels.getPathLabel(this.resource, this.contextService)); + case Verbosity.LONG: + return this.longTitle ? this.longTitle : (this.longTitle = labels.tildify(labels.getPathLabel(this.resource), this.environmentService.userHome)); + } } public isDirty(): boolean { diff --git a/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts b/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts index 0814537cd39..effd2d1f5f7 100644 --- a/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts +++ b/src/vs/workbench/parts/files/test/browser/fileEditorInput.test.ts @@ -15,6 +15,7 @@ import { EncodingMode } from 'vs/workbench/common/editor'; import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; import { FileOperationResult, IFileOperationResult } from 'vs/platform/files/common/files'; import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel'; +import { Verbosity } from 'vs/platform/editor/common/editor'; function toResource(path) { return URI.file(join('C:\\', new Buffer(this.test.fullTitle()).toString('base64'), path)); @@ -49,7 +50,7 @@ suite('Files - FileEditorInput', () => { assert(!input.matches(null)); assert.ok(input.getName()); assert.ok(input.getDescription()); - assert.ok(input.getDescription(true)); + assert.ok(input.getTitle(Verbosity.SHORT)); assert.strictEqual('file.js', input.getName()); -- GitLab