panelPart.ts 11.9 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';
I
isidor 已提交
27 28
import { PANEL_BACKGROUND, PANEL_BORDER, PANEL_ACTIVE_TITLE_FOREGROUND, PANEL_INACTIVE_TITLE_FOREGROUND, PANEL_ACTIVE_TITLE_BORDER, PANEL_DRAG_AND_DROP_BACKGROUND } from 'vs/workbench/common/theme';
import { activeContrastBorder, focusBorder, contrastBorder, editorBackground, badgeBackground, badgeForeground } 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;
44
	private dimension: Dimension;
I
isidor 已提交
45 46

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

I
isidor 已提交
77 78 79 80 81 82 83 84 85 86
		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 已提交
87
			hidePart: () => this.partService.setPanelHidden(true),
I
isidor 已提交
88
			overflowActionSize: 60,
I
isidor 已提交
89 90
			colors: {
				backgroundColor: PANEL_BACKGROUND,
I
isidor 已提交
91 92
				badgeBackground,
				badgeForeground,
I
isidor 已提交
93 94
				dragAndDropBackground: PANEL_DRAG_AND_DROP_BACKGROUND
			}
I
isidor 已提交
95 96
		});
		this.toUnbind.push(this.compositeBar);
97 98 99 100 101 102 103

		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate panel action on opening of a panel
104 105 106 107 108
		this.toUnbind.push(this.onDidPanelOpen(panel => {
			this.compositeBar.activateComposite(panel.getId());
			// Need to relayout composite bar since different panels have different action bar width
			this.layoutCompositeBar();
		}));
109 110

		// Deactivate panel action on close
I
isidor 已提交
111 112
		this.toUnbind.push(this.onDidPanelClose(panel => this.compositeBar.deactivateComposite(panel.getId())));
		this.toUnbind.push(this.compositeBar.onDidContextMenu(e => this.showContextMenu(e)));
I
isidor 已提交
113 114
	}

115
	public get onDidPanelOpen(): Event<IPanel> {
116
		return this._onDidCompositeOpen.event;
117 118 119
	}

	public get onDidPanelClose(): Event<IPanel> {
120
		return this._onDidCompositeClose.event;
121 122
	}

B
Benjamin Pasero 已提交
123
	protected updateStyles(): void {
124 125
		super.updateStyles();

B
Benjamin Pasero 已提交
126
		const container = this.getContainer();
127
		container.style('background-color', this.getColor(PANEL_BACKGROUND));
B
Benjamin Pasero 已提交
128 129

		const title = this.getTitleArea();
130
		title.style('border-top-color', this.getColor(PANEL_BORDER) || this.getColor(contrastBorder));
131 132
	}

I
isidor 已提交
133 134 135 136 137
	public openPanel(id: string, focus?: boolean): TPromise<Panel> {
		if (this.blockOpeningPanel) {
			return TPromise.as(null); // Workaround against a potential race condition
		}

138
		// First check if panel is hidden and show if so
139
		let promise = TPromise.as<any>(null);
140
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
I
isidor 已提交
141 142
			try {
				this.blockOpeningPanel = true;
143
				promise = this.partService.setPanelHidden(false);
I
isidor 已提交
144 145 146 147 148
			} finally {
				this.blockOpeningPanel = false;
			}
		}

149
		return promise.then(() => this.openComposite(id, focus));
I
isidor 已提交
150 151
	}

I
isidor 已提交
152 153 154 155
	public showActivity(panelId: string, badge: IBadge, clazz?: string): IDisposable {
		return this.compositeBar.showActivity(panelId, badge, clazz);
	}

I
isidor 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	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)
		});
	}

171 172
	public getPanels(): IPanelIdentifier[] {
		return Registry.as<PanelRegistry>(PanelExtensions.Panels).getPanels()
B
Benjamin Pasero 已提交
173
			.sort((v1, v2) => v1.order - v2.order);
174 175
	}

I
isidor 已提交
176
	protected getActions(): IAction[] {
I
isidor 已提交
177 178 179 180
		return [
			this.instantiationService.createInstance(ToggleMaximizedPanelAction, ToggleMaximizedPanelAction.ID, ToggleMaximizedPanelAction.LABEL),
			this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)
		];
I
isidor 已提交
181 182
	}

I
isidor 已提交
183 184 185 186 187 188 189 190 191
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

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

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

195
	protected createTitleLabel(parent: Builder): ICompositeTitleLabel {
I
isidor 已提交
196 197
		const titleArea = this.compositeBar.create(parent.getHTMLElement());
		titleArea.classList.add('panel-switcher-container');
I
isidor 已提交
198

199 200
		return {
			updateTitle: (id, title, keybinding) => {
I
isidor 已提交
201
				const action = this.compositeBar.getAction(id);
202 203 204
				if (action) {
					action.label = title;
				}
205 206
			},
			updateStyles: () => {
207
				// Handled via theming participant
208 209
			}
		};
I
isidor 已提交
210 211
	}

I
isidor 已提交
212
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
213

I
isidor 已提交
214 215
		// Pass to super
		const sizes = super.layout(dimension);
216 217 218 219 220 221 222 223
		this.dimension = dimension;
		this.layoutCompositeBar();

		return sizes;
	}

	private layoutCompositeBar(): void {
		let availableWidth = this.dimension.width - 8; // take padding into account
I
isidor 已提交
224 225 226 227
		if (this.toolBar) {
			// adjust height for global actions showing
			availableWidth -= this.toolBar.getContainer().getHTMLElement().offsetWidth;
		}
228
		this.compositeBar.layout(new Dimension(availableWidth, this.dimension.height));
I
isidor 已提交
229
	}
I
isidor 已提交
230

I
isidor 已提交
231 232 233
	public shutdown(): void {
		// Persist Hidden State
		this.compositeBar.store();
I
isidor 已提交
234

I
isidor 已提交
235 236
		// Pass to super
		super.shutdown();
I
isidor 已提交
237
	}
238 239 240 241
}

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

242 243 244 245 246 247 248 249 250 251 252 253 254 255
	// 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};
			}
		`);
	}

256
	// Title Active
257
	const titleActive = theme.getColor(PANEL_ACTIVE_TITLE_FOREGROUND);
258
	const titleActiveBorder = theme.getColor(PANEL_ACTIVE_TITLE_BORDER);
259 260 261
	if (titleActive || titleActiveBorder) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:hover .action-label,
I
isidor 已提交
262
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label {
263 264 265 266 267
				color: ${titleActive};
				border-bottom-color: ${titleActiveBorder};
			}
		`);
	}
268 269

	// Title Inactive
270
	const titleInactive = theme.getColor(PANEL_INACTIVE_TITLE_FOREGROUND);
271 272 273 274 275 276 277
	if (titleInactive) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item .action-label {
				color: ${titleInactive};
			}
		`);
	}
278 279

	// Title focus
280
	const focusBorderColor = theme.getColor(focusBorder);
281 282
	if (focusBorderColor) {
		collector.addRule(`
I
isidor 已提交
283
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus {
284 285 286 287 288 289 290
				color: ${titleActive};
				border-bottom-color: ${focusBorderColor} !important;
				border-bottom: 1px solid;
				outline: none;
			}
		`);
	}
291

B
Benjamin Pasero 已提交
292
	// Styling with Outline color (e.g. high contrast theme)
293
	const outline = theme.getColor(activeContrastBorder);
B
Benjamin Pasero 已提交
294
	if (outline) {
295
		const outline = theme.getColor(activeContrastBorder);
296 297

		collector.addRule(`
I
isidor 已提交
298
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label,
299 300 301 302 303 304 305 306 307
			.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 已提交
308
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:not(.checked) .action-label:hover {
309 310 311 312 313
				outline-style: dashed;
			}
		`);
	}
});