activitybarActions.ts 12.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/activityaction';
import nls = require('vs/nls');
10 11 12
import DOM = require('vs/base/browser/dom');
import errors = require('vs/base/common/errors');
import { TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
13 14 15
import { Builder, $ } from 'vs/base/browser/builder';
import { DelayedDragHandler } from 'vs/base/browser/dnd';
import { Action } from 'vs/base/common/actions';
16 17
import { BaseActionItem, Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IActivityBarService, ProgressBadge, TextBadge, NumberBadge, IconBadge, IBadge } from 'vs/workbench/services/activity/common/activityBarService';
J
Johannes Rieken 已提交
18
import Event, { Emitter } from 'vs/base/common/event';
19 20 21 22 23 24
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
25
import { IViewletService, } from 'vs/workbench/services/viewlet/browser/viewlet';
26
import { IPartService, Parts } from 'vs/workbench/services/part/common/partService';
E
Erich Gamma 已提交
27 28 29

export class ActivityAction extends Action {
	private badge: IBadge;
30
	private _onDidChangeBadge = new Emitter<this>();
E
Erich Gamma 已提交
31 32 33 34 35 36 37

	constructor(id: string, name: string, clazz: string) {
		super(id, name, clazz);

		this.badge = null;
	}

38 39 40 41
	public get onDidChangeBadge(): Event<this> {
		return this._onDidChangeBadge.event;
	}

E
Erich Gamma 已提交
42 43
	public activate(): void {
		if (!this.checked) {
44
			this._setChecked(true);
E
Erich Gamma 已提交
45 46 47 48 49
		}
	}

	public deactivate(): void {
		if (this.checked) {
50
			this._setChecked(false);
E
Erich Gamma 已提交
51 52 53 54 55 56 57 58 59
		}
	}

	public getBadge(): IBadge {
		return this.badge;
	}

	public setBadge(badge: IBadge): void {
		this.badge = badge;
60
		this._onDidChangeBadge.fire(this);
E
Erich Gamma 已提交
61 62 63
	}
}

64 65 66 67 68 69 70 71 72 73 74
export class ViewletActivityAction extends ActivityAction {

	private static preventDoubleClickDelay = 300;

	private lastRun: number = 0;

	constructor(
		private viewlet: ViewletDescriptor,
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
	) {
75
		super(viewlet.id, viewlet.name, viewlet.cssClass);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	}

	public run(event): TPromise<any> {
		if (event instanceof MouseEvent && event.button === 2) {
			return TPromise.as(false); // do not run on right click
		}

		// prevent accident trigger on a doubleclick (to help nervous people)
		const now = Date.now();
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
			return TPromise.as(true);
		}
		this.lastRun = now;

		const sideBarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
		const activeViewlet = this.viewletService.getActiveViewlet();

		// Hide sidebar if selected viewlet already visible
		if (sideBarVisible && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
			this.partService.setSideBarHidden(true);
		} else {
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
			this.activate();
		}

		return TPromise.as(true);
	}
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
export class ViewletOverflowActivityAction extends ActivityAction {

	constructor(
		private showMenu: () => void
	) {
		super('activitybar.additionalViewlets.action', nls.localize('additionalViewlets', "Additional Viewlets"), 'toggle-more');
	}

	public run(event): TPromise<any> {
		this.showMenu();

		return TPromise.as(true);
	}
}

export class ViewletOverflowActivityActionItem extends BaseActionItem {
	private $e: Builder;
	private name: string;
	private cssClass: string;
	private actions: OpenViewletAction[];

	constructor(
		action: ActivityAction,
128
		private getOverflowingViewlets: () => ViewletDescriptor[],
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		private getBadge: (viewlet: ViewletDescriptor) => IBadge,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IViewletService private viewletService: IViewletService,
		@IContextMenuService private contextMenuService: IContextMenuService,
	) {
		super(null, action);

		this.cssClass = action.class;
		this.name = action.label;
	}

	public render(container: HTMLElement): void {
		super.render(container);

		this.$e = $('a.action-label').attr({
			tabIndex: '0',
			role: 'button',
			title: this.name,
			class: this.cssClass
		}).appendTo(this.builder);
	}

	public showMenu(): void {
152 153 154 155 156
		if (this.actions) {
			dispose(this.actions);
		}

		this.actions = this.getActions();
157 158 159

		this.contextMenuService.showContextMenu({
			getAnchor: () => this.builder.getHTMLElement(),
160 161
			getActions: () => TPromise.as(this.actions),
			onHide: () => dispose(this.actions)
162 163 164
		});
	}

165
	private getActions(): OpenViewletAction[] {
166 167
		const activeViewlet = this.viewletService.getActiveViewlet();

168 169
		return this.getOverflowingViewlets().map(viewlet => {
			const action = this.instantiationService.createInstance(OpenViewletAction, viewlet);
170
			action.radio = activeViewlet && activeViewlet.getId() === action.id;
171 172 173 174 175 176 177 178 179 180 181 182 183 184

			const badge = this.getBadge(action.viewlet);
			let suffix: string | number;
			if (badge instanceof NumberBadge) {
				suffix = badge.number;
			} else if (badge instanceof TextBadge) {
				suffix = badge.text;
			}

			if (suffix) {
				action.label = nls.localize('numberBadge', "{0} ({1})", action.viewlet.name, suffix);
			} else {
				action.label = action.viewlet.name;
			}
185 186

			return action;
187 188 189 190 191 192 193 194 195 196
		});
	}

	public dispose(): void {
		super.dispose();

		this.actions = dispose(this.actions);
	}
}

E
Erich Gamma 已提交
197
export class ActivityActionItem extends BaseActionItem {
198 199 200 201

	private static manageExtensionAction: ManageExtensionAction;
	private static toggleViewletPinnedAction: ToggleViewletPinnedAction;

E
Erich Gamma 已提交
202 203 204 205 206 207
	private $e: Builder;
	private name: string;
	private _keybinding: string;
	private cssClass: string;
	private $badge: Builder;
	private $badgeContent: Builder;
208 209 210 211 212 213
	private toDispose: IDisposable[];

	constructor(
		action: ActivityAction,
		private viewlet: ViewletDescriptor,
		@IContextMenuService private contextMenuService: IContextMenuService,
214 215
		@IViewletService private viewletService: IViewletService,
		@IActivityBarService private activityBarService: IActivityBarService,
216 217 218
		@IKeybindingService private keybindingService: IKeybindingService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
E
Erich Gamma 已提交
219 220 221
		super(null, action);

		this.cssClass = action.class;
222 223 224 225
		this.name = viewlet.name;
		this._keybinding = this.getKeybindingLabel(viewlet.id);
		action.onDidChangeBadge(this.handleBadgeChangeEvenet, this, this._callOnDispose);

226 227 228 229 230 231
		if (!ActivityActionItem.manageExtensionAction) {
			ActivityActionItem.manageExtensionAction = instantiationService.createInstance(ManageExtensionAction);
		}

		if (!ActivityActionItem.toggleViewletPinnedAction) {
			ActivityActionItem.toggleViewletPinnedAction = instantiationService.createInstance(ToggleViewletPinnedAction, void 0);
232 233 234 235 236 237 238 239 240 241
		}
	}

	private getKeybindingLabel(id: string): string {
		const keys = this.keybindingService.lookupKeybindings(id).map(k => this.keybindingService.getLabelFor(k));
		if (keys && keys.length) {
			return keys[0];
		}

		return null;
E
Erich Gamma 已提交
242 243 244 245 246 247 248
	}

	public render(container: HTMLElement): void {
		super.render(container);

		this.$e = $('a.action-label').attr({
			tabIndex: '0',
249
			role: 'button'
E
Erich Gamma 已提交
250 251
		}).appendTo(this.builder);

252 253 254 255 256
		$(container).on('contextmenu', e => {
			DOM.EventHelper.stop(e, true);

			this.showContextMenu(container);
		}, this.toDispose);
257

E
Erich Gamma 已提交
258 259 260 261 262
		if (this.cssClass) {
			this.$e.addClass(this.cssClass);
		}

		this.$badge = this.builder.div({ 'class': 'badge' }, (badge: Builder) => {
B
Benjamin Pasero 已提交
263
			this.$badgeContent = badge.div({ 'class': 'badge-content' });
E
Erich Gamma 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
		});

		this.$badge.hide();

		this.keybinding = this._keybinding; // force update

		// Activate on drag over to reveal targets
		[this.$badge, this.$e].forEach(b => new DelayedDragHandler(b.getHTMLElement(), () => {
			if (!this.getAction().checked) {
				this.getAction().run();
			}
		}));
	}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	private showContextMenu(container: HTMLElement): void {
		const actions: Action[] = [ActivityActionItem.toggleViewletPinnedAction];
		if (this.viewlet.extensionId) {
			actions.push(new Separator());
			actions.push(ActivityActionItem.manageExtensionAction);
		}

		const isPinned = this.activityBarService.isPinned(this.viewlet.id);
		if (isPinned) {
			ActivityActionItem.toggleViewletPinnedAction.label = nls.localize('removeFromActivityBar', "Remove from Activity Bar");
		} else {
			ActivityActionItem.toggleViewletPinnedAction.label = nls.localize('keepInActivityBar', "Keep in Activity Bar");
		}

		this.contextMenuService.showContextMenu({
			getAnchor: () => container,
			getActionsContext: () => this.viewlet,
			getActions: () => TPromise.as(actions)
		});
	}

E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	public focus(): void {
		this.$e.domFocus();
	}

	public setBadge(badge: IBadge): void {
		this.updateBadge(badge);
	}

	public set keybinding(keybinding: string) {
		this._keybinding = keybinding;

		if (!this.$e) {
			return;
		}

		let title: string;
		if (keybinding) {
			title = nls.localize('titleKeybinding', "{0} ({1})", this.name, keybinding);
		} else {
			title = this.name;
		}

		this.$e.title(title);
		this.$badge.title(title);
	}

	private updateBadge(badge: IBadge): void {
		this.$badgeContent.empty();
		this.$badge.hide();

		if (badge) {

			// Number
			if (badge instanceof NumberBadge) {
333 334
				if (badge.number) {
					this.$badgeContent.text(badge.number > 99 ? '99+' : badge.number.toString());
E
Erich Gamma 已提交
335 336 337 338 339 340
					this.$badge.show();
				}
			}

			// Text
			else if (badge instanceof TextBadge) {
341
				this.$badgeContent.text(badge.text);
E
Erich Gamma 已提交
342 343 344
				this.$badge.show();
			}

I
isidor 已提交
345 346 347 348 349
			// Text
			else if (badge instanceof IconBadge) {
				this.$badge.show();
			}

E
Erich Gamma 已提交
350 351 352 353 354
			// Progress
			else if (badge instanceof ProgressBadge) {
				this.$badge.show();
			}

355
			this.$e.attr('aria-label', `${this.name} - ${badge.getDescription()}`);
E
Erich Gamma 已提交
356 357 358
		}
	}

359
	protected _updateClass(): void {
E
Erich Gamma 已提交
360 361 362 363 364 365 366 367
		if (this.cssClass) {
			this.$badge.removeClass(this.cssClass);
		}

		this.cssClass = this.getAction().class;
		this.$badge.addClass(this.cssClass);
	}

368
	protected _updateChecked(): void {
E
Erich Gamma 已提交
369 370 371 372 373 374 375
		if (this.getAction().checked) {
			this.$e.addClass('active');
		} else {
			this.$e.removeClass('active');
		}
	}

376 377
	private handleBadgeChangeEvenet(): void {
		const action = this.getAction();
378
		if (action instanceof ActivityAction) {
379
			this.updateBadge(action.getBadge());
E
Erich Gamma 已提交
380 381 382
		}
	}

383
	protected _updateEnabled(): void {
E
Erich Gamma 已提交
384 385 386 387 388 389 390 391 392 393
		if (this.getAction().enabled) {
			this.builder.removeClass('disabled');
		} else {
			this.builder.addClass('disabled');
		}
	}

	public dispose(): void {
		super.dispose();

394 395
		dispose(this.toDispose);

E
Erich Gamma 已提交
396 397 398
		this.$badge.destroy();
		this.$e.destroy();
	}
399 400 401 402 403 404 405
}

class ManageExtensionAction extends Action {

	constructor(
		@ICommandService private commandService: ICommandService
	) {
406
		super('activitybar.manage.extension', nls.localize('manageExtension', "Manage Extension"));
407 408
	}

409 410
	public run(viewlet: ViewletDescriptor): TPromise<any> {
		return this.commandService.executeCommand('_extensions.manage', viewlet.extensionId);
411
	}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

class OpenViewletAction extends Action {

	constructor(
		public viewlet: ViewletDescriptor,
		@IPartService private partService: IPartService,
		@IViewletService private viewletService: IViewletService
	) {
		super(viewlet.id, viewlet.name);
	}

	public run(): TPromise<any> {
		const sideBarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
		const activeViewlet = this.viewletService.getActiveViewlet();

		// Hide sidebar if selected viewlet already visible
		if (sideBarVisible && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
			this.partService.setSideBarHidden(true);
		} else {
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
		}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		return TPromise.as(true);
	}
}

export class ToggleViewletPinnedAction extends Action {

	constructor(
		private viewlet: ViewletDescriptor,
		@IActivityBarService private activityBarService: IActivityBarService
	) {
		super('activitybar.show.toggleViewletPinned', viewlet ? viewlet.name : nls.localize('toggle', "Toggle Viewlet Pinned"));

		this.checked = this.viewlet && this.activityBarService.isPinned(this.viewlet.id);
	}

	public run(context?: ViewletDescriptor): TPromise<any> {
		const viewlet = this.viewlet || context;

		if (this.activityBarService.isPinned(viewlet.id)) {
			this.activityBarService.unpin(viewlet.id);
		} else {
			this.activityBarService.pin(viewlet.id);
		}

459 460
		return TPromise.as(true);
	}
E
Erich Gamma 已提交
461
}