noTabsTitleControl.ts 4.5 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, getResource} from 'vs/workbench/common/editor';
B
Benjamin Pasero 已提交
11 12
import DOM = require('vs/base/browser/dom');
import {TitleControl} from 'vs/workbench/browser/parts/editor/titleControl';
B
Benjamin Pasero 已提交
13
import {EditorLabel} from 'vs/workbench/browser/labels';
B
Benjamin Pasero 已提交
14 15

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

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

		this.editorActionsToolbar.context = { group };
	}

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

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

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

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

36
		// Editor Label
B
Benjamin Pasero 已提交
37
		this.editorLabel = this.instantiationService.createInstance(EditorLabel, this.titleContainer, void 0);
38
		this.toDispose.push(this.editorLabel);
39 40
		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 已提交
41 42

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

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

50
		// Context Menu
51
		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 已提交
52 53
	}

54 55 56 57 58 59 60 61 62
	private onTitleLabelClick(e: MouseEvent): void {
		DOM.EventHelper.stop(e, false);
		if (!this.dragged) {
			this.quickOpenService.show();
		}
	}

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

		const group = this.context;

69
		this.editorGroupService.pinEditor(group, group.activeEditor);
B
Benjamin Pasero 已提交
70 71 72
	}

	private onTitleClick(e: MouseEvent): void {
73
		DOM.EventHelper.stop(e, false);
B
Benjamin Pasero 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
		if (!this.context) {
			return;
		}

		const group = this.context;
		const position = this.stacks.positionOfGroup(group);

		// Close editor on middle mouse click
		if (e.button === 1 /* Middle Button */) {
			this.editorService.closeEditor(position, group.activeEditor).done(null, errors.onUnexpectedError);
		}

		// Focus editor group unless click on toolbar
B
Benjamin Pasero 已提交
87
		else if (this.stacks.groups.length === 1 && !DOM.isAncestor(<any>e.target || e.srcElement, 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 120
		// Editor Label
		const resource = getResource(editor);
B
Benjamin Pasero 已提交
121 122
		const name = editor.getName() || '';
		const description = isActive ? (editor.getDescription() || '') : '';
B
Benjamin Pasero 已提交
123 124 125 126 127
		let verboseDescription = editor.getDescription(true) || '';
		if (description === verboseDescription) {
			verboseDescription = ''; // dont repeat what is already shown
		}

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

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