From 7712905526412c0b0b10181e0e4cd0c13035ff3c Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 4 Sep 2017 11:16:17 +0200 Subject: [PATCH] Improve activity title behaviour fixes #33540 fixes #27779 --- .../parts/activitybar/activitybarActions.ts | 79 +++++++++++-------- .../parts/activitybar/activitybarPart.ts | 18 +++-- .../parts/files/common/dirtyFilesTracker.ts | 2 +- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts b/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts index 1f376f232f0..6663815d3e8 100644 --- a/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts +++ b/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts @@ -83,11 +83,15 @@ export class ViewletActivityAction extends ActivityAction { private lastRun: number = 0; constructor( - private viewlet: ViewletDescriptor, + private _viewlet: ViewletDescriptor, @IViewletService private viewletService: IViewletService, @IPartService private partService: IPartService ) { - super(viewlet); + super(_viewlet); + } + + public get descriptor(): ViewletDescriptor { + return this._viewlet; } public run(event): TPromise { @@ -106,11 +110,11 @@ export class ViewletActivityAction extends ActivityAction { const activeViewlet = this.viewletService.getActiveViewlet(); // Hide sidebar if selected viewlet already visible - if (sideBarVisible && activeViewlet && activeViewlet.getId() === this.viewlet.id) { + if (sideBarVisible && activeViewlet && activeViewlet.getId() === this._viewlet.id) { return this.partService.setSideBarHidden(true); } - return this.viewletService.openViewlet(this.viewlet.id, true) + return this.viewletService.openViewlet(this._viewlet.id, true) .then(() => this.activate()); } } @@ -240,11 +244,22 @@ export class ActivityActionItem extends BaseActionItem { else if (badge instanceof ProgressBadge) { this.$badge.show(); } - - const description = badge.getDescription(); - this.$label.attr('aria-label', `${this.activity.name} - ${description}`); - this.$label.title(description); } + + // Title + let title: string; + if (badge && badge.getDescription()) { + title = `${this.activity.name} - ${badge.getDescription()}`; + } else { + title = this.activity.name; + } + + [this.$label, this.$badge, this.$container].forEach(b => { + if (b) { + b.attr('aria-label', title); + b.title(title); + } + }); } private handleBadgeChangeEvenet(): void { @@ -271,7 +286,7 @@ export class ViewletActionItem extends ActivityActionItem { private static toggleViewletPinnedAction: ToggleViewletPinnedAction; private static draggedViewlet: ViewletDescriptor; - private _keybinding: string; + private viewletActivity: IActivity; private cssClass: string; constructor( @@ -285,7 +300,6 @@ export class ViewletActionItem extends ActivityActionItem { super(action, { draggable: true }, themeService); this.cssClass = action.class; - this._keybinding = this.getKeybindingLabel(this.viewlet.id); if (!ViewletActionItem.manageExtensionAction) { ViewletActionItem.manageExtensionAction = instantiationService.createInstance(ManageExtensionAction); @@ -296,8 +310,29 @@ export class ViewletActionItem extends ActivityActionItem { } } + protected get activity(): IActivity { + if (!this.viewletActivity) { + let activityName: string; + + const keybinding = this.getKeybindingLabel(this.viewlet.id); + if (keybinding) { + activityName = nls.localize('titleKeybinding', "{0} ({1})", this.viewlet.name, keybinding); + } else { + activityName = this.viewlet.name; + } + + this.viewletActivity = { + id: this.viewlet.id, + cssClass: this.cssClass, + name: activityName + }; + } + + return this.viewletActivity; + } + private get viewlet(): ViewletDescriptor { - return this.action.activity as ViewletDescriptor; + return this.action.descriptor; } private getKeybindingLabel(id: string): string { @@ -374,9 +409,6 @@ export class ViewletActionItem extends ActivityActionItem { } }); - // Keybinding - this.keybinding = this._keybinding; // force update - // Activate on drag over to reveal targets [this.$badge, this.$label].forEach(b => new DelayedDragHandler(b.getHTMLElement(), () => { if (!ViewletActionItem.getDraggedViewlet() && !this.getAction().checked) { @@ -431,25 +463,6 @@ export class ViewletActionItem extends ActivityActionItem { this.$container.domFocus(); } - public set keybinding(keybinding: string) { - this._keybinding = keybinding; - - if (!this.$label) { - return; - } - - let title: string; - if (keybinding) { - title = nls.localize('titleKeybinding', "{0} ({1})", this.activity.name, keybinding); - } else { - title = this.activity.name; - } - - this.$label.title(title); - this.$badge.title(title); - this.$container.title(title); - } - protected _updateClass(): void { if (this.cssClass) { this.$badge.removeClass(this.cssClass); diff --git a/src/vs/workbench/browser/parts/activitybar/activitybarPart.ts b/src/vs/workbench/browser/parts/activitybar/activitybarPart.ts index 0376269e6b2..6588c1512bd 100644 --- a/src/vs/workbench/browser/parts/activitybar/activitybarPart.ts +++ b/src/vs/workbench/browser/parts/activitybar/activitybarPart.ts @@ -156,14 +156,17 @@ export class ActivitybarPart extends Part implements IActivityBarService { if (!stack) { return; } + const idx = stack.indexOf(activity); if (idx < 0) { return; } + stack.splice(idx, 1); if (stack.length === 0) { delete this.viewletIdToActivityStack[viewletId]; } + this.updateActivity(viewletId); } }; @@ -174,13 +177,16 @@ export class ActivitybarPart extends Part implements IActivityBarService { if (!action) { return; } - const stack = this.viewletIdToActivityStack[viewletId]; - if (!stack || !stack.length) { - // reset - action.setBadge(undefined); - } else { - // update + const stack = this.viewletIdToActivityStack[viewletId]; + + // reset + if (!stack || !stack.length) { + action.setBadge(undefined); + } + + // update + else { const [{ badge, clazz }] = stack; action.setBadge(badge); if (clazz) { diff --git a/src/vs/workbench/parts/files/common/dirtyFilesTracker.ts b/src/vs/workbench/parts/files/common/dirtyFilesTracker.ts index 8698ed99ca6..f02e1e86e24 100644 --- a/src/vs/workbench/parts/files/common/dirtyFilesTracker.ts +++ b/src/vs/workbench/parts/files/common/dirtyFilesTracker.ts @@ -141,7 +141,7 @@ export class DirtyFilesTracker implements IWorkbenchContribution { this.lastDirtyCount = dirtyCount; dispose(this.badgeHandle); if (dirtyCount > 0) { - this.badgeHandle = this.activityBarService.showActivity(VIEWLET_ID, new NumberBadge(dirtyCount, num => nls.localize('dirtyFiles', "{0} unsaved files", dirtyCount)), 'explorer-viewlet-label'); + this.badgeHandle = this.activityBarService.showActivity(VIEWLET_ID, new NumberBadge(dirtyCount, num => num === 1 ? nls.localize('dirtyFile', "1 unsaved file") : nls.localize('dirtyFiles', "{0} unsaved files", dirtyCount)), 'explorer-viewlet-label'); } }