activitybarPart.ts 10.7 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');
A
Alex Dima 已提交
10
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
11
import {Builder, $} from 'vs/base/browser/builder';
A
tslint  
Alex Dima 已提交
12
import {Action} from 'vs/base/common/actions';
E
Erich Gamma 已提交
13 14
import errors = require('vs/base/common/errors');
import {ActionsOrientation, ActionBar, IActionItem} from 'vs/base/browser/ui/actionbar/actionbar';
J
Joao Moreno 已提交
15
import {/*CONTEXT,*/ ToolBar} from 'vs/base/browser/ui/toolbar/toolbar';
E
Erich Gamma 已提交
16
import {Registry} from 'vs/platform/platform';
17
import {CompositeEvent, EventType} from 'vs/workbench/common/events';
I
isidor 已提交
18
import {ViewletDescriptor, ViewletRegistry, Extensions as ViewletExtensions} from 'vs/workbench/browser/viewlet';
E
Erich Gamma 已提交
19 20 21 22 23 24 25 26
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 {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
A
tslint  
Alex Dima 已提交
27
import {IMessageService} from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
28
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
29
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
J
Joao Moreno 已提交
30 31 32 33
// import {Scope, IActionBarRegistry, Extensions as ActionBarExtensions, prepareActions} from 'vs/workbench/browser/actionBarRegistry';
// import Severity from 'vs/base/common/severity';
// import {IAction} from 'vs/base/common/actions';
// import events = require('vs/base/common/events');
E
Erich Gamma 已提交
34 35 36 37

export class ActivitybarPart extends Part implements IActivityService {
	public serviceId = IActivityService;
	private viewletSwitcherBar: ActionBar;
J
Joao Moreno 已提交
38
	// private globalViewletSwitcherBar: ActionBar;
E
Erich Gamma 已提交
39 40 41 42 43
	private globalToolBar: ToolBar;
	private activityActionItems: { [actionId: string]: IActionItem; };
	private viewletIdToActions: { [viewletId: string]: ActivityAction; };

	constructor(
44 45 46 47 48 49
		id: string,
		@IViewletService private viewletService: IViewletService,
		@IMessageService private messageService: IMessageService,
		@ITelemetryService private telemetryService: ITelemetryService,
		@IEventService private eventService: IEventService,
		@IContextMenuService private contextMenuService: IContextMenuService,
50 51
		@IKeybindingService private keybindingService: IKeybindingService,
		@IInstantiationService private instantiationService: IInstantiationService
E
Erich Gamma 已提交
52 53 54 55 56 57 58 59 60 61 62 63
	) {
		super(id);

		this.activityActionItems = {};
		this.viewletIdToActions = {};

		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate viewlet action on opening of a viewlet
A
Alex Dima 已提交
64
		this.toUnbind.push(this.eventService.addListener2(EventType.COMPOSITE_OPENING, (e: CompositeEvent) => this.onCompositeOpening(e)));
E
Erich Gamma 已提交
65 66

		// Deactivate viewlet action on close
A
Alex Dima 已提交
67
		this.toUnbind.push(this.eventService.addListener2(EventType.COMPOSITE_CLOSED, (e: CompositeEvent) => this.onCompositeClosed(e)));
E
Erich Gamma 已提交
68 69
	}

70
	private onCompositeOpening(e: CompositeEvent): void {
71 72
		if (this.viewletIdToActions[e.compositeId]) {
			this.viewletIdToActions[e.compositeId].activate();
E
Erich Gamma 已提交
73 74 75

			// There can only be one active viewlet action
			for (let key in this.viewletIdToActions) {
76
				if (this.viewletIdToActions.hasOwnProperty(key) && key !== e.compositeId) {
E
Erich Gamma 已提交
77 78 79 80 81 82
					this.viewletIdToActions[key].deactivate();
				}
			}
		}
	}

83
	private onCompositeClosed(e: CompositeEvent): void {
84 85
		if (this.viewletIdToActions[e.compositeId]) {
			this.viewletIdToActions[e.compositeId].deactivate();
E
Erich Gamma 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		}
	}

	public showActivity(viewletId: string, badge: IBadge, clazz?: string): void {
		let action = this.viewletIdToActions[viewletId];
		if (action) {
			action.setBadge(badge);
			if (clazz) {
				action.class = clazz;
			}
		}
	}

	public clearActivity(viewletId: string): void {
		this.showActivity(viewletId, null);
	}

	public createContentArea(parent: Builder): Builder {
		let $el = $(parent);
		let $result = $('.content').appendTo($el);

		// Top Actionbar with action items for each viewlet action
		this.createViewletSwitcher($result.clone());

		// Bottom Toolbar with action items for global actions
J
Joao Moreno 已提交
111
		// this.createGlobalToolBarArea($result.clone()); // not used currently
E
Erich Gamma 已提交
112 113 114 115 116 117 118 119

		return $result;
	}

	private createViewletSwitcher(div: Builder): void {

		// Viewlet switcher is on top
		this.viewletSwitcherBar = new ActionBar(div, {
120
			actionItemProvider: (action: Action) => this.activityActionItems[action.id],
121 122
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('activityBarAriaLabel', "Active View Switcher")
E
Erich Gamma 已提交
123 124 125
		});
		this.viewletSwitcherBar.getContainer().addClass('position-top');

J
Joao Moreno 已提交
126
		// Global viewlet switcher is right below
J
Joao Moreno 已提交
127 128 129 130 131 132
		// this.globalViewletSwitcherBar = new ActionBar(div, {
		// 	actionItemProvider: (action: Action) => this.activityActionItems[action.id],
		// 	orientation: ActionsOrientation.VERTICAL,
		// 	ariaLabel: nls.localize('globalActivityBarAriaLabel', "Active Global View Switcher")
		// });
		// this.globalViewletSwitcherBar.getContainer().addClass('position-bottom');
E
Erich Gamma 已提交
133

J
Joao Moreno 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
		// Build Viewlet Actions in correct order
		const activeViewlet = this.viewletService.getActiveViewlet();
		const registry = (<ViewletRegistry>Registry.as(ViewletExtensions.Viewlets));
		const allViewletActions = registry.getViewlets();
		const actionOptions = { label: true, icon: true };

		const toAction = (viewlet: ViewletDescriptor) => {
			let action = this.instantiationService.createInstance(ViewletActivityAction, viewlet.id + '.activity-bar-action', viewlet);

			let keybinding: string = null;
			let keys = this.keybindingService.lookupKeybindings(viewlet.id).map(k => this.keybindingService.getLabelFor(k));
			if (keys && keys.length) {
				keybinding = keys[0];
			}
E
Erich Gamma 已提交
148

J
Joao Moreno 已提交
149 150
			this.activityActionItems[action.id] = new ActivityActionItem(action, viewlet.name, keybinding);
			this.viewletIdToActions[viewlet.id] = action;
E
Erich Gamma 已提交
151

J
Joao Moreno 已提交
152 153 154
			// Mark active viewlet action as active
			if (activeViewlet && activeViewlet.getId() === viewlet.id) {
				action.activate();
E
Erich Gamma 已提交
155
			}
J
Joao Moreno 已提交
156 157 158 159 160 161 162 163 164 165

			return action;
		};

		// Add to viewlet switcher
		this.viewletSwitcherBar.push(allViewletActions
			.filter(v => !v.isGlobal)
			.sort((v1, v2) => v1.order - v2.order)
			.map(toAction)
		, actionOptions);
E
Erich Gamma 已提交
166 167

		// Add to viewlet switcher
J
Joao Moreno 已提交
168 169 170 171 172
		// this.globalViewletSwitcherBar.push(allViewletActions
		// 	.filter(v => v.isGlobal)
		// 	.sort((v1, v2) => v1.order - v2.order)
		// 	.map(toAction),
		// actionOptions);
J
Joao Moreno 已提交
173 174
	}

J
Joao Moreno 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	// private createGlobalToolBarArea(div: Builder): void {

	// 	// Global action bar is on the bottom
	// 	this.globalToolBar = new ToolBar(div.getHTMLElement(), this.contextMenuService, {
	// 		actionItemProvider: (action: Action) => this.activityActionItems[action.id],
	// 		orientation: ActionsOrientation.VERTICAL
	// 	});
	// 	this.globalToolBar.getContainer().addClass('global');

	// 	this.globalToolBar.actionRunner.addListener2(events.EventType.RUN, (e: any) => {

	// 		// Check for Error
	// 		if (e.error && !errors.isPromiseCanceledError(e.error)) {
	// 			this.messageService.show(Severity.Error, e.error);
	// 		}

	// 		// Log in telemetry
	// 		if (this.telemetryService) {
	// 			this.telemetryService.publicLog('workbenchActionExecuted', { id: e.action.id, from: 'activityBar' });
	// 		}
	// 	});

	// 	// Build Global Actions in correct order
	// 	let primaryActions = this.getGlobalActions(true);
	// 	let secondaryActions = this.getGlobalActions(false);

	// 	if (primaryActions.length + secondaryActions.length > 0) {
	// 		this.globalToolBar.getContainer().addClass('position-bottom');
	// 	}

	// 	// Add to global action bar
	// 	this.globalToolBar.setActions(prepareActions(primaryActions), prepareActions(secondaryActions))();
	// }

	// private getGlobalActions(primary: boolean): IAction[] {
	// 	let actionBarRegistry = <IActionBarRegistry>Registry.as(ActionBarExtensions.Actionbar);

	// 	// Collect actions from actionbar contributor
	// 	let actions: IAction[];
	// 	if (primary) {
	// 		actions = actionBarRegistry.getActionBarActionsForContext(Scope.GLOBAL, CONTEXT);
	// 	} else {
	// 		actions = actionBarRegistry.getSecondaryActionBarActionsForContext(Scope.GLOBAL, CONTEXT);
	// 	}

	// 	return actions.map((action: Action) => {
	// 		if (primary) {
	// 			let keybinding: string = null;
	// 			let keys = this.keybindingService.lookupKeybindings(action.id).map(k => this.keybindingService.getLabelFor(k));
	// 			if (keys && keys.length) {
	// 				keybinding = keys[0];
	// 			}

	// 			let actionItem = actionBarRegistry.getActionItemForContext(Scope.GLOBAL, CONTEXT, action);

	// 			if (!actionItem) {
	// 				actionItem = new ActivityActionItem(action, action.label, keybinding);
	// 			}

	// 			if (actionItem instanceof ActivityActionItem) {
	// 				(<ActivityActionItem> actionItem).keybinding = keybinding;
	// 			}

	// 			this.activityActionItems[action.id] = actionItem;
	// 		}

	// 		return action;
	// 	});
	// }
E
Erich Gamma 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

	public dispose(): void {
		if (this.viewletSwitcherBar) {
			this.viewletSwitcherBar.dispose();
			this.viewletSwitcherBar = null;
		}

		if (this.globalToolBar) {
			this.globalToolBar.dispose();
			this.globalToolBar = null;
		}

		super.dispose();
	}
}

class ViewletActivityAction extends ActivityAction {
	private static preventDoubleClickDelay = 300;
262
	private lastRun: number = 0;
E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275

	private viewlet: ViewletDescriptor;

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

		this.viewlet = viewlet;
	}

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

278 279 280
		// prevent accident trigger on a doubleclick (to help nervous people)
		let now = Date.now();
		if (now - this.lastRun < ViewletActivityAction.preventDoubleClickDelay) {
A
Alex Dima 已提交
281
			return TPromise.as(true);
E
Erich Gamma 已提交
282
		}
283
		this.lastRun = now;
E
Erich Gamma 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

		let sideBarHidden = this.partService.isSideBarHidden();
		let activeViewlet = this.viewletService.getActiveViewlet();

		// Hide sidebar if selected viewlet already visible
		if (!sideBarHidden && activeViewlet && activeViewlet.getId() === this.viewlet.id) {
			this.partService.setSideBarHidden(true);
		}

		// Open viewlet and focus it
		else {
			this.viewletService.openViewlet(this.viewlet.id, true).done(null, errors.onUnexpectedError);
			this.activate();
		}

A
Alex Dima 已提交
299
		return TPromise.as(true);
E
Erich Gamma 已提交
300 301
	}
}