titleControl.ts 15.5 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8 9 10 11 12 13 14 15 16 17
import { applyDragImage } from 'vs/base/browser/dnd';
import { addDisposableListener, Dimension, EventType } from 'vs/base/browser/dom';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { ActionsOrientation, IActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
import { Action, IAction, IRunEvent } from 'vs/base/common/actions';
import * as arrays from 'vs/base/common/arrays';
import { ResolvedKeybinding } from 'vs/base/common/keyCodes';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base';
18
import 'vs/css!./media/titlecontrol';
19
import { getCodeEditor } from 'vs/editor/browser/editorBrowser';
20
import { localize } from 'vs/nls';
21 22 23 24
import { createActionItem, fillInActionBarActions, fillInContextMenuActions } from 'vs/platform/actions/browser/menuItemActionItem';
import { ExecuteCommandAction, IMenu, IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
25 26 27
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
28 29 30 31 32 33 34 35 36 37 38 39
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { listActiveSelectionBackground, listActiveSelectionForeground } from 'vs/platform/theme/common/colorRegistry';
import { ICssStyleCollector, ITheme, IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { prepareActions } from 'vs/workbench/browser/actions';
import { DraggedEditorGroupIdentifier, DraggedEditorIdentifier, fillResourceDataTransfers, LocalSelectionTransfer } from 'vs/workbench/browser/dnd';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { BreadcrumbsConfig } from 'vs/workbench/browser/parts/editor/breadcrumbs';
import { BreadcrumbsControl, IBreadcrumbsControlOptions } from 'vs/workbench/browser/parts/editor/breadcrumbsControl';
import { EDITOR_TITLE_HEIGHT, IEditorGroupsAccessor, IEditorGroupView, IEditorPartOptions } from 'vs/workbench/browser/parts/editor/editor';
import { EditorCommandsContextActionRunner, IEditorCommandsContext, IEditorInput, toResource } from 'vs/workbench/common/editor';
40
import { ResourceContextKey } from 'vs/workbench/common/resources';
B
Benjamin Pasero 已提交
41
import { Themable } from 'vs/workbench/common/theme';
42
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
B
Benjamin Pasero 已提交
43 44 45 46 47 48

export interface IToolbarActions {
	primary: IAction[];
	secondary: IAction[];
}

49
export abstract class TitleControl extends Themable {
50

B
Benjamin Pasero 已提交
51 52 53
	protected readonly groupTransfer = LocalSelectionTransfer.getInstance<DraggedEditorGroupIdentifier>();
	protected readonly editorTransfer = LocalSelectionTransfer.getInstance<DraggedEditorIdentifier>();

54 55
	protected breadcrumbsControl: BreadcrumbsControl;

56 57 58 59
	private currentPrimaryEditorActionIds: string[] = [];
	private currentSecondaryEditorActionIds: string[] = [];
	protected editorActionsToolbar: ToolBar;

60
	private mapEditorToActions: Map<string, IToolbarActions> = new Map();
B
Benjamin Pasero 已提交
61

62
	private resourceContext: ResourceContextKey;
B
Benjamin Pasero 已提交
63
	private editorToolBarMenuDisposables: IDisposable[] = [];
64

B
Benjamin Pasero 已提交
65 66
	private contextMenu: IMenu;

B
Benjamin Pasero 已提交
67
	constructor(
68
		parent: HTMLElement,
69
		protected accessor: IEditorGroupsAccessor,
B
Benjamin Pasero 已提交
70
		protected group: IEditorGroupView,
71
		@IContextMenuService private contextMenuService: IContextMenuService,
B
Benjamin Pasero 已提交
72
		@IInstantiationService protected instantiationService: IInstantiationService,
73 74 75
		@IContextKeyService private contextKeyService: IContextKeyService,
		@IKeybindingService private keybindingService: IKeybindingService,
		@ITelemetryService private telemetryService: ITelemetryService,
76
		@INotificationService private notificationService: INotificationService,
77
		@IMenuService private menuService: IMenuService,
78
		@IQuickOpenService protected quickOpenService: IQuickOpenService,
79
		@IThemeService themeService: IThemeService,
80 81
		@IExtensionService private extensionService: IExtensionService,
		@IConfigurationService protected configurationService: IConfigurationService
B
Benjamin Pasero 已提交
82
	) {
B
Benjamin Pasero 已提交
83
		super(themeService);
84

85
		this.resourceContext = instantiationService.createInstance(ResourceContextKey);
86
		this.contextMenu = this._register(this.menuService.createMenu(MenuId.EditorTitleContext, this.contextKeyService));
B
Benjamin Pasero 已提交
87

88
		this.create(parent);
89
		this.registerListeners();
90 91
	}

92
	private registerListeners(): void {
93
		this._register(this.extensionService.onDidRegisterExtensions(() => this.updateEditorActionsToolbar()));
B
Benjamin Pasero 已提交
94 95
	}

96
	protected abstract create(parent: HTMLElement): void;
B
Benjamin Pasero 已提交
97

98
	protected createBreadcrumbsControl(container: HTMLElement, options: IBreadcrumbsControlOptions): void {
99
		const config = this._register(BreadcrumbsConfig.IsEnabled.bindTo(this.configurationService));
100
		this._register(config.onDidChange(value => {
101 102 103
			if (!value && this.breadcrumbsControl) {
				this.breadcrumbsControl.dispose();
				this.breadcrumbsControl = undefined;
J
Johannes Rieken 已提交
104
				this.handleBreadcrumbsEnablementChange();
105
			} else if (value && !this.breadcrumbsControl) {
106
				this.breadcrumbsControl = this.instantiationService.createInstance(BreadcrumbsControl, container, options, this.group);
107
				this.breadcrumbsControl.update();
J
Johannes Rieken 已提交
108
				this.handleBreadcrumbsEnablementChange();
109
			}
110
		}));
J
Johannes Rieken 已提交
111
		if (config.getValue()) {
112
			this.breadcrumbsControl = this.instantiationService.createInstance(BreadcrumbsControl, container, options, this.group);
113 114 115
		}
	}

J
Johannes Rieken 已提交
116 117
	protected abstract handleBreadcrumbsEnablementChange(): void;

118
	protected createEditorActionsToolBar(container: HTMLElement): void {
B
Benjamin Pasero 已提交
119 120
		const context = { groupId: this.group.id } as IEditorCommandsContext;

121
		this.editorActionsToolbar = this._register(new ToolBar(container, this.contextMenuService, {
122
			actionItemProvider: action => this.actionItemProvider(action as Action),
B
Benjamin Pasero 已提交
123
			orientation: ActionsOrientation.HORIZONTAL,
124
			ariaLabel: localize('araLabelEditorActions', "Editor actions"),
B
Benjamin Pasero 已提交
125 126
			getKeyBinding: action => this.getKeybinding(action),
			actionRunner: this._register(new EditorCommandsContextActionRunner(context))
127
		}));
B
Benjamin Pasero 已提交
128

129
		// Context
B
Benjamin Pasero 已提交
130
		this.editorActionsToolbar.context = context;
131

B
Benjamin Pasero 已提交
132
		// Action Run Handling
133
		this._register(this.editorActionsToolbar.actionRunner.onDidRun((e: IRunEvent) => {
B
Benjamin Pasero 已提交
134

135 136
			// Notify for Error
			this.notificationService.error(e.error);
B
Benjamin Pasero 已提交
137 138 139

			// Log in telemetry
			if (this.telemetryService) {
K
kieferrm 已提交
140
				/* __GDPR__
K
kieferrm 已提交
141 142 143 144 145
					"workbenchActionExecuted" : {
						"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
						"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
					}
				*/
B
Benjamin Pasero 已提交
146 147 148 149 150
				this.telemetryService.publicLog('workbenchActionExecuted', { id: e.action.id, from: 'editorPart' });
			}
		}));
	}

151
	private actionItemProvider(action: Action): IActionItem {
152
		const activeControl = this.group.activeControl;
B
Benjamin Pasero 已提交
153 154

		// Check Active Editor
155
		let actionItem: IActionItem;
156 157
		if (activeControl instanceof BaseEditor) {
			actionItem = activeControl.getActionItem(action);
B
Benjamin Pasero 已提交
158 159
		}

160 161
		// Check extensions
		if (!actionItem) {
162
			actionItem = createActionItem(action, this.keybindingService, this.notificationService, this.contextMenuService);
163 164
		}

B
Benjamin Pasero 已提交
165 166 167
		return actionItem;
	}

B
Benjamin Pasero 已提交
168 169 170
	protected updateEditorActionsToolbar(): void {

		// Update Editor Actions Toolbar
B
Benjamin Pasero 已提交
171
		const { primaryEditorActions, secondaryEditorActions } = this.prepareEditorActions(this.getEditorActions());
B
Benjamin Pasero 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

		// Only update if something actually has changed
		const primaryEditorActionIds = primaryEditorActions.map(a => a.id);
		const secondaryEditorActionIds = secondaryEditorActions.map(a => a.id);
		if (
			!arrays.equals(primaryEditorActionIds, this.currentPrimaryEditorActionIds) ||
			!arrays.equals(secondaryEditorActionIds, this.currentSecondaryEditorActionIds) ||
			primaryEditorActions.some(action => action instanceof ExecuteCommandAction) || // execute command actions can have the same ID but different arguments
			secondaryEditorActions.some(action => action instanceof ExecuteCommandAction)  // see also https://github.com/Microsoft/vscode/issues/16298
		) {
			this.editorActionsToolbar.setActions(primaryEditorActions, secondaryEditorActions)();

			this.currentPrimaryEditorActionIds = primaryEditorActionIds;
			this.currentSecondaryEditorActionIds = secondaryEditorActionIds;
		}
	}

B
Benjamin Pasero 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	protected prepareEditorActions(editorActions: IToolbarActions): { primaryEditorActions: IAction[]; secondaryEditorActions: IAction[]; } {
		let primaryEditorActions: IAction[];
		let secondaryEditorActions: IAction[];

		// Primary actions only for the active group
		if (this.accessor.activeGroup === this.group) {
			primaryEditorActions = prepareActions(editorActions.primary);
		} else {
			primaryEditorActions = [];
		}

		// Secondary actions for all groups
		secondaryEditorActions = prepareActions(editorActions.secondary);

		return { primaryEditorActions, secondaryEditorActions };
	}

206
	private getEditorActions(): IToolbarActions {
B
Benjamin Pasero 已提交
207 208 209
		const primary: IAction[] = [];
		const secondary: IAction[] = [];

B
Benjamin Pasero 已提交
210 211 212
		// Dispose previous listeners
		this.editorToolBarMenuDisposables = dispose(this.editorToolBarMenuDisposables);

213
		// Update the resource context
214
		this.resourceContext.set(toResource(this.group.activeEditor, { supportSideBySide: true }));
215

216
		// Editor actions require the editor control to be there, so we retrieve it via service
217
		const activeControl = this.group.activeControl;
218
		if (activeControl instanceof BaseEditor) {
219 220

			// Editor Control Actions
221
			let editorActions = this.mapEditorToActions.get(activeControl.getId());
B
Benjamin Pasero 已提交
222
			if (!editorActions) {
223
				editorActions = { primary: activeControl.getActions(), secondary: activeControl.getSecondaryActions() };
224
				this.mapEditorToActions.set(activeControl.getId(), editorActions);
B
Benjamin Pasero 已提交
225 226 227 228
			}
			primary.push(...editorActions.primary);
			secondary.push(...editorActions.secondary);

229
			// Contributed Actions
B
Benjamin Pasero 已提交
230
			const codeEditor = getCodeEditor(activeControl.getControl());
231 232
			const scopedContextKeyService = codeEditor && codeEditor.invokeWithinContext(accessor => accessor.get(IContextKeyService)) || this.contextKeyService;
			const titleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, scopedContextKeyService);
B
Benjamin Pasero 已提交
233 234
			this.editorToolBarMenuDisposables.push(titleBarMenu);
			this.editorToolBarMenuDisposables.push(titleBarMenu.onDidChange(() => {
B
Benjamin Pasero 已提交
235
				this.updateEditorActionsToolbar(); // Update editor toolbar whenever contributed actions change
B
Benjamin Pasero 已提交
236
			}));
237

238
			fillInActionBarActions(titleBarMenu, { arg: this.resourceContext.get(), shouldForwardArgs: true }, { primary, secondary });
B
Benjamin Pasero 已提交
239 240 241 242 243
		}

		return { primary, secondary };
	}

244 245 246 247 248
	protected clearEditorActionsToolbar(): void {
		this.editorActionsToolbar.setActions([], [])();

		this.currentPrimaryEditorActionIds = [];
		this.currentSecondaryEditorActionIds = [];
B
Benjamin Pasero 已提交
249 250
	}

B
Benjamin Pasero 已提交
251
	protected enableGroupDragging(element: HTMLElement): void {
B
Benjamin Pasero 已提交
252 253 254 255 256 257 258 259 260 261 262

		// Drag start
		this._register(addDisposableListener(element, EventType.DRAG_START, (e: DragEvent) => {
			if (e.target !== element) {
				return; // only if originating from tabs container
			}

			// Set editor group as transfer
			this.groupTransfer.setData([new DraggedEditorGroupIdentifier(this.group.id)], DraggedEditorGroupIdentifier.prototype);
			e.dataTransfer.effectAllowed = 'copyMove';

B
Benjamin Pasero 已提交
263 264
			// If tabs are disabled, treat dragging as if an editor tab was dragged
			if (!this.accessor.partOptions.showTabs) {
265 266 267 268
				const resource = toResource(this.group.activeEditor, { supportSideBySide: true });
				if (resource) {
					this.instantiationService.invokeFunction(fillResourceDataTransfers, [resource], e);
				}
B
Benjamin Pasero 已提交
269 270 271
			}

			// Drag Image
B
Benjamin Pasero 已提交
272 273 274 275 276 277
			let label = this.group.activeEditor.getName();
			if (this.accessor.partOptions.showTabs && this.group.count > 1) {
				label = localize('draggedEditorGroup', "{0} (+{1})", label, this.group.count - 1);
			}

			applyDragImage(e, label, 'monaco-editor-group-drag-image');
B
Benjamin Pasero 已提交
278 279 280 281 282 283 284 285
		}));

		// Drag end
		this._register(addDisposableListener(element, EventType.DRAG_END, () => {
			this.groupTransfer.clearData(DraggedEditorGroupIdentifier.prototype);
		}));
	}

286
	protected onContextMenu(editor: IEditorInput, e: Event, node: HTMLElement): void {
287 288 289

		// Update the resource context
		const currentContext = this.resourceContext.get();
290
		this.resourceContext.set(toResource(editor, { supportSideBySide: true }));
291 292

		// Find target anchor
293 294 295 296 297 298
		let anchor: HTMLElement | { x: number, y: number } = node;
		if (e instanceof MouseEvent) {
			const event = new StandardMouseEvent(e);
			anchor = { x: event.posx, y: event.posy };
		}

299 300
		// Fill in contributed actions
		const actions: IAction[] = [];
301
		fillInContextMenuActions(this.contextMenu, { shouldForwardArgs: true, arg: this.resourceContext.get() }, actions, this.contextMenuService);
302 303

		// Show it
304 305
		this.contextMenuService.showContextMenu({
			getAnchor: () => anchor,
306
			getActions: () => TPromise.as(actions),
307
			getActionsContext: () => ({ groupId: this.group.id, editorIndex: this.group.getIndexOfEditor(editor) } as IEditorCommandsContext),
B
Benjamin Pasero 已提交
308
			getKeyBinding: (action) => this.getKeybinding(action),
B
Benjamin Pasero 已提交
309
			onHide: () => {
B
Benjamin Pasero 已提交
310 311 312 313

				// restore previous context
				this.resourceContext.set(currentContext);

314
				// restore focus to active group
315
				this.accessor.activeGroup.focus();
B
Benjamin Pasero 已提交
316
			}
317 318 319
		});
	}

320
	protected getKeybinding(action: IAction): ResolvedKeybinding {
321
		return this.keybindingService.lookupKeybinding(action.id);
B
Benjamin Pasero 已提交
322 323 324 325 326
	}

	protected getKeybindingLabel(action: IAction): string {
		const keybinding = this.getKeybinding(action);

327
		return keybinding ? keybinding.getLabel() : void 0;
B
Benjamin Pasero 已提交
328 329
	}

330
	//#region ITitleAreaControl
331

332
	abstract openEditor(editor: IEditorInput): void;
333

334 335 336 337 338
	abstract closeEditor(editor: IEditorInput): void;

	abstract closeEditors(editors: IEditorInput[]): void;

	abstract closeAllEditors(): void;
339

340
	abstract moveEditor(editor: IEditorInput, fromIndex: number, targetIndex: number): void;
341

342
	abstract pinEditor(editor: IEditorInput): void;
343

344
	abstract setActive(isActive: boolean): void;
345

346 347 348
	abstract updateEditorLabel(editor: IEditorInput): void;

	abstract updateEditorDirty(editor: IEditorInput): void;
349

350
	abstract updateOptions(oldOptions: IEditorPartOptions, newOptions: IEditorPartOptions): void;
351 352 353

	abstract updateStyles(): void;

354 355
	layout(dimension: Dimension): void {
		// Optionally implemented in subclasses
356 357 358 359

		if (this.breadcrumbsControl) {
			this.breadcrumbsControl.layout(undefined);
		}
360 361
	}

362
	getPreferredHeight(): number {
363
		return EDITOR_TITLE_HEIGHT + (this.breadcrumbsControl && !this.breadcrumbsControl.isHidden() ? BreadcrumbsControl.HEIGHT : 0);
364 365
	}

366
	dispose(): void {
367
		this.breadcrumbsControl = dispose(this.breadcrumbsControl);
B
Benjamin Pasero 已提交
368
		this.editorToolBarMenuDisposables = dispose(this.editorToolBarMenuDisposables);
369 370 371 372

		super.dispose();
	}

373
	//#endregion
J
Johannes Rieken 已提交
374
}
375 376 377 378

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

	// Drag Feedback
B
Benjamin Pasero 已提交
379 380 381 382 383 384 385 386
	const dragImageBackground = theme.getColor(listActiveSelectionBackground);
	const dragImageForeground = theme.getColor(listActiveSelectionForeground);
	collector.addRule(`
		.monaco-editor-group-drag-image {
			background: ${dragImageBackground};
			color: ${dragImageForeground};
		}
	`);
387
});