activitybarPart.ts 12.3 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';
10
import { TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
11
import { illegalArgument } from 'vs/base/common/errors';
12
import { $ } from 'vs/base/browser/builder';
J
Johannes Rieken 已提交
13
import { Action } from 'vs/base/common/actions';
14
import { ActionsOrientation, ActionBar, Separator } from 'vs/base/browser/ui/actionbar/actionbar';
B
Benjamin Pasero 已提交
15
import { GlobalActivityExtensions, IGlobalActivityRegistry } from 'vs/workbench/common/activity';
16
import { Registry } from 'vs/platform/registry/common/platform';
J
Johannes Rieken 已提交
17
import { Part } from 'vs/workbench/browser/part';
18
import { GlobalActivityActionItem, GlobalActivityAction, ViewletActivityAction, ToggleViewletAction } from 'vs/workbench/browser/parts/activitybar/activitybarActions';
B
Benjamin Pasero 已提交
19
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
I
isidor 已提交
20
import { IBadge } from 'vs/workbench/services/activity/common/activity';
I
isidor 已提交
21
import { IPartService, Parts, Position as SideBarPosition } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
22
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
23 24
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
J
Joao Moreno 已提交
25
import { dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
26
import { ToggleActivityBarVisibilityAction } from 'vs/workbench/browser/actions/toggleActivityBarVisibility';
B
Benjamin Pasero 已提交
27
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
I
isidor 已提交
28
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';
29
import { contrastBorder } from 'vs/platform/theme/common/colorRegistry';
30
import { CompositeBar } from 'vs/workbench/browser/parts/compositebar/compositeBar';
31
import { ToggleCompositePinnedAction } from 'vs/workbench/browser/parts/compositebar/compositeBarActions';
B
Benjamin Pasero 已提交
32 33 34 35
import { isMacintosh } from 'vs/base/common/platform';
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { Dimension, scheduleAtNextAnimationFrame } from 'vs/base/browser/dom';
import { Color } from 'vs/base/common/color';
36 37
import { ViewLocation, ViewsRegistry } from 'vs/workbench/common/views';
import { ViewletDescriptor } from 'vs/workbench/browser/viewlet';
E
Erich Gamma 已提交
38

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

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

50
	public _serviceBrand: any;
P
Pine Wu 已提交
51

52 53
	private dimension: Dimension;

54 55 56
	private globalActionBar: ActionBar;
	private globalActivityIdToActions: { [globalActivityId: string]: GlobalActivityAction; };

57
	private compositeBar: CompositeBar;
58

E
Erich Gamma 已提交
59
	constructor(
60 61
		id: string,
		@IViewletService private viewletService: IViewletService,
62
		@IContextMenuService private contextMenuService: IContextMenuService,
I
isidor 已提交
63
		@IInstantiationService private instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
64
		@IPartService private partService: IPartService,
B
Benjamin Pasero 已提交
65 66
		@IThemeService themeService: IThemeService,
		@ILifecycleService private lifecycleService: ILifecycleService
E
Erich Gamma 已提交
67
	) {
B
Benjamin Pasero 已提交
68
		super(id, { hasTitle: false }, themeService);
E
Erich Gamma 已提交
69

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

72
		this.compositeBar = this.instantiationService.createInstance(CompositeBar, {
I
isidor 已提交
73
			icon: true,
74 75 76
			storageId: ActivitybarPart.PINNED_VIEWLETS,
			orientation: ActionsOrientation.VERTICAL,
			composites: this.viewletService.getViewlets(),
77
			openComposite: (compositeId: string) => this.viewletService.openViewlet(compositeId, true),
78
			getActivityAction: (compositeId: string) => this.instantiationService.createInstance(ViewletActivityAction, this.viewletService.getViewlet(compositeId)),
79
			getCompositePinnedAction: (compositeId: string) => new ToggleCompositePinnedAction(this.viewletService.getViewlet(compositeId), this.compositeBar),
80 81
			getOnCompositeClickAction: (compositeId: string) => this.instantiationService.createInstance(ToggleViewletAction, this.viewletService.getViewlet(compositeId)),
			getDefaultCompositeId: () => this.viewletService.getDefaultViewletId(),
I
isidor 已提交
82
			hidePart: () => this.partService.setSideBarHidden(true),
I
isidor 已提交
83
			colors: ActivitybarPart.COLORS,
I
isidor 已提交
84
			overflowActionSize: ActivitybarPart.ACTION_HEIGHT
85
		});
86

E
Erich Gamma 已提交
87
		this.registerListeners();
88
		this.updateCompositebar();
E
Erich Gamma 已提交
89 90 91 92
	}

	private registerListeners(): void {

93 94 95
		this.toUnbind.push(this.viewletService.onDidViewletRegister(() => this.updateCompositebar()));
		this.toUnbind.push(ViewsRegistry.onViewsRegistered(() => this.updateCompositebar()));
		this.toUnbind.push(ViewsRegistry.onViewsDeregistered(() => this.updateCompositebar()));
96

E
Erich Gamma 已提交
97
		// Activate viewlet action on opening of a viewlet
98
		this.toUnbind.push(this.viewletService.onDidViewletOpen(viewlet => this.compositeBar.activateComposite(viewlet.getId())));
E
Erich Gamma 已提交
99 100

		// Deactivate viewlet action on close
101 102
		this.toUnbind.push(this.viewletService.onDidViewletClose(viewlet => this.compositeBar.deactivateComposite(viewlet.getId())));
		this.toUnbind.push(this.compositeBar.onDidContextMenu(e => this.showContextMenu(e)));
I
isidor 已提交
103
		this.toUnbind.push(this.viewletService.onDidViewletEnablementChange(({ id, enabled }) => {
104
			if (enabled) {
105
				this.compositeBar.addComposite(this.viewletService.getViewlet(id), true);
106 107
			} else {
				this.compositeBar.removeComposite(id);
108 109
			}
		}));
E
Erich Gamma 已提交
110 111
	}

J
Joao Moreno 已提交
112
	public showActivity(viewletOrActionId: string, badge: IBadge, clazz?: string, priority?: number): IDisposable {
B
Benjamin Pasero 已提交
113
		if (this.viewletService.getViewlet(viewletOrActionId)) {
J
Joao Moreno 已提交
114
			return this.compositeBar.showActivity(viewletOrActionId, badge, clazz, priority);
B
Benjamin Pasero 已提交
115 116
		}

117
		return this.showGlobalActivity(viewletOrActionId, badge, clazz);
B
Benjamin Pasero 已提交
118 119
	}

120
	private showGlobalActivity(globalActivityId: string, badge: IBadge, clazz?: string): IDisposable {
J
Joao Moreno 已提交
121 122 123 124 125 126 127 128 129
		if (!badge) {
			throw illegalArgument('badge');
		}

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

130
		action.setBadge(badge, clazz);
B
Benjamin Pasero 已提交
131

J
Joao Moreno 已提交
132 133 134
		return toDisposable(() => action.setBadge(undefined));
	}

135
	public createContentArea(parent: HTMLElement): HTMLElement {
136 137
		const $el = $(parent);
		const $result = $('.content').appendTo($el);
E
Erich Gamma 已提交
138 139

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

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

B
Benjamin Pasero 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		// 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 > .composite-bar > .monaco-action-bar .action-label { will-change: transform; }');
							}
						});
					});
				});
			});
		}

166
		return $result.getHTMLElement();
E
Erich Gamma 已提交
167 168
	}

169 170 171 172
	public updateStyles(): void {
		super.updateStyles();

		// Part container
173
		const container = $(this.getContainer());
174 175
		const background = this.getColor(ACTIVITY_BAR_BACKGROUND);
		container.style('background-color', background);
176

177
		const borderColor = this.getColor(ACTIVITY_BAR_BORDER) || this.getColor(contrastBorder);
178
		const isPositionLeft = this.partService.getSideBarPosition() === SideBarPosition.LEFT;
179 180 181 182 183 184 185
		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);
186 187
	}

188 189 190
	private showContextMenu(e: MouseEvent): void {
		const event = new StandardMouseEvent(e);

191 192 193
		const actions: Action[] = this.viewletService.getViewlets()
			.filter(viewlet => this.canShow(viewlet))
			.map(viewlet => this.instantiationService.createInstance(ToggleCompositePinnedAction, viewlet, this.compositeBar));
194 195 196 197
		actions.push(new Separator());
		actions.push(this.instantiationService.createInstance(ToggleActivityBarVisibilityAction, ToggleActivityBarVisibilityAction.ID, nls.localize('hideActivitBar', "Hide Activity Bar")));

		this.contextMenuService.showContextMenu({
B
Benjamin Pasero 已提交
198
			getAnchor: () => { return { x: event.posx, y: event.posy }; },
199 200 201 202 203
			getActions: () => TPromise.as(actions),
			onHide: () => dispose(actions)
		});
	}

J
Joao Moreno 已提交
204
	private createGlobalActivityActionBar(container: HTMLElement): void {
J
Joao Moreno 已提交
205
		const activityRegistry = Registry.as<IGlobalActivityRegistry>(GlobalActivityExtensions);
206 207 208 209 210
		const descriptors = activityRegistry.getActivities();
		const actions = descriptors
			.map(d => this.instantiationService.createInstance(d))
			.map(a => new GlobalActivityAction(a));

211
		this.globalActionBar = new ActionBar(container, {
I
isidor 已提交
212
			actionItemProvider: a => this.instantiationService.createInstance(GlobalActivityActionItem, a, ActivitybarPart.COLORS),
213 214 215 216
			orientation: ActionsOrientation.VERTICAL,
			ariaLabel: nls.localize('globalActions', "Global Actions"),
			animated: false
		});
B
Benjamin Pasero 已提交
217
		this.toUnbind.push(this.globalActionBar);
218

J
Joao Moreno 已提交
219 220
		actions.forEach(a => {
			this.globalActivityIdToActions[a.id] = a;
221
			this.globalActionBar.push(a);
J
Joao Moreno 已提交
222
		});
223 224
	}

225 226 227 228 229 230 231 232 233 234 235 236 237 238
	private updateCompositebar(): void {
		const viewlets = this.viewletService.getViewlets();
		for (const viewlet of viewlets) {
			const canShow = this.canShow(viewlet);
			if (canShow) {
				this.compositeBar.addComposite(viewlet, false);
			} else {
				this.compositeBar.removeComposite(viewlet.id);
			}
		}
	}

	private canShow(viewlet: ViewletDescriptor): boolean {
		const viewLocation = ViewLocation.get(viewlet.id);
S
Sandeep Somavarapu 已提交
239
		if (viewLocation) {
240 241 242 243 244
			return ViewsRegistry.getViews(viewLocation).length > 0;
		}
		return true;
	}

245
	public getPinned(): string[] {
246
		return this.viewletService.getViewlets().map(v => v.id).filter(id => this.compositeBar.isPinned(id));
247 248
	}

249 250 251 252
	/**
	 * Layout title, content and status area in the given dimension.
	 */
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
253 254 255
		if (!this.partService.isVisible(Parts.ACTIVITYBAR_PART)) {
			return [dimension];
		}
256 257 258 259 260 261

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

		this.dimension = sizes[1];

262 263 264
		let availableHeight = this.dimension.height;
		if (this.globalActionBar) {
			// adjust height for global actions showing
I
isidor 已提交
265
			availableHeight -= (this.globalActionBar.items.length * ActivitybarPart.ACTION_HEIGHT);
266 267
		}
		this.compositeBar.layout(new Dimension(dimension.width, availableHeight));
268 269 270

		return sizes;
	}
I
isidor 已提交
271

272 273 274 275 276
	public shutdown(): void {
		this.compositeBar.shutdown();
		super.shutdown();
	}

E
Erich Gamma 已提交
277
	public dispose(): void {
278 279 280
		if (this.compositeBar) {
			this.compositeBar.dispose();
			this.compositeBar = null;
I
isidor 已提交
281 282
		}

B
Benjamin Pasero 已提交
283 284 285 286 287
		if (this.globalActionBar) {
			this.globalActionBar.dispose();
			this.globalActionBar = null;
		}

E
Erich Gamma 已提交
288 289
		super.dispose();
	}
290
}