activitybarPart.ts 14.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 * 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';
B
Benjamin Pasero 已提交
13
import { GlobalActivityExtensions, IGlobalActivityRegistry } 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';
B
Benjamin Pasero 已提交
16
import { GlobalActivityActionItem, GlobalActivityAction, ViewletActivityAction, ToggleViewletAction, PlaceHolderToggleCompositePinnedAction, PlaceHolderViewletActivityAction } 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, registerThemingParticipant } 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 28 29 30
import { isMacintosh } from 'vs/base/common/platform';
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { scheduleAtNextAnimationFrame, Dimension } from 'vs/base/browser/dom';
import { Color } from 'vs/base/common/color';
31 32
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
33
import URI from 'vs/base/common/uri';
34 35
import { ToggleCompositePinnedAction } from 'vs/workbench/browser/parts/compositebar/compositeBarActions';
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
36 37 38

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

I
isidor 已提交
42
export class ActivitybarPart extends Part {
43

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

54 55
	private dimension: Dimension;

56
	private globalActionBar: ActionBar;
B
Benjamin Pasero 已提交
57
	private globalActivityIdToActions: { [globalActivityId: string]: GlobalActivityAction; } = Object.create(null);
58

59
	private placeholderComposites: IPlaceholderComposite[] = [];
60
	private compositeBar: CompositeBar;
B
Benjamin Pasero 已提交
61
	private compositeActions: { [compositeId: string]: { activityAction: ViewletActivityAction, pinnedAction: ToggleCompositePinnedAction } } = Object.create(null);
62

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

B
Benjamin Pasero 已提交
75
		this.compositeBar = this._register(this.instantiationService.createInstance(CompositeBar, {
I
isidor 已提交
76
			icon: true,
77 78
			storageId: ActivitybarPart.PINNED_VIEWLETS,
			orientation: ActionsOrientation.VERTICAL,
79
			openComposite: (compositeId: string) => this.viewletService.openViewlet(compositeId, true),
80 81
			getActivityAction: (compositeId: string) => this.getCompositeActions(compositeId).activityAction,
			getCompositePinnedAction: (compositeId: string) => this.getCompositeActions(compositeId).pinnedAction,
82
			getOnCompositeClickAction: (compositeId: string) => this.instantiationService.createInstance(ToggleViewletAction, this.viewletService.getViewlet(compositeId)),
83
			getContextMenuActions: () => [this.instantiationService.createInstance(ToggleActivityBarVisibilityAction, ToggleActivityBarVisibilityAction.ID, nls.localize('hideActivitBar', "Hide Activity Bar"))],
84
			getDefaultCompositeId: () => this.viewletService.getDefaultViewletId(),
I
isidor 已提交
85
			hidePart: () => this.partService.setSideBarHidden(true),
86
			compositeSize: 50,
I
isidor 已提交
87
			colors: ActivitybarPart.COLORS,
I
isidor 已提交
88
			overflowActionSize: ActivitybarPart.ACTION_HEIGHT
B
Benjamin Pasero 已提交
89 90
		}));

S
Sandeep Somavarapu 已提交
91
		const previousState = this.storageService.get(ActivitybarPart.PLACEHOLDER_VIEWLETS, StorageScope.GLOBAL, void 0);
92 93 94 95 96 97 98 99 100 101 102 103 104
		if (previousState) {
			let parsedPreviousState = <IPlaceholderComposite[]>JSON.parse(previousState);
			parsedPreviousState.forEach((s) => {
				if (typeof s.iconUrl === 'object') {
					s.iconUrl = URI.revive(s.iconUrl);
				} else {
					s.iconUrl = void 0;
				}
			});
			this.placeholderComposites = parsedPreviousState;
		} else {
			this.placeholderComposites = this.compositeBar.getCompositesFromStorage().map(id => (<IPlaceholderComposite>{ id, iconUrl: void 0 }));
		}
105

E
Erich Gamma 已提交
106
		this.registerListeners();
107
		this.updateCompositebar();
108
		this.updatePlaceholderComposites();
E
Erich Gamma 已提交
109 110 111
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
112
		this._register(this.viewletService.onDidViewletRegister(() => this.updateCompositebar()));
113

E
Erich Gamma 已提交
114
		// Activate viewlet action on opening of a viewlet
B
Benjamin Pasero 已提交
115
		this._register(this.viewletService.onDidViewletOpen(viewlet => this.compositeBar.activateComposite(viewlet.getId())));
E
Erich Gamma 已提交
116 117

		// Deactivate viewlet action on close
B
Benjamin Pasero 已提交
118 119
		this._register(this.viewletService.onDidViewletClose(viewlet => this.compositeBar.deactivateComposite(viewlet.getId())));
		this._register(this.viewletService.onDidViewletEnablementChange(({ id, enabled }) => {
120
			if (enabled) {
S
Sandeep Somavarapu 已提交
121
				this.compositeBar.addComposite(this.viewletService.getViewlet(id));
122
			} else {
123
				this.removeComposite(id);
124 125
			}
		}));
B
Benjamin Pasero 已提交
126 127

		this._register(this.extensionService.onDidRegisterExtensions(() => this.onDidRegisterExtensions()));
E
Erich Gamma 已提交
128 129
	}

B
Benjamin Pasero 已提交
130 131 132
	private onDidRegisterExtensions(): void {
		this.removeNotExistingPlaceholderComposites();
		this.updateCompositebar();
E
Erich Gamma 已提交
133 134
	}

B
Benjamin Pasero 已提交
135
	showActivity(viewletOrActionId: string, badge: IBadge, clazz?: string, priority?: number): IDisposable {
B
Benjamin Pasero 已提交
136
		if (this.viewletService.getViewlet(viewletOrActionId)) {
J
Joao Moreno 已提交
137
			return this.compositeBar.showActivity(viewletOrActionId, badge, clazz, priority);
B
Benjamin Pasero 已提交
138 139
		}

140
		return this.showGlobalActivity(viewletOrActionId, badge, clazz);
B
Benjamin Pasero 已提交
141 142
	}

143
	private showGlobalActivity(globalActivityId: string, badge: IBadge, clazz?: string): IDisposable {
J
Joao Moreno 已提交
144 145 146 147 148 149 150 151 152
		if (!badge) {
			throw illegalArgument('badge');
		}

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

153
		action.setBadge(badge, clazz);
B
Benjamin Pasero 已提交
154

J
Joao Moreno 已提交
155 156 157
		return toDisposable(() => action.setBadge(undefined));
	}

B
Benjamin Pasero 已提交
158
	createContentArea(parent: HTMLElement): HTMLElement {
159 160
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
161 162

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

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

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		// TODO@Ben: workaround for https://github.com/Microsoft/vscode/issues/45700
		// It looks like there are rendering glitches on macOS with Chrome 61 when
		// using --webkit-mask with a background color that is different from the image
		// The workaround is to promote the element onto its own drawing layer. We do
		// this only after the workbench has loaded because otherwise there is ugly flicker.
		if (isMacintosh) {
			this.lifecycleService.when(LifecyclePhase.Running).then(() => {
				scheduleAtNextAnimationFrame(() => { // another delay...
					scheduleAtNextAnimationFrame(() => { // ...to prevent more flickering on startup
						registerThemingParticipant((theme, collector) => {
							const activityBarForeground = theme.getColor(ACTIVITY_BAR_FOREGROUND);
							if (activityBarForeground && !activityBarForeground.equals(Color.white)) {
								// only apply this workaround if the color is different from the image one (white)
								collector.addRule('.monaco-workbench .activitybar > .content .monaco-action-bar .action-label { will-change: transform; }');
							}
						});
					});
				});
			});
		}

189
		return $result.getHTMLElement();
E
Erich Gamma 已提交
190 191
	}

B
Benjamin Pasero 已提交
192
	updateStyles(): void {
193 194 195
		super.updateStyles();

		// Part container
196
		const container = $(this.getContainer());
197 198
		const background = this.getColor(ACTIVITY_BAR_BACKGROUND);
		container.style('background-color', background);
199

200
		const borderColor = this.getColor(ACTIVITY_BAR_BORDER) || this.getColor(contrastBorder);
201
		const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
202 203 204 205 206 207 208
		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);
209 210
	}

J
Joao Moreno 已提交
211
	private createGlobalActivityActionBar(container: HTMLElement): void {
J
Joao Moreno 已提交
212
		const activityRegistry = Registry.as<IGlobalActivityRegistry>(GlobalActivityExtensions);
213 214 215 216 217
		const descriptors = activityRegistry.getActivities();
		const actions = descriptors
			.map(d => this.instantiationService.createInstance(d))
			.map(a => new GlobalActivityAction(a));

B
Benjamin Pasero 已提交
218
		this.globalActionBar = this._register(new ActionBar(container, {
I
isidor 已提交
219
			actionItemProvider: a => this.instantiationService.createInstance(GlobalActivityActionItem, a, ActivitybarPart.COLORS),
220 221 222
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('globalActions', "Global Actions"),
			animated: false
B
Benjamin Pasero 已提交
223
		}));
224

J
Joao Moreno 已提交
225 226
		actions.forEach(a => {
			this.globalActivityIdToActions[a.id] = a;
227
			this.globalActionBar.push(a);
J
Joao Moreno 已提交
228
		});
229 230
	}

231 232 233
	private getCompositeActions(compositeId: string): { activityAction: ViewletActivityAction, pinnedAction: ToggleCompositePinnedAction } {
		let compositeActions = this.compositeActions[compositeId];
		if (!compositeActions) {
234 235 236 237 238 239 240 241 242 243
			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),
244
					pinnedAction: new PlaceHolderToggleCompositePinnedAction(compositeId, this.compositeBar)
245 246
				};
			}
B
Benjamin Pasero 已提交
247

248 249
			this.compositeActions[compositeId] = compositeActions;
		}
B
Benjamin Pasero 已提交
250

251 252 253
		return compositeActions;
	}

254 255 256
	private updateCompositebar(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const viewlet of viewlets) {
S
Sandeep Somavarapu 已提交
257 258 259 260 261 262 263 264 265 266 267 268
			this.compositeBar.addComposite(viewlet);

			// Pin it by default if it is new => it does not has a placeholder
			if (this.placeholderComposites.every(c => c.id !== viewlet.id)) {
				this.compositeBar.pin(viewlet.id);
			}

			this.enableCompositeActions(viewlet);
			const activeViewlet = this.viewletService.getActiveViewlet();
			if (activeViewlet && activeViewlet.getId() === viewlet.id) {
				this.compositeBar.pin(viewlet.id);
				this.compositeBar.activateComposite(viewlet.id);
269 270 271 272
			}
		}
	}

273 274 275 276
	private updatePlaceholderComposites(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const { id } of this.placeholderComposites) {
			if (viewlets.every(viewlet => viewlet.id !== id)) {
S
Sandeep Somavarapu 已提交
277
				this.compositeBar.addComposite({ id, name: id, order: void 0 });
278 279 280 281 282 283 284 285 286 287 288 289 290
			}
		}
	}

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

291 292 293 294 295 296 297 298 299 300
	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];
		}
	}

301 302 303
	private enableCompositeActions(viewlet: ViewletDescriptor): void {
		const { activityAction, pinnedAction } = this.getCompositeActions(viewlet.id);
		if (activityAction instanceof PlaceHolderViewletActivityAction) {
S
Sandeep Somavarapu 已提交
304
			activityAction.setActivity(viewlet);
305 306
		}
		if (pinnedAction instanceof PlaceHolderToggleCompositePinnedAction) {
S
Sandeep Somavarapu 已提交
307
			pinnedAction.setActivity(viewlet);
308 309 310
		}
	}

B
Benjamin Pasero 已提交
311
	getPinned(): string[] {
312
		return this.viewletService.getViewlets().map(v => v.id).filter(id => this.compositeBar.isPinned(id));
313 314
	}

B
Benjamin Pasero 已提交
315
	layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
316 317 318
		if (!this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return [dimension];
		}
319 320 321 322 323 324

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

		this.dimension = sizes[1];

325 326 327
		let availableHeight = this.dimension.height;
		if (this.globalActionBar) {
			// adjust height for global actions showing
I
isidor 已提交
328
			availableHeight -= (this.globalActionBar.items.length * ActivitybarPart.ACTION_HEIGHT);
329 330
		}
		this.compositeBar.layout(new Dimension(dimension.width, availableHeight));
331 332 333

		return sizes;
	}
I
isidor 已提交
334

B
Benjamin Pasero 已提交
335
	shutdown(): void {
336
		const state = this.viewletService.getViewlets().map(viewlet => ({ id: viewlet.id, iconUrl: viewlet.iconUrl }));
337
		this.storageService.store(ActivitybarPart.PLACEHOLDER_VIEWLETS, JSON.stringify(state), StorageScope.GLOBAL);
B
Benjamin Pasero 已提交
338

B
Benjamin Pasero 已提交
339
		super.shutdown();
340 341
	}
}