panelPart.ts 11.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';
J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
8
import { IAction, Action } from 'vs/base/common/actions';
9
import Event from 'vs/base/common/event';
I
isidor 已提交
10
import { Builder, Dimension } from 'vs/base/browser/builder';
11
import { Registry } from 'vs/platform/registry/common/platform';
I
isidor 已提交
12
import { ActionsOrientation } from 'vs/base/browser/ui/actionbar/actionbar';
13
import { Scope } from 'vs/workbench/browser/actions';
J
Johannes Rieken 已提交
14
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';
I
isidor 已提交
25
import { ClosePanelAction, ToggleMaximizedPanelAction, PanelActivityAction, OpenPanelAction } from 'vs/workbench/browser/parts/panel/panelActions';
26
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
27
import { PANEL_BACKGROUND, PANEL_BORDER, PANEL_ACTIVE_TITLE_FOREGROUND, PANEL_INACTIVE_TITLE_FOREGROUND, PANEL_ACTIVE_TITLE_BORDER } from 'vs/workbench/common/theme';
28
import { activeContrastBorder, focusBorder, contrastBorder, editorBackground } from 'vs/platform/theme/common/colorRegistry';
I
isidor 已提交
29 30 31
import { CompositeBar } from 'vs/workbench/browser/parts/compositebar/compositeBar';
import { ToggleCompositePinnedAction } from 'vs/workbench/browser/parts/compositebar/compositeBarActions';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
I
isidor 已提交
32 33
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { IBadge } from 'vs/workbench/services/activity/common/activity';
I
isidor 已提交
34 35 36 37

export class PanelPart extends CompositePart<Panel> implements IPanelService {

	public static activePanelSettingsKey = 'workbench.panelpart.activepanelid';
I
isidor 已提交
38
	private static readonly PINNED_PANELS = 'workbench.panel.pinnedPanels';
I
isidor 已提交
39

40
	public _serviceBrand: any;
41

I
isidor 已提交
42
	private blockOpeningPanel: boolean;
I
isidor 已提交
43
	private compositeBar: CompositeBar;
I
isidor 已提交
44 45

	constructor(
46 47 48 49 50 51
		id: string,
		@IMessageService messageService: IMessageService,
		@IStorageService storageService: IStorageService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IPartService partService: IPartService,
52
		@IKeybindingService keybindingService: IKeybindingService,
53
		@IInstantiationService instantiationService: IInstantiationService,
I
isidor 已提交
54
		@IThemeService themeService: IThemeService,
I
isidor 已提交
55
	) {
56 57 58 59 60 61 62
		super(
			messageService,
			storageService,
			telemetryService,
			contextMenuService,
			partService,
			keybindingService,
63
			instantiationService,
64
			themeService,
65
			Registry.as<PanelRegistry>(PanelExtensions.Panels),
66
			PanelPart.activePanelSettingsKey,
67
			Registry.as<PanelRegistry>(PanelExtensions.Panels).getDefaultPanelId(),
68 69 70
			'panel',
			'panel',
			Scope.PANEL,
71
			null,
72 73
			id,
			{ hasTitle: true }
74
		);
75

I
isidor 已提交
76 77 78 79 80 81 82 83 84 85
		this.compositeBar = this.instantiationService.createInstance(CompositeBar, {
			icon: false,
			storageId: PanelPart.PINNED_PANELS,
			orientation: ActionsOrientation.HORIZONTAL,
			composites: this.getPanels(),
			openComposite: (compositeId: string) => this.openPanel(compositeId, true),
			getActivityAction: (compositeId: string) => this.instantiationService.createInstance(PanelActivityAction, this.getPanel(compositeId)),
			getCompositePinnedAction: (compositeId: string) => new ToggleCompositePinnedAction(this.getPanel(compositeId), this.compositeBar),
			getOnCompositeClickAction: (compositeId: string) => this.instantiationService.createInstance(OpenPanelAction, this.getPanel(compositeId)),
			getDefaultCompositeId: () => Registry.as<PanelRegistry>(PanelExtensions.Panels).getDefaultPanelId(),
I
isidor 已提交
86 87
			hidePart: () => this.partService.setPanelHidden(true),
			backgroundColor: PANEL_BACKGROUND
I
isidor 已提交
88 89
		});
		this.toUnbind.push(this.compositeBar);
90 91 92 93 94 95 96

		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate panel action on opening of a panel
I
isidor 已提交
97
		this.toUnbind.push(this.onDidPanelOpen(panel => this.compositeBar.activateComposite(panel.getId())));
98 99

		// Deactivate panel action on close
I
isidor 已提交
100 101
		this.toUnbind.push(this.onDidPanelClose(panel => this.compositeBar.deactivateComposite(panel.getId())));
		this.toUnbind.push(this.compositeBar.onDidContextMenu(e => this.showContextMenu(e)));
I
isidor 已提交
102 103
	}

104
	public get onDidPanelOpen(): Event<IPanel> {
105
		return this._onDidCompositeOpen.event;
106 107 108
	}

	public get onDidPanelClose(): Event<IPanel> {
109
		return this._onDidCompositeClose.event;
110 111
	}

B
Benjamin Pasero 已提交
112
	protected updateStyles(): void {
113 114
		super.updateStyles();

B
Benjamin Pasero 已提交
115
		const container = this.getContainer();
116
		container.style('background-color', this.getColor(PANEL_BACKGROUND));
B
Benjamin Pasero 已提交
117 118

		const title = this.getTitleArea();
119
		title.style('border-top-color', this.getColor(PANEL_BORDER) || this.getColor(contrastBorder));
120 121
	}

I
isidor 已提交
122 123 124 125 126
	public openPanel(id: string, focus?: boolean): TPromise<Panel> {
		if (this.blockOpeningPanel) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

127
		// First check if panel is hidden and show if so
128
		let promise = TPromise.as<any>(null);
129
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
I
isidor 已提交
130 131
			try {
				this.blockOpeningPanel = true;
132
				promise = this.partService.setPanelHidden(false);
I
isidor 已提交
133 134 135 136 137
			} finally {
				this.blockOpeningPanel = false;
			}
		}

138
		return promise.then(() => this.openComposite(id, focus));
I
isidor 已提交
139 140
	}

I
isidor 已提交
141 142 143 144
	public showActivity(panelId: string, badge: IBadge, clazz?: string): IDisposable {
		return this.compositeBar.showActivity(panelId, badge, clazz);
	}

I
isidor 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	private getPanel(panelId: string): IPanelIdentifier {
		return Registry.as<PanelRegistry>(PanelExtensions.Panels).getPanels().filter(p => p.id === panelId).pop();
	}

	private showContextMenu(e: MouseEvent): void {
		const event = new StandardMouseEvent(e);
		const actions: Action[] = this.getPanels().map(panel => this.instantiationService.createInstance(ToggleCompositePinnedAction, panel, this.compositeBar));

		this.contextMenuService.showContextMenu({
			getAnchor: () => { return { x: event.posx, y: event.posy }; },
			getActions: () => TPromise.as(actions),
			onHide: () => dispose(actions)
		});
	}

160 161
	public getPanels(): IPanelIdentifier[] {
		return Registry.as<PanelRegistry>(PanelExtensions.Panels).getPanels()
B
Benjamin Pasero 已提交
162
			.sort((v1, v2) => v1.order - v2.order);
163 164
	}

I
isidor 已提交
165
	protected getActions(): IAction[] {
I
isidor 已提交
166 167 168 169
		return [
			this.instantiationService.createInstance(ToggleMaximizedPanelAction, ToggleMaximizedPanelAction.ID, ToggleMaximizedPanelAction.LABEL),
			this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)
		];
I
isidor 已提交
170 171
	}

I
isidor 已提交
172 173 174 175 176 177 178 179 180
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

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

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

184
	protected createTitleLabel(parent: Builder): ICompositeTitleLabel {
I
isidor 已提交
185 186
		const titleArea = this.compositeBar.create(parent.getHTMLElement());
		titleArea.classList.add('panel-switcher-container');
I
isidor 已提交
187

188 189
		return {
			updateTitle: (id, title, keybinding) => {
I
isidor 已提交
190
				const action = this.compositeBar.getAction(id);
191 192 193
				if (action) {
					action.label = title;
				}
194 195
			},
			updateStyles: () => {
196
				// Handled via theming participant
197 198
			}
		};
I
isidor 已提交
199 200
	}

I
isidor 已提交
201
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
202

I
isidor 已提交
203 204 205 206 207 208 209 210 211 212 213
		// Pass to super
		const sizes = super.layout(dimension);
		let availableWidth = dimension.width;
		if (this.toolBar) {
			// adjust height for global actions showing
			availableWidth -= this.toolBar.getContainer().getHTMLElement().offsetWidth;
		}
		this.compositeBar.layout(new Dimension(availableWidth, dimension.height));

		return sizes;
	}
I
isidor 已提交
214

I
isidor 已提交
215 216 217
	public shutdown(): void {
		// Persist Hidden State
		this.compositeBar.store();
I
isidor 已提交
218

I
isidor 已提交
219 220
		// Pass to super
		super.shutdown();
I
isidor 已提交
221
	}
222 223 224 225
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {

226 227 228 229 230 231 232 233 234 235 236 237 238 239
	// Panel Background: since panels can host editors, we apply a background rule if the panel background
	// color is different from the editor background color. This is a bit of a hack though. The better way
	// would be to have a way to push the background color onto each editor widget itself somehow.
	const panelBackground = theme.getColor(PANEL_BACKGROUND);
	if (panelBackground && panelBackground !== theme.getColor(editorBackground)) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .content .monaco-editor,
			.monaco-workbench > .part.panel > .content .monaco-editor .margin,
			.monaco-workbench > .part.panel > .content .monaco-editor .monaco-editor-background {
				background-color: ${panelBackground};
			}
		`);
	}

240
	// Title Active
241
	const titleActive = theme.getColor(PANEL_ACTIVE_TITLE_FOREGROUND);
242
	const titleActiveBorder = theme.getColor(PANEL_ACTIVE_TITLE_BORDER);
243 244 245
	if (titleActive || titleActiveBorder) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:hover .action-label,
I
isidor 已提交
246
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label {
247 248 249 250 251
				color: ${titleActive};
				border-bottom-color: ${titleActiveBorder};
			}
		`);
	}
252 253

	// Title Inactive
254
	const titleInactive = theme.getColor(PANEL_INACTIVE_TITLE_FOREGROUND);
255 256 257 258 259 260 261
	if (titleInactive) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item .action-label {
				color: ${titleInactive};
			}
		`);
	}
262 263

	// Title focus
264
	const focusBorderColor = theme.getColor(focusBorder);
265 266
	if (focusBorderColor) {
		collector.addRule(`
I
isidor 已提交
267
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus {
268 269 270 271 272 273 274
				color: ${titleActive};
				border-bottom-color: ${focusBorderColor} !important;
				border-bottom: 1px solid;
				outline: none;
			}
		`);
	}
275

B
Benjamin Pasero 已提交
276
	// Styling with Outline color (e.g. high contrast theme)
277
	const outline = theme.getColor(activeContrastBorder);
B
Benjamin Pasero 已提交
278
	if (outline) {
279
		const outline = theme.getColor(activeContrastBorder);
280 281

		collector.addRule(`
I
isidor 已提交
282
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label,
283 284 285 286 287 288 289 290 291
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item .action-label:hover {
				outline-color: ${outline};
				outline-width: 1px;
				outline-style: solid;
				border-bottom: none;
				padding-bottom: 0;
				outline-offset: 3px;
			}

I
isidor 已提交
292
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:not(.checked) .action-label:hover {
293 294 295 296 297
				outline-style: dashed;
			}
		`);
	}
});