activitybarPart.ts 15.4 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 * as nls from 'vs/nls';
J
Johannes Rieken 已提交
10
import { illegalArgument } from 'vs/base/common/errors';
11
import { $ } from 'vs/base/browser/builder';
12
import { ActionsOrientation, ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
13
import { GlobalActivityExtensions, IGlobalActivityRegistry, IActivity } from 'vs/workbench/common/activity';
14
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
15
import { Part } from 'vs/workbench/browser/part';
16
import { GlobalActivityActionItem, GlobalActivityAction, ViewletActivityAction, ToggleViewletAction } from 'vs/workbench/browser/parts/activitybar/activitybarActions';
B
Benjamin Pasero 已提交
17
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
I
isidor 已提交
18
import { IBadge } from 'vs/workbench/services/activity/common/activity';
I
isidor 已提交
19
import { IPartService, Parts, Position as SideBarPosition } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
20
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
21
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
22
import { ToggleActivityBarVisibilityAction } from 'vs/workbench/browser/actions/toggleActivityBarVisibility';
23
import { IThemeService } from 'vs/platform/theme/common/themeService';
I
isidor 已提交
24
import { ACTIVITY_BAR_BACKGROUND, ACTIVITY_BAR_BORDER, ACTIVITY_BAR_FOREGROUND, ACTIVITY_BAR_BADGE_BACKGROUND, ACTIVITY_BAR_BADGE_FOREGROUND, ACTIVITY_BAR_DRAG_AND_DROP_BACKGROUND } from 'vs/workbench/common/theme';
25
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
26
import { CompositeBar } from 'vs/workbench/browser/parts/compositebar/compositeBar';
27
import { ToggleCompositePinnedAction, ICompositeBar } from 'vs/workbench/browser/parts/compositebar/compositeBarActions';
28 29
import { ViewLocation, ViewsRegistry } from 'vs/workbench/common/views';
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
30 31 32 33 34 35 36 37 38
import { Dimension, createCSSRule } from 'vs/base/browser/dom';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';

interface IPlaceholderComposite {
	id: string;
	iconUrl: string;
}
E
Erich Gamma 已提交
39

I
isidor 已提交
40
export class ActivitybarPart extends Part {
41

42
	private static readonly PINNED_VIEWLETS = 'workbench.activity.pinnedViewlets';
43
	private static readonly PLACEHOLDER_VIEWLETS = 'workbench.activity.placeholderViewlets';
44
	private static readonly COLORS = {
I
isidor 已提交
45 46 47 48 49
		backgroundColor: ACTIVITY_BAR_FOREGROUND,
		badgeBackground: ACTIVITY_BAR_BADGE_BACKGROUND,
		badgeForeground: ACTIVITY_BAR_BADGE_FOREGROUND,
		dragAndDropBackground: ACTIVITY_BAR_DRAG_AND_DROP_BACKGROUND
	};
I
isidor 已提交
50
	private static readonly ACTION_HEIGHT = 50;
51

52
	public _serviceBrand: any;
P
Pine Wu 已提交
53

54 55
	private dimension: Dimension;

56 57 58
	private globalActionBar: ActionBar;
	private globalActivityIdToActions: { [globalActivityId: string]: GlobalActivityAction; };

59
	private placeholderComposites: IPlaceholderComposite[] = [];
60
	private extensionsRegistrationCompleted: boolean = false;
61
	private compositeBar: CompositeBar;
62
	private compositeActions: { [compositeId: string]: { activityAction: ViewletActivityAction, pinnedAction: ToggleCompositePinnedAction } };
63

E
Erich Gamma 已提交
64
	constructor(
65 66
		id: string,
		@IViewletService private viewletService: IViewletService,
I
isidor 已提交
67
		@IInstantiationService private instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
68
		@IPartService private partService: IPartService,
69 70 71
		@IThemeService themeService: IThemeService,
		@IStorageService private storageService: IStorageService,
		@IExtensionService extensionService: IExtensionService,
E
Erich Gamma 已提交
72
	) {
B
Benjamin Pasero 已提交
73
		super(id, { hasTitle: false }, themeService);
E
Erich Gamma 已提交
74

J
Joao Moreno 已提交
75
		this.globalActivityIdToActions = Object.create(null);
76

77
		this.compositeActions = Object.create(null);
78
		this.compositeBar = this.instantiationService.createInstance(CompositeBar, {
I
isidor 已提交
79
			icon: true,
80 81
			storageId: ActivitybarPart.PINNED_VIEWLETS,
			orientation: ActionsOrientation.VERTICAL,
82
			openComposite: (compositeId: string) => this.viewletService.openViewlet(compositeId, true),
83 84
			getActivityAction: (compositeId: string) => this.getCompositeActions(compositeId).activityAction,
			getCompositePinnedAction: (compositeId: string) => this.getCompositeActions(compositeId).pinnedAction,
85
			getOnCompositeClickAction: (compositeId: string) => this.instantiationService.createInstance(ToggleViewletAction, this.viewletService.getViewlet(compositeId)),
86
			getContextMenuActions: () => [this.instantiationService.createInstance(ToggleActivityBarVisibilityAction, ToggleActivityBarVisibilityAction.ID, nls.localize('hideActivitBar', "Hide Activity Bar"))],
87
			getDefaultCompositeId: () => this.viewletService.getDefaultViewletId(),
I
isidor 已提交
88
			hidePart: () => this.partService.setSideBarHidden(true),
89
			compositeSize: 50,
I
isidor 已提交
90
			colors: ActivitybarPart.COLORS,
I
isidor 已提交
91
			overflowActionSize: ActivitybarPart.ACTION_HEIGHT
92
		});
S
Sandeep Somavarapu 已提交
93 94
		const previousState = this.storageService.get(ActivitybarPart.PLACEHOLDER_VIEWLETS, StorageScope.GLOBAL, void 0);
		this.placeholderComposites = previousState ? JSON.parse(previousState) : this.compositeBar.getCompositesFromStorage().map(id => (<IPlaceholderComposite>{ id, iconUrl: void 0 }));
95

E
Erich Gamma 已提交
96
		this.registerListeners();
97
		this.updateCompositebar();
98 99 100 101 102 103
		this.updatePlaceholderComposites();

		extensionService.onDidRegisterExtensions(() => this.onDidRegisterExtensions());
	}

	private onDidRegisterExtensions(): void {
104
		this.extensionsRegistrationCompleted = true;
105 106
		this.removeNotExistingPlaceholderComposites();
		this.updateCompositebar();
E
Erich Gamma 已提交
107 108 109 110
	}

	private registerListeners(): void {

111 112 113
		this.toUnbind.push(this.viewletService.onDidViewletRegister(() => this.updateCompositebar()));
		this.toUnbind.push(ViewsRegistry.onViewsRegistered(() => this.updateCompositebar()));
		this.toUnbind.push(ViewsRegistry.onViewsDeregistered(() => this.updateCompositebar()));
114

E
Erich Gamma 已提交
115
		// Activate viewlet action on opening of a viewlet
116
		this.toUnbind.push(this.viewletService.onDidViewletOpen(viewlet => this.compositeBar.activateComposite(viewlet.getId())));
E
Erich Gamma 已提交
117 118

		// Deactivate viewlet action on close
119
		this.toUnbind.push(this.viewletService.onDidViewletClose(viewlet => this.compositeBar.deactivateComposite(viewlet.getId())));
I
isidor 已提交
120
		this.toUnbind.push(this.viewletService.onDidViewletEnablementChange(({ id, enabled }) => {
121
			if (enabled) {
122
				this.compositeBar.addComposite(this.viewletService.getViewlet(id), true);
123
			} else {
124
				this.removeComposite(id);
125 126
			}
		}));
E
Erich Gamma 已提交
127 128
	}

J
Joao Moreno 已提交
129
	public showActivity(viewletOrActionId: string, badge: IBadge, clazz?: string, priority?: number): IDisposable {
B
Benjamin Pasero 已提交
130
		if (this.viewletService.getViewlet(viewletOrActionId)) {
J
Joao Moreno 已提交
131
			return this.compositeBar.showActivity(viewletOrActionId, badge, clazz, priority);
B
Benjamin Pasero 已提交
132 133
		}

134
		return this.showGlobalActivity(viewletOrActionId, badge, clazz);
B
Benjamin Pasero 已提交
135 136
	}

137
	private showGlobalActivity(globalActivityId: string, badge: IBadge, clazz?: string): IDisposable {
J
Joao Moreno 已提交
138 139 140 141 142 143 144 145 146
		if (!badge) {
			throw illegalArgument('badge');
		}

		const action = this.globalActivityIdToActions[globalActivityId];
		if (!action) {
			throw illegalArgument('globalActivityId');
		}

147
		action.setBadge(badge, clazz);
B
Benjamin Pasero 已提交
148

J
Joao Moreno 已提交
149 150 151
		return toDisposable(() => action.setBadge(undefined));
	}

152
	public createContentArea(parent: HTMLElement): HTMLElement {
153 154
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
155 156

		// Top Actionbar with action items for each viewlet action
I
isidor 已提交
157
		this.compositeBar.create($result.getHTMLElement());
E
Erich Gamma 已提交
158

159
		// Top Actionbar with action items for each viewlet action
I
isidor 已提交
160
		this.createGlobalActivityActionBar($('.global-activity').appendTo($result).getHTMLElement());
161

162
		return $result.getHTMLElement();
E
Erich Gamma 已提交
163 164
	}

165 166 167 168
	public updateStyles(): void {
		super.updateStyles();

		// Part container
169
		const container = $(this.getContainer());
170 171
		const background = this.getColor(ACTIVITY_BAR_BACKGROUND);
		container.style('background-color', background);
172

173
		const borderColor = this.getColor(ACTIVITY_BAR_BORDER) || this.getColor(contrastBorder);
174
		const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
175 176 177 178 179 180 181
		container.style('box-sizing', borderColor && isPositionLeft ? 'border-box' : null);
		container.style('border-right-width', borderColor && isPositionLeft ? '1px' : null);
		container.style('border-right-style', borderColor && isPositionLeft ? 'solid' : null);
		container.style('border-right-color', isPositionLeft ? borderColor : null);
		container.style('border-left-width', borderColor && !isPositionLeft ? '1px' : null);
		container.style('border-left-style', borderColor && !isPositionLeft ? 'solid' : null);
		container.style('border-left-color', !isPositionLeft ? borderColor : null);
182 183
	}

J
Joao Moreno 已提交
184
	private createGlobalActivityActionBar(container: HTMLElement): void {
J
Joao Moreno 已提交
185
		const activityRegistry = Registry.as<IGlobalActivityRegistry>(GlobalActivityExtensions);
186 187 188 189 190
		const descriptors = activityRegistry.getActivities();
		const actions = descriptors
			.map(d => this.instantiationService.createInstance(d))
			.map(a => new GlobalActivityAction(a));

191
		this.globalActionBar = new ActionBar(container, {
I
isidor 已提交
192
			actionItemProvider: a => this.instantiationService.createInstance(GlobalActivityActionItem, a, ActivitybarPart.COLORS),
193 194 195 196
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('globalActions', "Global Actions"),
			animated: false
		});
B
Benjamin Pasero 已提交
197
		this.toUnbind.push(this.globalActionBar);
198

J
Joao Moreno 已提交
199 200
		actions.forEach(a => {
			this.globalActivityIdToActions[a.id] = a;
201
			this.globalActionBar.push(a);
J
Joao Moreno 已提交
202
		});
203 204
	}

205 206 207
	private getCompositeActions(compositeId: string): { activityAction: ViewletActivityAction, pinnedAction: ToggleCompositePinnedAction } {
		let compositeActions = this.compositeActions[compositeId];
		if (!compositeActions) {
208 209 210 211 212 213 214 215 216 217
			const viewlet = this.viewletService.getViewlet(compositeId);
			if (viewlet) {
				compositeActions = {
					activityAction: this.instantiationService.createInstance(ViewletActivityAction, viewlet),
					pinnedAction: new ToggleCompositePinnedAction(viewlet, this.compositeBar)
				};
			} else {
				const placeHolderComposite = this.placeholderComposites.filter(c => c.id === compositeId)[0];
				compositeActions = {
					activityAction: this.instantiationService.createInstance(PlaceHolderViewletActivityAction, compositeId, placeHolderComposite.iconUrl),
218
					pinnedAction: new PlaceHolderToggleCompositePinnedAction(compositeId, this.compositeBar)
219 220
				};
			}
221 222 223 224 225
			this.compositeActions[compositeId] = compositeActions;
		}
		return compositeActions;
	}

226 227 228
	private updateCompositebar(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const viewlet of viewlets) {
229 230 231 232 233 234 235
			const hasPlaceholder = this.placeholderComposites.some(c => c.id === viewlet.id);

			// Add the composite if it has views registered or
			// If it was existing before and the extensions registration is not yet completed.
			if (this.hasRegisteredViews(viewlet)
				|| (hasPlaceholder && !this.extensionsRegistrationCompleted)
			) {
236
				this.compositeBar.addComposite(viewlet, false);
237 238 239 240
				// Pin it by default if it is new => it does not has a placeholder
				if (!hasPlaceholder) {
					this.compositeBar.pin(viewlet.id);
				}
241
				this.enableCompositeActions(viewlet);
S
Sandeep Somavarapu 已提交
242 243 244 245 246
				const activeViewlet = this.viewletService.getActiveViewlet();
				if (activeViewlet && activeViewlet.getId() === viewlet.id) {
					this.compositeBar.pin(viewlet.id);
					this.compositeBar.activateComposite(viewlet.id);
				}
247
			} else {
248
				this.removeComposite(viewlet.id);
249 250 251 252
			}
		}
	}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	private updatePlaceholderComposites(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const { id } of this.placeholderComposites) {
			if (viewlets.every(viewlet => viewlet.id !== id)) {
				this.compositeBar.addComposite({ id, name: id, order: void 0 }, false);
			}
		}
	}

	private removeNotExistingPlaceholderComposites(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const { id } of this.placeholderComposites) {
			if (viewlets.every(viewlet => viewlet.id !== id)) {
				this.removeComposite(id);
			}
		}
	}

271 272 273 274 275 276 277 278 279 280
	private removeComposite(compositeId: string): void {
		this.compositeBar.removeComposite(compositeId);
		const compositeActions = this.compositeActions[compositeId];
		if (compositeActions) {
			compositeActions.activityAction.dispose();
			compositeActions.pinnedAction.dispose();
			delete this.compositeActions[compositeId];
		}
	}

281 282 283 284 285 286 287 288 289 290
	private enableCompositeActions(viewlet: ViewletDescriptor): void {
		const { activityAction, pinnedAction } = this.getCompositeActions(viewlet.id);
		if (activityAction instanceof PlaceHolderViewletActivityAction) {
			activityAction.enable(viewlet);
		}
		if (pinnedAction instanceof PlaceHolderToggleCompositePinnedAction) {
			pinnedAction.enable(viewlet);
		}
	}

291
	private hasRegisteredViews(viewlet: ViewletDescriptor): boolean {
292
		const viewLocation = ViewLocation.get(viewlet.id);
S
Sandeep Somavarapu 已提交
293
		if (viewLocation) {
294 295 296 297 298
			return ViewsRegistry.getViews(viewLocation).length > 0;
		}
		return true;
	}

299
	public getPinned(): string[] {
300
		return this.viewletService.getViewlets().map(v => v.id).filter(id => this.compositeBar.isPinned(id));
301 302
	}

303 304 305 306
	/**
	 * Layout title, content and status area in the given dimension.
	 */
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
307 308 309
		if (!this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return [dimension];
		}
310 311 312 313 314 315

		// Pass to super
		const sizes = super.layout(dimension);

		this.dimension = sizes[1];

316 317 318
		let availableHeight = this.dimension.height;
		if (this.globalActionBar) {
			// adjust height for global actions showing
I
isidor 已提交
319
			availableHeight -= (this.globalActionBar.items.length * ActivitybarPart.ACTION_HEIGHT);
320 321
		}
		this.compositeBar.layout(new Dimension(dimension.width, availableHeight));
322 323 324

		return sizes;
	}
I
isidor 已提交
325

326
	public shutdown(): void {
327
		const state = this.viewletService.getViewlets().filter(viewlet => this.hasRegisteredViews(viewlet)).map(viewlet => ({ id: viewlet.id, iconUrl: viewlet.iconUrl }));
328
		this.storageService.store(ActivitybarPart.PLACEHOLDER_VIEWLETS, JSON.stringify(state), StorageScope.GLOBAL);
329 330 331
		super.shutdown();
	}

E
Erich Gamma 已提交
332
	public dispose(): void {
333 334 335
		if (this.compositeBar) {
			this.compositeBar.dispose();
			this.compositeBar = null;
I
isidor 已提交
336 337
		}

B
Benjamin Pasero 已提交
338 339 340 341 342
		if (this.globalActionBar) {
			this.globalActionBar.dispose();
			this.globalActionBar = null;
		}

E
Erich Gamma 已提交
343 344
		super.dispose();
	}
345 346 347 348 349 350 351 352 353 354 355
}

class PlaceHolderViewletActivityAction extends ViewletActivityAction {

	constructor(
		id: string, iconUrl: string,
		@IViewletService viewletService: IViewletService,
		@IPartService partService: IPartService,
		@ITelemetryService telemetryService: ITelemetryService
	) {
		super({ id, name: id, cssClass: `extensionViewlet-placeholder-${id.replace(/\./g, '-')}` }, viewletService, partService, telemetryService);
S
Sandeep Somavarapu 已提交
356 357 358
		// Generate Placeholder CSS to show the icon in the activity bar
		const iconClass = `.monaco-workbench > .activitybar .monaco-action-bar .action-label.${this.class}`;
		createCSSRule(iconClass, `-webkit-mask: url('${iconUrl || ''}') no-repeat 50% 50%`);
359 360 361 362 363 364 365 366 367 368 369 370 371 372
		this.enabled = false;
	}

	enable(activity: IActivity): void {
		this.label = activity.name;
		this.class = activity.cssClass;
		this.enabled = true;
	}

}

class PlaceHolderToggleCompositePinnedAction extends ToggleCompositePinnedAction {

	constructor(
373
		id: string, compositeBar: ICompositeBar
374 375 376 377 378 379 380 381 382 383
	) {
		super({ id, name: id, cssClass: void 0 }, compositeBar);
		this.enabled = false;
	}

	enable(activity: IActivity): void {
		this.label = activity.name;
		this.enabled = true;
	}

384
}