activitybarPart.ts 7.1 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 11 12
import { TPromise } from 'vs/base/common/winjs.base';
import { Builder, $ } from 'vs/base/browser/builder';
import { Action } from 'vs/base/common/actions';
E
Erich Gamma 已提交
13
import errors = require('vs/base/common/errors');
J
Johannes Rieken 已提交
14 15
import { ActionsOrientation, ActionBar, IActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { IComposite } from 'vs/workbench/common/composite';
16
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
J
Johannes Rieken 已提交
17 18
import { Part } from 'vs/workbench/browser/part';
import { ActivityAction, ActivityActionItem } from 'vs/workbench/browser/parts/activitybar/activityAction';
B
Benjamin Pasero 已提交
19
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
J
Johannes Rieken 已提交
20
import { IActivityService, IBadge } from 'vs/workbench/services/activity/common/activityService';
21
import { IPartService, Parts } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
22 23
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
E
Erich Gamma 已提交
24 25

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

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

	constructor(
33 34
		id: string,
		@IViewletService private viewletService: IViewletService,
35
		@IKeybindingService private keybindingService: IKeybindingService,
I
isidor 已提交
36
		@IInstantiationService private instantiationService: IInstantiationService,
37
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
38 39 40 41
	) {
		super(id);

		this.activityActionItems = {};
42
		this.compositeIdToActions = {};
E
Erich Gamma 已提交
43

44
		// Update viewlet switcher when external viewlets become ready
45
		this.viewletService.onReady().then(() => this.updateViewletSwitcher());
46

E
Erich Gamma 已提交
47 48 49 50 51 52
		this.registerListeners();
	}

	private registerListeners(): void {

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

		// Deactivate viewlet action on close
56
		this.toUnbind.push(this.viewletService.onDidViewletClose(viewlet => this.onCompositeClosed(viewlet)));
P
Pine Wu 已提交
57 58
	}

59 60 61
	private onActiveCompositeChanged(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].activate();
E
Erich Gamma 已提交
62 63 64
		}
	}

65 66 67
	private onCompositeClosed(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].deactivate();
E
Erich Gamma 已提交
68 69 70
		}
	}

71 72
	public showActivity(compositeId: string, badge: IBadge, clazz?: string): void {
		const action = this.compositeIdToActions[compositeId];
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80
		if (action) {
			action.setBadge(badge);
			if (clazz) {
				action.class = clazz;
			}
		}
	}

81 82
	public clearActivity(compositeId: string): void {
		this.showActivity(compositeId, null);
E
Erich Gamma 已提交
83 84 85
	}

	public createContentArea(parent: Builder): Builder {
86 87
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
88 89

		// Top Actionbar with action items for each viewlet action
P
Pine Wu 已提交
90
		this.createViewletSwitcher($result.clone());
E
Erich Gamma 已提交
91 92 93 94

		return $result;
	}

I
isidor 已提交
95 96
	private createViewletSwitcher(div: Builder): void {
		this.viewletSwitcherBar = new ActionBar(div, {
97
			actionItemProvider: (action: Action) => this.activityActionItems[action.id],
98 99
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('activityBarAriaLabel', "Active View Switcher")
E
Erich Gamma 已提交
100 101
		});

102
		this.updateViewletSwitcher();
P
Pine Wu 已提交
103 104
	}

105 106
	private updateViewletSwitcher() {
		const viewlets = this.viewletService.getViewlets();
107

P
Pine Wu 已提交
108
		// Pull out viewlets no longer needed
P
Pine Wu 已提交
109 110 111
		const newViewletIds = viewlets.map(v => v.id);
		const existingViewletIds = Object.keys(this.compositeIdToActions);
		existingViewletIds.forEach(viewletId => {
P
Pine Wu 已提交
112 113 114 115 116
			if (newViewletIds.indexOf(viewletId) === -1) {
				this.pullViewlet(viewletId);
			}
		});

117
		// Built actions for viewlets to show
P
Pine Wu 已提交
118
		const actionsToPush = viewlets
P
Pine Wu 已提交
119 120 121
			.filter(viewlet => !this.compositeIdToActions[viewlet.id])
			.map(viewlet => this.toAction(viewlet));

122
		// Add to viewlet switcher
P
Pine Wu 已提交
123
		this.viewletSwitcherBar.push(actionsToPush, { label: true, icon: true });
124 125 126 127 128 129 130 131 132

		// Make sure to activate the active one
		const activeViewlet = this.viewletService.getActiveViewlet();
		if (activeViewlet) {
			const activeViewletEntry = this.compositeIdToActions[activeViewlet.getId()];
			if (activeViewletEntry) {
				activeViewletEntry.activate();
			}
		}
P
Pine Wu 已提交
133 134 135 136
	}

	private pullViewlet(viewletId: string): void {
		const index = Object.keys(this.compositeIdToActions).indexOf(viewletId);
137

P
Pine Wu 已提交
138 139
		const action = this.compositeIdToActions[viewletId];
		action.dispose();
140 141 142
		delete this.compositeIdToActions[viewletId];

		const actionItem = this.activityActionItems[action.id];
P
Pine Wu 已提交
143
		actionItem.dispose();
144 145
		delete this.activityActionItems[action.id];

P
Pine Wu 已提交
146
		this.viewletSwitcherBar.pull(index);
I
isidor 已提交
147
	}
J
Joao Moreno 已提交
148

I
isidor 已提交
149
	private toAction(composite: ViewletDescriptor): ActivityAction {
150
		const action = this.instantiationService.createInstance(ViewletActivityAction, `${composite.id}.activity-bar-action`, composite);
I
isidor 已提交
151

152
		this.activityActionItems[action.id] = new ActivityActionItem(action, composite.name, this.getKeybindingLabel(composite.id));
I
isidor 已提交
153 154 155 156 157
		this.compositeIdToActions[composite.id] = action;

		return action;
	};

158 159 160 161 162 163 164 165 166
	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 已提交
167
	public dispose(): void {
I
isidor 已提交
168 169 170 171 172
		if (this.viewletSwitcherBar) {
			this.viewletSwitcherBar.dispose();
			this.viewletSwitcherBar = null;
		}

E
Erich Gamma 已提交
173 174 175 176
		super.dispose();
	}
}

177
class ViewletActivityAction extends ActivityAction {
E
Erich Gamma 已提交
178
	private static preventDoubleClickDelay = 300;
179
	private lastRun: number = 0;
E
Erich Gamma 已提交
180 181

	constructor(
182 183
		id: string,
		private viewlet: ViewletDescriptor,
184 185
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
186
	) {
187
		super(id, viewlet.name, viewlet.cssClass);
E
Erich Gamma 已提交
188 189
	}

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

192
		// prevent accident trigger on a doubleclick (to help nervous people)
193
		const now = Date.now();
194
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
A
Alex Dima 已提交
195
			return TPromise.as(true);
E
Erich Gamma 已提交
196
		}
197
		this.lastRun = now;
E
Erich Gamma 已提交
198

199
		const sideBarVisible = this.partService.isVisible(Parts.SIDEBAR_PART);
I
isidor 已提交
200
		const activeViewlet = this.viewletService.getActiveViewlet();
E
Erich Gamma 已提交
201 202

		// Hide sidebar if selected viewlet already visible
203
		if (sideBarVisible && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
E
Erich Gamma 已提交
204
			this.partService.setSideBarHidden(true);
I
isidor 已提交
205
		} else {
206
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
I
isidor 已提交
207
			this.activate();
E
Erich Gamma 已提交
208
		}
209 210

		return TPromise.as(true);
I
isidor 已提交
211 212
	}
}