titleControl.ts 15.6 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';

B
Benjamin Pasero 已提交
8
import 'vs/css!./media/titlecontrol';
B
Benjamin Pasero 已提交
9 10 11 12 13
import nls = require('vs/nls');
import {Registry} from 'vs/platform/platform';
import {Scope, IActionBarRegistry, Extensions} from 'vs/workbench/browser/actionBarRegistry';
import {IAction, Action} from 'vs/base/common/actions';
import errors = require('vs/base/common/errors');
B
Benjamin Pasero 已提交
14
import DOM = require('vs/base/browser/dom');
15
import {TPromise} from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
16 17
import {BaseEditor, IEditorInputActionContext} from 'vs/workbench/browser/parts/editor/baseEditor';
import {RunOnceScheduler} from 'vs/base/common/async';
18
import {IEditorStacksModel, IEditorGroup, IEditorIdentifier, EditorInput, IWorkbenchEditorConfiguration} from 'vs/workbench/common/editor';
B
Benjamin Pasero 已提交
19 20 21 22 23 24
import {EventType as BaseEventType} from 'vs/base/common/events';
import {IActionItem, ActionsOrientation, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {ToolBar} from 'vs/base/browser/ui/toolbar/toolbar';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {Position} from 'vs/platform/editor/common/editor';
25
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
26 27 28
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {QuickOpenAction} from 'vs/workbench/browser/quickopen';
29
import {StandardMouseEvent} from 'vs/base/browser/mouseEvent';
B
Benjamin Pasero 已提交
30 31 32
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
B
Benjamin Pasero 已提交
33
import {ShowEditorsInLeftGroupAction, ShowAllEditorsAction, ShowEditorsInCenterGroupAction, ShowEditorsInRightGroupAction, CloseEditorsInGroupAction, MoveGroupLeftAction,
B
Benjamin Pasero 已提交
34
	MoveGroupRightAction, SplitEditorAction, CloseEditorAction, KeepEditorAction, CloseOtherEditorsInGroupAction, CloseRightEditorsInGroupAction}
B
Benjamin Pasero 已提交
35
from 'vs/workbench/browser/parts/editor/editorActions';
B
Benjamin Pasero 已提交
36 37 38 39 40 41 42 43 44
import {IDisposable, dispose} from 'vs/base/common/lifecycle';

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

export interface ITitleAreaControl {
	setContext(group: IEditorGroup): void;
B
Benjamin Pasero 已提交
45
	allowDragging(element: HTMLElement): boolean;
46
	create(parent: HTMLElement): void;
47 48
	refresh(instant?: boolean): void;
	update(instant?: boolean): void;
49
	layout(): void;
B
Benjamin Pasero 已提交
50 51 52 53
	dispose(): void;
}

export abstract class TitleControl {
54 55 56

	private static draggedEditor: IEditorIdentifier;

B
Benjamin Pasero 已提交
57 58
	protected stacks: IEditorStacksModel;
	protected context: IEditorGroup;
59
	protected toDispose: IDisposable[];
B
Benjamin Pasero 已提交
60 61

	protected closeEditorAction: CloseEditorAction;
B
Benjamin Pasero 已提交
62
	protected pinEditorAction: KeepEditorAction;
B
Benjamin Pasero 已提交
63
	protected closeOtherEditorsAction: CloseOtherEditorsInGroupAction;
B
Benjamin Pasero 已提交
64
	protected closeRightEditorsAction: CloseRightEditorsInGroupAction;
B
Benjamin Pasero 已提交
65 66 67 68 69 70 71 72 73
	protected showEditorsOfLeftGroup: QuickOpenAction;
	protected showEditorsOfCenterGroup: QuickOpenAction;
	protected showEditorsOfRightGroup: QuickOpenAction;
	protected moveGroupLeftAction: MoveGroupLeftAction;
	protected moveGroupRightAction: MoveGroupRightAction;
	protected closeEditorsInGroupAction: CloseEditorsInGroupAction;
	protected splitEditorAction: SplitEditorAction;
	protected showAllEditorsAction: ShowAllEditorsAction;

74 75 76
	private previewEditors: boolean;
	private showTabs: boolean;

B
Benjamin Pasero 已提交
77 78
	private mapActionsToEditors: { [editorId: string]: IToolbarActions; };
	private scheduler: RunOnceScheduler;
79
	private refreshScheduled: boolean;
B
Benjamin Pasero 已提交
80 81 82 83

	constructor(
		@IContextMenuService protected contextMenuService: IContextMenuService,
		@IInstantiationService protected instantiationService: IInstantiationService,
84
		@IConfigurationService protected configurationService: IConfigurationService,
B
Benjamin Pasero 已提交
85 86 87 88 89 90 91 92 93 94
		@IWorkbenchEditorService protected editorService: IWorkbenchEditorService,
		@IEditorGroupService protected editorGroupService: IEditorGroupService,
		@IKeybindingService protected keybindingService: IKeybindingService,
		@ITelemetryService protected telemetryService: ITelemetryService,
		@IMessageService protected messageService: IMessageService
	) {
		this.toDispose = [];
		this.stacks = editorGroupService.getStacksModel();
		this.mapActionsToEditors = Object.create(null);

95 96
		this.onConfigurationUpdated(configurationService.getConfiguration<IWorkbenchEditorConfiguration>());

97
		this.scheduler = new RunOnceScheduler(() => this.onSchedule(), 0);
B
Benjamin Pasero 已提交
98 99 100
		this.toDispose.push(this.scheduler);

		this.initActions();
101 102 103
		this.registerListeners();
	}

104 105 106 107 108 109 110 111 112 113 114 115
	public static getDraggedEditor(): IEditorIdentifier {
		return TitleControl.draggedEditor;
	}

	protected onEditorDragStart(editor: IEditorIdentifier): void {
		TitleControl.draggedEditor = editor;
	}

	protected onEditorDragEnd(): void {
		TitleControl.draggedEditor = void 0;
	}

116 117 118 119 120
	private registerListeners(): void {
		this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config)));
	}

	private onConfigurationUpdated(config: IWorkbenchEditorConfiguration): void {
121 122
		this.previewEditors = config.workbench.editor.enablePreview;
		this.showTabs = config.workbench.editor.showTabs;
B
Benjamin Pasero 已提交
123 124
	}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	private updateActionEnablement(): void {
		if (!this.context) {
			return;
		}

		const group = this.context;
		const groupCount = this.stacks.groups.length;

		// Move group
		switch (this.stacks.positionOfGroup(group)) {
			case Position.LEFT:
				this.moveGroupLeftAction.enabled = false;
				this.moveGroupRightAction.enabled = this.stacks.groups.length > 1;
				break;

			case Position.CENTER:
				this.moveGroupRightAction.enabled = this.stacks.groups.length > 2;
				break;

			case Position.RIGHT:
				this.moveGroupRightAction.enabled = false;
				break;
		}

		// Split editor
		this.splitEditorAction.enabled = groupCount < 3;
	}

153
	private onSchedule(): void {
154 155
		this.updateActionEnablement();

156 157 158 159 160 161 162 163 164
		if (this.refreshScheduled) {
			this.doRefresh();
		} else {
			this.doUpdate();
		}

		this.refreshScheduled = false;
	}

B
Benjamin Pasero 已提交
165 166
	public setContext(group: IEditorGroup): void {
		this.context = group;
167 168 169 170 171 172 173 174 175 176
	}

	public update(instant?: boolean): void {
		if (instant) {
			this.scheduler.cancel();
			this.onSchedule();
		} else {
			this.scheduler.schedule();
		}
	}
B
Benjamin Pasero 已提交
177

178 179 180 181 182 183 184 185 186
	public refresh(instant?: boolean) {
		this.refreshScheduled = true;

		if (instant) {
			this.scheduler.cancel();
			this.onSchedule();
		} else {
			this.scheduler.schedule();
		}
B
Benjamin Pasero 已提交
187 188
	}

189 190 191 192 193
	protected abstract doRefresh(): void;

	protected doUpdate(): void {
		this.doRefresh();
	}
B
Benjamin Pasero 已提交
194

195 196 197 198
	public layout(): void {
		// Subclasses can opt in to react on layout
	}

B
Benjamin Pasero 已提交
199 200 201 202
	public allowDragging(element: HTMLElement): boolean {
		return !DOM.findParentWithClass(element, 'monaco-action-bar', 'one-editor-container');
	}

B
Benjamin Pasero 已提交
203 204
	private initActions(): void {
		this.closeEditorAction = this.instantiationService.createInstance(CloseEditorAction, CloseEditorAction.ID, nls.localize('close', "Close"));
B
Benjamin Pasero 已提交
205
		this.closeOtherEditorsAction = this.instantiationService.createInstance(CloseOtherEditorsInGroupAction, CloseOtherEditorsInGroupAction.ID, nls.localize('closeOthers', "Close Others"));
B
Benjamin Pasero 已提交
206 207
		this.closeRightEditorsAction = this.instantiationService.createInstance(CloseRightEditorsInGroupAction, CloseRightEditorsInGroupAction.ID, nls.localize('closeRight', "Close to the Right"));
		this.closeEditorsInGroupAction = this.instantiationService.createInstance(CloseEditorsInGroupAction, CloseEditorsInGroupAction.ID, nls.localize('closeAll', "Close All"));
B
Benjamin Pasero 已提交
208
		this.pinEditorAction = this.instantiationService.createInstance(KeepEditorAction, KeepEditorAction.ID, nls.localize('keepEditor', "Keep Editor"));
B
Benjamin Pasero 已提交
209 210 211 212 213 214 215 216 217 218 219
		this.showAllEditorsAction = this.instantiationService.createInstance(ShowAllEditorsAction, ShowAllEditorsAction.ID, nls.localize('showEditors', "Show Editors"));
		this.splitEditorAction = this.instantiationService.createInstance(SplitEditorAction, SplitEditorAction.ID, SplitEditorAction.LABEL);
		this.moveGroupLeftAction = this.instantiationService.createInstance(MoveGroupLeftAction, MoveGroupLeftAction.ID, nls.localize('moveLeft', "Move Left"));
		this.moveGroupRightAction = this.instantiationService.createInstance(MoveGroupRightAction, MoveGroupRightAction.ID, nls.localize('moveRight', "Move Right"));
		this.showEditorsOfLeftGroup = this.instantiationService.createInstance(ShowEditorsInLeftGroupAction, ShowEditorsInLeftGroupAction.ID, nls.localize('showEditors', "Show Editors"));
		this.showEditorsOfCenterGroup = this.instantiationService.createInstance(ShowEditorsInCenterGroupAction, ShowEditorsInCenterGroupAction.ID, nls.localize('showEditors', "Show Editors"));
		this.showEditorsOfRightGroup = this.instantiationService.createInstance(ShowEditorsInRightGroupAction, ShowEditorsInRightGroupAction.ID, nls.localize('showEditors', "Show Editors"));

		[this.showEditorsOfLeftGroup, this.showEditorsOfCenterGroup, this.showEditorsOfRightGroup, this.showAllEditorsAction].forEach(a => a.class = 'show-group-editors-action');
	}

220 221
	protected doCreateToolbar(container: HTMLElement): ToolBar {
		const toolbar = new ToolBar(container, this.contextMenuService, {
B
Benjamin Pasero 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
			actionItemProvider: (action: Action) => this.actionItemProvider(action),
			orientation: ActionsOrientation.HORIZONTAL,
			ariaLabel: nls.localize('araLabelEditorActions', "Editor actions"),
			getKeyBinding: (action) => {
				const opts = this.keybindingService.lookupKeybindings(action.id);
				if (opts.length > 0) {
					return opts[0]; // only take the first one
				}

				return null;
			}
		});

		// Action Run Handling
		this.toDispose.push(toolbar.actionRunner.addListener2(BaseEventType.RUN, (e: any) => {

			// Check for Error
			if (e.error && !errors.isPromiseCanceledError(e.error)) {
				this.messageService.show(Severity.Error, e.error);
			}

			// Log in telemetry
			if (this.telemetryService) {
				this.telemetryService.publicLog('workbenchActionExecuted', { id: e.action.id, from: 'editorPart' });
			}
		}));

		return toolbar;
	}

	protected actionItemProvider(action: Action): IActionItem {
		if (!this.context) {
			return null;
		}

		const group = this.context;
		const position = this.stacks.positionOfGroup(group);
		const editor = this.editorService.getVisibleEditors()[position];

		let actionItem: IActionItem;

		// Check Active Editor
		if (editor instanceof BaseEditor) {
			actionItem = editor.getActionItem(action);
		}

		// Check Registry
		if (!actionItem) {
			let actionBarRegistry = <IActionBarRegistry>Registry.as(Extensions.Actionbar);
			actionItem = actionBarRegistry.getActionItemForContext(Scope.EDITOR, { input: editor && editor.input, editor, position }, action);
		}

		return actionItem;
	}

	protected getEditorActions(group: IEditorGroup): IToolbarActions {
		const position = this.stacks.positionOfGroup(group);
		const isActive = this.stacks.isActive(group);
		const primary: IAction[] = [];
		const secondary: IAction[] = [];
		const editor = this.editorService.getVisibleEditors()[position];

		if (isActive && editor instanceof BaseEditor) {
			let editorActions = this.mapActionsToEditors[editor.getId()];
			if (!editorActions) {
				editorActions = this.getEditorActionsForContext(editor);
				this.mapActionsToEditors[editor.getId()] = editorActions;
			}

			primary.push(...editorActions.primary);
			secondary.push(...editorActions.secondary);

			// Handle Editor Input Actions
			let editorInputActions = this.getEditorActionsForContext({ input: editor.input, editor, position: editor.position });

			primary.push(...editorInputActions.primary);
			secondary.push(...editorInputActions.secondary);
		}

		return { primary, secondary };
	}

304 305 306
	private getEditorActionsForContext(context: BaseEditor): IToolbarActions;
	private getEditorActionsForContext(context: IEditorInputActionContext): IToolbarActions;
	private getEditorActionsForContext(context: any): IToolbarActions {
B
Benjamin Pasero 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
		let primaryActions: IAction[] = [];
		let secondaryActions: IAction[] = [];

		// From Editor
		if (context instanceof BaseEditor) {
			primaryActions.push(...(<BaseEditor>context).getActions());
			secondaryActions.push(...(<BaseEditor>context).getSecondaryActions());
		}

		// From Contributions
		let actionBarRegistry = <IActionBarRegistry>Registry.as(Extensions.Actionbar);
		primaryActions.push(...actionBarRegistry.getActionBarActionsForContext(Scope.EDITOR, context));
		secondaryActions.push(...actionBarRegistry.getSecondaryActionBarActionsForContext(Scope.EDITOR, context));

		return {
			primary: primaryActions,
			secondary: secondaryActions
		};
	}

	protected getGroupActions(group: IEditorGroup): IToolbarActions {
328
		const editor = group.activeEditor;
B
Benjamin Pasero 已提交
329 330 331 332 333
		const primary: IAction[] = [];

		const groupCount = this.stacks.groups.length;

		// Overflow
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		let overflowAction: Action;
		if (groupCount === 1) {
			overflowAction = this.showAllEditorsAction;
		} else {
			switch (this.stacks.positionOfGroup(group)) {
				case Position.LEFT:
					overflowAction = this.showEditorsOfLeftGroup;
					break;

				case Position.CENTER:
					overflowAction = (groupCount === 2) ? this.showEditorsOfRightGroup : this.showEditorsOfCenterGroup;
					break;

				case Position.RIGHT:
					overflowAction = this.showEditorsOfRightGroup;
					break;
B
Benjamin Pasero 已提交
350 351 352
			}
		}

353 354
		primary.push(overflowAction);

B
Benjamin Pasero 已提交
355
		// Splitting
356
		if (editor instanceof EditorInput && editor.supportsSplitEditor()) {
B
Benjamin Pasero 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370
			primary.push(this.splitEditorAction);
		}

		// Return actions
		const secondary = [
			this.moveGroupLeftAction,
			this.moveGroupRightAction,
			new Separator(),
			this.closeEditorsInGroupAction
		];

		return { primary, secondary };
	}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	protected onContextMenu(identifier: IEditorIdentifier, e: Event, node: HTMLElement): void {
		let anchor: HTMLElement | { x: number, y: number } = node;
		if (e instanceof MouseEvent) {
			const event = new StandardMouseEvent(e);
			anchor = { x: event.posx, y: event.posy };
		}

		this.contextMenuService.showContextMenu({
			getAnchor: () => anchor,
			getActions: () => TPromise.as(this.getContextMenuActions(identifier)),
			getActionsContext: () => identifier,
			getKeyBinding: (action) => {
				var opts = this.keybindingService.lookupKeybindings(action.id);
				if (opts.length > 0) {
					return opts[0]; // only take the first one
				}

				return null;
			}
		});
	}

	protected getContextMenuActions(identifier: IEditorIdentifier): IAction[] {
		const {editor, group} = identifier;

		// Enablement
		this.closeOtherEditorsAction.enabled = group.count > 1;
		this.pinEditorAction.enabled = !group.isPinned(editor);
		this.closeRightEditorsAction.enabled = group.indexOf(editor) !== group.count - 1;

		// Actions: For all editors
		const actions: IAction[] = [
			this.closeEditorAction,
			this.closeOtherEditorsAction
		];

		if (this.showTabs) {
			actions.push(this.closeRightEditorsAction);
		}

		actions.push(this.closeEditorsInGroupAction);

		if (this.previewEditors) {
			actions.push(new Separator(), this.pinEditorAction);
		}

		return actions;
	}

B
Benjamin Pasero 已提交
420 421 422 423 424 425 426 427 428 429 430
	public dispose(): void {
		dispose(this.toDispose);

		// Actions
		[
			this.splitEditorAction,
			this.showAllEditorsAction,
			this.showEditorsOfLeftGroup,
			this.showEditorsOfCenterGroup,
			this.showEditorsOfRightGroup,
			this.closeEditorAction,
B
Benjamin Pasero 已提交
431
			this.closeRightEditorsAction,
B
Benjamin Pasero 已提交
432
			this.closeOtherEditorsAction,
B
Benjamin Pasero 已提交
433
			this.closeEditorsInGroupAction,
B
Benjamin Pasero 已提交
434 435
			this.moveGroupLeftAction,
			this.moveGroupRightAction,
B
Benjamin Pasero 已提交
436
			this.pinEditorAction
B
Benjamin Pasero 已提交
437 438 439 440 441
		].forEach((action) => {
			action.dispose();
		});
	}
}