panelPart.ts 12.7 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 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
	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 47

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

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

		this.registerListeners();
	}

	private registerListeners(): void {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

I
isidor 已提交
215
	public layout(dimension: Dimension): Dimension[] {
I
isidor 已提交
216

217 218 219 220 221 222 223
		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);
224 225 226 227 228 229
		this.layoutCompositeBar();

		return sizes;
	}

	private layoutCompositeBar(): void {
I
isidor 已提交
230 231 232 233
		if (this.dimension) {
			let availableWidth = this.dimension.width - 8; // take padding into account
			if (this.toolBar) {
				// adjust height for global actions showing
I
isidor 已提交
234
				availableWidth = Math.max(PanelPart.MIN_COMPOSITE_BAR_WIDTH, availableWidth - this.toolBar.getContainer().getHTMLElement().offsetWidth);
I
isidor 已提交
235 236
			}
			this.compositeBar.layout(new Dimension(availableWidth, this.dimension.height));
I
isidor 已提交
237 238
		}
	}
I
isidor 已提交
239

I
isidor 已提交
240 241 242
	public shutdown(): void {
		// Persist Hidden State
		this.compositeBar.store();
I
isidor 已提交
243

I
isidor 已提交
244 245
		// Pass to super
		super.shutdown();
I
isidor 已提交
246
	}
247 248 249 250
}

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

251 252 253 254 255 256 257 258 259 260 261 262 263 264
	// 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};
			}
		`);
	}

265
	// Title Active
266
	const titleActive = theme.getColor(PANEL_ACTIVE_TITLE_FOREGROUND);
267
	const titleActiveBorder = theme.getColor(PANEL_ACTIVE_TITLE_BORDER);
268 269 270
	if (titleActive || titleActiveBorder) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:hover .action-label,
I
isidor 已提交
271
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label {
272 273 274 275 276
				color: ${titleActive};
				border-bottom-color: ${titleActiveBorder};
			}
		`);
	}
277 278

	// Title Inactive
279
	const titleInactive = theme.getColor(PANEL_INACTIVE_TITLE_FOREGROUND);
280 281 282 283 284 285 286
	if (titleInactive) {
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item .action-label {
				color: ${titleInactive};
			}
		`);
	}
287 288

	// Title focus
289
	const focusBorderColor = theme.getColor(focusBorder);
290 291
	if (focusBorderColor) {
		collector.addRule(`
I
isidor 已提交
292
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus .action-label {
293 294 295
				color: ${titleActive};
				border-bottom-color: ${focusBorderColor} !important;
				border-bottom: 1px solid;
I
isidor 已提交
296 297 298 299
			}
			`);
		collector.addRule(`
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:focus {
300 301
				outline: none;
			}
I
isidor 已提交
302
			`);
303
	}
304

B
Benjamin Pasero 已提交
305
	// Styling with Outline color (e.g. high contrast theme)
306
	const outline = theme.getColor(activeContrastBorder);
B
Benjamin Pasero 已提交
307
	if (outline) {
308
		const outline = theme.getColor(activeContrastBorder);
309 310

		collector.addRule(`
I
isidor 已提交
311
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item.checked .action-label,
312 313 314 315 316 317 318 319 320
			.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 已提交
321
			.monaco-workbench > .part.panel > .title > .panel-switcher-container > .monaco-action-bar .action-item:not(.checked) .action-label:hover {
322 323 324 325 326
				outline-style: dashed;
			}
		`);
	}
});