tabsTitleControl.ts 21.4 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 33 34 35 36 37 38
import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl';
import { IQuickOpenService } from 'vs/workbench/services/quickopen/common/quickOpenService';
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 110 111
				const group = this.context;

				return this.editorService.openEditor(this.untitledEditorService.createOrGet(), { pinned: true, index: group.count /* always at the end */ }); // untitled are always pinned
112 113 114
			}
		}));

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		// 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 已提交
130

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		// 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) {
272
				mapLabelAndDescriptionToDuplicates.getOrSet(item.name + item.description, []).push(item);
B
Benjamin Pasero 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285
			}
		});

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

286 287
		// React to duplicates for combination of label and description
		const descriptionDuplicates = mapLabelAndDescriptionToDuplicates.values();
B
Benjamin Pasero 已提交
288 289 290 291 292 293 294 295 296 297 298
		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;
	}

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

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

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

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

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

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

		this.tabDisposeables = dispose(this.tabDisposeables);
		this.editorLabels = dispose(this.editorLabels);
322 323 324 325 326 327
	}

	private refreshTabs(group: IEditorGroup): void {

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

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

		// Add a tab for each opened editor
		this.context.getEditors().forEach(editor => {
333

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

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

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

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

			this.tabDisposeables.push(bar);
B
Benjamin Pasero 已提交
361 362 363

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

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

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

375 376 377 378 379 380 381 382 383
		const visibleContainerWidth = this.tabsContainer.offsetWidth;
		const totalContainerWidth = this.tabsContainer.scrollWidth;

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

B
Benjamin Pasero 已提交
384
		// Always reveal the active one
B
Benjamin Pasero 已提交
385 386 387
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
388
		const activeTabFits = activeTabWidth <= visibleContainerWidth;
B
Benjamin Pasero 已提交
389 390

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
391 392
		// 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) {
393 394 395
			this.scrollbar.updateState({
				scrollLeft: containerScrollPosX + ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + visibleContainerWidth) /* right corner of view port */)
			});
B
Benjamin Pasero 已提交
396 397
		}

398 399
		// Tab is overlflowng to the left or does not fit: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX || !activeTabFits) {
400 401 402
			this.scrollbar.updateState({
				scrollLeft: this.activeTab.offsetLeft
			});
B
Benjamin Pasero 已提交
403
		}
B
Benjamin Pasero 已提交
404 405
	}

B
Benjamin Pasero 已提交
406
	private hookTabListeners(tab: HTMLElement, identifier: IEditorIdentifier): void {
B
Benjamin Pasero 已提交
407 408
		const {editor, group} = identifier;
		const position = this.stacks.positionOfGroup(group);
B
Benjamin Pasero 已提交
409 410

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

414
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass(<any>e.target || e.srcElement, 'monaco-action-bar', 'tab')) {
415 416 417 418 419 420 421
				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);
422
			tab.blur();
423 424 425

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

429 430 431 432 433 434 435 436 437 438
		// 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 已提交
439
		// Keyboard accessibility
B
Benjamin Pasero 已提交
440 441
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
442
			let handled = false;
B
Benjamin Pasero 已提交
443 444

			// Run action on Enter/Space
A
Alexandru Dima 已提交
445
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
B
Benjamin Pasero 已提交
446
				handled = true;
B
Benjamin Pasero 已提交
447
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
448 449
			}

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

B
Benjamin Pasero 已提交
454
				let targetIndex: number;
A
Alexandru Dima 已提交
455
				if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.UpArrow)) {
B
Benjamin Pasero 已提交
456
					targetIndex = index - 1;
A
Alexandru Dima 已提交
457
				} else if (event.equals(KeyCode.RightArrow) || event.equals(KeyCode.DownArrow)) {
B
Benjamin Pasero 已提交
458
					targetIndex = index + 1;
A
Alexandru Dima 已提交
459
				} else if (event.equals(KeyCode.Home)) {
B
Benjamin Pasero 已提交
460 461 462 463 464
					targetIndex = 0;
				} else {
					targetIndex = group.count - 1;
				}

B
Benjamin Pasero 已提交
465 466 467
				const target = group.getEditor(targetIndex);
				if (target) {
					handled = true;
468
					this.editorService.openEditor(target, { preserveFocus: true }, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
469 470 471
					(<HTMLElement>this.tabsContainer.childNodes[targetIndex]).focus();
				}
			}
B
Benjamin Pasero 已提交
472

B
Benjamin Pasero 已提交
473
			if (handled) {
474
				DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
475
			}
476 477 478 479 480

			// 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 已提交
481 482
		}));

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

487
			this.editorGroupService.pinEditor(group, editor);
B
Benjamin Pasero 已提交
488
		}));
B
Benjamin Pasero 已提交
489

B
Benjamin Pasero 已提交
490
		// Context menu
491
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.CONTEXT_MENU, (e: Event) => this.onContextMenu(identifier, e, tab)));
B
Benjamin Pasero 已提交
492 493 494

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

B
Benjamin Pasero 已提交
498
			// Insert transfer accordingly
B
Benjamin Pasero 已提交
499 500
			const fileInput = asFileEditorInput(editor, true);
			if (fileInput) {
B
Benjamin Pasero 已提交
501 502 503
				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 已提交
504
			}
B
Benjamin Pasero 已提交
505 506
		}));

507 508 509 510 511 512
		// 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 已提交
513
		// Drag over
514 515
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_ENTER, (e: DragEvent) => {
			counter++;
B
Benjamin Pasero 已提交
516 517 518 519 520
			DOM.addClass(tab, 'dropfeedback');
		}));

		// Drag leave
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_LEAVE, (e: DragEvent) => {
521 522 523 524
			counter--;
			if (counter === 0) {
				DOM.removeClass(tab, 'dropfeedback');
			}
B
Benjamin Pasero 已提交
525 526 527 528
		}));

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

532
			this.onEditorDragEnd();
B
Benjamin Pasero 已提交
533 534 535 536
		}));

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

540 541 542
			const targetPosition = this.stacks.positionOfGroup(group);
			const targetIndex = group.indexOf(editor);

B
Benjamin Pasero 已提交
543 544 545
			this.onDrop(e, group, targetPosition, targetIndex);
		}));
	}
B
Benjamin Pasero 已提交
546

B
Benjamin Pasero 已提交
547
	private onDrop(e: DragEvent, group: IEditorGroup, targetPosition: Position, targetIndex: number): void {
548 549
		DOM.removeClass(this.tabsContainer, 'dropfeedback');
		DOM.removeClass(this.tabsContainer, 'scroll');
550

B
Benjamin Pasero 已提交
551 552 553 554
		// Local DND
		const draggedEditor = TabsTitleControl.getDraggedEditor();
		if (draggedEditor) {
			DOM.EventHelper.stop(e, true);
555

B
Benjamin Pasero 已提交
556 557 558
			// 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 已提交
559
			}
560

B
Benjamin Pasero 已提交
561
			// Copy: just open editor at target index
562
			else {
B
Benjamin Pasero 已提交
563
				this.editorService.openEditor(draggedEditor.editor, { pinned: true, index: targetIndex }, targetPosition).done(null, errors.onUnexpectedError);
564
			}
B
Benjamin Pasero 已提交
565 566 567 568 569 570 571 572

			this.onEditorDragEnd();
		}

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

575
	private handleExternalDrop(e: DragEvent, targetPosition: Position, targetIndex: number): void {
576
		const resources = extractResources(e).filter(r => r.scheme === 'file' || r.scheme === 'untitled');
577 578 579

		// Open resources if found
		if (resources.length) {
B
Benjamin Pasero 已提交
580
			DOM.EventHelper.stop(e, true);
581 582 583 584 585 586

			this.editorService.openEditors(resources.map(resource => {
				return {
					input: { resource, options: { pinned: true, index: targetIndex } },
					position: targetPosition
				};
J
Joao Moreno 已提交
587
			})).then(() => {
588
				this.editorGroupService.focusGroup(targetPosition);
J
Joao Moreno 已提交
589 590
				return this.windowService.focusWindow();
			}).done(null, errors.onUnexpectedError);
591 592 593
		}
	}

594 595 596 597 598
	private isMoveOperation(e: DragEvent, source: IEditorGroup, target: IEditorGroup) {
		const isCopy = (e.ctrlKey && !isMacintosh) || (e.altKey && isMacintosh);

		return !isCopy || source.id === target.id;
	}
B
wip  
Benjamin Pasero 已提交
599
}