tabsTitleControl.ts 20.1 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 {IAction} from 'vs/base/common/actions';
11
import {prepareActions} from 'vs/workbench/browser/actionBarRegistry';
B
wip  
Benjamin Pasero 已提交
12
import arrays = require('vs/base/common/arrays');
B
Benjamin Pasero 已提交
13 14
import errors = require('vs/base/common/errors');
import DOM = require('vs/base/browser/dom');
15
import {isMacintosh} from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
16
import {MIME_BINARY} from 'vs/base/common/mime';
17
import {Position} from 'vs/platform/editor/common/editor';
18
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
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
21
import {CommonKeybindings as Kb, KeyCode} from 'vs/base/common/keyCodes';
B
Benjamin Pasero 已提交
22
import {ActionBar, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
23
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
B
wip  
Benjamin Pasero 已提交
24 25 26
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';
27
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
B
wip  
Benjamin Pasero 已提交
28 29 30 31
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';
32
import {IMenuService} from 'vs/platform/actions/common/actions';
B
wip  
Benjamin Pasero 已提交
33
import {TitleControl} from 'vs/workbench/browser/parts/editor/titleControl';
B
Benjamin Pasero 已提交
34
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
35 36
import {ScrollableElement} from 'vs/base/browser/ui/scrollbar/scrollableElement';
import {ScrollbarVisibility} from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
37
import {extractResources} from 'vs/base/browser/dnd';
B
wip  
Benjamin Pasero 已提交
38 39

export class TabsTitleControl extends TitleControl {
B
Benjamin Pasero 已提交
40 41 42
	private titleContainer: HTMLElement;
	private tabsContainer: HTMLElement;
	private activeTab: HTMLElement;
43
	private scrollbar: ScrollableElement;
B
wip  
Benjamin Pasero 已提交
44 45

	private groupActionsToolbar: ToolBar;
46
	private tabDisposeables: IDisposable[] = [];
B
wip  
Benjamin Pasero 已提交
47

48 49
	private currentPrimaryGroupActionIds: string[] = [];
	private currentSecondaryGroupActionIds: string[] = [];
B
wip  
Benjamin Pasero 已提交
50 51 52 53

	constructor(
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService instantiationService: IInstantiationService,
54
		@IConfigurationService configurationService: IConfigurationService,
B
wip  
Benjamin Pasero 已提交
55 56
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IEditorGroupService editorGroupService: IEditorGroupService,
57
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
B
wip  
Benjamin Pasero 已提交
58 59
		@IKeybindingService keybindingService: IKeybindingService,
		@ITelemetryService telemetryService: ITelemetryService,
60 61
		@IMessageService messageService: IMessageService,
		@IMenuService menuService: IMenuService
B
wip  
Benjamin Pasero 已提交
62
	) {
63
		super(contextMenuService, instantiationService, configurationService, editorService, editorGroupService, keybindingService, telemetryService, messageService, menuService);
B
wip  
Benjamin Pasero 已提交
64 65 66

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

B
Benjamin Pasero 已提交
68
		this.tabDisposeables = [];
B
wip  
Benjamin Pasero 已提交
69 70 71 72 73 74 75 76
	}

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

		this.groupActionsToolbar.context = { group };
	}

77 78
	public create(parent: HTMLElement): void {
		this.titleContainer = parent;
B
wip  
Benjamin Pasero 已提交
79

80
		// Tabs Container
B
Benjamin Pasero 已提交
81
		this.tabsContainer = document.createElement('div');
82
		this.tabsContainer.setAttribute('role', 'tablist');
B
Benjamin Pasero 已提交
83
		DOM.addClass(this.tabsContainer, 'tabs-container');
84

85
		// Forward scrolling inside the container to our custom scrollbar
B
Benjamin Pasero 已提交
86 87 88 89 90 91 92 93
		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
				});
			}
		}));

94 95 96 97 98 99
		// 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);

100
				return this.editorService.openEditor(this.untitledEditorService.createOrGet(), { pinned: true }); // untitled are always pinned
101 102 103
			}
		}));

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		// 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 已提交
119

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

B
Benjamin Pasero 已提交
124 125 126 127 128 129 130 131 132
			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 已提交
133
			DOM.removeClass(this.tabsContainer, 'scroll');
B
Benjamin Pasero 已提交
134 135 136 137 138
		}));

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

B
Benjamin Pasero 已提交
142 143
		// Drop onto tabs container
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DROP, (e: DragEvent) => {
B
Benjamin Pasero 已提交
144
			DOM.removeClass(this.tabsContainer, 'dropfeedback');
B
Benjamin Pasero 已提交
145
			DOM.removeClass(this.tabsContainer, 'scroll');
B
Benjamin Pasero 已提交
146

B
Benjamin Pasero 已提交
147
			const target = e.target;
B
Benjamin Pasero 已提交
148
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
B
Benjamin Pasero 已提交
149 150
				const group = this.context;
				if (group) {
151 152 153 154
					const targetPosition = this.stacks.positionOfGroup(group);
					const targetIndex = group.count;

					// Local DND
155 156
					const draggedEditor = TitleControl.getDraggedEditor();
					if (draggedEditor) {
B
Benjamin Pasero 已提交
157
						DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
158

159
						const sourcePosition = this.stacks.positionOfGroup(draggedEditor.group);
B
Benjamin Pasero 已提交
160

161
						// Move editor to target position and index
162 163
						if (this.isMoveOperation(e, draggedEditor.group, group)) {
							this.editorGroupService.moveEditor(draggedEditor.editor, sourcePosition, targetPosition, targetIndex);
164 165 166 167
						}

						// Copy: just open editor at target index
						else {
168
							this.editorService.openEditor(draggedEditor.editor, { pinned: true, index: targetIndex }, targetPosition).done(null, errors.onUnexpectedError);
169
						}
170 171

						this.onEditorDragEnd();
B
Benjamin Pasero 已提交
172
					}
173 174 175 176 177

					// External DND
					else {
						this.handleExternalDrop(e, targetPosition, targetIndex);
					}
B
Benjamin Pasero 已提交
178 179 180 181
				}
			}
		}));

B
Benjamin Pasero 已提交
182
		// Group Actions
B
Benjamin Pasero 已提交
183 184 185
		const groupActionsContainer = document.createElement('div');
		DOM.addClass(groupActionsContainer, 'group-actions');
		this.titleContainer.appendChild(groupActionsContainer);
186
		this.groupActionsToolbar = this.doCreateToolbar(groupActionsContainer);
B
wip  
Benjamin Pasero 已提交
187 188
	}

B
Benjamin Pasero 已提交
189 190 191 192
	public allowDragging(element: HTMLElement): boolean {
		return (element.className === 'tabs-container');
	}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	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');
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

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

	protected doRefresh(): void {
B
wip  
Benjamin Pasero 已提交
247
		const group = this.context;
248
		const editor = group && group.activeEditor;
B
wip  
Benjamin Pasero 已提交
249
		if (!editor) {
250 251
			this.clearTabs();

B
wip  
Benjamin Pasero 已提交
252 253 254 255 256 257 258 259
			this.groupActionsToolbar.setActions([], [])();

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

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

260
		// Refresh Group Actions Toolbar
B
wip  
Benjamin Pasero 已提交
261
		const groupActions = this.getGroupActions(group);
B
Benjamin Pasero 已提交
262 263
		const primaryGroupActions = groupActions.primary;
		const secondaryGroupActions = groupActions.secondary;
B
wip  
Benjamin Pasero 已提交
264 265 266 267 268 269 270 271
		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 已提交
272 273 274

		// Refresh Tabs
		this.refreshTabs(group);
275 276 277

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

280
	private clearTabs(): void {
B
Benjamin Pasero 已提交
281 282 283
		DOM.clearNode(this.tabsContainer);
		dispose(this.tabDisposeables);
		this.tabDisposeables = [];
284 285 286 287 288 289
	}

	private refreshTabs(group: IEditorGroup): void {

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

		const tabContainers: HTMLElement[] = [];
B
Benjamin Pasero 已提交
292 293 294

		// Add a tab for each opened editor
		this.context.getEditors().forEach(editor => {
295 296
			const description = editor.getDescription(true) || '';

B
Benjamin Pasero 已提交
297
			const tabContainer = document.createElement('div');
298
			tabContainer.title = description;
B
Benjamin Pasero 已提交
299
			tabContainer.draggable = true;
B
Benjamin Pasero 已提交
300
			tabContainer.tabIndex = 0;
301 302
			tabContainer.setAttribute('role', 'presentation'); // cannot use role "tab" here due to https://github.com/Microsoft/vscode/issues/8659
			tabContainer.setAttribute('aria-label', `tab, ${editor.getName()}`);
B
Benjamin Pasero 已提交
303 304
			DOM.addClass(tabContainer, 'tab monaco-editor-background');
			tabContainers.push(tabContainer);
B
Benjamin Pasero 已提交
305

B
Benjamin Pasero 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
			// 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();
			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 已提交
325 326 327

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

B
Benjamin Pasero 已提交
330 331
		// Add to tabs container
		tabContainers.forEach(tab => this.tabsContainer.appendChild(tab));
332 333 334 335 336 337 338
	}

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

339 340 341 342 343 344 345 346 347
		const visibleContainerWidth = this.tabsContainer.offsetWidth;
		const totalContainerWidth = this.tabsContainer.scrollWidth;

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

B
Benjamin Pasero 已提交
348
		// Always reveal the active one
B
Benjamin Pasero 已提交
349 350 351
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
352
		const activeTabFits = activeTabWidth <= visibleContainerWidth;
B
Benjamin Pasero 已提交
353 354

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
355 356
		// 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) {
357 358 359
			this.scrollbar.updateState({
				scrollLeft: containerScrollPosX + ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + visibleContainerWidth) /* right corner of view port */)
			});
B
Benjamin Pasero 已提交
360 361
		}

362 363
		// Tab is overlflowng to the left or does not fit: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX || !activeTabFits) {
364 365 366
			this.scrollbar.updateState({
				scrollLeft: this.activeTab.offsetLeft
			});
B
Benjamin Pasero 已提交
367
		}
368 369

		// Update enablement of certain actions that depend on overflow
370
		const isOverflowing = (totalContainerWidth > visibleContainerWidth);
B
Benjamin Pasero 已提交
371
		this.showEditorsInGroupAction.enabled = isOverflowing;
B
Benjamin Pasero 已提交
372 373
	}

B
Benjamin Pasero 已提交
374
	private hookTabListeners(tab: HTMLElement, identifier: IEditorIdentifier): void {
B
Benjamin Pasero 已提交
375 376
		const {editor, group} = identifier;
		const position = this.stacks.positionOfGroup(group);
B
Benjamin Pasero 已提交
377 378

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

382
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass(<any>e.target || e.srcElement, 'monaco-action-bar', 'tab')) {
383 384 385 386 387 388 389
				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);
390
			tab.blur();
391 392 393

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

397 398 399 400 401 402 403 404 405 406
		// 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 已提交
407
		// Keyboard accessibility
B
Benjamin Pasero 已提交
408 409
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
410
			let handled = false;
B
Benjamin Pasero 已提交
411 412

			// Run action on Enter/Space
B
Benjamin Pasero 已提交
413
			if (event.equals(Kb.ENTER) || event.equals(Kb.SPACE)) {
B
Benjamin Pasero 已提交
414
				handled = true;
B
Benjamin Pasero 已提交
415
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
416 417
			}

B
Benjamin Pasero 已提交
418
			// Navigate in editors
B
Benjamin Pasero 已提交
419
			else if ([Kb.LEFT_ARROW, Kb.RIGHT_ARROW, Kb.UP_ARROW, Kb.DOWN_ARROW, Kb.HOME, Kb.END].some(kb => event.equals(kb))) {
B
Benjamin Pasero 已提交
420
				const index = group.indexOf(editor);
B
Benjamin Pasero 已提交
421

B
Benjamin Pasero 已提交
422
				let targetIndex: number;
B
Benjamin Pasero 已提交
423
				if (event.equals(Kb.LEFT_ARROW) || event.equals(Kb.UP_ARROW)) {
B
Benjamin Pasero 已提交
424
					targetIndex = index - 1;
B
Benjamin Pasero 已提交
425
				} else if (event.equals(Kb.RIGHT_ARROW) || event.equals(Kb.DOWN_ARROW)) {
B
Benjamin Pasero 已提交
426
					targetIndex = index + 1;
B
Benjamin Pasero 已提交
427
				} else if (event.equals(Kb.HOME)) {
B
Benjamin Pasero 已提交
428 429 430 431 432
					targetIndex = 0;
				} else {
					targetIndex = group.count - 1;
				}

B
Benjamin Pasero 已提交
433 434 435
				const target = group.getEditor(targetIndex);
				if (target) {
					handled = true;
436
					this.editorService.openEditor(target, { preserveFocus: true }, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
437 438 439
					(<HTMLElement>this.tabsContainer.childNodes[targetIndex]).focus();
				}
			}
B
Benjamin Pasero 已提交
440

B
Benjamin Pasero 已提交
441
			if (handled) {
442
				DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
443
			}
444 445 446 447 448

			// 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 已提交
449 450
		}));

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

B
Benjamin Pasero 已提交
455
			this.editorGroupService.pinEditor(position, editor);
B
Benjamin Pasero 已提交
456
		}));
B
Benjamin Pasero 已提交
457

B
Benjamin Pasero 已提交
458
		// Context menu
459
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.CONTEXT_MENU, (e: Event) => this.onContextMenu(identifier, e, tab)));
B
Benjamin Pasero 已提交
460 461 462

		// Drag start
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_START, (e: DragEvent) => {
463
			this.onEditorDragStart({ editor, group });
464
			e.dataTransfer.effectAllowed = 'copyMove';
B
Benjamin Pasero 已提交
465 466 467 468 469 470

			// 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 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
		}));

		// 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, 'dropfeedback');
B
Benjamin Pasero 已提交
486

487
			this.onEditorDragEnd();
B
Benjamin Pasero 已提交
488 489 490 491
		}));

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

494 495 496 497
			const targetPosition = this.stacks.positionOfGroup(group);
			const targetIndex = group.indexOf(editor);

			// Local DND
498 499
			const draggedEditor = TabsTitleControl.getDraggedEditor();
			if (draggedEditor) {
B
Benjamin Pasero 已提交
500
				DOM.EventHelper.stop(e, true);
B
Benjamin Pasero 已提交
501

502
				const sourcePosition = this.stacks.positionOfGroup(draggedEditor.group);
B
Benjamin Pasero 已提交
503 504

				// Move editor to target position and index
505 506
				if (this.isMoveOperation(e, draggedEditor.group, group)) {
					this.editorGroupService.moveEditor(draggedEditor.editor, sourcePosition, targetPosition, targetIndex);
507 508 509 510
				}

				// Copy: just open editor at target index
				else {
511
					this.editorService.openEditor(draggedEditor.editor, { pinned: true, index: targetIndex }, targetPosition).done(null, errors.onUnexpectedError);
512
				}
513 514

				this.onEditorDragEnd();
B
Benjamin Pasero 已提交
515
			}
516 517 518 519 520

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

524
	private handleExternalDrop(e: DragEvent, targetPosition: Position, targetIndex: number): void {
525
		const resources = extractResources(e).filter(r => r.scheme === 'file' || r.scheme === 'untitled');
526 527 528

		// Open resources if found
		if (resources.length) {
B
Benjamin Pasero 已提交
529
			DOM.EventHelper.stop(e, true);
530 531 532 533 534 535

			this.editorService.openEditors(resources.map(resource => {
				return {
					input: { resource, options: { pinned: true, index: targetIndex } },
					position: targetPosition
				};
536 537 538 539
			})).done(() => {
				this.editorGroupService.focusGroup(targetPosition);
				window.focus();
			}, errors.onUnexpectedError);
540 541 542
		}
	}

543 544 545 546 547
	private isMoveOperation(e: DragEvent, source: IEditorGroup, target: IEditorGroup) {
		const isCopy = (e.ctrlKey && !isMacintosh) || (e.altKey && isMacintosh);

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

549 550
	protected getContextMenuActions(identifier: IEditorIdentifier): IAction[] {
		const actions = super.getContextMenuActions(identifier);
B
Benjamin Pasero 已提交
551
		const {editor, group} = identifier;
B
Benjamin Pasero 已提交
552

553
		// Actions: For active editor
B
Benjamin Pasero 已提交
554
		if (group.isActive(editor)) {
555
			const editorActions = this.getEditorActions(identifier);
556 557 558
			if (editorActions.primary.length) {
				actions.push(new Separator(), ...prepareActions(editorActions.primary));
			}
559

560 561 562
			if (editorActions.secondary.length) {
				actions.push(new Separator(), ...prepareActions(editorActions.secondary));
			}
563 564 565
		}

		return actions;
B
Benjamin Pasero 已提交
566 567
	}

B
wip  
Benjamin Pasero 已提交
568 569 570
	public dispose(): void {
		super.dispose();

B
Benjamin Pasero 已提交
571
		// Toolbar
B
wip  
Benjamin Pasero 已提交
572 573 574
		this.groupActionsToolbar.dispose();
	}
}