tabsTitleControl.ts 22.0 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 nls = require('vs/nls');
B
Benjamin Pasero 已提交
10 11
import errors = require('vs/base/common/errors');
import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import { isMacintosh } from 'vs/base/common/platform';
import { MIME_BINARY } from 'vs/base/common/mime';
import { Position, IEditorInput } from 'vs/platform/editor/common/editor';
import { IEditorGroup, IEditorIdentifier, asFileEditorInput, getResource } from 'vs/workbench/common/editor';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import { EditorLabel } from 'vs/workbench/browser/labels';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
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 { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
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/keybinding';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IMenuService } from 'vs/platform/actions/common/actions';
J
Joao Moreno 已提交
31
import { IWindowService } from 'vs/platform/windows/common/windows';
J
Johannes Rieken 已提交
32
import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl';
J
Johannes Rieken 已提交
33
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
J
Johannes Rieken 已提交
34 35 36 37 38
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
import { extractResources } from 'vs/base/browser/dnd';
import { LinkedMap } from 'vs/base/common/map';
B
Benjamin Pasero 已提交
39 40 41 42 43 44 45 46 47
import paths = require('vs/base/common/paths');

interface IEditorInputLabel {
	editor: IEditorInput;
	name: string;
	hasAmbiguousName?: boolean;
	description?: string;
	verboseDescription?: string;
}
B
wip  
Benjamin Pasero 已提交
48 49

export class TabsTitleControl extends TitleControl {
B
Benjamin Pasero 已提交
50 51 52
	private titleContainer: HTMLElement;
	private tabsContainer: HTMLElement;
	private activeTab: HTMLElement;
53
	private editorLabels: EditorLabel[];
54
	private scrollbar: ScrollableElement;
55
	private tabDisposeables: IDisposable[] = [];
B
wip  
Benjamin Pasero 已提交
56 57 58 59

	constructor(
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService instantiationService: IInstantiationService,
60
		@IConfigurationService configurationService: IConfigurationService,
B
wip  
Benjamin Pasero 已提交
61 62
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IEditorGroupService editorGroupService: IEditorGroupService,
63
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
64
		@IContextKeyService contextKeyService: IContextKeyService,
65
		@IKeybindingService keybindingService: IKeybindingService,
B
wip  
Benjamin Pasero 已提交
66
		@ITelemetryService telemetryService: ITelemetryService,
67
		@IMessageService messageService: IMessageService,
68
		@IMenuService menuService: IMenuService,
J
Joao Moreno 已提交
69 70
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IWindowService private windowService: IWindowService
B
wip  
Benjamin Pasero 已提交
71
	) {
72
		super(contextMenuService, instantiationService, configurationService, editorService, editorGroupService, contextKeyService, keybindingService, telemetryService, messageService, menuService, quickOpenService);
B
wip  
Benjamin Pasero 已提交
73

B
Benjamin Pasero 已提交
74
		this.tabDisposeables = [];
75
		this.editorLabels = [];
B
wip  
Benjamin Pasero 已提交
76 77 78 79 80
	}

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

81
		this.editorActionsToolbar.context = { group };
B
wip  
Benjamin Pasero 已提交
82 83
	}

84
	public create(parent: HTMLElement): void {
85
		super.create(parent);
86

87
		this.titleContainer = parent;
B
wip  
Benjamin Pasero 已提交
88

89
		// Tabs Container
B
Benjamin Pasero 已提交
90
		this.tabsContainer = document.createElement('div');
91
		this.tabsContainer.setAttribute('role', 'tablist');
B
Benjamin Pasero 已提交
92
		DOM.addClass(this.tabsContainer, 'tabs-container');
93

94
		// Forward scrolling inside the container to our custom scrollbar
B
Benjamin Pasero 已提交
95 96 97 98 99 100 101 102
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.SCROLL, e => {
			if (DOM.hasClass(this.tabsContainer, 'scroll')) {
				this.scrollbar.updateState({
					scrollLeft: this.tabsContainer.scrollLeft // during DND the  container gets scrolled so we need to update the custom scrollbar
				});
			}
		}));

103 104 105 106 107 108
		// New file when double clicking on tabs container (but not tabs)
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DBLCLICK, e => {
			const target = e.target;
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
				DOM.EventHelper.stop(e);

109
				const group = this.context;
B
Benjamin Pasero 已提交
110 111 112
				if (group) {
					this.editorService.openEditor(this.untitledEditorService.createOrGet(), { pinned: true, index: group.count /* always at the end */ }).done(null, errors.onUnexpectedError); // untitled are always pinned
				}
113 114 115
			}
		}));

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		// Custom Scrollbar
		this.scrollbar = new ScrollableElement(this.tabsContainer, {
			horizontal: ScrollbarVisibility.Auto,
			vertical: ScrollbarVisibility.Hidden,
			scrollYToX: true,
			useShadows: false,
			canUseTranslate3d: true,
			horizontalScrollbarSize: 3
		});

		this.scrollbar.onScroll(e => {
			this.tabsContainer.scrollLeft = e.scrollLeft;
		});

		this.titleContainer.appendChild(this.scrollbar.getDomNode());
B
Benjamin Pasero 已提交
131

B
Benjamin Pasero 已提交
132 133
		// Drag over
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_OVER, (e: DragEvent) => {
B
Benjamin Pasero 已提交
134 135
			DOM.addClass(this.tabsContainer, 'scroll'); // enable support to scroll while dragging

B
Benjamin Pasero 已提交
136 137 138 139 140 141 142 143 144
			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');
B
Benjamin Pasero 已提交
145
			DOM.removeClass(this.tabsContainer, 'scroll');
B
Benjamin Pasero 已提交
146 147 148 149 150
		}));

		// Drag end
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_END, (e: DragEvent) => {
			DOM.removeClass(this.tabsContainer, 'dropfeedback');
B
Benjamin Pasero 已提交
151
			DOM.removeClass(this.tabsContainer, 'scroll');
B
Benjamin Pasero 已提交
152 153
		}));

B
Benjamin Pasero 已提交
154 155
		// Drop onto tabs container
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DROP, (e: DragEvent) => {
B
Benjamin Pasero 已提交
156
			DOM.removeClass(this.tabsContainer, 'dropfeedback');
B
Benjamin Pasero 已提交
157
			DOM.removeClass(this.tabsContainer, 'scroll');
B
Benjamin Pasero 已提交
158

B
Benjamin Pasero 已提交
159
			const target = e.target;
B
Benjamin Pasero 已提交
160
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
B
Benjamin Pasero 已提交
161 162
				const group = this.context;
				if (group) {
163 164 165
					const targetPosition = this.stacks.positionOfGroup(group);
					const targetIndex = group.count;

B
Benjamin Pasero 已提交
166
					this.onDrop(e, group, targetPosition, targetIndex);
B
Benjamin Pasero 已提交
167 168 169 170
				}
			}
		}));

171
		// Editor Actions Container
172 173 174
		const editorActionsContainer = document.createElement('div');
		DOM.addClass(editorActionsContainer, 'editor-actions');
		this.titleContainer.appendChild(editorActionsContainer);
175 176 177

		// Editor Actions Toolbar
		this.createEditorActionsToolBar(editorActionsContainer);
B
wip  
Benjamin Pasero 已提交
178 179
	}

B
Benjamin Pasero 已提交
180 181 182 183
	public allowDragging(element: HTMLElement): boolean {
		return (element.className === 'tabs-container');
	}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	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');
		}

199 200
		// Compute labels and protect against duplicates
		const editorsOfGroup = this.context.getEditors();
B
Benjamin Pasero 已提交
201
		const labels = this.getUniqueTabLabels(editorsOfGroup);
202

203
		// Tab label and styles
204
		editorsOfGroup.forEach((editor, index) => {
205 206
			const tabContainer = this.tabsContainer.children[index];
			if (tabContainer instanceof HTMLElement) {
207
				const isPinned = group.isPinned(index);
208 209 210
				const isActive = group.isActive(editor);
				const isDirty = editor.isDirty();

211 212
				const label = labels[index];
				const name = label.name;
B
Benjamin Pasero 已提交
213
				const description = label.hasAmbiguousName && label.description ? label.description : '';
214
				const verboseDescription = label.verboseDescription || '';
215

216
				// Container
217
				tabContainer.setAttribute('aria-label', `tab, ${name}`);
218
				tabContainer.title = verboseDescription;
219

220 221 222
				// Label
				const tabLabel = this.editorLabels[index];
				tabLabel.setLabel({ name, description, resource: getResource(editor) }, { extraClasses: ['tab-label'], italic: !isPinned });
223 224 225 226

				// Active state
				if (isActive) {
					DOM.addClass(tabContainer, 'active');
227
					tabContainer.setAttribute('aria-selected', 'true');
228 229 230
					this.activeTab = tabContainer;
				} else {
					DOM.removeClass(tabContainer, 'active');
231
					tabContainer.setAttribute('aria-selected', 'false');
232 233 234 235 236 237 238 239 240 241
				}

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

243
		// Update Editor Actions Toolbar
244
		this.updateEditorActionsToolbar();
245

B
Benjamin Pasero 已提交
246
		// Ensure the active tab is always revealed
247
		this.layout();
248 249
	}

B
Benjamin Pasero 已提交
250 251 252 253
	private getUniqueTabLabels(editors: IEditorInput[]): IEditorInputLabel[] {
		const labels: IEditorInputLabel[] = [];

		const mapLabelToDuplicates = new LinkedMap<string, IEditorInputLabel[]>();
254
		const mapLabelAndDescriptionToDuplicates = new LinkedMap<string, IEditorInputLabel[]>();
B
Benjamin Pasero 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

		// Build labels and descriptions for each editor
		editors.forEach(editor => {
			let description = editor.getDescription();
			if (description && description.indexOf(paths.nativeSep) >= 0) {
				description = paths.basename(description); // optimize for editors that show paths and build a shorter description to keep tab width small
			}

			const item: IEditorInputLabel = {
				editor,
				name: editor.getName(),
				description,
				verboseDescription: editor.getDescription(true)
			};
			labels.push(item);

			mapLabelToDuplicates.getOrSet(item.name, []).push(item);
			if (item.description) {
273
				mapLabelAndDescriptionToDuplicates.getOrSet(item.name + item.description, []).push(item);
B
Benjamin Pasero 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
			}
		});

		// Mark label duplicates
		const labelDuplicates = mapLabelToDuplicates.values();
		labelDuplicates.forEach(duplicates => {
			if (duplicates.length > 1) {
				duplicates.forEach(duplicate => {
					duplicate.hasAmbiguousName = true;
				});
			}
		});

287 288
		// React to duplicates for combination of label and description
		const descriptionDuplicates = mapLabelAndDescriptionToDuplicates.values();
B
Benjamin Pasero 已提交
289 290 291 292 293 294 295 296 297 298 299
		descriptionDuplicates.forEach(duplicates => {
			if (duplicates.length > 1) {
				duplicates.forEach(duplicate => {
					duplicate.description = duplicate.editor.getDescription(); // fallback to full description if the short description still has duplicates
				});
			}
		});

		return labels;
	}

300
	protected doRefresh(): void {
B
wip  
Benjamin Pasero 已提交
301
		const group = this.context;
302
		const editor = group && group.activeEditor;
B
wip  
Benjamin Pasero 已提交
303
		if (!editor) {
304 305
			this.clearTabs();

306
			this.clearEditorActionsToolbar();
B
wip  
Benjamin Pasero 已提交
307 308 309 310

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

B
Benjamin Pasero 已提交
311 312
		// Refresh Tabs
		this.refreshTabs(group);
313

314
		// Update Tabs
315
		this.doUpdate();
B
wip  
Benjamin Pasero 已提交
316 317
	}

318
	private clearTabs(): void {
B
Benjamin Pasero 已提交
319
		DOM.clearNode(this.tabsContainer);
320 321 322

		this.tabDisposeables = dispose(this.tabDisposeables);
		this.editorLabels = dispose(this.editorLabels);
323
		DOM.removeClass(this.titleContainer, 'shows-tabs');
324 325 326 327 328 329
	}

	private refreshTabs(group: IEditorGroup): void {

		// Empty container first
		this.clearTabs();
B
Benjamin Pasero 已提交
330 331

		const tabContainers: HTMLElement[] = [];
B
Benjamin Pasero 已提交
332 333

		// Add a tab for each opened editor
334 335
		const editors = this.context.getEditors();
		editors.forEach(editor => {
336

337
			// Tab Container
B
Benjamin Pasero 已提交
338
			const tabContainer = document.createElement('div');
B
Benjamin Pasero 已提交
339
			tabContainer.draggable = true;
B
Benjamin Pasero 已提交
340
			tabContainer.tabIndex = 0;
341
			tabContainer.setAttribute('role', 'presentation'); // cannot use role "tab" here due to https://github.com/Microsoft/vscode/issues/8659
B
Benjamin Pasero 已提交
342 343
			DOM.addClass(tabContainer, 'tab monaco-editor-background');
			tabContainers.push(tabContainer);
B
Benjamin Pasero 已提交
344

345 346 347 348 349 350
			if (!this.showTabCloseButton) {
				DOM.addClass(tabContainer, 'no-close-button');
			} else {
				DOM.removeClass(tabContainer, 'no-close-button');
			}

351
			// Tab Editor Label
B
Benjamin Pasero 已提交
352
			const editorLabel = this.instantiationService.createInstance(EditorLabel, tabContainer, void 0);
353
			this.editorLabels.push(editorLabel);
354

B
Benjamin Pasero 已提交
355 356 357 358 359 360
			// 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") });
B
Benjamin Pasero 已提交
361
			bar.push(this.closeEditorAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(this.closeEditorAction) });
B
Benjamin Pasero 已提交
362 363

			this.tabDisposeables.push(bar);
B
Benjamin Pasero 已提交
364 365 366

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

B
Benjamin Pasero 已提交
369 370
		// Add to tabs container
		tabContainers.forEach(tab => this.tabsContainer.appendChild(tab));
371 372 373 374

		if (editors.length > 0) {
			DOM.addClass(this.titleContainer, 'shows-tabs');
		}
375 376 377 378 379 380 381
	}

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

382 383 384 385 386 387 388 389 390
		const visibleContainerWidth = this.tabsContainer.offsetWidth;
		const totalContainerWidth = this.tabsContainer.scrollWidth;

		// Update scrollbar
		this.scrollbar.updateState({
			width: visibleContainerWidth,
			scrollWidth: totalContainerWidth
		});

B
Benjamin Pasero 已提交
391
		// Always reveal the active one
B
Benjamin Pasero 已提交
392 393 394
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
395
		const activeTabFits = activeTabWidth <= visibleContainerWidth;
B
Benjamin Pasero 已提交
396 397

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
398 399
		// Note: only try to do this if we actually have enough width to give to show the tab fully!
		if (activeTabFits && containerScrollPosX + visibleContainerWidth < activeTabPosX + activeTabWidth) {
400 401 402
			this.scrollbar.updateState({
				scrollLeft: containerScrollPosX + ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + visibleContainerWidth) /* right corner of view port */)
			});
B
Benjamin Pasero 已提交
403 404
		}

405 406
		// Tab is overlflowng to the left or does not fit: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX || !activeTabFits) {
407 408 409
			this.scrollbar.updateState({
				scrollLeft: this.activeTab.offsetLeft
			});
B
Benjamin Pasero 已提交
410
		}
B
Benjamin Pasero 已提交
411 412
	}

B
Benjamin Pasero 已提交
413
	private hookTabListeners(tab: HTMLElement, identifier: IEditorIdentifier): void {
B
Benjamin Pasero 已提交
414 415
		const {editor, group} = identifier;
		const position = this.stacks.positionOfGroup(group);
B
Benjamin Pasero 已提交
416 417

		// Open on Click
B
Benjamin Pasero 已提交
418
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
419 420
			tab.blur();

B
Benjamin Pasero 已提交
421
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass((e.target || e.srcElement) as HTMLElement, 'monaco-action-bar', 'tab')) {
422 423 424 425 426 427 428
				setTimeout(() => this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError)); // timeout to keep focus in editor after mouse up
			}
		}));

		// Close on mouse middle click
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_UP, (e: MouseEvent) => {
			DOM.EventHelper.stop(e);
429
			tab.blur();
430 431 432

			if (e.button === 1 /* Middle Button */) {
				this.editorService.closeEditor(position, editor).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
433
			}
B
Benjamin Pasero 已提交
434
		}));
B
Benjamin Pasero 已提交
435

436 437 438 439 440 441 442 443 444 445
		// Context menu on Shift+F10
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
			if (event.shiftKey && event.keyCode === KeyCode.F10) {
				DOM.EventHelper.stop(e);

				this.onContextMenu(identifier, e, tab);
			}
		}));

B
Benjamin Pasero 已提交
446
		// Keyboard accessibility
B
Benjamin Pasero 已提交
447 448
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
449
			let handled = false;
B
Benjamin Pasero 已提交
450 451

			// Run action on Enter/Space
A
Alexandru Dima 已提交
452
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
B
Benjamin Pasero 已提交
453
				handled = true;
B
Benjamin Pasero 已提交
454
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
455 456
			}

B
Benjamin Pasero 已提交
457
			// Navigate in editors
A
Alexandru Dima 已提交
458
			else if ([KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.Home, KeyCode.End].some(kb => event.equals(kb))) {
B
Benjamin Pasero 已提交
459
				const index = group.indexOf(editor);
B
Benjamin Pasero 已提交
460

B
Benjamin Pasero 已提交
461
				let targetIndex: number;
A
Alexandru Dima 已提交
462
				if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.UpArrow)) {
B
Benjamin Pasero 已提交
463
					targetIndex = index - 1;
A
Alexandru Dima 已提交
464
				} else if (event.equals(KeyCode.RightArrow) || event.equals(KeyCode.DownArrow)) {
B
Benjamin Pasero 已提交
465
					targetIndex = index + 1;
A
Alexandru Dima 已提交
466
				} else if (event.equals(KeyCode.Home)) {
B
Benjamin Pasero 已提交
467 468 469 470 471
					targetIndex = 0;
				} else {
					targetIndex = group.count - 1;
				}

B
Benjamin Pasero 已提交
472 473 474
				const target = group.getEditor(targetIndex);
				if (target) {
					handled = true;
475
					this.editorService.openEditor(target, { preserveFocus: true }, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
476 477 478
					(<HTMLElement>this.tabsContainer.childNodes[targetIndex]).focus();
				}
			}
B
Benjamin Pasero 已提交
479

B
Benjamin Pasero 已提交
480
			if (handled) {
481
				DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
482
			}
483 484 485 486 487

			// moving in the tabs container can have an impact on scrolling position, so we need to update the custom scrollbar
			this.scrollbar.updateState({
				scrollLeft: this.tabsContainer.scrollLeft
			});
B
Benjamin Pasero 已提交
488 489
		}));

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

494
			this.editorGroupService.pinEditor(group, editor);
B
Benjamin Pasero 已提交
495
		}));
B
Benjamin Pasero 已提交
496

B
Benjamin Pasero 已提交
497
		// Context menu
498
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.CONTEXT_MENU, (e: Event) => this.onContextMenu(identifier, e, tab)));
B
Benjamin Pasero 已提交
499 500 501

		// Drag start
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_START, (e: DragEvent) => {
502
			this.onEditorDragStart({ editor, group });
503
			e.dataTransfer.effectAllowed = 'copyMove';
B
Benjamin Pasero 已提交
504

B
Benjamin Pasero 已提交
505
			// Insert transfer accordingly
B
Benjamin Pasero 已提交
506 507
			const fileInput = asFileEditorInput(editor, true);
			if (fileInput) {
B
Benjamin Pasero 已提交
508 509 510
				const resource = fileInput.getResource().toString();
				e.dataTransfer.setData('URL', resource); // enables cross window DND of tabs
				e.dataTransfer.setData('DownloadURL', [MIME_BINARY, editor.getName(), resource].join(':')); // enables support to drag a tab as file to desktop
B
Benjamin Pasero 已提交
511
			}
B
Benjamin Pasero 已提交
512 513
		}));

514 515 516 517 518 519
		// We need to keep track of DRAG_ENTER and DRAG_LEAVE events because a tab is not just a div without children,
		// it contains a label and a close button. HTML gives us DRAG_ENTER and DRAG_LEAVE events when hovering over
		// these children and this can cause flicker of the drop feedback. The workaround is to count the events and only
		// remove the drop feedback when the counter is 0 (see https://github.com/Microsoft/vscode/issues/14470)
		let counter = 0;

B
Benjamin Pasero 已提交
520
		// Drag over
521 522
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_ENTER, (e: DragEvent) => {
			counter++;
B
Benjamin Pasero 已提交
523 524 525 526 527
			DOM.addClass(tab, 'dropfeedback');
		}));

		// Drag leave
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_LEAVE, (e: DragEvent) => {
528 529 530 531
			counter--;
			if (counter === 0) {
				DOM.removeClass(tab, 'dropfeedback');
			}
B
Benjamin Pasero 已提交
532 533 534 535
		}));

		// Drag end
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_END, (e: DragEvent) => {
536
			counter = 0;
B
Benjamin Pasero 已提交
537
			DOM.removeClass(tab, 'dropfeedback');
B
Benjamin Pasero 已提交
538

539
			this.onEditorDragEnd();
B
Benjamin Pasero 已提交
540 541 542 543
		}));

		// Drop
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DROP, (e: DragEvent) => {
544
			counter = 0;
B
Benjamin Pasero 已提交
545 546
			DOM.removeClass(tab, 'dropfeedback');

547 548 549
			const targetPosition = this.stacks.positionOfGroup(group);
			const targetIndex = group.indexOf(editor);

B
Benjamin Pasero 已提交
550 551 552
			this.onDrop(e, group, targetPosition, targetIndex);
		}));
	}
B
Benjamin Pasero 已提交
553

B
Benjamin Pasero 已提交
554
	private onDrop(e: DragEvent, group: IEditorGroup, targetPosition: Position, targetIndex: number): void {
555 556
		DOM.removeClass(this.tabsContainer, 'dropfeedback');
		DOM.removeClass(this.tabsContainer, 'scroll');
557

B
Benjamin Pasero 已提交
558 559 560 561
		// Local DND
		const draggedEditor = TabsTitleControl.getDraggedEditor();
		if (draggedEditor) {
			DOM.EventHelper.stop(e, true);
562

B
Benjamin Pasero 已提交
563 564 565
			// Move editor to target position and index
			if (this.isMoveOperation(e, draggedEditor.group, group)) {
				this.editorGroupService.moveEditor(draggedEditor.editor, draggedEditor.group, group, targetIndex);
B
Benjamin Pasero 已提交
566
			}
567

B
Benjamin Pasero 已提交
568
			// Copy: just open editor at target index
569
			else {
B
Benjamin Pasero 已提交
570
				this.editorService.openEditor(draggedEditor.editor, { pinned: true, index: targetIndex }, targetPosition).done(null, errors.onUnexpectedError);
571
			}
B
Benjamin Pasero 已提交
572 573 574 575 576 577 578 579

			this.onEditorDragEnd();
		}

		// External DND
		else {
			this.handleExternalDrop(e, targetPosition, targetIndex);
		}
B
Benjamin Pasero 已提交
580
	}
581

582
	private handleExternalDrop(e: DragEvent, targetPosition: Position, targetIndex: number): void {
583
		const resources = extractResources(e).filter(d => d.resource.scheme === 'file' || d.resource.scheme === 'untitled');
584

585
		// Handle resources
586
		if (resources.length) {
B
Benjamin Pasero 已提交
587
			DOM.EventHelper.stop(e, true);
588

589 590 591 592 593 594 595 596 597 598 599 600 601
			// Add external ones to recently open list
			const externalResources = resources.filter(d => d.isExternal).map(d => d.resource);
			if (externalResources.length) {
				this.windowService.addToRecentlyOpen(externalResources.map(resource => {
					return {
						path: resource.fsPath,
						isFile: true
					};
				}));
			}

			// Open in Editor
			this.editorService.openEditors(resources.map(d => {
602
				return {
603
					input: { resource: d.resource, options: { pinned: true, index: targetIndex } },
604 605
					position: targetPosition
				};
J
Joao Moreno 已提交
606
			})).then(() => {
607
				this.editorGroupService.focusGroup(targetPosition);
J
Joao Moreno 已提交
608 609
				return this.windowService.focusWindow();
			}).done(null, errors.onUnexpectedError);
610 611 612
		}
	}

613 614 615 616 617
	private isMoveOperation(e: DragEvent, source: IEditorGroup, target: IEditorGroup) {
		const isCopy = (e.ctrlKey && !isMacintosh) || (e.altKey && isMacintosh);

		return !isCopy || source.id === target.id;
	}
J
Johannes Rieken 已提交
618
}