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

'use strict';

B
Benjamin Pasero 已提交
8
import 'vs/css!./media/activitybarpart';
9
import nls = require('vs/nls');
J
Johannes Rieken 已提交
10
import { TPromise } from 'vs/base/common/winjs.base';
P
Pine Wu 已提交
11
import Event, { Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
12 13
import { Builder, $ } from 'vs/base/browser/builder';
import { Action } from 'vs/base/common/actions';
E
Erich Gamma 已提交
14
import errors = require('vs/base/common/errors');
J
Johannes Rieken 已提交
15 16 17 18 19 20 21 22 23 24 25
import { ActionsOrientation, ActionBar, IActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { Registry } from 'vs/platform/platform';
import { IComposite } from 'vs/workbench/common/composite';
import { ViewletDescriptor, ViewletRegistry, Extensions as ViewletExtensions } from 'vs/workbench/browser/viewlet';
import { Part } from 'vs/workbench/browser/part';
import { ActivityAction, ActivityActionItem } from 'vs/workbench/browser/parts/activitybar/activityAction';
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
import { IActivityService, IBadge } from 'vs/workbench/services/activity/common/activityService';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
P
Pine Wu 已提交
26
import { IStorageService } from 'vs/platform/storage/common/storage';
E
Erich Gamma 已提交
27 28

export class ActivitybarPart extends Part implements IActivityService {
29
	public _serviceBrand: any;
P
Pine Wu 已提交
30

I
isidor 已提交
31
	private viewletSwitcherBar: ActionBar;
E
Erich Gamma 已提交
32
	private activityActionItems: { [actionId: string]: IActionItem; };
33
	private compositeIdToActions: { [compositeId: string]: ActivityAction; };
E
Erich Gamma 已提交
34

P
Pine Wu 已提交
35 36
	private enabledExternalViewlets: string[];
	private registeredViewlets: { [viewletId: string]: ViewletDescriptor; };
37

P
Pine Wu 已提交
38 39
	private externalViewletIdToOpen: string;

P
Pine Wu 已提交
40
	private static ENABLED_EXTERNAL_VIEWLETS = 'workbench.activityBar.enabledExternalViewlets';
41

E
Erich Gamma 已提交
42
	constructor(
43 44
		id: string,
		@IViewletService private viewletService: IViewletService,
45
		@IKeybindingService private keybindingService: IKeybindingService,
I
isidor 已提交
46
		@IInstantiationService private instantiationService: IInstantiationService,
P
Pine Wu 已提交
47 48
		@IPartService private partService: IPartService,
		@IStorageService private storageService: IStorageService
E
Erich Gamma 已提交
49 50 51 52
	) {
		super(id);

		this.activityActionItems = {};
53
		this.compositeIdToActions = {};
E
Erich Gamma 已提交
54

P
Pine Wu 已提交
55 56 57
		const enabledExternalViewletsJson = this.storageService.get(ActivitybarPart.ENABLED_EXTERNAL_VIEWLETS);
		this.enabledExternalViewlets = enabledExternalViewletsJson ? JSON.parse(enabledExternalViewletsJson) : [];
		this.registeredViewlets = {};
58

E
Erich Gamma 已提交
59 60 61 62 63 64
		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate viewlet action on opening of a viewlet
65
		this.toUnbind.push(this.viewletService.onDidViewletOpen(viewlet => this.onActiveCompositeChanged(viewlet)));
E
Erich Gamma 已提交
66 67

		// Deactivate viewlet action on close
68
		this.toUnbind.push(this.viewletService.onDidViewletClose(viewlet => this.onCompositeClosed(viewlet)));
69

P
Pine Wu 已提交
70
		// Update activity bar on registering external viewlets
71
		this.toUnbind.push(
72
			(<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets))
P
Pine Wu 已提交
73
				.onDidRegisterExternalViewlets(viewlets => this.onDidRegisterExternalViewlets(viewlets))
74 75 76
		);
	}

P
Pine Wu 已提交
77 78 79
	private onDidRegisterExternalViewlets(viewlets: ViewletDescriptor[]): void {
		viewlets.forEach(v => {
			this.registeredViewlets[v.id] = v;
P
Pine Wu 已提交
80 81 82
		});

		this.viewletSwitcherBar.push(this.getAllEnabledExternalViewlets().map(d => this.toAction(d)), { label: true, icon: true });
P
Pine Wu 已提交
83 84 85
		if (this.externalViewletIdToOpen) {
			this.compositeIdToActions[this.externalViewletIdToOpen].run().done();
		}
P
Pine Wu 已提交
86 87
	}

88 89 90
	private onActiveCompositeChanged(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].activate();
E
Erich Gamma 已提交
91 92 93
		}
	}

94 95 96
	private onCompositeClosed(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].deactivate();
E
Erich Gamma 已提交
97 98 99
		}
	}

P
Pine Wu 已提交
100
	getIsEnabledForRegisteredViewlets(): { [viewletId: string]: boolean } {
101
		const result = {};
P
Pine Wu 已提交
102 103 104
		for (let viewletId in this.registeredViewlets) {
			result[viewletId] = (this.enabledExternalViewlets.indexOf(viewletId) !== -1);
		}
105 106 107
		return result;
	}

P
Pine Wu 已提交
108
	toggleViewlet(viewletId: string): void {
P
Pine Wu 已提交
109 110 111 112 113 114 115 116
		const index = this.enabledExternalViewlets.indexOf(viewletId);
		if (index === -1) {
			this.enabledExternalViewlets.push(viewletId);
		} else {
			this.enabledExternalViewlets.splice(index, 1);
		}

		this.setEnabledExternalViewlets();
P
Pine Wu 已提交
117
		this.refreshViewletSwitcher();
118 119
	}

P
Pine Wu 已提交
120 121
	private setEnabledExternalViewlets(): void {
		this.storageService.store(ActivitybarPart.ENABLED_EXTERNAL_VIEWLETS, JSON.stringify(this.enabledExternalViewlets));
122 123
	}

124 125
	public showActivity(compositeId: string, badge: IBadge, clazz?: string): void {
		const action = this.compositeIdToActions[compositeId];
E
Erich Gamma 已提交
126 127 128 129 130 131 132 133
		if (action) {
			action.setBadge(badge);
			if (clazz) {
				action.class = clazz;
			}
		}
	}

134 135
	public clearActivity(compositeId: string): void {
		this.showActivity(compositeId, null);
E
Erich Gamma 已提交
136 137 138
	}

	public createContentArea(parent: Builder): Builder {
139 140
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
141 142

		// Top Actionbar with action items for each viewlet action
P
Pine Wu 已提交
143
		this.createViewletSwitcher($result.clone().addClass('position-top'));
E
Erich Gamma 已提交
144 145 146 147

		return $result;
	}

I
isidor 已提交
148 149
	private createViewletSwitcher(div: Builder): void {
		this.viewletSwitcherBar = new ActionBar(div, {
150
			actionItemProvider: (action: Action) => this.activityActionItems[action.id],
151 152
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('activityBarAriaLabel', "Active View Switcher")
E
Erich Gamma 已提交
153 154
		});

P
Pine Wu 已提交
155
		const allStockViewlets = this.getAllStockViewlets();
P
Pine Wu 已提交
156
		this.fillViewletSwitcher(allStockViewlets);
P
Pine Wu 已提交
157
	}
J
Joao Moreno 已提交
158

P
Pine Wu 已提交
159 160 161
	private refreshViewletSwitcher(): void {
		this.viewletSwitcherBar.clear();

P
Pine Wu 已提交
162
		const allStockViewlets = this.getAllStockViewlets();
P
Pine Wu 已提交
163 164
		const allEnabledExternalViewlets = this.getAllEnabledExternalViewlets();
		this.fillViewletSwitcher(allStockViewlets.concat(allEnabledExternalViewlets));
P
Pine Wu 已提交
165 166 167
	}

	private fillViewletSwitcher(viewlets: ViewletDescriptor[]) {
P
Pine Wu 已提交
168
		const viewletActions = viewlets.map(v => this.toAction(v));
I
isidor 已提交
169 170
		this.viewletSwitcherBar.push(viewletActions, { label: true, icon: true });
	}
J
Joao Moreno 已提交
171

P
Pine Wu 已提交
172 173 174 175 176 177 178 179
	// Get an ordered list of all stock viewlets
	private getAllStockViewlets(): ViewletDescriptor[] {
		return (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets))
			.getViewlets()
			.filter(viewlet => !viewlet.isExternal)
			.sort((v1, v2) => v1.order - v2.order);
	}

P
Pine Wu 已提交
180
	// Get a list of all enabled external viewlets, ordered by the enabling sequence
P
Pine Wu 已提交
181 182 183 184 185 186 187 188 189 190
	private getAllEnabledExternalViewlets(): ViewletDescriptor[] {
		const externalViewletsToShow: ViewletDescriptor[] = [];
		this.enabledExternalViewlets.forEach(viewletId => {
			if (this.registeredViewlets[viewletId]) {
				externalViewletsToShow.push(this.registeredViewlets[viewletId]);
			}
		});
		return externalViewletsToShow;
	}

I
isidor 已提交
191
	private toAction(composite: ViewletDescriptor): ActivityAction {
192
		const action = this.instantiationService.createInstance(ViewletActivityAction, composite.id + '.activity-bar-action', composite);
P
Pine Wu 已提交
193 194 195
		// Store the viewletId of the external viewlet that is about to open.
		// Later retrieved by TreeExplorerViewlet, which wouldn't know its id until
		// its construction at runtime.
P
Pine Wu 已提交
196
		action.onOpenExternalViewlet((viewletId) => {
P
Pine Wu 已提交
197 198
			this.externalViewletIdToOpen = viewletId;
		});
I
isidor 已提交
199

200
		this.activityActionItems[action.id] = new ActivityActionItem(action, composite.name, this.getKeybindingLabel(composite.id));
I
isidor 已提交
201 202 203 204 205
		this.compositeIdToActions[composite.id] = action;

		return action;
	};

P
Pine Wu 已提交
206 207 208 209
	setExternalViewletIdToOpen(viewletId: string): void {
		this.externalViewletIdToOpen = viewletId;
	}

P
Pine Wu 已提交
210 211 212 213
	getExternalViewletIdToOpen(): string {
		return this.externalViewletIdToOpen;
	}

214 215 216 217 218 219 220 221 222
	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 已提交
223
	public dispose(): void {
I
isidor 已提交
224 225 226 227 228
		if (this.viewletSwitcherBar) {
			this.viewletSwitcherBar.dispose();
			this.viewletSwitcherBar = null;
		}

E
Erich Gamma 已提交
229 230 231 232
		super.dispose();
	}
}

233
class ViewletActivityAction extends ActivityAction {
E
Erich Gamma 已提交
234
	private static preventDoubleClickDelay = 300;
235
	private lastRun: number = 0;
E
Erich Gamma 已提交
236

P
Pine Wu 已提交
237 238
	private _onOpenExternalViewlet = new Emitter<string>();
	get onOpenExternalViewlet(): Event<string> { return this._onOpenExternalViewlet.event; };
P
Pine Wu 已提交
239

E
Erich Gamma 已提交
240
	constructor(
241 242
		id: string,
		private viewlet: ViewletDescriptor,
243 244
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
245
	) {
246
		super(id, viewlet.name, viewlet.cssClass);
E
Erich Gamma 已提交
247 248
	}

A
Alex Dima 已提交
249
	public run(): TPromise<any> {
E
Erich Gamma 已提交
250

251
		// prevent accident trigger on a doubleclick (to help nervous people)
252
		const now = Date.now();
253
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
A
Alex Dima 已提交
254
			return TPromise.as(true);
E
Erich Gamma 已提交
255
		}
256
		this.lastRun = now;
E
Erich Gamma 已提交
257

I
isidor 已提交
258 259
		const sideBarHidden = this.partService.isSideBarHidden();
		const activeViewlet = this.viewletService.getActiveViewlet();
E
Erich Gamma 已提交
260 261

		// Hide sidebar if selected viewlet already visible
262
		if (!sideBarHidden && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
E
Erich Gamma 已提交
263
			this.partService.setSideBarHidden(true);
I
isidor 已提交
264
		} else {
P
Pine Wu 已提交
265 266 267
			if (this.viewlet.isExternal) {
				this._onOpenExternalViewlet.fire(this.viewlet.id);
			}
268
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
I
isidor 已提交
269
			this.activate();
E
Erich Gamma 已提交
270
		}
271 272

		return TPromise.as(true);
I
isidor 已提交
273 274
	}
}