noTabsTitleControl.ts 5.8 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
import { TitleControl } from 'vs/workbench/browser/parts/editor/titleControl';
B
Benjamin Pasero 已提交
13
import { ResourceLabel } from 'vs/workbench/browser/labels';
14
import { Verbosity } from 'vs/platform/editor/common/editor';
15
import { TAB_ACTIVE_FOREGROUND, TAB_UNFOCUSED_ACTIVE_FOREGROUND } from 'vs/workbench/common/theme';
16
import { EventType as TouchEventType, GestureEvent, Gesture } from 'vs/base/browser/touch';
B
Benjamin Pasero 已提交
17 18

export class NoTabsTitleControl extends TitleControl {
19
	private titleContainer: HTMLElement;
B
Benjamin Pasero 已提交
20
	private editorLabel: ResourceLabel;
21
	private titleTouchSupport: Gesture;
B
Benjamin Pasero 已提交
22 23 24 25 26 27 28

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

		this.editorActionsToolbar.context = { group };
	}

29
	public create(parent: HTMLElement): void {
30
		super.create(parent);
31

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

34 35 36
		// Gesture Support
		this.titleTouchSupport = new Gesture(this.titleContainer);

B
Benjamin Pasero 已提交
37
		// Pin on double click
38
		this.toUnbind.push(DOM.addDisposableListener(this.titleContainer, DOM.EventType.DBLCLICK, (e: MouseEvent) => this.onTitleDoubleClick(e)));
39

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

43 44 45
		// Detect touch
		this.toUnbind.push(DOM.addDisposableListener(this.titleContainer, TouchEventType.Tap, (e: GestureEvent) => this.onTitleClick(e)));

46
		// Editor Label
B
Benjamin Pasero 已提交
47
		this.editorLabel = this.instantiationService.createInstance(ResourceLabel, this.titleContainer, void 0);
48
		this.toUnbind.push(this.editorLabel);
B
Benjamin Pasero 已提交
49
		this.toUnbind.push(this.editorLabel.onClick(e => this.onTitleLabelClick(e)));
B
Benjamin Pasero 已提交
50 51

		// Right Actions Container
52 53 54
		const actionsContainer = document.createElement('div');
		DOM.addClass(actionsContainer, 'title-actions');
		this.titleContainer.appendChild(actionsContainer);
55

56 57 58
		// Editor actions toolbar
		this.createEditorActionsToolBar(actionsContainer);

59
		// Context Menu
60
		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)));
61
		this.toUnbind.push(DOM.addDisposableListener(this.titleContainer, TouchEventType.Contextmenu, (e: Event) => this.onContextMenu({ group: this.context, editor: this.context.activeEditor }, e, this.titleContainer)));
B
Benjamin Pasero 已提交
62 63
	}

64 65 66
	private onTitleLabelClick(e: MouseEvent): void {
		DOM.EventHelper.stop(e, false);
		if (!this.dragged) {
67
			setTimeout(() => this.quickOpenService.show()); // delayed to let the onTitleClick() come first which can cause a focus change which can close quick open
68 69 70 71 72
		}
	}

	private onTitleDoubleClick(e: MouseEvent): void {
		DOM.EventHelper.stop(e);
B
Benjamin Pasero 已提交
73 74 75 76 77 78
		if (!this.context) {
			return;
		}

		const group = this.context;

79
		this.editorGroupService.pinEditor(group, group.activeEditor);
B
Benjamin Pasero 已提交
80 81
	}

82
	private onTitleClick(e: MouseEvent | GestureEvent): void {
B
Benjamin Pasero 已提交
83 84 85 86 87 88 89
		if (!this.context) {
			return;
		}

		const group = this.context;

		// Close editor on middle mouse click
90
		if (e instanceof MouseEvent && e.button === 1 /* Middle Button */) {
91
			this.closeEditorAction.run({ group, editor: group.activeEditor }).done(null, errors.onUnexpectedError);
B
Benjamin Pasero 已提交
92 93 94
		}

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

100
	protected doRefresh(): void {
B
Benjamin Pasero 已提交
101
		const group = this.context;
102
		const editor = group && group.activeEditor;
B
Benjamin Pasero 已提交
103
		if (!editor) {
104
			this.editorLabel.clear();
105
			this.clearEditorActionsToolbar();
B
Benjamin Pasero 已提交
106 107 108 109 110 111 112 113 114

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

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

		// Activity state
		if (isActive) {
115
			DOM.addClass(this.titleContainer, 'active');
B
Benjamin Pasero 已提交
116
		} else {
117
			DOM.removeClass(this.titleContainer, 'active');
B
Benjamin Pasero 已提交
118 119
		}

120 121 122 123 124 125 126
		// Dirty state
		if (editor.isDirty()) {
			DOM.addClass(this.titleContainer, 'dirty');
		} else {
			DOM.removeClass(this.titleContainer, 'dirty');
		}

127
		// Editor Label
128
		const resource = toResource(editor, { supportSideBySide: true });
B
Benjamin Pasero 已提交
129
		const name = editor.getName() || '';
130 131 132 133 134 135 136 137 138

		const labelFormat = this.editorGroupService.getTabOptions().labelFormat;
		let description: string;
		if (labelFormat === 'default' && !isActive) {
			description = ''; // hide description when group is not active and style is 'default'
		} else {
			description = editor.getDescription(this.getVerbosity(labelFormat)) || '';
		}

139 140 141
		let title = editor.getTitle(Verbosity.LONG);
		if (description === title) {
			title = ''; // dont repeat what is already shown
B
Benjamin Pasero 已提交
142 143
		}

144
		this.editorLabel.setLabel({ name, description, resource }, { title, italic: !isPinned, extraClasses: ['title-label'] });
145 146 147 148 149
		if (isActive) {
			this.editorLabel.element.style.color = this.getColor(TAB_ACTIVE_FOREGROUND);
		} else {
			this.editorLabel.element.style.color = this.getColor(TAB_UNFOCUSED_ACTIVE_FOREGROUND);
		}
B
Benjamin Pasero 已提交
150 151

		// Update Editor Actions Toolbar
152
		this.updateEditorActionsToolbar();
B
Benjamin Pasero 已提交
153
	}
154 155 156 157 158 159 160 161

	private getVerbosity(style: string): Verbosity {
		switch (style) {
			case 'short': return Verbosity.SHORT;
			case 'long': return Verbosity.LONG;
			default: return Verbosity.MEDIUM;
		}
	}
162 163 164 165 166 167

	public dispose(): void {
		super.dispose();

		this.titleTouchSupport.dispose();
	}
B
Benjamin Pasero 已提交
168
}