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

'use strict';

import 'vs/css!./media/tabstitle';
B
Benjamin Pasero 已提交
9
import {TPromise} from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
10
import nls = require('vs/nls');
B
Benjamin Pasero 已提交
11
import {IAction} from 'vs/base/common/actions';
12
import {prepareActions} from 'vs/workbench/browser/actionBarRegistry';
B
wip  
Benjamin Pasero 已提交
13
import arrays = require('vs/base/common/arrays');
B
Benjamin Pasero 已提交
14 15
import errors = require('vs/base/common/errors');
import DOM = require('vs/base/browser/dom');
B
wip  
Benjamin Pasero 已提交
16
import {Builder, $} from 'vs/base/browser/builder';
B
Benjamin Pasero 已提交
17 18
import {MIME_BINARY} from 'vs/base/common/mime';
import {IEditorGroup, IEditorIdentifier, asFileEditorInput} from 'vs/workbench/common/editor';
B
wip  
Benjamin Pasero 已提交
19
import {ToolBar} from 'vs/base/browser/ui/toolbar/toolbar';
B
Benjamin Pasero 已提交
20 21
import {ActionBar, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {StandardMouseEvent} from 'vs/base/browser/mouseEvent';
B
wip  
Benjamin Pasero 已提交
22 23 24 25 26 27 28 29
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
import {IMessageService} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {TitleControl} from 'vs/workbench/browser/parts/editor/titleControl';
B
Benjamin Pasero 已提交
30
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
B
wip  
Benjamin Pasero 已提交
31 32

export class TabsTitleControl extends TitleControl {
B
Benjamin Pasero 已提交
33 34 35

	private static draggedEditor: IEditorIdentifier;

B
Benjamin Pasero 已提交
36 37 38
	private titleContainer: HTMLElement;
	private tabsContainer: HTMLElement;
	private activeTab: HTMLElement;
B
wip  
Benjamin Pasero 已提交
39 40

	private groupActionsToolbar: ToolBar;
B
Benjamin Pasero 已提交
41
	private tabDisposeables: IDisposable[];
B
wip  
Benjamin Pasero 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

	private currentPrimaryGroupActionIds: string[];
	private currentSecondaryGroupActionIds: string[];

	constructor(
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IKeybindingService keybindingService: IKeybindingService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IMessageService messageService: IMessageService
	) {
		super(contextMenuService, instantiationService, editorService, editorGroupService, keybindingService, telemetryService, messageService);

		this.currentPrimaryGroupActionIds = [];
		this.currentSecondaryGroupActionIds = [];
B
Benjamin Pasero 已提交
59

B
Benjamin Pasero 已提交
60
		this.tabDisposeables = [];
B
wip  
Benjamin Pasero 已提交
61 62 63 64 65 66 67 68 69
	}

	public setContext(group: IEditorGroup): void {
		super.setContext(group);

		this.groupActionsToolbar.context = { group };
	}

	public create(parent: Builder): void {
B
Benjamin Pasero 已提交
70
		this.titleContainer = parent.getHTMLElement();
B
wip  
Benjamin Pasero 已提交
71

72
		// Tabs Container
B
Benjamin Pasero 已提交
73 74 75 76
		this.tabsContainer = document.createElement('div');
		DOM.addClass(this.tabsContainer, 'tabs-container');
		this.titleContainer.appendChild(this.tabsContainer);

B
Benjamin Pasero 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		// Drag over
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_OVER, (e: DragEvent) => {
			const target = e.target;
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
				DOM.addClass(this.tabsContainer, 'dropfeedback');
			}
		}));

		// Drag leave
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_LEAVE, (e: DragEvent) => {
			DOM.removeClass(this.tabsContainer, 'dropfeedback');
		}));

		// Drag end
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_END, (e: DragEvent) => {
			DOM.removeClass(this.tabsContainer, 'dropfeedback');
		}));

B
Benjamin Pasero 已提交
95 96
		// Drop onto tabs container
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DROP, (e: DragEvent) => {
B
Benjamin Pasero 已提交
97 98
			DOM.removeClass(this.tabsContainer, 'dropfeedback');

B
Benjamin Pasero 已提交
99
			const target = e.target;
B
Benjamin Pasero 已提交
100
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
B
Benjamin Pasero 已提交
101 102
				const group = this.context;
				if (group) {
B
Benjamin Pasero 已提交
103
					if (TabsTitleControl.draggedEditor) {
B
Benjamin Pasero 已提交
104 105
						e.preventDefault();

B
Benjamin Pasero 已提交
106
						const sourcePosition = this.stacks.positionOfGroup(TabsTitleControl.draggedEditor.group);
B
Benjamin Pasero 已提交
107 108 109
						const targetPosition = this.stacks.positionOfGroup(group);

						// Move editor to target position at the end
B
Benjamin Pasero 已提交
110
						this.editorGroupService.moveEditor(TabsTitleControl.draggedEditor.editor, sourcePosition, targetPosition, group.count);
B
Benjamin Pasero 已提交
111 112 113 114 115
					}
				}
			}
		}));

B
Benjamin Pasero 已提交
116 117 118 119 120 121 122
		// Convert mouse wheel vertical scroll to horizontal
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.WHEEL, (e: WheelEvent) => {
			if (e.deltaY && !e.deltaX) {
				DOM.EventHelper.stop(e);
				this.tabsContainer.scrollLeft += e.deltaY;
			}
		}));
123

B
Benjamin Pasero 已提交
124
		// Group Actions
B
Benjamin Pasero 已提交
125 126 127 128
		const groupActionsContainer = document.createElement('div');
		DOM.addClass(groupActionsContainer, 'group-actions');
		this.titleContainer.appendChild(groupActionsContainer);
		this.groupActionsToolbar = this.doCreateToolbar($(groupActionsContainer));
B
wip  
Benjamin Pasero 已提交
129 130
	}

B
Benjamin Pasero 已提交
131 132 133 134
	public allowDragging(element: HTMLElement): boolean {
		return (element.className === 'tabs-container');
	}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	protected doUpdate(): void {
		if (!this.context) {
			return;
		}

		const group = this.context;

		// Tabs container activity state
		const isActive = this.stacks.isActive(group);
		if (isActive) {
			DOM.addClass(this.titleContainer, 'active');
		} else {
			DOM.removeClass(this.titleContainer, 'active');
		}

		// Tab styles
		this.context.getEditors().forEach((editor, index) => {
			const tabContainer = this.tabsContainer.children[index];
			if (tabContainer instanceof HTMLElement) {
				const isPinned = group.isPinned(editor);
				const isActive = group.isActive(editor);
				const isDirty = editor.isDirty();

				// Pinned state
				if (isPinned) {
					DOM.addClass(tabContainer, 'pinned');
				} else {
					DOM.removeClass(tabContainer, 'pinned');
				}

				// Active state
				if (isActive) {
					DOM.addClass(tabContainer, 'active');
					this.activeTab = tabContainer;
				} else {
					DOM.removeClass(tabContainer, 'active');
				}

				// Dirty State
				if (isDirty) {
					DOM.addClass(tabContainer, 'dirty');
				} else {
					DOM.removeClass(tabContainer, 'dirty');
				}
			}
		});
	}

	protected doRefresh(): void {
B
wip  
Benjamin Pasero 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		if (!this.context) {
			return;
		}

		const group = this.context;
		const editor = group.activeEditor;
		if (!editor) {
			this.groupActionsToolbar.setActions([], [])();

			this.currentPrimaryGroupActionIds = [];
			this.currentSecondaryGroupActionIds = [];

			return; // return early if we are being closed
		}

199
		// Refresh Group Actions Toolbar
B
wip  
Benjamin Pasero 已提交
200
		const groupActions = this.getGroupActions(group);
B
Benjamin Pasero 已提交
201 202
		const primaryGroupActions = groupActions.primary;
		const secondaryGroupActions = groupActions.secondary;
B
wip  
Benjamin Pasero 已提交
203 204 205 206 207 208 209 210
		const primaryGroupActionIds = primaryGroupActions.map(a => a.id);
		const secondaryGroupActionIds = secondaryGroupActions.map(a => a.id);

		if (!arrays.equals(primaryGroupActionIds, this.currentPrimaryGroupActionIds) || !arrays.equals(secondaryGroupActionIds, this.currentSecondaryGroupActionIds)) {
			this.groupActionsToolbar.setActions(primaryGroupActions, secondaryGroupActions)();
			this.currentPrimaryGroupActionIds = primaryGroupActionIds;
			this.currentSecondaryGroupActionIds = secondaryGroupActionIds;
		}
B
Benjamin Pasero 已提交
211 212 213

		// Refresh Tabs
		this.refreshTabs(group);
214 215 216

		// Update styles
		this.doUpdate();
B
wip  
Benjamin Pasero 已提交
217 218
	}

B
Benjamin Pasero 已提交
219 220 221
	private refreshTabs(group: IEditorGroup): void {

		// Empty container first
B
Benjamin Pasero 已提交
222 223 224 225 226
		DOM.clearNode(this.tabsContainer);
		dispose(this.tabDisposeables);
		this.tabDisposeables = [];

		const tabContainers: HTMLElement[] = [];
B
Benjamin Pasero 已提交
227 228 229

		// Add a tab for each opened editor
		this.context.getEditors().forEach(editor => {
B
Benjamin Pasero 已提交
230
			const tabContainer = document.createElement('div');
B
Benjamin Pasero 已提交
231
			tabContainer.draggable = true;
B
Benjamin Pasero 已提交
232 233
			DOM.addClass(tabContainer, 'tab monaco-editor-background');
			tabContainers.push(tabContainer);
B
Benjamin Pasero 已提交
234

B
Benjamin Pasero 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
			// Tab Label Container
			const tabLabelContainer = document.createElement('div');
			tabContainer.appendChild(tabLabelContainer);
			DOM.addClass(tabLabelContainer, 'tab-label');

			// Tab Label
			const tabLabel = document.createElement('a');
			tabLabel.innerText = editor.getName();
			tabLabel.title = editor.getDescription(true) || '';
			tabLabelContainer.appendChild(tabLabel);

			// Tab Close
			const tabCloseContainer = document.createElement('div');
			DOM.addClass(tabCloseContainer, 'tab-close');
			tabContainer.appendChild(tabCloseContainer);

			const bar = new ActionBar(tabCloseContainer, { context: { editor, group }, ariaLabel: nls.localize('araLabelTabActions', "Tab actions") });
			bar.push(this.closeEditorAction, { icon: true, label: false });

			this.tabDisposeables.push(bar);
B
Benjamin Pasero 已提交
255 256 257

			// Eventing
			this.hookTabListeners(tabContainer, { editor, group });
B
Benjamin Pasero 已提交
258
		});
B
Benjamin Pasero 已提交
259

B
Benjamin Pasero 已提交
260 261 262
		// Add to tabs container
		tabContainers.forEach(tab => this.tabsContainer.appendChild(tab));

263
		// Ensure active tab is always revealed
264 265 266 267 268 269 270 271
		this.layout();
	}

	public layout(): void {
		if (!this.activeTab) {
			return;
		}

B
Benjamin Pasero 已提交
272
		// Always reveal the active one
B
Benjamin Pasero 已提交
273 274 275 276
		const containerWidth = this.tabsContainer.offsetWidth;
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
B
Benjamin Pasero 已提交
277 278 279

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
		if (containerScrollPosX + containerWidth < activeTabPosX + activeTabWidth) {
B
Benjamin Pasero 已提交
280
			this.tabsContainer.scrollLeft += ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + containerWidth) /* right corner of view port */);
B
Benjamin Pasero 已提交
281 282 283 284
		}

		// Tab is overlflowng to the left: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX) {
B
Benjamin Pasero 已提交
285
			this.tabsContainer.scrollLeft = this.activeTab.offsetLeft;
B
Benjamin Pasero 已提交
286
		}
287 288 289 290 291 292

		// Update enablement of certain actions that depend on overflow
		const isOverflowing = (this.tabsContainer.scrollWidth > containerWidth);
		this.showEditorsOfLeftGroup.enabled = isOverflowing;
		this.showEditorsOfCenterGroup.enabled = isOverflowing;
		this.showEditorsOfRightGroup.enabled = isOverflowing;
B
Benjamin Pasero 已提交
293 294
	}

B
Benjamin Pasero 已提交
295
	private hookTabListeners(tab: HTMLElement, identifier: IEditorIdentifier): void {
B
Benjamin Pasero 已提交
296 297
		const {editor, group} = identifier;
		const position = this.stacks.positionOfGroup(group);
B
Benjamin Pasero 已提交
298 299

		// Open on Click
B
Benjamin Pasero 已提交
300
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
301
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass(<any>e.target || e.srcElement, 'monaco-action-bar', 'tab')) {
B
Benjamin Pasero 已提交
302
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
303
			}
B
Benjamin Pasero 已提交
304
		}));
B
Benjamin Pasero 已提交
305 306

		// Pin on double click
B
Benjamin Pasero 已提交
307
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DBLCLICK, (e: MouseEvent) => {
B
Benjamin Pasero 已提交
308 309
			DOM.EventHelper.stop(e);

B
Benjamin Pasero 已提交
310
			this.editorGroupService.pinEditor(position, editor);
B
Benjamin Pasero 已提交
311
		}));
B
Benjamin Pasero 已提交
312 313

		// Close on mouse middle click
B
Benjamin Pasero 已提交
314
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_UP, (e: MouseEvent) => {
B
Benjamin Pasero 已提交
315 316 317
			DOM.EventHelper.stop(e);

			if (e.button === 1 /* Middle Button */) {
B
Benjamin Pasero 已提交
318
				this.editorService.closeEditor(position, editor).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
319
			}
B
Benjamin Pasero 已提交
320
		}));
B
Benjamin Pasero 已提交
321 322

		// Context menu
B
Benjamin Pasero 已提交
323
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.CONTEXT_MENU, (e: Event) => {
B
Benjamin Pasero 已提交
324 325
			DOM.EventHelper.stop(e);

B
Benjamin Pasero 已提交
326
			let anchor: HTMLElement | { x: number, y: number } = tab;
B
Benjamin Pasero 已提交
327 328 329 330 331 332 333
			if (e instanceof MouseEvent) {
				const event = new StandardMouseEvent(e);
				anchor = { x: event.posx, y: event.posy };
			}

			this.contextMenuService.showContextMenu({
				getAnchor: () => anchor,
B
Benjamin Pasero 已提交
334 335
				getActions: () => TPromise.as(this.getTabActions(identifier)),
				getActionsContext: () => identifier,
B
Benjamin Pasero 已提交
336 337 338 339 340 341 342 343 344
				getKeyBinding: (action) => {
					var opts = this.keybindingService.lookupKeybindings(action.id);
					if (opts.length > 0) {
						return opts[0]; // only take the first one
					}

					return null;
				}
			});
B
Benjamin Pasero 已提交
345
		}));
B
Benjamin Pasero 已提交
346 347 348 349

		// Drag start
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_START, (e: DragEvent) => {
			DOM.addClass(tab, 'dragged');
B
Benjamin Pasero 已提交
350
			TabsTitleControl.draggedEditor = { editor, group };
B
Benjamin Pasero 已提交
351
			e.dataTransfer.effectAllowed = 'move';
B
Benjamin Pasero 已提交
352 353 354 355 356 357

			// Enable support to drag a file to desktop
			const fileInput = asFileEditorInput(editor, true);
			if (fileInput) {
				e.dataTransfer.setData('DownloadURL', [MIME_BINARY, editor.getName(), fileInput.getResource().toString()].join(':'));
			}
B
Benjamin Pasero 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		}));

		// Drag over
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_OVER, (e: DragEvent) => {
			DOM.addClass(tab, 'dropfeedback');
		}));

		// Drag leave
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_LEAVE, (e: DragEvent) => {
			DOM.removeClass(tab, 'dropfeedback');
		}));

		// Drag end
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_END, (e: DragEvent) => {
			DOM.removeClass(tab, 'dragged');
			DOM.removeClass(tab, 'dropfeedback');
B
Benjamin Pasero 已提交
374
			TabsTitleControl.draggedEditor = void 0;
B
Benjamin Pasero 已提交
375 376 377 378
		}));

		// Drop
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DROP, (e: DragEvent) => {
B
Benjamin Pasero 已提交
379
			if (TabsTitleControl.draggedEditor) {
B
Benjamin Pasero 已提交
380 381
				e.preventDefault();

B
Benjamin Pasero 已提交
382
				const sourcePosition = this.stacks.positionOfGroup(TabsTitleControl.draggedEditor.group);
B
Benjamin Pasero 已提交
383 384 385 386
				const targetPosition = this.stacks.positionOfGroup(group);
				const targetIndex = group.indexOf(editor);

				// Move editor to target position and index
B
Benjamin Pasero 已提交
387
				this.editorGroupService.moveEditor(TabsTitleControl.draggedEditor.editor, sourcePosition, targetPosition, targetIndex);
B
Benjamin Pasero 已提交
388 389 390
			}
		}));
	}
B
Benjamin Pasero 已提交
391

B
Benjamin Pasero 已提交
392
	private getTabActions(identifier: IEditorIdentifier): IAction[] {
B
Benjamin Pasero 已提交
393
		const {editor, group} = identifier;
B
Benjamin Pasero 已提交
394 395

		// Enablement
B
Benjamin Pasero 已提交
396 397 398
		this.closeOtherEditorsAction.enabled = group.count > 1;
		this.pinEditorAction.enabled = !group.isPinned(editor);
		this.closeRightEditorsAction.enabled = group.indexOf(editor) !== group.count - 1;
B
Benjamin Pasero 已提交
399

400
		// Actions: For all editors
B
Benjamin Pasero 已提交
401
		const actions: IAction[] = [
B
Benjamin Pasero 已提交
402 403
			this.closeEditorAction,
			this.closeOtherEditorsAction,
B
Benjamin Pasero 已提交
404
			this.closeRightEditorsAction,
B
Benjamin Pasero 已提交
405
			new Separator(),
406
			this.pinEditorAction,
B
Benjamin Pasero 已提交
407
		];
408

409
		// Actions: For active editor
B
Benjamin Pasero 已提交
410 411
		if (group.isActive(editor)) {
			const editorActions = this.getEditorActions(group);
412 413 414
			if (editorActions.primary.length) {
				actions.push(new Separator(), ...prepareActions(editorActions.primary));
			}
415

416 417 418
			if (editorActions.secondary.length) {
				actions.push(new Separator(), ...prepareActions(editorActions.secondary));
			}
419 420 421
		}

		return actions;
B
Benjamin Pasero 已提交
422 423
	}

B
wip  
Benjamin Pasero 已提交
424 425 426
	public dispose(): void {
		super.dispose();

B
Benjamin Pasero 已提交
427
		// Toolbar
B
wip  
Benjamin Pasero 已提交
428 429 430
		this.groupActionsToolbar.dispose();
	}
}