panelPart.ts 6.4 KB
Newer Older
I
isidor 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import 'vs/css!./media/panelpart';
I
isidor 已提交
7
import nls = require('vs/nls');
J
Johannes Rieken 已提交
8
import { TPromise } from 'vs/base/common/winjs.base';
9
import { IAction } from 'vs/base/common/actions';
10
import Event from 'vs/base/common/event';
11
import { Builder, $ } from 'vs/base/browser/builder';
J
Johannes Rieken 已提交
12 13 14
import { Registry } from 'vs/platform/platform';
import { Scope } from 'vs/workbench/browser/actionBarRegistry';
import { IPanel } from 'vs/workbench/common/panel';
15
import { CompositePart, ICompositeTitleLabel } from 'vs/workbench/browser/parts/compositePart';
16 17
import { Panel, PanelRegistry, Extensions as PanelExtensions } from 'vs/workbench/browser/panel';
import { IPanelService, IPanelIdentifier } from 'vs/workbench/services/panel/common/panelService';
18
import { IPartService, Parts } from 'vs/workbench/services/part/common/partService';
J
Johannes Rieken 已提交
19 20 21 22 23 24
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IMessageService } from 'vs/platform/message/common/message';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
25
import { ActionsOrientation, ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
I
isidor 已提交
26
import { ClosePanelAction, PanelAction, ToggleMaximizedPanelAction } from 'vs/workbench/browser/parts/panel/panelActions';
27
import { IThemeService } from 'vs/platform/theme/common/themeService';
B
Benjamin Pasero 已提交
28
import { PANEL_BACKGROUND, PANEL_BORDER_TOP_COLOR } from 'vs/workbench/common/theme';
I
isidor 已提交
29 30 31 32 33

export class PanelPart extends CompositePart<Panel> implements IPanelService {

	public static activePanelSettingsKey = 'workbench.panelpart.activepanelid';

34
	public _serviceBrand: any;
35

I
isidor 已提交
36
	private blockOpeningPanel: boolean;
37 38 39
	private panelSwitcherBar: ActionBar;

	private panelIdToActions: { [panelId: string]: PanelAction; };
I
isidor 已提交
40 41

	constructor(
42 43 44 45 46 47
		id: string,
		@IMessageService messageService: IMessageService,
		@IStorageService storageService: IStorageService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IPartService partService: IPartService,
48
		@IKeybindingService keybindingService: IKeybindingService,
49 50
		@IInstantiationService instantiationService: IInstantiationService,
		@IThemeService themeService: IThemeService
I
isidor 已提交
51
	) {
52 53 54 55 56 57 58
		super(
			messageService,
			storageService,
			telemetryService,
			contextMenuService,
			partService,
			keybindingService,
59
			instantiationService,
60
			themeService,
61
			Registry.as<PanelRegistry>(PanelExtensions.Panels),
62 63 64 65
			PanelPart.activePanelSettingsKey,
			'panel',
			'panel',
			Scope.PANEL,
66
			null, // TODO@theme
67 68
			id,
			{ hasTitle: true }
69
		);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

		this.panelIdToActions = Object.create(null);

		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate panel action on opening of a panel
		this.toUnbind.push(this.onDidPanelOpen(panel => this.updatePanelActions(panel.getId(), true)));

		// Deactivate panel action on close
		this.toUnbind.push(this.onDidPanelClose(panel => this.updatePanelActions(panel.getId(), false)));
	}

	private updatePanelActions(id: string, didOpen: boolean): void {
		if (this.panelIdToActions[id]) {
			didOpen ? this.panelIdToActions[id].activate() : this.panelIdToActions[id].deactivate();
		}
I
isidor 已提交
89 90
	}

91
	public get onDidPanelOpen(): Event<IPanel> {
92
		return this._onDidCompositeOpen.event;
93 94 95
	}

	public get onDidPanelClose(): Event<IPanel> {
96
		return this._onDidCompositeClose.event;
97 98
	}

B
Benjamin Pasero 已提交
99
	protected updateStyles(): void {
100 101
		super.updateStyles();

B
Benjamin Pasero 已提交
102 103
		const container = this.getContainer();
		container.style('background-color', this.getColor(PANEL_BACKGROUND));
B
Benjamin Pasero 已提交
104 105 106

		const title = this.getTitleArea();
		title.style('border-top-color', this.getColor(PANEL_BORDER_TOP_COLOR));
107 108
	}

I
isidor 已提交
109 110 111 112 113
	public openPanel(id: string, focus?: boolean): TPromise<Panel> {
		if (this.blockOpeningPanel) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

114
		// First check if panel is hidden and show if so
115
		let promise = TPromise.as(null);
116
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
I
isidor 已提交
117 118
			try {
				this.blockOpeningPanel = true;
119
				promise = this.partService.setPanelHidden(false);
I
isidor 已提交
120 121 122 123 124
			} finally {
				this.blockOpeningPanel = false;
			}
		}

125
		return promise.then(() => this.openComposite(id, focus));
I
isidor 已提交
126 127
	}

128 129
	public getPanels(): IPanelIdentifier[] {
		return Registry.as<PanelRegistry>(PanelExtensions.Panels).getPanels()
B
Benjamin Pasero 已提交
130
			.sort((v1, v2) => v1.order - v2.order);
131 132
	}

I
isidor 已提交
133
	protected getActions(): IAction[] {
I
isidor 已提交
134 135 136 137
		return [
			this.instantiationService.createInstance(ToggleMaximizedPanelAction, ToggleMaximizedPanelAction.ID, ToggleMaximizedPanelAction.LABEL),
			this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)
		];
I
isidor 已提交
138 139
	}

I
isidor 已提交
140 141 142 143 144 145 146 147 148
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

	public getLastActivePanelId(): string {
		return this.getLastActiveCompositetId();
	}

	public hideActivePanel(): TPromise<void> {
149
		return this.hideActiveComposite().then(composite => void 0);
I
isidor 已提交
150
	}
I
isidor 已提交
151

152 153 154 155
	protected createTitleLabel(parent: Builder): ICompositeTitleLabel {
		let titleArea = $(parent).div({
			'class': ['panel-switcher-container']
		});
I
isidor 已提交
156

157 158 159 160 161 162
		// Show a panel switcher
		this.panelSwitcherBar = new ActionBar(titleArea, {
			orientation: ActionsOrientation.HORIZONTAL,
			ariaLabel: nls.localize('panelSwitcherBarAriaLabel', "Active Panel Switcher"),
			animated: false
		});
B
Benjamin Pasero 已提交
163
		this.toUnbind.push(this.panelSwitcherBar);
I
isidor 已提交
164

B
Benjamin Pasero 已提交
165
		this.fillPanelSwitcher();
I
isidor 已提交
166

167 168 169 170 171 172
		return {
			updateTitle: (id, title, keybinding) => {
				const action = this.panelIdToActions[id];
				if (action) {
					action.label = title;
				}
173 174 175
			},
			updateStyles: () => {
				// TODO@theme
176 177
			}
		};
I
isidor 已提交
178 179
	}

B
Benjamin Pasero 已提交
180
	private fillPanelSwitcher(): void {
181
		const panels = this.getPanels();
I
isidor 已提交
182

183 184
		this.panelSwitcherBar.push(panels.map(panel => {
			const action = this.instantiationService.createInstance(PanelAction, panel);
I
isidor 已提交
185

186
			this.panelIdToActions[panel.id] = action;
B
Benjamin Pasero 已提交
187
			this.toUnbind.push(action);
I
isidor 已提交
188

189
			return action;
B
Benjamin Pasero 已提交
190
		}));
I
isidor 已提交
191
	}
192
}