titlebarPart.ts 15.3 KB
Newer Older
B
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/titlebarpart';
B
Benjamin Pasero 已提交
9
import { TPromise } from 'vs/base/common/winjs.base';
10
import { Builder, $ } from 'vs/base/browser/builder';
B
Benjamin Pasero 已提交
11
import * as paths from 'vs/base/common/paths';
B
Benjamin Pasero 已提交
12
import { Part } from 'vs/workbench/browser/part';
13
import { ITitleService, ITitleProperties } from 'vs/workbench/services/title/common/titleService';
B
Benjamin Pasero 已提交
14
import { getZoomFactor } from 'vs/base/browser/browser';
B
Benjamin Pasero 已提交
15 16 17 18 19
import { IWindowService, IWindowsService } from 'vs/platform/windows/common/windows';
import * as errors from 'vs/base/common/errors';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { IAction, Action } from 'vs/base/common/actions';
20
import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
21
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
22
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
23
import * as nls from 'vs/nls';
24
import * as labels from 'vs/base/common/labels';
B
Benjamin Pasero 已提交
25
import { EditorInput, toResource, Verbosity } from 'vs/workbench/common/editor';
26
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
27
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
28
import { IThemeService } from 'vs/platform/theme/common/themeService';
29
import { TITLE_BAR_ACTIVE_BACKGROUND, TITLE_BAR_ACTIVE_FOREGROUND, TITLE_BAR_INACTIVE_FOREGROUND, TITLE_BAR_INACTIVE_BACKGROUND, TITLE_BAR_BORDER } from 'vs/workbench/common/theme';
R
Ryan Adolf 已提交
30
import { isMacintosh, isWindows } from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
31
import URI from 'vs/base/common/uri';
R
Ryan Adolf 已提交
32
import { Color } from 'vs/base/common/color';
B
Benjamin Pasero 已提交
33
import { trim } from 'vs/base/common/strings';
34
import { addDisposableListener, EventType, EventHelper, Dimension } from 'vs/base/browser/dom';
B
Benjamin Pasero 已提交
35 36 37 38 39

export class TitlebarPart extends Part implements ITitleService {

	public _serviceBrand: any;

40
	private static readonly NLS_UNSUPPORTED = nls.localize('patchedWindowTitle', "[Unsupported]");
41
	private static readonly NLS_USER_IS_ADMIN = isWindows ? nls.localize('userIsAdmin', "[Administrator]") : nls.localize('userIsSudo', "[Superuser]");
42 43 44
	private static readonly NLS_EXTENSION_HOST = nls.localize('devExtensionWindowTitlePrefix', "[Extension Development Host]");
	private static readonly TITLE_DIRTY = '\u25cf ';
	private static readonly TITLE_SEPARATOR = isMacintosh ? '' : ' - '; // macOS uses special - separator
45

B
Benjamin Pasero 已提交
46 47 48 49
	private titleContainer: Builder;
	private title: Builder;
	private pendingTitle: string;
	private initialTitleFontSize: number;
B
Benjamin Pasero 已提交
50
	private representedFileName: string;
B
Benjamin Pasero 已提交
51

B
Benjamin Pasero 已提交
52 53
	private isInactive: boolean;

54
	private properties: ITitleProperties;
55 56
	private activeEditorListeners: IDisposable[];

57 58
	constructor(
		id: string,
B
Benjamin Pasero 已提交
59 60
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IWindowService private windowService: IWindowService,
61 62
		@IConfigurationService private configurationService: IConfigurationService,
		@IWindowsService private windowsService: IWindowsService,
B
Benjamin Pasero 已提交
63
		@IEditorService private editorService: IEditorService,
64
		@IEnvironmentService private environmentService: IEnvironmentService,
B
Benjamin Pasero 已提交
65
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
B
Benjamin Pasero 已提交
66
		@IThemeService themeService: IThemeService
67
	) {
B
Benjamin Pasero 已提交
68
		super(id, { hasTitle: false }, themeService);
69

70
		this.properties = { isPure: true, isAdmin: false };
71 72
		this.activeEditorListeners = [];

73 74 75 76
		this.registerListeners();
	}

	private registerListeners(): void {
77 78
		this.toUnbind.push(addDisposableListener(window, EventType.BLUR, () => this.onBlur()));
		this.toUnbind.push(addDisposableListener(window, EventType.FOCUS, () => this.onFocus()));
79
		this.toUnbind.push(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChanged(e)));
B
Benjamin Pasero 已提交
80
		this.toUnbind.push(this.editorService.onDidActiveEditorChange(() => this.onActiveEditorChange()));
81 82 83
		this.toUnbind.push(this.contextService.onDidChangeWorkspaceFolders(() => this.setTitle(this.getWindowTitle())));
		this.toUnbind.push(this.contextService.onDidChangeWorkbenchState(() => this.setTitle(this.getWindowTitle())));
		this.toUnbind.push(this.contextService.onDidChangeWorkspaceName(() => this.setTitle(this.getWindowTitle())));
84 85
	}

B
Benjamin Pasero 已提交
86 87 88 89 90 91 92 93 94 95
	private onBlur(): void {
		this.isInactive = true;
		this.updateStyles();
	}

	private onFocus(): void {
		this.isInactive = false;
		this.updateStyles();
	}

96 97
	private onConfigurationChanged(event: IConfigurationChangeEvent): void {
		if (event.affectsConfiguration('window.title')) {
98 99 100 101
			this.setTitle(this.getWindowTitle());
		}
	}

B
Benjamin Pasero 已提交
102
	private onActiveEditorChange(): void {
103 104 105 106 107 108 109 110 111

		// Dispose old listeners
		dispose(this.activeEditorListeners);
		this.activeEditorListeners = [];

		// Calculate New Window Title
		this.setTitle(this.getWindowTitle());

		// Apply listener for dirty and label changes
B
Benjamin Pasero 已提交
112 113 114
		const activeEditor = this.editorService.activeEditor;
		if (activeEditor instanceof EditorInput) {
			this.activeEditorListeners.push(activeEditor.onDidChangeDirty(() => {
115 116 117
				this.setTitle(this.getWindowTitle());
			}));

B
Benjamin Pasero 已提交
118
			this.activeEditorListeners.push(activeEditor.onDidChangeLabel(() => {
119 120 121
				this.setTitle(this.getWindowTitle());
			}));
		}
B
Benjamin Pasero 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135

		// Represented File Name
		this.updateRepresentedFilename();
	}

	private updateRepresentedFilename(): void {
		const file = toResource(this.editorService.activeEditor, { supportSideBySide: true, filter: 'file' });
		const path = file ? file.fsPath : '';

		// Apply to window
		this.windowService.setRepresentedFilename(path);

		// Keep for context menu
		this.representedFileName = path;
136 137 138 139
	}

	private getWindowTitle(): string {
		let title = this.doGetWindowTitle();
B
Benjamin Pasero 已提交
140
		if (!trim(title)) {
141 142 143
			title = this.environmentService.appNameLong;
		}

144 145 146 147 148
		if (this.properties.isAdmin) {
			title = `${title} ${TitlebarPart.NLS_USER_IS_ADMIN}`;
		}

		if (!this.properties.isPure) {
149 150 151 152 153 154 155 156 157 158 159
			title = `${title} ${TitlebarPart.NLS_UNSUPPORTED}`;
		}

		// Extension Development Host gets a special title to identify itself
		if (this.environmentService.isExtensionDevelopment) {
			title = `${TitlebarPart.NLS_EXTENSION_HOST} - ${title}`;
		}

		return title;
	}

160 161 162 163 164 165 166 167 168 169
	public setTitleOffset(offset: number) {
		if (this.title) {
			if (offset) {
				this.title.style('padding-left', offset + 'px');
			} else {
				this.title.style('padding-left', null);
			}
		}
	}

170 171 172 173 174 175 176 177 178 179 180 181
	public updateProperties(properties: ITitleProperties): void {
		const isAdmin = typeof properties.isAdmin === 'boolean' ? properties.isAdmin : this.properties.isAdmin;
		const isPure = typeof properties.isPure === 'boolean' ? properties.isPure : this.properties.isPure;

		if (isAdmin !== this.properties.isAdmin || isPure !== this.properties.isPure) {
			this.properties.isAdmin = isAdmin;
			this.properties.isPure = isPure;

			this.setTitle(this.getWindowTitle());
		}
	}

182 183 184
	/**
	 * Possible template values:
	 *
B
Benjamin Pasero 已提交
185 186 187
	 * {activeEditorLong}: e.g. /Users/Development/myProject/myFolder/myFile.txt
	 * {activeEditorMedium}: e.g. myFolder/myFile.txt
	 * {activeEditorShort}: e.g. myFile.txt
188
	 * {rootName}: e.g. myFolder1, myFolder2, myFolder3
189
	 * {rootPath}: e.g. /Users/Development/myProject
190 191
	 * {folderName}: e.g. myFolder
	 * {folderPath}: e.g. /Users/Development/myFolder
192 193 194 195 196
	 * {appName}: e.g. VS Code
	 * {dirty}: indiactor
	 * {separator}: conditional separator
	 */
	private doGetWindowTitle(): string {
B
Benjamin Pasero 已提交
197
		const editor = this.editorService.activeEditor;
B
Benjamin Pasero 已提交
198
		const workspace = this.contextService.getWorkspace();
199

200
		let root: URI;
201
		if (workspace.configuration) {
202
			root = workspace.configuration;
203 204
		} else if (workspace.folders.length) {
			root = workspace.folders[0].uri;
205 206 207 208
		}

		// Compute folder resource
		// Single Root Workspace: always the root single workspace in this case
209
		// Otherwise: root folder of the currently active file if any
B
Benjamin Pasero 已提交
210
		let folder = this.contextService.getWorkbenchState() === WorkbenchState.FOLDER ? workspace.folders[0] : this.contextService.getWorkspaceFolder(toResource(editor, { supportSideBySide: true }));
211

212
		// Variables
B
Benjamin Pasero 已提交
213 214 215
		const activeEditorShort = editor ? editor.getTitle(Verbosity.SHORT) : '';
		const activeEditorMedium = editor ? editor.getTitle(Verbosity.MEDIUM) : activeEditorShort;
		const activeEditorLong = editor ? editor.getTitle(Verbosity.LONG) : activeEditorMedium;
216
		const rootName = workspace.name;
217
		const rootPath = root ? labels.getPathLabel(root, void 0, this.environmentService) : '';
B
Benjamin Pasero 已提交
218
		const folderName = folder ? folder.name : '';
219
		const folderPath = folder ? labels.getPathLabel(folder.uri, void 0, this.environmentService) : '';
B
Benjamin Pasero 已提交
220
		const dirty = editor && editor.isDirty() ? TitlebarPart.TITLE_DIRTY : '';
221 222
		const appName = this.environmentService.appNameLong;
		const separator = TitlebarPart.TITLE_SEPARATOR;
223
		const titleTemplate = this.configurationService.getValue<string>('window.title');
224

225
		return labels.template(titleTemplate, {
B
Benjamin Pasero 已提交
226 227 228
			activeEditorShort,
			activeEditorLong,
			activeEditorMedium,
229 230
			rootName,
			rootPath,
231 232
			folderName,
			folderPath,
233 234 235 236 237 238
			dirty,
			appName,
			separator: { label: separator }
		});
	}

239
	public createContentArea(parent: HTMLElement): HTMLElement {
B
Benjamin Pasero 已提交
240 241
		this.titleContainer = $(parent);

R
Ryan Adolf 已提交
242
		if (!isMacintosh) {
243 244
			$(this.titleContainer).img({
				class: 'window-appicon',
S
SteVen Batten 已提交
245
				src: paths.join(this.environmentService.appRoot, 'resources/linux/code.png')
246 247
			}).on(EventType.DBLCLICK, (e) => {
				EventHelper.stop(e, true);
248 249
				this.windowService.closeWindow().then(null, errors.onUnexpectedError);
			});
250
		}
S
SteVen Batten 已提交
251

B
Benjamin Pasero 已提交
252 253 254 255 256 257
		// Title
		this.title = $(this.titleContainer).div({ class: 'window-title' });
		if (this.pendingTitle) {
			this.title.text(this.pendingTitle);
		}

258
		// Maximize/Restore on doubleclick
259 260
		this.titleContainer.on(EventType.DBLCLICK, (e) => {
			EventHelper.stop(e);
261 262 263 264

			this.onTitleDoubleclick();
		});

B
Benjamin Pasero 已提交
265
		// Context menu on title
266 267 268
		this.title.on([EventType.CONTEXT_MENU, EventType.MOUSE_DOWN], (e: MouseEvent) => {
			if (e.type === EventType.CONTEXT_MENU || e.metaKey) {
				EventHelper.stop(e);
B
Benjamin Pasero 已提交
269 270 271 272 273

				this.onContextMenu(e);
			}
		});

R
Ryan Adolf 已提交
274
		if (!isMacintosh) {
S
SteVen Batten 已提交
275 276
			let windowControls = $(this.titleContainer).div({ class: 'window-controls-container' });

S
SteVen Batten 已提交
277
			$(windowControls).div({ class: 'window-icon window-minimize' }).on(EventType.CLICK, () => {
R
Ryan Adolf 已提交
278
				this.windowService.minimizeWindow().then(null, errors.onUnexpectedError);
279
			});
R
Ryan Adolf 已提交
280

S
SteVen Batten 已提交
281 282 283 284 285 286 287 288 289
			let maxRestore = $(windowControls).div({ class: 'window-icon window-max-restore' });
			maxRestore.on(EventType.CLICK, (e, builder) => {
				this.windowService.isMaximized().then((maximized) => {
					if (maximized) {
						return this.windowService.unmaximizeWindow();
					} else {
						return this.windowService.maximizeWindow();
					}
				}).then(null, errors.onUnexpectedError);
290
			});
R
Ryan Adolf 已提交
291

S
SteVen Batten 已提交
292
			$(windowControls).div({ class: 'window-icon window-close' }).on(EventType.CLICK, () => {
R
Ryan Adolf 已提交
293
				this.windowService.closeWindow().then(null, errors.onUnexpectedError);
294
			});
295

296 297
			this.windowService.isMaximized().then((max) => this.onDidChangeMaximized(max), errors.onUnexpectedError);
			this.windowService.onDidChangeMaximize(this.onDidChangeMaximized, this);
298
		}
R
Ryan Adolf 已提交
299

300 301
		// Since the title area is used to drag the window, we do not want to steal focus from the
		// currently active element. So we restore focus after a timeout back to where it was.
302
		this.titleContainer.on([EventType.MOUSE_DOWN], () => {
303 304 305 306 307 308 309 310
			const active = document.activeElement;
			setTimeout(() => {
				if (active instanceof HTMLElement) {
					active.focus();
				}
			}, 0 /* need a timeout because we are in capture phase */);
		}, void 0, true /* use capture to know the currently active element properly */);

311
		// Now that there exists a titelbar, we don't need the whole page to be a drag region anymore
R
Ryan Adolf 已提交
312 313
		(document.documentElement.style as any).webkitAppRegion = '';
		document.documentElement.style.height = '';
314

315
		return this.titleContainer.getHTMLElement();
B
Benjamin Pasero 已提交
316 317
	}

318
	private onDidChangeMaximized(maximized: boolean) {
S
SteVen Batten 已提交
319 320 321 322
		let element = $(this.titleContainer).getHTMLElement().querySelector('.window-max-restore');
		if (!element) {
			return;
		}
S
SteVen Batten 已提交
323

S
SteVen Batten 已提交
324
		if (maximized) {
S
SteVen Batten 已提交
325 326 327 328 329 330
			element.classList.remove('window-maximize');
			element.classList.add('window-unmaximize');
		} else {
			element.classList.remove('window-unmaximize');
			element.classList.add('window-maximize');
		}
331 332
	}

B
Benjamin Pasero 已提交
333 334 335 336
	protected updateStyles(): void {
		super.updateStyles();

		// Part container
337
		if (this.titleContainer) {
R
Ryan Adolf 已提交
338
			const bgColor = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_BACKGROUND : TITLE_BAR_ACTIVE_BACKGROUND);
339
			this.titleContainer.style('color', this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_FOREGROUND : TITLE_BAR_ACTIVE_FOREGROUND));
340
			this.titleContainer.style('background-color', bgColor);
S
SteVen Batten 已提交
341 342 343 344 345
			if (Color.fromHex(bgColor).isLighter()) {
				this.titleContainer.addClass('light');
			} else {
				this.titleContainer.removeClass('light');
			}
346 347

			const titleBorder = this.getColor(TITLE_BAR_BORDER);
348
			this.titleContainer.style('border-bottom', titleBorder ? `1px solid ${titleBorder}` : null);
349
		}
B
Benjamin Pasero 已提交
350 351
	}

352
	private onTitleDoubleclick(): void {
353
		this.windowService.onWindowTitleDoubleClick().then(null, errors.onUnexpectedError);
354 355
	}

B
Benjamin Pasero 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	private onContextMenu(e: MouseEvent): void {

		// Find target anchor
		const event = new StandardMouseEvent(e);
		const anchor = { x: event.posx, y: event.posy };

		// Show menu
		const actions = this.getContextMenuActions();
		if (actions.length) {
			this.contextMenuService.showContextMenu({
				getAnchor: () => anchor,
				getActions: () => TPromise.as(actions),
				onHide: () => actions.forEach(a => a.dispose())
			});
		}
	}

	private getContextMenuActions(): IAction[] {
		const actions: IAction[] = [];

		if (this.representedFileName) {
			const segments = this.representedFileName.split(paths.sep);
			for (let i = segments.length; i > 0; i--) {
379 380 381 382 383 384 385 386 387
				const isFile = (i === segments.length);

				let pathOffset = i;
				if (!isFile) {
					pathOffset++; // for segments which are not the file name we want to open the folder
				}

				const path = segments.slice(0, pathOffset).join(paths.sep);

B
Benjamin Pasero 已提交
388
				let label: string;
389
				if (!isFile) {
B
Benjamin Pasero 已提交
390 391 392
					label = labels.getBaseLabel(paths.dirname(path));
				} else {
					label = labels.getBaseLabel(path);
393 394 395
				}

				actions.push(new ShowItemInFolderAction(path, label || paths.sep, this.windowsService));
B
Benjamin Pasero 已提交
396 397 398 399 400 401
			}
		}

		return actions;
	}

402
	public setTitle(title: string): void {
B
Benjamin Pasero 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

		// Always set the native window title to identify us properly to the OS
		window.document.title = title;

		// Apply if we can
		if (this.title) {
			this.title.text(title);
		} else {
			this.pendingTitle = title;
		}
	}

	public layout(dimension: Dimension): Dimension[] {

		// To prevent zooming we need to adjust the font size with the zoom factor
		if (typeof this.initialTitleFontSize !== 'number') {
			this.initialTitleFontSize = parseInt(this.titleContainer.getComputedStyle().fontSize, 10);
		}
		this.titleContainer.style({ fontSize: `${this.initialTitleFontSize / getZoomFactor()}px` });

		return super.layout(dimension);
	}
B
Benjamin Pasero 已提交
425 426 427 428
}

class ShowItemInFolderAction extends Action {

429 430
	constructor(private path: string, label: string, private windowsService: IWindowsService) {
		super('showItemInFolder.action.id', label);
B
Benjamin Pasero 已提交
431 432 433 434 435
	}

	public run(): TPromise<void> {
		return this.windowsService.showItemInFolder(this.path);
	}
R
Ryan Adolf 已提交
436
}