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;
I
isidor 已提交
30
	private viewletSwitcherBar: ActionBar;
E
Erich Gamma 已提交
31
	private activityActionItems: { [actionId: string]: IActionItem; };
32
	private compositeIdToActions: { [compositeId: string]: ActivityAction; };
E
Erich Gamma 已提交
33

34
	private viewletsToggleStatus: { [viewletId: string]: boolean; };
35
	private registeredViewlets: string[];
36

37 38 39 40
	// Since the internal viewlet can't get the external viewlet's id upon initialization,
	// internal viewlet initializes with id like 'workbench.view.customTreeExplorerViewlet.1'.
	// This maps the internal viewlet id to externalId like 'workbench.view.customTreeExplorerViewlet.myTree'
	private internalViewletIdMap: { [externalViewletId: string]: string; };
P
Pine Wu 已提交
41 42 43
	private externalViewletIdToOpen: string;

	private VIEWLETS_TOGGLE_STATUS = 'workbench.activityBar.viewletsToggleStatus';
44

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

		this.activityActionItems = {};
56
		this.compositeIdToActions = {};
E
Erich Gamma 已提交
57

58 59 60 61
		const viewletsToggleStatusJson = this.storageService.get(this.VIEWLETS_TOGGLE_STATUS);
		this.viewletsToggleStatus = viewletsToggleStatusJson ? JSON.parse(viewletsToggleStatusJson) : {};

		this.registeredViewlets = [];
62

63 64
		this.internalViewletIdMap = {};

E
Erich Gamma 已提交
65 66 67 68 69 70
		this.registerListeners();
	}

	private registerListeners(): void {

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

		// Deactivate viewlet action on close
74
		this.toUnbind.push(this.viewletService.onDidViewletClose(viewlet => this.onCompositeClosed(viewlet)));
75 76 77

		// Update activity bar on registering an external viewlet
		this.toUnbind.push(
78 79
			(<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets))
				.onDidRegisterExternalViewlets(descriptors => this.onDidRegisterExternalViewlets(descriptors))
80 81 82
		);
	}

83 84
	private onDidRegisterExternalViewlets(descriptors: ViewletDescriptor[]) {
		descriptors.forEach(descriptor => {
85 86
			this.registeredViewlets.push(descriptor.id);
			if (this.viewletsToggleStatus[descriptor.id]) {
87 88
				this.viewletSwitcherBar.push(this.toAction(descriptor), { label: true, icon: true });
			}
P
Pine Wu 已提交
89
		});
E
Erich Gamma 已提交
90 91
	}

92 93 94
	private onActiveCompositeChanged(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].activate();
E
Erich Gamma 已提交
95 96 97
		}
	}

98 99 100
	private onCompositeClosed(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].deactivate();
E
Erich Gamma 已提交
101 102 103
		}
	}

P
Pine Wu 已提交
104
	getRegisteredViewletsToggleStatus(): { [viewletId: string]: boolean } {
105 106 107 108 109 110 111
		const result = {};
		this.registeredViewlets.forEach(viewletId => {
			result[viewletId] = this.viewletsToggleStatus[viewletId];
		});
		return result;
	}

P
Pine Wu 已提交
112
	toggleViewlet(viewletId: string): void {
113 114
		this.viewletsToggleStatus[viewletId] = !this.viewletsToggleStatus[viewletId];
		this.setViewletsToggleStatus();
P
Pine Wu 已提交
115
		this.refreshViewletSwitcher();
116 117 118 119
	}

	private setViewletsToggleStatus(): void {
		this.storageService.store(this.VIEWLETS_TOGGLE_STATUS, JSON.stringify(this.viewletsToggleStatus));
120 121
	}

P
Pine Wu 已提交
122 123 124 125
	getExternalViewletIdToOpen(): string {
		return this.externalViewletIdToOpen;
	}

126 127 128 129
	setInternalViewletId(externalViewletId: string, internalViewletId: string): void {
		this.internalViewletIdMap[externalViewletId] = internalViewletId;
	}

130 131
	public showActivity(compositeId: string, badge: IBadge, clazz?: string): void {
		const action = this.compositeIdToActions[compositeId];
E
Erich Gamma 已提交
132 133 134 135 136 137 138 139
		if (action) {
			action.setBadge(badge);
			if (clazz) {
				action.class = clazz;
			}
		}
	}

140 141
	public clearActivity(compositeId: string): void {
		this.showActivity(compositeId, null);
E
Erich Gamma 已提交
142 143 144
	}

	public createContentArea(parent: Builder): Builder {
145 146
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
147 148

		// Top Actionbar with action items for each viewlet action
P
Pine Wu 已提交
149
		this.createViewletSwitcher($result.clone().addClass('position-top'));
E
Erich Gamma 已提交
150 151 152 153

		return $result;
	}

I
isidor 已提交
154 155
	private createViewletSwitcher(div: Builder): void {
		this.viewletSwitcherBar = new ActionBar(div, {
156
			actionItemProvider: (action: Action) => this.activityActionItems[action.id],
157 158
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('activityBarAriaLabel', "Active View Switcher")
E
Erich Gamma 已提交
159 160
		});

P
Pine Wu 已提交
161 162 163 164
		// Load stock viewlets
		const allViewlets = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets)).getViewlets().filter(v => !v.isExternal);
		this.fillViewletSwitcher(allViewlets);
	}
J
Joao Moreno 已提交
165

P
Pine Wu 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	private refreshViewletSwitcher(): void {
		this.viewletSwitcherBar.clear();

		// Load stock viewlets + enabled external viewlets
		const allEnabledViewlets = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets)).getViewlets().filter(descriptor => {
			if (!descriptor.isExternal) {
				return true;
			} else {
				return this.viewletsToggleStatus[descriptor.id];
			}
		});
		this.fillViewletSwitcher(allEnabledViewlets);
	}

	private fillViewletSwitcher(viewlets: ViewletDescriptor[]) {
		// Build Viewlet Actions in correct order
		const viewletActions = viewlets.sort((v1, v2) => v1.order - v2.order).map(v => this.toAction(v));
I
isidor 已提交
183 184
		this.viewletSwitcherBar.push(viewletActions, { label: true, icon: true });
	}
J
Joao Moreno 已提交
185

I
isidor 已提交
186
	private toAction(composite: ViewletDescriptor): ActivityAction {
187
		const action = this.instantiationService.createInstance(ViewletActivityAction, composite.id + '.activity-bar-action', composite, this.internalViewletIdMap);
P
Pine Wu 已提交
188 189 190
		action.onOpenViewlet((viewletId) => {
			this.externalViewletIdToOpen = viewletId;
		});
I
isidor 已提交
191

192
		this.activityActionItems[action.id] = new ActivityActionItem(action, composite.name, this.getKeybindingLabel(composite.id));
I
isidor 已提交
193 194 195 196 197
		this.compositeIdToActions[composite.id] = action;

		return action;
	};

198 199 200 201 202 203 204 205 206
	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 已提交
207
	public dispose(): void {
I
isidor 已提交
208 209 210 211 212
		if (this.viewletSwitcherBar) {
			this.viewletSwitcherBar.dispose();
			this.viewletSwitcherBar = null;
		}

E
Erich Gamma 已提交
213 214 215 216
		super.dispose();
	}
}

217
class ViewletActivityAction extends ActivityAction {
E
Erich Gamma 已提交
218
	private static preventDoubleClickDelay = 300;
219
	private lastRun: number = 0;
E
Erich Gamma 已提交
220

P
Pine Wu 已提交
221 222 223
	private _onOpenViewlet = new Emitter<string>();
	get onOpenViewlet(): Event<string> { return this._onOpenViewlet.event; };

E
Erich Gamma 已提交
224
	constructor(
225 226 227
		id: string,
		private viewlet: ViewletDescriptor,
		private internalViewletIdMap: { [externalViewletId: string]: string; },
228 229
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
230
	) {
231
		super(id, viewlet.name, viewlet.cssClass);
E
Erich Gamma 已提交
232 233
	}

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

236
		// prevent accident trigger on a doubleclick (to help nervous people)
237
		const now = Date.now();
238
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
A
Alex Dima 已提交
239
			return TPromise.as(true);
E
Erich Gamma 已提交
240
		}
241
		this.lastRun = now;
E
Erich Gamma 已提交
242

I
isidor 已提交
243 244
		const sideBarHidden = this.partService.isSideBarHidden();
		const activeViewlet = this.viewletService.getActiveViewlet();
E
Erich Gamma 已提交
245 246

		// Hide sidebar if selected viewlet already visible
247
		if (!sideBarHidden && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
E
Erich Gamma 已提交
248
			this.partService.setSideBarHidden(true);
I
isidor 已提交
249
		} else {
P
Pine Wu 已提交
250
			this._onOpenViewlet.fire(this.viewlet.id);
251 252 253 254 255 256
			// If the viewlet is external and has been initialized, find the internal viewlet id for opening
			if (this.viewlet.isExternal && this.internalViewletIdMap[this.viewlet.id]) {
				this.viewletService.openViewlet(this.internalViewletIdMap[this.viewlet.id], true).done(null, errors.onUnexpectedError);
			} else {
				this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
			}
I
isidor 已提交
257
			this.activate();
E
Erich Gamma 已提交
258
		}
259 260

		return TPromise.as(true);
I
isidor 已提交
261 262
	}
}