tabsTitleControl.ts 19.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 {TPromise} from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
10
import nls = require('vs/nls');
B
Benjamin Pasero 已提交
11
import {IAction} from 'vs/base/common/actions';
12
import {prepareActions} from 'vs/workbench/browser/actionBarRegistry';
B
wip  
Benjamin Pasero 已提交
13
import arrays = require('vs/base/common/arrays');
B
Benjamin Pasero 已提交
14
import errors = require('vs/base/common/errors');
15
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
16
import DOM = require('vs/base/browser/dom');
17
import {isMacintosh} from 'vs/base/common/platform';
B
wip  
Benjamin Pasero 已提交
18
import {Builder, $} from 'vs/base/browser/builder';
B
Benjamin Pasero 已提交
19
import {MIME_BINARY} from 'vs/base/common/mime';
20
import {Position} from 'vs/platform/editor/common/editor';
21
import {IEditorGroup, IEditorIdentifier, asFileEditorInput, EditorOptions} from 'vs/workbench/common/editor';
B
wip  
Benjamin Pasero 已提交
22
import {ToolBar} from 'vs/base/browser/ui/toolbar/toolbar';
B
Benjamin Pasero 已提交
23
import {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
B
Benjamin Pasero 已提交
24
import {CommonKeybindings as Kb} from 'vs/base/common/keyCodes';
B
Benjamin Pasero 已提交
25 26
import {ActionBar, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {StandardMouseEvent} from 'vs/base/browser/mouseEvent';
B
wip  
Benjamin Pasero 已提交
27 28 29 30 31 32 33 34
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEditorGroupService} from 'vs/workbench/services/group/common/groupService';
import {IMessageService} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {TitleControl} from 'vs/workbench/browser/parts/editor/titleControl';
B
Benjamin Pasero 已提交
35
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
36 37
import {ScrollableElement} from 'vs/base/browser/ui/scrollbar/scrollableElement';
import {ScrollbarVisibility} from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
B
wip  
Benjamin Pasero 已提交
38 39

export class TabsTitleControl extends TitleControl {
B
Benjamin Pasero 已提交
40 41 42

	private static draggedEditor: IEditorIdentifier;

B
Benjamin Pasero 已提交
43 44 45
	private titleContainer: HTMLElement;
	private tabsContainer: HTMLElement;
	private activeTab: HTMLElement;
46
	private scrollbar: ScrollableElement;
B
wip  
Benjamin Pasero 已提交
47 48

	private groupActionsToolbar: ToolBar;
B
Benjamin Pasero 已提交
49
	private tabDisposeables: IDisposable[];
B
wip  
Benjamin Pasero 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

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

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

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

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

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

		this.groupActionsToolbar.context = { group };
	}

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

80
		// Tabs Container
B
Benjamin Pasero 已提交
81 82
		this.tabsContainer = document.createElement('div');
		DOM.addClass(this.tabsContainer, 'tabs-container');
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

		// Custom Scrollbar
		this.scrollbar = new ScrollableElement(this.tabsContainer, {
			horizontal: ScrollbarVisibility.Auto,
			vertical: ScrollbarVisibility.Hidden,
			scrollYToX: true,
			useShadows: false,
			canUseTranslate3d: true,
			horizontalScrollbarSize: 3
		});
		this.tabsContainer.style.overflow = 'scroll'; // custom scrollbar is eager on removing this style but we want it for DND scroll feedback

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

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

B
Benjamin Pasero 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		// Drag over
		this.toDispose.push(DOM.addDisposableListener(this.tabsContainer, DOM.EventType.DRAG_OVER, (e: DragEvent) => {
			const target = e.target;
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
				DOM.addClass(this.tabsContainer, 'dropfeedback');
			}
		}));

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

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

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

B
Benjamin Pasero 已提交
123
			const target = e.target;
B
Benjamin Pasero 已提交
124
			if (target instanceof HTMLElement && target.className.indexOf('tabs-container') === 0) {
B
Benjamin Pasero 已提交
125 126
				const group = this.context;
				if (group) {
127 128 129 130
					const targetPosition = this.stacks.positionOfGroup(group);
					const targetIndex = group.count;

					// Local DND
B
Benjamin Pasero 已提交
131
					if (TabsTitleControl.draggedEditor) {
B
Benjamin Pasero 已提交
132 133
						e.preventDefault();

B
Benjamin Pasero 已提交
134
						const sourcePosition = this.stacks.positionOfGroup(TabsTitleControl.draggedEditor.group);
B
Benjamin Pasero 已提交
135

136 137 138 139 140 141 142 143 144
						// Move editor to target position and index
						if (this.isMoveOperation(e, TabsTitleControl.draggedEditor.group, group)) {
							this.editorGroupService.moveEditor(TabsTitleControl.draggedEditor.editor, sourcePosition, targetPosition, targetIndex);
						}

						// Copy: just open editor at target index
						else {
							this.editorService.openEditor(TabsTitleControl.draggedEditor.editor, EditorOptions.create({ pinned: true, index: targetIndex }), targetPosition).done(null, errors.onUnexpectedError);
						}
B
Benjamin Pasero 已提交
145
					}
146 147 148 149 150

					// External DND
					else {
						this.handleExternalDrop(e, targetPosition, targetIndex);
					}
B
Benjamin Pasero 已提交
151 152 153 154
				}
			}
		}));

B
Benjamin Pasero 已提交
155
		// Group Actions
B
Benjamin Pasero 已提交
156 157 158 159
		const groupActionsContainer = document.createElement('div');
		DOM.addClass(groupActionsContainer, 'group-actions');
		this.titleContainer.appendChild(groupActionsContainer);
		this.groupActionsToolbar = this.doCreateToolbar($(groupActionsContainer));
B
wip  
Benjamin Pasero 已提交
160 161
	}

B
Benjamin Pasero 已提交
162 163 164 165
	public allowDragging(element: HTMLElement): boolean {
		return (element.className === 'tabs-container');
	}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	protected doUpdate(): void {
		if (!this.context) {
			return;
		}

		const group = this.context;

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

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

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

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

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

		// Ensure active tab is always revealed
		this.layout();
215 216 217
	}

	protected doRefresh(): void {
B
wip  
Benjamin Pasero 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		if (!this.context) {
			return;
		}

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

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

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

233
		// Refresh Group Actions Toolbar
B
wip  
Benjamin Pasero 已提交
234
		const groupActions = this.getGroupActions(group);
B
Benjamin Pasero 已提交
235 236
		const primaryGroupActions = groupActions.primary;
		const secondaryGroupActions = groupActions.secondary;
B
wip  
Benjamin Pasero 已提交
237 238 239 240 241 242 243 244
		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 已提交
245 246 247

		// Refresh Tabs
		this.refreshTabs(group);
248 249 250

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

B
Benjamin Pasero 已提交
253 254 255
	private refreshTabs(group: IEditorGroup): void {

		// Empty container first
B
Benjamin Pasero 已提交
256 257 258 259 260
		DOM.clearNode(this.tabsContainer);
		dispose(this.tabDisposeables);
		this.tabDisposeables = [];

		const tabContainers: HTMLElement[] = [];
B
Benjamin Pasero 已提交
261 262 263

		// Add a tab for each opened editor
		this.context.getEditors().forEach(editor => {
B
Benjamin Pasero 已提交
264
			const tabContainer = document.createElement('div');
B
Benjamin Pasero 已提交
265
			tabContainer.draggable = true;
B
Benjamin Pasero 已提交
266
			tabContainer.tabIndex = 0;
B
Benjamin Pasero 已提交
267 268
			DOM.addClass(tabContainer, 'tab monaco-editor-background');
			tabContainers.push(tabContainer);
B
Benjamin Pasero 已提交
269

B
Benjamin Pasero 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
			// Tab Label Container
			const tabLabelContainer = document.createElement('div');
			tabContainer.appendChild(tabLabelContainer);
			DOM.addClass(tabLabelContainer, 'tab-label');

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

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

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

			this.tabDisposeables.push(bar);
B
Benjamin Pasero 已提交
290 291 292

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

B
Benjamin Pasero 已提交
295 296
		// Add to tabs container
		tabContainers.forEach(tab => this.tabsContainer.appendChild(tab));
297 298 299 300 301 302 303
	}

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

304 305 306 307 308 309 310 311 312
		const visibleContainerWidth = this.tabsContainer.offsetWidth;
		const totalContainerWidth = this.tabsContainer.scrollWidth;

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

B
Benjamin Pasero 已提交
313
		// Always reveal the active one
B
Benjamin Pasero 已提交
314 315 316
		const containerScrollPosX = this.tabsContainer.scrollLeft;
		const activeTabPosX = this.activeTab.offsetLeft;
		const activeTabWidth = this.activeTab.offsetWidth;
B
Benjamin Pasero 已提交
317 318

		// Tab is overflowing to the right: Scroll minimally until the element is fully visible to the right
319 320 321 322
		if (containerScrollPosX + visibleContainerWidth < activeTabPosX + activeTabWidth) {
			this.scrollbar.updateState({
				scrollLeft: containerScrollPosX + ((activeTabPosX + activeTabWidth) /* right corner of tab */ - (containerScrollPosX + visibleContainerWidth) /* right corner of view port */)
			});
B
Benjamin Pasero 已提交
323 324 325 326
		}

		// Tab is overlflowng to the left: Scroll it into view to the left
		else if (containerScrollPosX > activeTabPosX) {
327 328 329
			this.scrollbar.updateState({
				scrollLeft: this.activeTab.offsetLeft
			});
B
Benjamin Pasero 已提交
330
		}
331 332

		// Update enablement of certain actions that depend on overflow
333
		const isOverflowing = (totalContainerWidth > visibleContainerWidth);
334 335 336
		this.showEditorsOfLeftGroup.enabled = isOverflowing;
		this.showEditorsOfCenterGroup.enabled = isOverflowing;
		this.showEditorsOfRightGroup.enabled = isOverflowing;
B
Benjamin Pasero 已提交
337 338
	}

B
Benjamin Pasero 已提交
339
	private hookTabListeners(tab: HTMLElement, identifier: IEditorIdentifier): void {
B
Benjamin Pasero 已提交
340 341
		const {editor, group} = identifier;
		const position = this.stacks.positionOfGroup(group);
B
Benjamin Pasero 已提交
342 343

		// Open on Click
B
Benjamin Pasero 已提交
344
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.MOUSE_DOWN, (e: MouseEvent) => {
345
			if (e.button === 0 /* Left Button */ && !DOM.findParentWithClass(<any>e.target || e.srcElement, 'monaco-action-bar', 'tab')) {
346 347 348 349 350 351 352 353 354 355
				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);

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

B
Benjamin Pasero 已提交
359
		// Keyboard accessibility
B
Benjamin Pasero 已提交
360 361
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
362
			let handled = false;
B
Benjamin Pasero 已提交
363 364

			// Run action on Enter/Space
B
Benjamin Pasero 已提交
365
			if (event.equals(Kb.ENTER) || event.equals(Kb.SPACE)) {
B
Benjamin Pasero 已提交
366
				handled = true;
B
Benjamin Pasero 已提交
367
				this.editorService.openEditor(editor, null, position).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
368 369
			}

B
Benjamin Pasero 已提交
370
			// Navigate in editors
B
Benjamin Pasero 已提交
371
			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 已提交
372
				const index = group.indexOf(editor);
B
Benjamin Pasero 已提交
373

B
Benjamin Pasero 已提交
374
				let targetIndex: number;
B
Benjamin Pasero 已提交
375
				if (event.equals(Kb.LEFT_ARROW) || event.equals(Kb.UP_ARROW)) {
B
Benjamin Pasero 已提交
376
					targetIndex = index - 1;
B
Benjamin Pasero 已提交
377
				} else if (event.equals(Kb.RIGHT_ARROW) || event.equals(Kb.DOWN_ARROW)) {
B
Benjamin Pasero 已提交
378
					targetIndex = index + 1;
B
Benjamin Pasero 已提交
379
				} else if (event.equals(Kb.HOME)) {
B
Benjamin Pasero 已提交
380 381 382 383 384
					targetIndex = 0;
				} else {
					targetIndex = group.count - 1;
				}

B
Benjamin Pasero 已提交
385 386 387 388 389 390 391
				const target = group.getEditor(targetIndex);
				if (target) {
					handled = true;
					this.editorService.openEditor(target, EditorOptions.create({ preserveFocus: true }), position).done(null, errors.onUnexpectedError);
					(<HTMLElement>this.tabsContainer.childNodes[targetIndex]).focus();
				}
			}
B
Benjamin Pasero 已提交
392

B
Benjamin Pasero 已提交
393
			if (handled) {
B
Benjamin Pasero 已提交
394 395 396 397 398
				event.preventDefault();
				event.stopPropagation();
			}
		}));

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

B
Benjamin Pasero 已提交
403
			this.editorGroupService.pinEditor(position, editor);
B
Benjamin Pasero 已提交
404
		}));
B
Benjamin Pasero 已提交
405

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

B
Benjamin Pasero 已提交
410
			let anchor: HTMLElement | { x: number, y: number } = tab;
B
Benjamin Pasero 已提交
411 412 413 414 415 416 417
			if (e instanceof MouseEvent) {
				const event = new StandardMouseEvent(e);
				anchor = { x: event.posx, y: event.posy };
			}

			this.contextMenuService.showContextMenu({
				getAnchor: () => anchor,
B
Benjamin Pasero 已提交
418 419
				getActions: () => TPromise.as(this.getTabActions(identifier)),
				getActionsContext: () => identifier,
B
Benjamin Pasero 已提交
420 421 422 423 424 425 426 427 428
				getKeyBinding: (action) => {
					var opts = this.keybindingService.lookupKeybindings(action.id);
					if (opts.length > 0) {
						return opts[0]; // only take the first one
					}

					return null;
				}
			});
B
Benjamin Pasero 已提交
429
		}));
B
Benjamin Pasero 已提交
430 431 432 433

		// Drag start
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_START, (e: DragEvent) => {
			DOM.addClass(tab, 'dragged');
B
Benjamin Pasero 已提交
434
			TabsTitleControl.draggedEditor = { editor, group };
435
			e.dataTransfer.effectAllowed = 'copyMove';
B
Benjamin Pasero 已提交
436 437 438 439 440 441

			// 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 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
		}));

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

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

		// Drag end
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DRAG_END, (e: DragEvent) => {
			DOM.removeClass(tab, 'dragged');
			DOM.removeClass(tab, 'dropfeedback');
B
Benjamin Pasero 已提交
458
			TabsTitleControl.draggedEditor = void 0;
B
Benjamin Pasero 已提交
459 460 461 462
		}));

		// Drop
		this.tabDisposeables.push(DOM.addDisposableListener(tab, DOM.EventType.DROP, (e: DragEvent) => {
463 464 465 466
			const targetPosition = this.stacks.positionOfGroup(group);
			const targetIndex = group.indexOf(editor);

			// Local DND
B
Benjamin Pasero 已提交
467
			if (TabsTitleControl.draggedEditor) {
B
Benjamin Pasero 已提交
468 469
				e.preventDefault();

B
Benjamin Pasero 已提交
470
				const sourcePosition = this.stacks.positionOfGroup(TabsTitleControl.draggedEditor.group);
B
Benjamin Pasero 已提交
471 472

				// Move editor to target position and index
473 474 475 476 477 478 479 480
				if (this.isMoveOperation(e, TabsTitleControl.draggedEditor.group, group)) {
					this.editorGroupService.moveEditor(TabsTitleControl.draggedEditor.editor, sourcePosition, targetPosition, targetIndex);
				}

				// Copy: just open editor at target index
				else {
					this.editorService.openEditor(TabsTitleControl.draggedEditor.editor, EditorOptions.create({ pinned: true, index: targetIndex }), targetPosition).done(null, errors.onUnexpectedError);
				}
B
Benjamin Pasero 已提交
481
			}
482 483 484 485 486

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

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	private handleExternalDrop(e: DragEvent, targetPosition: Position, targetIndex: number): void {
		let resources: URI[] = [];

		if (e.dataTransfer.types.length > 0) {

			// Check for in-app DND
			const rawData = e.dataTransfer.getData(e.dataTransfer.types[0]);
			if (rawData) {
				const resource = URI.parse(rawData);
				if (resource.scheme === 'file' || resource.scheme === 'untitled') {
					resources.push(resource);
				}
			}

			// Check for external app DND
			else if (e.dataTransfer && e.dataTransfer.files) {
				let thepaths: string[] = [];
				for (let i = 0; i < e.dataTransfer.files.length; i++) {
					if (e.dataTransfer.files[i] && (e.dataTransfer.files[i]).path) {
						thepaths.push(e.dataTransfer.files[i].path);
					}
				}

				resources = thepaths.map(p => URI.file(p));
			}
		}

		// Open resources if found
		if (resources.length) {
B
Benjamin Pasero 已提交
519
			DOM.EventHelper.stop(e, true);
520 521 522 523 524 525 526 527 528 529

			this.editorService.openEditors(resources.map(resource => {
				return {
					input: { resource, options: { pinned: true, index: targetIndex } },
					position: targetPosition
				};
			})).done(() => this.editorGroupService.focusGroup(targetPosition), errors.onUnexpectedError);
		}
	}

530 531 532 533 534
	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 已提交
535

B
Benjamin Pasero 已提交
536
	private getTabActions(identifier: IEditorIdentifier): IAction[] {
B
Benjamin Pasero 已提交
537
		const {editor, group} = identifier;
B
Benjamin Pasero 已提交
538 539

		// Enablement
B
Benjamin Pasero 已提交
540 541 542
		this.closeOtherEditorsAction.enabled = group.count > 1;
		this.pinEditorAction.enabled = !group.isPinned(editor);
		this.closeRightEditorsAction.enabled = group.indexOf(editor) !== group.count - 1;
B
Benjamin Pasero 已提交
543

544
		// Actions: For all editors
B
Benjamin Pasero 已提交
545
		const actions: IAction[] = [
B
Benjamin Pasero 已提交
546 547
			this.closeEditorAction,
			this.closeOtherEditorsAction,
B
Benjamin Pasero 已提交
548
			this.closeRightEditorsAction,
B
Benjamin Pasero 已提交
549
			new Separator(),
550
			this.pinEditorAction,
B
Benjamin Pasero 已提交
551
		];
552

553
		// Actions: For active editor
B
Benjamin Pasero 已提交
554 555
		if (group.isActive(editor)) {
			const editorActions = this.getEditorActions(group);
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();
	}
}