panelPart.ts 13.1 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, Position } 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 { ClosePanelAction, TogglePanelPositionAction, PanelActivityAction, ToggleMaximizedPanelAction } 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

export class PanelPart extends CompositePart<Panel> implements IPanelService {

M
Matt Bierner 已提交
37
	public static readonly activePanelSettingsKey = 'workbench.panelpart.activepanelid';
I
isidor 已提交
38
	private static readonly PINNED_PANELS = 'workbench.panel.pinnedPanels';
I
isidor 已提交
39
	private static readonly MIN_COMPOSITE_BAR_WIDTH = 50;
I
isidor 已提交
40

41
	public _serviceBrand: any;
42

I
isidor 已提交
43
	private blockOpeningPanel: boolean;
I
isidor 已提交
44
	private compositeBar: CompositeBar;
45
	private dimension: Dimension;
I
isidor 已提交
46
	private toolbarWidth = new Map<string, number>();
I
isidor 已提交
47 48

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

I
isidor 已提交
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),
87
			getOnCompositeClickAction: (compositeId: string) => this.instantiationService.createInstance(PanelActivityAction, this.getPanel(compositeId)),
I
isidor 已提交
88
			getDefaultCompositeId: () => Registry.as<PanelRegistry>(PanelExtensions.Panels).getDefaultPanelId(),
I
isidor 已提交
89
			hidePart: () => this.partService.setPanelHidden(true),
I
isidor 已提交
90
			overflowActionSize: 44,
I
isidor 已提交
91 92
			colors: {
				backgroundColor: PANEL_BACKGROUND,
I
isidor 已提交
93 94
				badgeBackground,
				badgeForeground,
I
isidor 已提交
95 96
				dragAndDropBackground: PANEL_DRAG_AND_DROP_BACKGROUND
			}
I
isidor 已提交
97 98
		});
		this.toUnbind.push(this.compositeBar);
99 100 101 102 103 104 105

		this.registerListeners();
	}

	private registerListeners(): void {

		// Activate panel action on opening of a panel
106 107 108 109 110
		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();
		}));
111 112

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

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

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

I
isidor 已提交
125
	public updateStyles(): void {
126 127
		super.updateStyles();

B
Benjamin Pasero 已提交
128
		const container = this.getContainer();
129
		container.style('background-color', this.getColor(PANEL_BACKGROUND));
I
isidor 已提交
130
		container.style('border-left-color', this.getColor(PANEL_BORDER) || this.getColor(contrastBorder));
B
Benjamin Pasero 已提交
131 132

		const title = this.getTitleArea();
133
		title.style('border-top-color', this.getColor(PANEL_BORDER) || this.getColor(contrastBorder));
134 135
	}

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

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

152
		return promise.then(() => this.openComposite(id, focus));
I
isidor 已提交
153 154
	}

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

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

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

I
isidor 已提交
179
	protected getActions(): IAction[] {
I
isidor 已提交
180
		return [
181
			this.instantiationService.createInstance(ToggleMaximizedPanelAction, ToggleMaximizedPanelAction.ID, ToggleMaximizedPanelAction.LABEL),
I
isidor 已提交
182
			this.instantiationService.createInstance(TogglePanelPositionAction, TogglePanelPositionAction.ID, TogglePanelPositionAction.LABEL),
I
isidor 已提交
183 184
			this.instantiationService.createInstance(ClosePanelAction, ClosePanelAction.ID, ClosePanelAction.LABEL)
		];
I
isidor 已提交
185 186
	}

I
isidor 已提交
187 188 189 190 191 192 193 194 195
	public getActivePanel(): IPanel {
		return this.getActiveComposite();
	}

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

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

199
	protected createTitleLabel(parent: Builder): ICompositeTitleLabel {
I
isidor 已提交
200 201
		const titleArea = this.compositeBar.create(parent.getHTMLElement());
		titleArea.classList.add('panel-switcher-container');
I
isidor 已提交
202

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

I
isidor 已提交
216
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
217 218 219
		if (!this.partService.isVisible(Parts.PANEL_PART)) {
			return [dimension];
		}
I
isidor 已提交
220

221 222 223 224 225 226 227
		if (this.partService.getPanelPosition() === Position.RIGHT) {
			// Take into account the 1px border when layouting
			this.dimension = new Dimension(dimension.width - 1, dimension.height);
		} else {
			this.dimension = dimension;
		}
		const sizes = super.layout(this.dimension);
228 229 230 231 232 233
		this.layoutCompositeBar();

		return sizes;
	}

	private layoutCompositeBar(): void {
I
isidor 已提交
234
		if (this.dimension) {
I
isidor 已提交
235
			let availableWidth = this.dimension.width - 40; // take padding into account
I
isidor 已提交
236 237
			if (this.toolBar) {
				// adjust height for global actions showing
I
isidor 已提交
238
				availableWidth = Math.max(PanelPart.MIN_COMPOSITE_BAR_WIDTH, availableWidth - this.getToolbarWidth());
I
isidor 已提交
239 240
			}
			this.compositeBar.layout(new Dimension(availableWidth, this.dimension.height));
I
isidor 已提交
241 242
		}
	}
I
isidor 已提交
243

I
isidor 已提交
244 245 246 247 248 249 250 251 252 253
	private getToolbarWidth(): number {
		const activePanel = this.getActivePanel();
		if (!activePanel) {
			return 0;
		}
		if (!this.toolbarWidth.has(activePanel.getId())) {
			this.toolbarWidth.set(activePanel.getId(), this.toolBar.getContainer().getHTMLElement().offsetWidth);
		}

		return this.toolbarWidth.get(activePanel.getId());
I
isidor 已提交
254 255
	}

I
isidor 已提交
256 257 258
	public shutdown(): void {
		// Persist Hidden State
		this.compositeBar.store();
I
isidor 已提交
259

I
isidor 已提交
260 261
		// Pass to super
		super.shutdown();
I
isidor 已提交
262
	}
263 264 265 266
}

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

267 268 269 270 271 272 273 274 275 276 277 278 279 280
	// 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};
			}
		`);
	}

281
	// Title Active
282
	const titleActive = theme.getColor(PANEL_ACTIVE_TITLE_FOREGROUND);
283
	const titleActiveBorder = theme.getColor(PANEL_ACTIVE_TITLE_BORDER);
284 285 286
	if (titleActive || titleActiveBorder) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:hover .action-label,
I
isidor 已提交
287
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label {
288 289 290 291 292
				color: ${titleActive};
				border-bottom-color: ${titleActiveBorder};
			}
		`);
	}
293 294

	// Title Inactive
295
	const titleInactive = theme.getColor(PANEL_INACTIVE_TITLE_FOREGROUND);
296 297 298 299 300 301 302
	if (titleInactive) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item .action-label {
				color: ${titleInactive};
			}
		`);
	}
303 304

	// Title focus
305
	const focusBorderColor = theme.getColor(focusBorder);
306 307
	if (focusBorderColor) {
		collector.addRule(`
I
isidor 已提交
308
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus .action-label {
309 310 311
				color: ${titleActive};
				border-bottom-color: ${focusBorderColor} !important;
				border-bottom: 1px solid;
I
isidor 已提交
312 313 314 315
			}
			`);
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus {
316 317
				outline: none;
			}
I
isidor 已提交
318
			`);
319
	}
320

B
Benjamin Pasero 已提交
321
	// Styling with Outline color (e.g. high contrast theme)
322
	const outline = theme.getColor(activeContrastBorder);
B
Benjamin Pasero 已提交
323
	if (outline) {
324
		const outline = theme.getColor(activeContrastBorder);
325 326

		collector.addRule(`
I
isidor 已提交
327
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label,
328 329 330 331 332 333 334 335 336
			.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 已提交
337
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:not(.checked) .action-label:hover {
338 339 340 341 342
				outline-style: dashed;
			}
		`);
	}
});