sidebarPart.ts 18.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/sidebarpart';
import {TPromise} from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import {Registry} from 'vs/platform/platform';
import {IDisposable} from 'vs/base/common/lifecycle';
import {Dimension, Builder, $} from 'vs/base/browser/builder';
import uuid = require('vs/base/common/uuid');
import events = require('vs/base/common/events');
import timer = require('vs/base/common/timer');
import strings = require('vs/base/common/strings');
import types = require('vs/base/common/types');
import errors = require('vs/base/common/errors');
import {CONTEXT as ToolBarContext, ToolBar} from 'vs/base/browser/ui/toolbar/toolbar';
import {IActionItem, ActionsOrientation} from 'vs/base/browser/ui/actionbar/actionbar';
import {ProgressBar} from 'vs/base/browser/ui/progressbar/progressbar';
import {Scope, IActionBarRegistry, Extensions, prepareActions} from 'vs/workbench/browser/actionBarRegistry';
import {Action, IAction} from 'vs/base/common/actions';
import {Part} from 'vs/workbench/browser/part';
import {EventType as WorkbenchEventType, ViewletEvent} from 'vs/workbench/browser/events';
import {Viewlet, EventType as ViewletEventType, IViewletRegistry, Extensions as ViewletExtensions} from 'vs/workbench/browser/viewlet';
import {IWorkbenchActionRegistry, Extensions as ActionExtensions} from 'vs/workbench/browser/actionRegistry';
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {WorkbenchProgressService} from 'vs/workbench/services/progress/browser/progressService';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IPartService} from 'vs/workbench/services/part/common/partService';
import {IViewlet} from 'vs/workbench/common/viewlet';
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IProgressService} from 'vs/platform/progress/common/progress';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {KeybindingsUtils} from 'vs/platform/keybinding/common/keybindingsUtils';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';

export class SidebarPart extends Part implements IViewletService {

	public serviceId = IViewletService;

	private instantiationService: IInstantiationService;
	private activeViewletListeners: { (): void; }[];
	private instantiatedViewletListeners: { (): void; }[];
	private mapViewletToViewletContainer: { [viewletId: string]: Builder; };
	private mapActionsBindingToViewlet: { [viewletId: string]: () => void; };
	private mapProgressServiceToViewlet: { [viewletId: string]: IProgressService; };
	private activeViewlet: Viewlet;
	private lastActiveViewletId: string;
	private instantiatedViewlets: Viewlet[];
	private titleLabel: Builder;
	private toolBar: ToolBar;
	private viewletLoaderPromises: { [viewletId: string]: TPromise<Viewlet>; };
	private progressBar: ProgressBar;
	private contentAreaSize: Dimension;
	private blockOpeningViewlet: boolean;
	private registry: IViewletRegistry;
	private telemetryActionsListener: IDisposable;
	private currentViewletOpenToken: string;

	constructor(
		private messageService: IMessageService,
		private storageService: IStorageService,
		private eventService: IEventService,
		private telemetryService: ITelemetryService,
		private contextMenuService: IContextMenuService,
		private partService: IPartService,
		private keybindingService: IKeybindingService,
		id: string
	) {
		super(id);

		this.activeViewletListeners = [];
		this.instantiatedViewletListeners = [];
		this.mapViewletToViewletContainer = {};
		this.mapActionsBindingToViewlet = {};
		this.mapProgressServiceToViewlet = {};
		this.activeViewlet = null;
		this.instantiatedViewlets = [];
		this.viewletLoaderPromises = {};
		this.registry = (<IViewletRegistry>Registry.as(ViewletExtensions.Viewlets));
	}

	public setInstantiationService(service: IInstantiationService): void {
		this.instantiationService = service;
	}

	public openViewlet(id: string, focus?: boolean): TPromise<Viewlet> {
		if (this.blockOpeningViewlet) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

		// First check if sidebar is hidden and show if so
		if (this.partService.isSideBarHidden()) {
			try {
				this.blockOpeningViewlet = true;
				this.partService.setSideBarHidden(false);
			} finally {
				this.blockOpeningViewlet = false;
			}
		}

		// Check if viewlet already visible and just focus in that case
		if (this.activeViewlet && this.activeViewlet.getId() === id) {
			if (focus) {
				this.activeViewlet.focus();
			}

			// Fullfill promise with viewlet that is being opened
			return TPromise.as(this.activeViewlet);
		}

		// Open
		return this.doOpenViewlet(id, focus);
	}

	private doOpenViewlet(id: string, focus?: boolean): TPromise<Viewlet> {
		let timerEvent = timer.start(timer.Topic.WORKBENCH, strings.format('Open Viewlet {0}', id.substr(id.lastIndexOf('.') + 1)));

		// Use a generated token to avoid race conditions from long running promises
		let currentViewletOpenToken = uuid.generateUuid();
		this.currentViewletOpenToken = currentViewletOpenToken;

		// Emit Viewlet Opening Event
		this.emit(WorkbenchEventType.VIEWLET_OPENING, new ViewletEvent(id));

		// Hide current
		let hidePromise: TPromise<void>;
		if (this.activeViewlet) {
			hidePromise = this.hideActiveViewlet();
		} else {
			hidePromise = TPromise.as(null);
		}

		return hidePromise.then(() => {

			// Update Title
			this.updateTitle(id);

			// Create viewlet
			return this.createViewlet(id, true).then((viewlet: Viewlet) => {

				// Check if another viewlet opened meanwhile and return in that case
				if ((this.currentViewletOpenToken !== currentViewletOpenToken) || (this.activeViewlet && this.activeViewlet.getId() !== viewlet.getId())) {
					timerEvent.stop();

					return TPromise.as(null);
				}

				// Check if viewlet already visible and just focus in that case
				if (this.activeViewlet && this.activeViewlet.getId() === viewlet.getId()) {
					if (focus) {
						viewlet.focus();
					}

					timerEvent.stop();

					// Fullfill promise with viewlet that is being opened
					return TPromise.as(viewlet);
				}

				// Show Viewlet and Focus
				return this.showViewlet(viewlet).then(() => {
					if (focus) {
						viewlet.focus();
					}

					timerEvent.stop();

					// Fullfill promise with viewlet that is being opened
					return viewlet;
				});
			});
		});
	}

	private createViewlet(id: string, isActive?: boolean): TPromise<Viewlet> {

		// Check if viewlet is already created
		for (let i = 0; i < this.instantiatedViewlets.length; i++) {
			if (this.instantiatedViewlets[i].getId() === id) {
				return TPromise.as(this.instantiatedViewlets[i]);
			}
		}

		// Instantiate viewlet from registry otherwise
		let viewletDescriptor = this.registry.getViewlet(id);
		if (viewletDescriptor) {
			let loaderPromise = this.viewletLoaderPromises[id];
			if (!loaderPromise) {
				let progressService = new WorkbenchProgressService(this.eventService, this.progressBar, viewletDescriptor.id, isActive);
				let services = {
					progressService: progressService
				};
				let viewletInstantiationService = this.instantiationService.createChild(services);

				loaderPromise = viewletInstantiationService.createInstance(viewletDescriptor).then((viewlet: Viewlet) => {
					this.mapProgressServiceToViewlet[viewlet.getId()] = progressService;

					// Remember as Instantiated
					this.instantiatedViewlets.push(viewlet);

					// Register to title area update events from the viewlet
					this.instantiatedViewletListeners.push(viewlet.addListener(ViewletEventType.INTERNAL_VIEWLET_TITLE_AREA_UPDATE, (e) => { this.onTitleAreaUpdate(e); }));

					// Remove from Promises Cache since Loaded
					delete this.viewletLoaderPromises[id];

					return viewlet;
				});

219 220
				// Report progress for slow loading viewlets
				progressService.showWhile(loaderPromise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */);
E
Erich Gamma 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

				// Add to Promise Cache until Loaded
				this.viewletLoaderPromises[id] = loaderPromise;
			}

			return loaderPromise;
		}

		throw new Error(strings.format('Unable to find viewlet with id {0}', id));
	}

	private showViewlet(viewlet: Viewlet): TPromise<void> {

		// Remember Viewlet
		this.activeViewlet = viewlet;

		// Remember
		this.lastActiveViewletId = this.activeViewlet.getId();

		// Register as Emitter to Workbench Bus
		this.activeViewletListeners.push(this.eventService.addEmitter(this.activeViewlet, this.activeViewlet.getId()));

		let createViewletPromise: TPromise<void>;

		// Viewlet created for the first time
		let viewletContainer = this.mapViewletToViewletContainer[viewlet.getId()];
		if (!viewletContainer) {

			// Build Container off-DOM
			viewletContainer = $().div({
				'class': 'viewlet',
				id: viewlet.getId()
			}, (div: Builder) => {
				createViewletPromise = viewlet.create(div);
			});

			// Remember viewlet container
			this.mapViewletToViewletContainer[viewlet.getId()] = viewletContainer;
		}

		// Viewlet already exists but is hidden
		else {
			createViewletPromise = TPromise.as(null);
		}

266
		// Report progress for slow loading viewlets (but only if we did not create the viewlet before already)
E
Erich Gamma 已提交
267
		let progressService = this.mapProgressServiceToViewlet[viewlet.getId()];
268 269
		if (progressService && !viewletContainer) {
			this.mapProgressServiceToViewlet[viewlet.getId()].showWhile(createViewletPromise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */);
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		}

		// Fill Content and Actions
		return createViewletPromise.then(() => {

			// Make sure that the user meanwhile did not open another viewlet or closed the sidebar
			if (!this.activeViewlet || viewlet.getId() !== this.activeViewlet.getId()) {
				return;
			}

			// Take Viewlet on-DOM and show
			viewletContainer.build(this.getContentArea());
			viewletContainer.show();

			// Setup action runner
			this.toolBar.actionRunner = viewlet.getActionRunner();

			// Update title with viewlet title if it differs from descriptor
			let descriptor = this.registry.getViewlet(viewlet.getId());
			if (descriptor && descriptor.name !== viewlet.getTitle()) {
				this.updateTitle(viewlet.getId(), viewlet.getTitle());
			}

			// Handle Viewlet Actions
			let actionsBinding = this.mapActionsBindingToViewlet[viewlet.getId()];
			if (!actionsBinding) {
				actionsBinding = this.collectViewletActions(viewlet);
				this.mapActionsBindingToViewlet[viewlet.getId()] = actionsBinding;
			}
			actionsBinding();

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

			// Action Run Handling
			this.telemetryActionsListener = this.toolBar.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: 'sideBar' });
				}
			});

			// Indicate to viewlet that it is now visible
			return viewlet.setVisible(true).then(() => {

				// Make sure that the user meanwhile did not open another viewlet or closed the sidebar
				if (!this.activeViewlet || viewlet.getId() !== this.activeViewlet.getId()) {
					return;
				}

				// Make sure the viewlet is layed out
				if (this.contentAreaSize) {
					viewlet.layout(this.contentAreaSize);
				}

				// Emit Viewlet Opened Event
				this.emit(WorkbenchEventType.VIEWLET_OPENED, new ViewletEvent(this.activeViewlet.getId()));
			});
		}, (error: any) => this.onError(error));
	}

	private onTitleAreaUpdate(e: ViewletEvent): void {

		// Active Viewlet
		if (this.activeViewlet && this.activeViewlet.getId() === e.viewletId) {

			// Title
			this.updateTitle(this.activeViewlet.getId(), this.activeViewlet.getTitle());

			// Actions
			let actionsBinding = this.collectViewletActions(this.activeViewlet);
			this.mapActionsBindingToViewlet[this.activeViewlet.getId()] = actionsBinding;
			actionsBinding();
		}

		// Otherwise invalidate actions binding for next time when the viewlet becomes visible
		else {
			delete this.mapActionsBindingToViewlet[e.viewletId];
		}
	}

	private updateTitle(viewletId: string, viewletTitle?: string): void {
		let viewletDescriptor = this.registry.getViewlet(viewletId);
		if (!viewletDescriptor) {
			return;
		}

		if (!viewletTitle) {
			viewletTitle = viewletDescriptor.name;
		}

		let keybinding: string = null;
370
		let keys = this.keybindingService.lookupKeybindings(viewletId).map(k => this.keybindingService.getLabelFor(k));
E
Erich Gamma 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
		if (keys && keys.length) {
			keybinding = keys[0];
		}

		this.titleLabel.safeInnerHtml(viewletTitle);
		this.titleLabel.title(keybinding ? nls.localize('viewletTitleTooltip', "{0} ({1})", viewletTitle, keybinding) : viewletTitle);
	}

	private collectViewletActions(viewlet: Viewlet): () => void {

		// From Viewlet
		let primaryActions: IAction[] = viewlet.getActions();
		let secondaryActions: IAction[] = viewlet.getSecondaryActions();

		// From Contributions
		let actionBarRegistry = <IActionBarRegistry>Registry.as(Extensions.Actionbar);
		primaryActions.push(...actionBarRegistry.getActionBarActionsForContext(Scope.VIEW, viewlet));
		secondaryActions.push(...actionBarRegistry.getSecondaryActionBarActionsForContext(Scope.VIEW, viewlet));

		// Return fn to set into toolbar
		return this.toolBar.setActions(prepareActions(primaryActions), prepareActions(secondaryActions));
	}

	public getActiveViewlet(): IViewlet {
		return this.activeViewlet;
	}

	public getLastActiveViewletId(): string {
		return this.lastActiveViewletId;
	}

	public hideActiveViewlet(): TPromise<void> {
		if (!this.activeViewlet) {
			return TPromise.as(null); // Nothing to do
		}

		let viewlet = this.activeViewlet;
		this.activeViewlet = null;

		let viewletContainer = this.mapViewletToViewletContainer[viewlet.getId()];

		// Indicate to Viewlet
		return viewlet.setVisible(false).then(() => {

			// Take Container Off-DOM and hide
			viewletContainer.offDOM();
			viewletContainer.hide();

			// Clear any running Progress
			this.progressBar.stop().getContainer().hide();

			// Empty Actions
			this.toolBar.setActions([])();

			// Clear Listeners
			while (this.activeViewletListeners.length) {
				this.activeViewletListeners.pop()();
			}

			// Emit Viewlet Closed Event
			this.emit(WorkbenchEventType.VIEWLET_CLOSED, new ViewletEvent(viewlet.getId()));
		});
	}

	public createTitleArea(parent: Builder): Builder {

		// Title Area Container
		let titleArea = $(parent).div({
			'class': 'title'
		});

		// Right Actions Container
		$(titleArea).div({
			'class': 'title-actions'
		}, (div) => {

			// Toolbar
			this.toolBar = new ToolBar(div.getHTMLElement(), this.contextMenuService, {
				actionItemProvider: (action: Action) => this.actionItemProvider(action),
				orientation: ActionsOrientation.HORIZONTAL
			});
		});

		// Left Title Label
		$(titleArea).div({
			'class': 'title-label'
		}, (div) => {
			this.titleLabel = div.span();
		});

		return titleArea;
	}

	private actionItemProvider(action: Action): IActionItem {
		let actionItem: IActionItem;

		// Check Active Viewlet
		if (this.activeViewlet) {
			actionItem = this.activeViewlet.getActionItem(action);
		}

		// Check Registry
		if (!actionItem) {
			let actionBarRegistry = <IActionBarRegistry>Registry.as(Extensions.Actionbar);
			actionItem = actionBarRegistry.getActionItemForContext(Scope.VIEW, ToolBarContext, action);
		}

		return actionItem;
	}

	public createContentArea(parent: Builder): Builder {
		return $(parent).div({
			'class': 'content'
		}, (div: Builder) => {
			this.progressBar = new ProgressBar(div);
			this.progressBar.getContainer().hide();
		});
	}

	private onError(error: any): void {
		this.messageService.show(Severity.Error, types.isString(error) ? new Error(error) : error);
	}

	public layout(dimension: Dimension): Dimension[] {

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

		// Pass Contentsize to viewlet
		this.contentAreaSize = sizes[1];
		if (this.activeViewlet) {
			this.activeViewlet.layout(this.contentAreaSize);
		}

		return sizes;
	}

	public shutdown(): void {
		this.instantiatedViewlets.forEach(i => i.shutdown());

		super.shutdown();
	}

	public dispose(): void {
		this.mapViewletToViewletContainer = null;
		this.mapProgressServiceToViewlet = null;
		this.mapActionsBindingToViewlet = null;

		for (let i = 0; i < this.instantiatedViewlets.length; i++) {
			this.instantiatedViewlets[i].dispose();
		}

		this.instantiatedViewlets = [];

		while (this.activeViewletListeners.length) {
			this.activeViewletListeners.pop()();
		}

		while (this.instantiatedViewletListeners.length) {
			this.instantiatedViewletListeners.pop()();
		}

		this.progressBar.dispose();
		this.toolBar.dispose();

		// Super Dispose
		super.dispose();
	}
}

export class FocusSideBarAction extends Action {

	public static ID = 'workbench.action.focusSideBar';
	public static LABEL = nls.localize('focusSideBar', "Focus into Side Bar");

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService,
		@IPartService private partService: IPartService
	) {
		super(id, label);
	}

	public run(): TPromise<boolean> {

		// Show side bar
		if (this.partService.isSideBarHidden()) {
			this.partService.setSideBarHidden(false);
		}

		// Focus into active viewlet
		else {
			let viewlet = this.viewletService.getActiveViewlet();
			if (viewlet) {
				viewlet.focus();
			}
		}

		return TPromise.as(true);
	}
}

let registry = <IWorkbenchActionRegistry>Registry.as(ActionExtensions.WorkbenchActions);
registry.registerWorkbenchAction(new SyncActionDescriptor(FocusSideBarAction, FocusSideBarAction.ID, FocusSideBarAction.LABEL, {
	primary: KeyMod.CtrlCmd | KeyCode.KEY_0
}), nls.localize('viewCategory', "View"));