noTabsTitleControl.ts 4.6 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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/notabstitle';
import errors = require('vs/base/common/errors');
10
import { IEditorGroup, toResource } from 'vs/workbench/common/editor';
B
Benjamin Pasero 已提交
11
import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
12 13
import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl';
import { EditorLabel } from 'vs/workbench/browser/labels';
14
import { Verbosity } from 'vs/platform/editor/common/editor';
B
Benjamin Pasero 已提交
15 16

export class NoTabsTitleControl extends TitleControl {
17
	private titleContainer: HTMLElement;
18
	private editorLabel: EditorLabel;
B
Benjamin Pasero 已提交
19 20 21 22 23 24 25

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

		this.editorActionsToolbar.context = { group };
	}

26
	public create(parent: HTMLElement): void {
27
		super.create(parent);
28

29
		this.titleContainer = parent;
B
Benjamin Pasero 已提交
30 31

		// Pin on double click
32
		this.toDispose.push(DOM.addDisposableListener(this.titleContainer, DOM.EventType.DBLCLICK, (e: MouseEvent) => this.onTitleDoubleClick(e)));
33

B
Benjamin Pasero 已提交
34
		// Detect mouse click
B
Benjamin Pasero 已提交
35
		this.toDispose.push(DOM.addDisposableListener(this.titleContainer, DOM.EventType.CLICK, (e: MouseEvent) => this.onTitleClick(e)));
B
Benjamin Pasero 已提交
36

37
		// Editor Label
B
Benjamin Pasero 已提交
38
		this.editorLabel = this.instantiationService.createInstance(EditorLabel, this.titleContainer, void 0);
39
		this.toDispose.push(this.editorLabel);
40 41
		this.toDispose.push(DOM.addDisposableListener(this.editorLabel.labelElement, DOM.EventType.CLICK, (e: MouseEvent) => this.onTitleLabelClick(e)));
		this.toDispose.push(DOM.addDisposableListener(this.editorLabel.descriptionElement, DOM.EventType.CLICK, (e: MouseEvent) => this.onTitleLabelClick(e)));
B
Benjamin Pasero 已提交
42 43

		// Right Actions Container
44 45 46
		const actionsContainer = document.createElement('div');
		DOM.addClass(actionsContainer, 'title-actions');
		this.titleContainer.appendChild(actionsContainer);
47

48 49 50
		// Editor actions toolbar
		this.createEditorActionsToolBar(actionsContainer);

51
		// Context Menu
52
		this.toDispose.push(DOM.addDisposableListener(this.titleContainer, DOM.EventType.CONTEXT_MENU, (e: Event) => this.onContextMenu({ group: this.context, editor: this.context.activeEditor }, e, this.titleContainer)));
B
Benjamin Pasero 已提交
53 54
	}

55 56 57
	private onTitleLabelClick(e: MouseEvent): void {
		DOM.EventHelper.stop(e, false);
		if (!this.dragged) {
58
			setTimeout(() => this.quickOpenService.show()); // delayed to let the onTitleClick() come first which can cause a focus change which can close quick open
59 60 61 62 63
		}
	}

	private onTitleDoubleClick(e: MouseEvent): void {
		DOM.EventHelper.stop(e);
B
Benjamin Pasero 已提交
64 65 66 67 68 69
		if (!this.context) {
			return;
		}

		const group = this.context;

70
		this.editorGroupService.pinEditor(group, group.activeEditor);
B
Benjamin Pasero 已提交
71 72 73 74 75 76 77 78 79 80 81
	}

	private onTitleClick(e: MouseEvent): void {
		if (!this.context) {
			return;
		}

		const group = this.context;

		// Close editor on middle mouse click
		if (e.button === 1 /* Middle Button */) {
82
			this.closeEditorAction.run({ group, editor: group.activeEditor }).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
83 84 85
		}

		// Focus editor group unless click on toolbar
B
Benjamin Pasero 已提交
86
		else if (this.stacks.groups.length === 1 && !DOM.isAncestor((e.target || e.srcElement) as HTMLElement, this.editorActionsToolbar.getContainer().getHTMLElement())) {
87
			this.editorGroupService.focusGroup(group);
B
Benjamin Pasero 已提交
88 89 90
		}
	}

91
	protected doRefresh(): void {
B
Benjamin Pasero 已提交
92
		const group = this.context;
93
		const editor = group && group.activeEditor;
B
Benjamin Pasero 已提交
94
		if (!editor) {
95
			this.editorLabel.clear();
96
			this.clearEditorActionsToolbar();
B
Benjamin Pasero 已提交
97 98 99 100 101 102 103 104 105

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

		const isPinned = group.isPinned(group.activeEditor);
		const isActive = this.stacks.isActive(group);

		// Activity state
		if (isActive) {
106
			DOM.addClass(this.titleContainer, 'active');
B
Benjamin Pasero 已提交
107
		} else {
108
			DOM.removeClass(this.titleContainer, 'active');
B
Benjamin Pasero 已提交
109 110
		}

111 112 113 114 115 116 117
		// Dirty state
		if (editor.isDirty()) {
			DOM.addClass(this.titleContainer, 'dirty');
		} else {
			DOM.removeClass(this.titleContainer, 'dirty');
		}

118
		// Editor Label
119
		const resource = toResource(editor, { supportSideBySide: true });
B
Benjamin Pasero 已提交
120 121
		const name = editor.getName() || '';
		const description = isActive ? (editor.getDescription() || '') : '';
122 123 124
		let title = editor.getTitle(Verbosity.LONG);
		if (description === title) {
			title = ''; // dont repeat what is already shown
B
Benjamin Pasero 已提交
125 126
		}

127
		this.editorLabel.setLabel({ name, description, resource }, { title, italic: !isPinned, extraClasses: ['title-label'] });
B
Benjamin Pasero 已提交
128 129

		// Update Editor Actions Toolbar
130
		this.updateEditorActionsToolbar();
B
Benjamin Pasero 已提交
131 132
	}
}