noTabsTitleControl.ts 4.9 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
import { TAB_ACTIVE_GROUP_INACTIVE_FOREGROUND, TAB_ACTIVE_GROUP_ACTIVE_FOREGROUND } from 'vs/workbench/common/theme';
B
Benjamin Pasero 已提交
16 17

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

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

		this.editorActionsToolbar.context = { group };
	}

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

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

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

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

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

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

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

52
		// Context Menu
53
		this.toUnbind.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 已提交
54 55
	}

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

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

		const group = this.context;

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

	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 */) {
83
			this.closeEditorAction.run({ group, editor: group.activeEditor }).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
84 85 86
		}

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

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

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

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

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

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

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

128
		this.editorLabel.setLabel({ name, description, resource }, { title, italic: !isPinned, extraClasses: ['title-label'] });
129
		if (isActive) {
B
Benjamin Pasero 已提交
130
			this.editorLabel.element.style.color = this.getColor(TAB_ACTIVE_GROUP_ACTIVE_FOREGROUND);
131
		} else {
B
Benjamin Pasero 已提交
132
			this.editorLabel.element.style.color = this.getColor(TAB_ACTIVE_GROUP_INACTIVE_FOREGROUND);
133
		}
B
Benjamin Pasero 已提交
134 135

		// Update Editor Actions Toolbar
136
		this.updateEditorActionsToolbar();
B
Benjamin Pasero 已提交
137 138
	}
}