tabsTitleControl.ts 23.2 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
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
11 12
import errors = require('vs/base/common/errors');
import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
13 14
import { isMacintosh } from 'vs/base/common/platform';
import { MIME_BINARY } from 'vs/base/common/mime';
B
Benjamin Pasero 已提交
15
import { shorten } from 'vs/base/common/labels';
B
Benjamin Pasero 已提交
16
import { ActionRunner, IAction } from 'vs/base/common/actions';
17
import { Position, IEditorInput, Verbosity } from 'vs/platform/editor/common/editor';
18
import { IEditorGroup, toResource } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
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 { 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 已提交
33
import { IWindowService } from 'vs/platform/windows/common/windows';
J
Johannes Rieken 已提交
34
import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl';
J
Johannes Rieken 已提交
35
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
36
import { IDisposable, dispose, combinedDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
37 38 39 40
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 已提交
41 42 43 44 45 46

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

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

	constructor(
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService instantiationService: IInstantiationService,
		@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, 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 title = label.title || '';
215

216
				// Container
B
Benjamin Pasero 已提交
217
				tabContainer.setAttribute('aria-label', `${name}, tab`);
218
				tabContainer.title = title;
219 220 221 222
				['off', 'left'].forEach(option => {
					const domAction = this.tabOptions.tabCloseButton === option ? DOM.addClass : DOM.removeClass;
					domAction(tabContainer, `close-button-${option}`);
				});
223

224 225
				// Label
				const tabLabel = this.editorLabels[index];
226
				tabLabel.setLabel({ name, description, resource: toResource(editor, { supportSideBySide: true }) }, { extraClasses: ['tab-label'], italic: !isPinned });
227 228 229 230

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

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

247
		// Update Editor Actions Toolbar
248
		this.updateEditorActionsToolbar();
249

B
Benjamin Pasero 已提交
250
		// Ensure the active tab is always revealed
251
		this.layout();
252 253
	}

B
Benjamin Pasero 已提交
254 255 256 257
	private getUniqueTabLabels(editors: IEditorInput[]): IEditorInputLabel[] {
		const labels: IEditorInputLabel[] = [];

		const mapLabelToDuplicates = new LinkedMap<string, IEditorInputLabel[]>();
B
Benjamin Pasero 已提交
258
		const mapLabelAndDescriptionToDuplicates = new LinkedMap<string, IEditorInputLabel[]>();
B
Benjamin Pasero 已提交
259 260 261 262 263 264 265 266

		// Build labels and descriptions for each editor
		editors.forEach(editor => {
			let description = editor.getDescription();
			const item: IEditorInputLabel = {
				editor,
				name: editor.getName(),
				description,
267
				title: editor.getTitle(Verbosity.LONG)
B
Benjamin Pasero 已提交
268 269 270 271
			};
			labels.push(item);

			mapLabelToDuplicates.getOrSet(item.name, []).push(item);
B
Benjamin Pasero 已提交
272 273 274 275

			if (typeof description === 'string') {
				mapLabelAndDescriptionToDuplicates.getOrSet(`${item.name}${item.description}`, []).push(item);
			}
B
Benjamin Pasero 已提交
276 277
		});

H
hun1ahpu 已提交
278
		// Mark duplicates and shorten their descriptions
B
Benjamin Pasero 已提交
279 280
		const labelDuplicates = mapLabelToDuplicates.values();
		labelDuplicates.forEach(duplicates => {
281
			if (duplicates.length > 1) {
B
Benjamin Pasero 已提交
282 283 284 285
				duplicates = duplicates.filter(d => {
					// we could have items with equal label and description. in that case it does not make much
					// sense to produce a shortened version of the label, so we ignore those kind of items
					return typeof d.description === 'string' && mapLabelAndDescriptionToDuplicates.get(`${d.name}${d.description}`).length === 1;
B
Benjamin Pasero 已提交
286
				});
B
Benjamin Pasero 已提交
287 288 289 290 291 292 293 294

				if (duplicates.length > 1) {
					const shortenedDescriptions = shorten(duplicates.map(duplicate => duplicate.editor.getDescription()));
					duplicates.forEach((duplicate, i) => {
						duplicate.description = shortenedDescriptions[i];
						duplicate.hasAmbiguousName = true;
					});
				}
B
Benjamin Pasero 已提交
295 296 297 298 299 300
			}
		});

		return labels;
	}

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

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

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

B
Benjamin Pasero 已提交
312 313 314
		// Handle Tabs
		this.handleTabs(group.count);
		DOM.addClass(this.titleContainer, 'shows-tabs');
315

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

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

		this.tabDisposeables = dispose(this.tabDisposeables);
324
		this.editorLabels = [];
B
Benjamin Pasero 已提交
325

326
		DOM.removeClass(this.titleContainer, 'shows-tabs');
327 328
	}

B
Benjamin Pasero 已提交
329 330 331
	private handleTabs(tabsNeeded: number): void {
		const tabs = this.tabsContainer.children;
		const tabsCount = tabs.length;
B
Benjamin Pasero 已提交
332

B
Benjamin Pasero 已提交
333 334 335 336
		// Nothing to do if count did not change
		if (tabsCount === tabsNeeded) {
			return;
		}
B
Benjamin Pasero 已提交
337

B
Benjamin Pasero 已提交
338 339 340 341 342 343
		// We need more tabs: create new ones
		if (tabsCount < tabsNeeded) {
			for (let i = tabsCount; i < tabsNeeded; i++) {
				this.tabsContainer.appendChild(this.createTab(i));
			}
		}
344

B
Benjamin Pasero 已提交
345 346 347 348
		// We need less tabs: delete the ones we do not need
		else {
			for (let i = 0; i < tabsCount - tabsNeeded; i++) {
				(this.tabsContainer.lastChild as HTMLElement).remove();
349
				this.editorLabels.pop();
B
Benjamin Pasero 已提交
350
				this.tabDisposeables.pop().dispose();
351
			}
B
Benjamin Pasero 已提交
352 353
		}
	}
354

B
Benjamin Pasero 已提交
355
	private createTab(index: number): HTMLElement {
356

B
Benjamin Pasero 已提交
357 358 359 360 361 362
		// Tab Container
		const tabContainer = document.createElement('div');
		tabContainer.draggable = true;
		tabContainer.tabIndex = 0;
		tabContainer.setAttribute('role', 'presentation'); // cannot use role "tab" here due to https://github.com/Microsoft/vscode/issues/8659
		DOM.addClass(tabContainer, 'tab monaco-editor-background');
B
Benjamin Pasero 已提交
363

B
Benjamin Pasero 已提交
364 365 366
		// Tab Editor Label
		const editorLabel = this.instantiationService.createInstance(EditorLabel, tabContainer, void 0);
		this.editorLabels.push(editorLabel);
B
Benjamin Pasero 已提交
367

B
Benjamin Pasero 已提交
368 369 370 371
		// Tab Close
		const tabCloseContainer = document.createElement('div');
		DOM.addClass(tabCloseContainer, 'tab-close');
		tabContainer.appendChild(tabCloseContainer);
B
Benjamin Pasero 已提交
372

B
Benjamin Pasero 已提交
373 374
		const bar = new ActionBar(tabCloseContainer, { ariaLabel: nls.localize('araLabelTabActions', "Tab actions"), actionRunner: new TabActionRunner(() => this.context, index) });
		bar.push(this.closeEditorAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(this.closeEditorAction) });
375

B
Benjamin Pasero 已提交
376
		// Eventing
377 378 379
		const disposable = this.hookTabListeners(tabContainer, index);

		this.tabDisposeables.push(combinedDisposable([disposable, bar, editorLabel]));
B
Benjamin Pasero 已提交
380 381

		return tabContainer;
382 383 384 385 386 387 388
	}

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

389 390 391 392 393 394 395 396 397
		const visibleContainerWidth = this.tabsContainer.offsetWidth;
		const totalContainerWidth = this.tabsContainer.scrollWidth;

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

B
Benjamin Pasero 已提交
398
		// Always reveal the active one
B
Benjamin Pasero 已提交
399 400 401
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
402
		const activeTabFits = activeTabWidth <= visibleContainerWidth;
B
Benjamin Pasero 已提交
403 404

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
405 406
		// 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) {
407 408 409
			this.scrollbar.updateState({
				scrollLeft: containerScrollPosX + ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + visibleContainerWidth) /* right corner of view port */)
			});
B
Benjamin Pasero 已提交
410 411
		}

412 413
		// Tab is overlflowng to the left or does not fit: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX || !activeTabFits) {
414 415 416
			this.scrollbar.updateState({
				scrollLeft: this.activeTab.offsetLeft
			});
B
Benjamin Pasero 已提交
417
		}
B
Benjamin Pasero 已提交
418 419
	}

420
	private hookTabListeners(tab: HTMLElement, index: number): IDisposable {
B
Benjamin Pasero 已提交
421
		const disposables: IDisposable[] = [];
B
Benjamin Pasero 已提交
422 423

		// Open on Click
424
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
425 426
			tab.blur();

B
Benjamin Pasero 已提交
427
			const { editor, position } = this.toTabContext(index);
B
Benjamin Pasero 已提交
428
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass((e.target || e.srcElement) as HTMLElement, 'monaco-action-bar', 'tab')) {
429 430 431 432 433
				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
434
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_UP, (e: MouseEvent) => {
435
			DOM.EventHelper.stop(e);
436
			tab.blur();
437 438

			if (e.button === 1 /* Middle Button */) {
B
Benjamin Pasero 已提交
439 440
				const { editor, position } = this.toTabContext(index);

441
				this.editorService.closeEditor(position, editor).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
442
			}
B
Benjamin Pasero 已提交
443
		}));
B
Benjamin Pasero 已提交
444

445
		// Context menu on Shift+F10
446
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
447 448 449 450
			const event = new StandardKeyboardEvent(e);
			if (event.shiftKey && event.keyCode === KeyCode.F10) {
				DOM.EventHelper.stop(e);

B
Benjamin Pasero 已提交
451 452 453
				const { group, editor } = this.toTabContext(index);

				this.onContextMenu({ group, editor }, e, tab);
454 455 456
			}
		}));

B
Benjamin Pasero 已提交
457
		// Keyboard accessibility
458
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
B
Benjamin Pasero 已提交
459
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
460
			let handled = false;
B
Benjamin Pasero 已提交
461

B
Benjamin Pasero 已提交
462 463
			const { group, position, editor } = this.toTabContext(index);

B
Benjamin Pasero 已提交
464
			// Run action on Enter/Space
A
Alexandru Dima 已提交
465
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
B
Benjamin Pasero 已提交
466
				handled = true;
B
Benjamin Pasero 已提交
467
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
468 469
			}

B
Benjamin Pasero 已提交
470
			// Navigate in editors
A
Alexandru Dima 已提交
471
			else if ([KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.Home, KeyCode.End].some(kb => event.equals(kb))) {
B
Benjamin Pasero 已提交
472
				let targetIndex: number;
A
Alexandru Dima 已提交
473
				if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.UpArrow)) {
B
Benjamin Pasero 已提交
474
					targetIndex = index - 1;
A
Alexandru Dima 已提交
475
				} else if (event.equals(KeyCode.RightArrow) || event.equals(KeyCode.DownArrow)) {
B
Benjamin Pasero 已提交
476
					targetIndex = index + 1;
A
Alexandru Dima 已提交
477
				} else if (event.equals(KeyCode.Home)) {
B
Benjamin Pasero 已提交
478 479 480 481 482
					targetIndex = 0;
				} else {
					targetIndex = group.count - 1;
				}

B
Benjamin Pasero 已提交
483 484 485
				const target = group.getEditor(targetIndex);
				if (target) {
					handled = true;
486
					this.editorService.openEditor(target, { preserveFocus: true }, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
487 488 489
					(<HTMLElement>this.tabsContainer.childNodes[targetIndex]).focus();
				}
			}
B
Benjamin Pasero 已提交
490

B
Benjamin Pasero 已提交
491
			if (handled) {
492
				DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
493
			}
494 495 496 497 498

			// 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 已提交
499 500
		}));

B
Benjamin Pasero 已提交
501
		// Pin on double click
502
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DBLCLICK, (e: MouseEvent) => {
B
Benjamin Pasero 已提交
503 504
			DOM.EventHelper.stop(e);

B
Benjamin Pasero 已提交
505 506
			const { group, editor } = this.toTabContext(index);

507
			this.editorGroupService.pinEditor(group, editor);
B
Benjamin Pasero 已提交
508
		}));
B
Benjamin Pasero 已提交
509

B
Benjamin Pasero 已提交
510
		// Context menu
511
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.CONTEXT_MENU, (e: Event) => {
512
			DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
513 514 515
			const { group, editor } = this.toTabContext(index);

			this.onContextMenu({ group, editor }, e, tab);
516
		}, true /* use capture to fix https://github.com/Microsoft/vscode/issues/19145 */));
B
Benjamin Pasero 已提交
517 518

		// Drag start
519
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_START, (e: DragEvent) => {
B
Benjamin Pasero 已提交
520 521
			const { group, editor } = this.toTabContext(index);

522
			this.onEditorDragStart({ editor, group });
523
			e.dataTransfer.effectAllowed = 'copyMove';
B
Benjamin Pasero 已提交
524

B
Benjamin Pasero 已提交
525
			// Insert transfer accordingly
526 527 528
			const fileResource = toResource(editor, { supportSideBySide: true, filter: 'file' });
			if (fileResource) {
				const resource = fileResource.toString();
B
Benjamin Pasero 已提交
529 530
				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 已提交
531
			}
B
Benjamin Pasero 已提交
532 533
		}));

534 535 536 537 538 539
		// 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 已提交
540
		// Drag over
541
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_ENTER, (e: DragEvent) => {
542
			counter++;
B
Benjamin Pasero 已提交
543 544 545 546
			DOM.addClass(tab, 'dropfeedback');
		}));

		// Drag leave
547
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_LEAVE, (e: DragEvent) => {
548 549 550 551
			counter--;
			if (counter === 0) {
				DOM.removeClass(tab, 'dropfeedback');
			}
B
Benjamin Pasero 已提交
552 553 554
		}));

		// Drag end
555
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_END, (e: DragEvent) => {
556
			counter = 0;
B
Benjamin Pasero 已提交
557
			DOM.removeClass(tab, 'dropfeedback');
B
Benjamin Pasero 已提交
558

559
			this.onEditorDragEnd();
B
Benjamin Pasero 已提交
560 561 562
		}));

		// Drop
563
		disposables.push(DOM.addDisposableListener(tab, DOM.EventType.DROP, (e: DragEvent) => {
564
			counter = 0;
B
Benjamin Pasero 已提交
565 566
			DOM.removeClass(tab, 'dropfeedback');

B
Benjamin Pasero 已提交
567
			const { group, position } = this.toTabContext(index);
568

B
Benjamin Pasero 已提交
569
			this.onDrop(e, group, position, index);
B
Benjamin Pasero 已提交
570
		}));
571 572

		return combinedDisposable(disposables);
B
Benjamin Pasero 已提交
573
	}
B
Benjamin Pasero 已提交
574

B
Benjamin Pasero 已提交
575 576 577 578 579 580 581 582
	private toTabContext(index: number): { group: IEditorGroup, position: Position, editor: IEditorInput } {
		const group = this.context;
		const position = this.stacks.positionOfGroup(group);
		const editor = group.getEditor(index);

		return { group, position, editor };
	}

B
Benjamin Pasero 已提交
583
	private onDrop(e: DragEvent, group: IEditorGroup, targetPosition: Position, targetIndex: number): void {
584 585
		DOM.removeClass(this.tabsContainer, 'dropfeedback');
		DOM.removeClass(this.tabsContainer, 'scroll');
586

B
Benjamin Pasero 已提交
587 588 589 590
		// Local DND
		const draggedEditor = TabsTitleControl.getDraggedEditor();
		if (draggedEditor) {
			DOM.EventHelper.stop(e, true);
591

B
Benjamin Pasero 已提交
592 593 594
			// 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 已提交
595
			}
596

B
Benjamin Pasero 已提交
597
			// Copy: just open editor at target index
598
			else {
B
Benjamin Pasero 已提交
599
				this.editorService.openEditor(draggedEditor.editor, { pinned: true, index: targetIndex }, targetPosition).done(null, errors.onUnexpectedError);
600
			}
B
Benjamin Pasero 已提交
601 602 603 604 605 606 607 608

			this.onEditorDragEnd();
		}

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

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

614
		// Handle resources
615
		if (resources.length) {
B
Benjamin Pasero 已提交
616
			DOM.EventHelper.stop(e, true);
617

618 619 620 621 622 623 624 625 626 627 628 629 630
			// 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 => {
631
				return {
632
					input: { resource: d.resource, options: { pinned: true, index: targetIndex } },
633 634
					position: targetPosition
				};
J
Joao Moreno 已提交
635
			})).then(() => {
636
				this.editorGroupService.focusGroup(targetPosition);
J
Joao Moreno 已提交
637 638
				return this.windowService.focusWindow();
			}).done(null, errors.onUnexpectedError);
639 640 641
		}
	}

642 643 644 645 646
	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 已提交
647
}
B
Benjamin Pasero 已提交
648 649 650 651 652 653 654 655 656

class TabActionRunner extends ActionRunner {

	constructor(private group: () => IEditorGroup, private index: number) {
		super();
	}

	public run(action: IAction, context?: any): TPromise<any> {
		const group = this.group();
B
Benjamin Pasero 已提交
657 658 659
		if (!group) {
			return TPromise.as(void 0);
		}
B
Benjamin Pasero 已提交
660 661 662 663

		return super.run(action, { group, editor: group.getEditor(this.index) });
	}
}