activitybarPart.ts 7.2 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
import { ActionsOrientation, ActionBar, IActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { IComposite } from 'vs/workbench/common/composite';
17
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
J
Johannes Rieken 已提交
18 19 20 21 22 23 24
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';
E
Erich Gamma 已提交
25 26

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

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

P
Pine Wu 已提交
33
	// Serves two purposes:
P
Pine Wu 已提交
34
	// 1. Expose the viewletId that will be assigned to an extension viewlet,
P
Pine Wu 已提交
35
	//    which wouldn't know its viewletId until construction time.
P
Pine Wu 已提交
36
	// 2. When workbench restores sidebar, if the last-opened viewlet is an extension viewlet,
P
Pine Wu 已提交
37
	//    it'll set this value and defer restoration until all extensions are loaded.
P
Pine Wu 已提交
38 39 40
	private _extViewletIdToOpen: string;
	public get extViewletIdToOpen() { return this._extViewletIdToOpen; };
	public set extViewletIdToOpen(viewletId: string) { this._extViewletIdToOpen = viewletId; };
P
Pine Wu 已提交
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,
47
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
48 49 50 51
	) {
		super(id);

		this.activityActionItems = {};
52
		this.compositeIdToActions = {};
E
Erich Gamma 已提交
53 54 55 56 57 58 59

		this.registerListeners();
	}

	private registerListeners(): void {

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

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

65
		this.toUnbind.push(this.viewletService.onDidViewletRegister(() => this.onDidViewletRegister()));
P
Pine Wu 已提交
66 67
	}

68 69 70
	private onActiveCompositeChanged(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].activate();
E
Erich Gamma 已提交
71 72 73
		}
	}

74 75 76
	private onCompositeClosed(composite: IComposite): void {
		if (this.compositeIdToActions[composite.getId()]) {
			this.compositeIdToActions[composite.getId()].deactivate();
E
Erich Gamma 已提交
77 78 79
		}
	}

80
	private onDidViewletRegister(): void {
P
Pine Wu 已提交
81
		this.refreshViewletSwitcher();
82 83
	}

84 85
	public showActivity(compositeId: string, badge: IBadge, clazz?: string): void {
		const action = this.compositeIdToActions[compositeId];
E
Erich Gamma 已提交
86 87 88 89 90 91 92 93
		if (action) {
			action.setBadge(badge);
			if (clazz) {
				action.class = clazz;
			}
		}
	}

94 95
	public clearActivity(compositeId: string): void {
		this.showActivity(compositeId, null);
E
Erich Gamma 已提交
96 97 98
	}

	public createContentArea(parent: Builder): Builder {
99 100
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
101 102

		// Top Actionbar with action items for each viewlet action
P
Pine Wu 已提交
103
		this.createViewletSwitcher($result.clone());
E
Erich Gamma 已提交
104 105 106 107

		return $result;
	}

I
isidor 已提交
108 109
	private createViewletSwitcher(div: Builder): void {
		this.viewletSwitcherBar = new ActionBar(div, {
110
			actionItemProvider: (action: Action) => this.activityActionItems[action.id],
111 112
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('activityBarAriaLabel', "Active View Switcher")
E
Erich Gamma 已提交
113 114
		});

115
		this.fillViewletSwitcher(this.viewletService.getAllViewlets());
P
Pine Wu 已提交
116
	}
J
Joao Moreno 已提交
117

P
Pine Wu 已提交
118 119
	private refreshViewletSwitcher(): void {
		this.viewletSwitcherBar.clear();
120
		this.fillViewletSwitcher(this.viewletService.getAllViewlets());
P
Pine Wu 已提交
121 122 123
	}

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

I
isidor 已提交
128
	private toAction(composite: ViewletDescriptor): ActivityAction {
129
		const action = this.instantiationService.createInstance(ViewletActivityAction, composite.id + '.activity-bar-action', composite);
P
Pine Wu 已提交
130
		// Store the viewletId of the extension viewlet that is about to open.
P
Pine Wu 已提交
131 132
		// Later retrieved by TreeExplorerViewlet, which wouldn't know its id until
		// its construction at runtime.
P
Pine Wu 已提交
133 134
		action.onOpenExtViewlet((viewletId) => {
			this._extViewletIdToOpen = viewletId;
P
Pine Wu 已提交
135
		});
I
isidor 已提交
136

137
		this.activityActionItems[action.id] = new ActivityActionItem(action, composite.name, this.getKeybindingLabel(composite.id));
I
isidor 已提交
138 139 140 141 142
		this.compositeIdToActions[composite.id] = action;

		return action;
	};

143 144 145 146 147 148 149 150 151
	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 已提交
152
	public dispose(): void {
I
isidor 已提交
153 154 155 156 157
		if (this.viewletSwitcherBar) {
			this.viewletSwitcherBar.dispose();
			this.viewletSwitcherBar = null;
		}

E
Erich Gamma 已提交
158 159 160 161
		super.dispose();
	}
}

162
class ViewletActivityAction extends ActivityAction {
E
Erich Gamma 已提交
163
	private static preventDoubleClickDelay = 300;
164
	private lastRun: number = 0;
E
Erich Gamma 已提交
165

P
Pine Wu 已提交
166 167
	private _onOpenExtViewlet = new Emitter<string>();
	public get onOpenExtViewlet(): Event<string> { return this._onOpenExtViewlet.event; };
P
Pine Wu 已提交
168

E
Erich Gamma 已提交
169
	constructor(
170 171
		id: string,
		private viewlet: ViewletDescriptor,
172 173
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
E
Erich Gamma 已提交
174
	) {
175
		super(id, viewlet.name, viewlet.cssClass);
E
Erich Gamma 已提交
176 177
	}

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

180
		// prevent accident trigger on a doubleclick (to help nervous people)
181
		const now = Date.now();
182
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
A
Alex Dima 已提交
183
			return TPromise.as(true);
E
Erich Gamma 已提交
184
		}
185
		this.lastRun = now;
E
Erich Gamma 已提交
186

I
isidor 已提交
187 188
		const sideBarHidden = this.partService.isSideBarHidden();
		const activeViewlet = this.viewletService.getActiveViewlet();
E
Erich Gamma 已提交
189 190

		// Hide sidebar if selected viewlet already visible
191
		if (!sideBarHidden && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
E
Erich Gamma 已提交
192
			this.partService.setSideBarHidden(true);
I
isidor 已提交
193
		} else {
P
Pine Wu 已提交
194
			if (this.viewlet.isExtension) {
P
Pine Wu 已提交
195
				this._onOpenExtViewlet.fire(this.viewlet.id);
P
Pine Wu 已提交
196
			}
197
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
I
isidor 已提交
198
			this.activate();
E
Erich Gamma 已提交
199
		}
200 201

		return TPromise.as(true);
I
isidor 已提交
202 203
	}
}