titlebarPart.ts 21.2 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 10
import { TPromise } from 'vs/base/common/winjs.base';
import * as paths from 'vs/base/common/paths';
B
Benjamin Pasero 已提交
11
import { Part } from 'vs/workbench/browser/part';
12
import { ITitleService, ITitleProperties } from 'vs/workbench/services/title/common/titleService';
B
Benjamin Pasero 已提交
13
import { getZoomFactor } from 'vs/base/browser/browser';
14
import { IWindowService, IWindowsService, MenuBarVisibility } from 'vs/platform/windows/common/windows';
B
Benjamin Pasero 已提交
15 16 17 18
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';
19
import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
20
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
21
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
22
import * as nls from 'vs/nls';
B
Benjamin Pasero 已提交
23
import { EditorInput, toResource, Verbosity } from 'vs/workbench/common/editor';
24
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
25
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
S
SteVen Batten 已提交
26
import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector } from 'vs/platform/theme/common/themeService';
27
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';
B
Benjamin Pasero 已提交
28
import { isMacintosh, isWindows, isLinux } from 'vs/base/common/platform';
29
import { URI } from 'vs/base/common/uri';
R
Ryan Adolf 已提交
30
import { Color } from 'vs/base/common/color';
B
Benjamin Pasero 已提交
31
import { trim } from 'vs/base/common/strings';
32
import { EventType, EventHelper, Dimension, isAncestor, hide, show, removeClass, addClass, append, $, addDisposableListener } from 'vs/base/browser/dom';
33
import { MenubarControl } from 'vs/workbench/browser/parts/titlebar/menubarControl';
S
SteVen Batten 已提交
34
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
35
import { template, getBaseLabel } from 'vs/base/common/labels';
I
isidor 已提交
36
import { ILabelService } from 'vs/platform/label/common/label';
37
import { Event } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
38 39 40

export class TitlebarPart extends Part implements ITitleService {

B
Benjamin Pasero 已提交
41
	_serviceBrand: any;
B
Benjamin Pasero 已提交
42

43
	private static readonly NLS_UNSUPPORTED = nls.localize('patchedWindowTitle', "[Unsupported]");
44
	private static readonly NLS_USER_IS_ADMIN = isWindows ? nls.localize('userIsAdmin', "[Administrator]") : nls.localize('userIsSudo', "[Superuser]");
45 46 47
	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
48

49 50 51 52 53 54
	private titleContainer: HTMLElement;
	private title: HTMLElement;
	private dragRegion: HTMLElement;
	private windowControls: HTMLElement;
	private maxRestoreControl: HTMLElement;
	private appIcon: HTMLElement;
55
	private menubarPart: MenubarControl;
56 57
	private menubar: HTMLElement;
	private resizer: HTMLElement;
B
Benjamin Pasero 已提交
58

B
Benjamin Pasero 已提交
59
	private pendingTitle: string;
B
Benjamin Pasero 已提交
60
	private representedFileName: string;
61 62 63 64 65

	private initialSizing: {
		titleFontSize?: number;
		titlebarHeight?: number;
		controlsWidth?: number;
66
		appIconSize?: number;
67
		appIconWidth?: number;
B
Benjamin Pasero 已提交
68
	} = Object.create(null);
B
Benjamin Pasero 已提交
69

B
Benjamin Pasero 已提交
70 71
	private isInactive: boolean;

72
	private properties: ITitleProperties;
73 74
	private activeEditorListeners: IDisposable[];

75 76
	constructor(
		id: string,
B
Benjamin Pasero 已提交
77 78
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IWindowService private windowService: IWindowService,
79 80
		@IConfigurationService private configurationService: IConfigurationService,
		@IWindowsService private windowsService: IWindowsService,
B
Benjamin Pasero 已提交
81
		@IEditorService private editorService: IEditorService,
82
		@IEnvironmentService private environmentService: IEnvironmentService,
B
Benjamin Pasero 已提交
83
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
S
SteVen Batten 已提交
84
		@IInstantiationService private instantiationService: IInstantiationService,
I
isidor 已提交
85
		@IThemeService themeService: IThemeService,
I
isidor 已提交
86
		@ILabelService private labelService: ILabelService
87
	) {
B
Benjamin Pasero 已提交
88
		super(id, { hasTitle: false }, themeService);
89

90
		this.properties = { isPure: true, isAdmin: false };
91 92
		this.activeEditorListeners = [];

93 94 95 96
		this.registerListeners();
	}

	private registerListeners(): void {
97
		this._register(this.windowService.onDidChangeFocus(focused => focused ? this.onFocus() : this.onBlur()));
B
Benjamin Pasero 已提交
98 99 100 101 102
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChanged(e)));
		this._register(this.editorService.onDidActiveEditorChange(() => this.onActiveEditorChange()));
		this._register(this.contextService.onDidChangeWorkspaceFolders(() => this.setTitle(this.getWindowTitle())));
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.setTitle(this.getWindowTitle())));
		this._register(this.contextService.onDidChangeWorkspaceName(() => this.setTitle(this.getWindowTitle())));
103
		this._register(this.labelService.onDidRegisterFormatter(() => this.setTitle(this.getWindowTitle())));
104 105
	}

B
Benjamin Pasero 已提交
106 107 108 109 110 111 112 113 114 115
	private onBlur(): void {
		this.isInactive = true;
		this.updateStyles();
	}

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

116 117
	private onConfigurationChanged(event: IConfigurationChangeEvent): void {
		if (event.affectsConfiguration('window.title')) {
118 119 120 121
			this.setTitle(this.getWindowTitle());
		}
	}

S
SteVen Batten 已提交
122 123 124 125
	private onMenubarVisibilityChanged(visible: boolean) {
		if (isWindows || isLinux) {
			// Hide title when toggling menu bar
			if (this.configurationService.getValue<MenuBarVisibility>('window.menuBarVisibility') === 'toggle' && visible) {
126
				this.title.style.visibility = 'hidden';
127

S
SteVen Batten 已提交
128
				// Hack to fix issue #52522 with layered webkit-app-region elements appearing under cursor
129 130
				hide(this.dragRegion);
				setTimeout(() => show(this.dragRegion), 50);
S
SteVen Batten 已提交
131
			} else {
132
				this.title.style.visibility = null;
S
SteVen Batten 已提交
133 134
			}
		}
135 136
	}

S
SteVen Batten 已提交
137 138 139 140 141 142 143 144 145 146
	private onMenubarFocusChanged(focused: boolean) {
		if (isWindows || isLinux) {
			if (focused) {
				hide(this.dragRegion);
			} else {
				show(this.dragRegion);
			}
		}
	}

147 148 149 150
	onMenubarVisibilityChange(): Event<boolean> {
		return this.menubarPart.onVisibilityChange;
	}

B
Benjamin Pasero 已提交
151
	private onActiveEditorChange(): void {
152 153 154 155 156 157 158 159 160

		// 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 已提交
161 162 163
		const activeEditor = this.editorService.activeEditor;
		if (activeEditor instanceof EditorInput) {
			this.activeEditorListeners.push(activeEditor.onDidChangeDirty(() => {
164 165 166
				this.setTitle(this.getWindowTitle());
			}));

B
Benjamin Pasero 已提交
167
			this.activeEditorListeners.push(activeEditor.onDidChangeLabel(() => {
168 169 170
				this.setTitle(this.getWindowTitle());
			}));
		}
B
Benjamin Pasero 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184

		// 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;
185 186 187 188
	}

	private getWindowTitle(): string {
		let title = this.doGetWindowTitle();
B
Benjamin Pasero 已提交
189
		if (!trim(title)) {
190 191 192
			title = this.environmentService.appNameLong;
		}

193 194 195 196 197
		if (this.properties.isAdmin) {
			title = `${title} ${TitlebarPart.NLS_USER_IS_ADMIN}`;
		}

		if (!this.properties.isPure) {
198 199 200 201 202 203 204 205 206 207 208
			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;
	}

B
Benjamin Pasero 已提交
209
	updateProperties(properties: ITitleProperties): void {
210 211 212 213 214 215 216 217 218 219 220
		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());
		}
	}

221 222 223
	/**
	 * Possible template values:
	 *
B
Benjamin Pasero 已提交
224 225 226
	 * {activeEditorLong}: e.g. /Users/Development/myProject/myFolder/myFile.txt
	 * {activeEditorMedium}: e.g. myFolder/myFile.txt
	 * {activeEditorShort}: e.g. myFile.txt
227
	 * {rootName}: e.g. myFolder1, myFolder2, myFolder3
228
	 * {rootPath}: e.g. /Users/Development/myProject
229 230
	 * {folderName}: e.g. myFolder
	 * {folderPath}: e.g. /Users/Development/myFolder
231 232 233 234 235
	 * {appName}: e.g. VS Code
	 * {dirty}: indiactor
	 * {separator}: conditional separator
	 */
	private doGetWindowTitle(): string {
B
Benjamin Pasero 已提交
236
		const editor = this.editorService.activeEditor;
B
Benjamin Pasero 已提交
237
		const workspace = this.contextService.getWorkspace();
238

239
		let root: URI;
240
		if (workspace.configuration) {
241
			root = workspace.configuration;
242 243
		} else if (workspace.folders.length) {
			root = workspace.folders[0].uri;
244 245 246 247
		}

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

251
		// Variables
B
Benjamin Pasero 已提交
252 253 254
		const activeEditorShort = editor ? editor.getTitle(Verbosity.SHORT) : '';
		const activeEditorMedium = editor ? editor.getTitle(Verbosity.MEDIUM) : activeEditorShort;
		const activeEditorLong = editor ? editor.getTitle(Verbosity.LONG) : activeEditorMedium;
I
isidor 已提交
255
		const rootName = this.labelService.getWorkspaceLabel(workspace);
I
isidor 已提交
256
		const rootPath = root ? this.labelService.getUriLabel(root) : '';
B
Benjamin Pasero 已提交
257
		const folderName = folder ? folder.name : '';
I
isidor 已提交
258
		const folderPath = folder ? this.labelService.getUriLabel(folder.uri) : '';
B
Benjamin Pasero 已提交
259
		const dirty = editor && editor.isDirty() ? TitlebarPart.TITLE_DIRTY : '';
260 261
		const appName = this.environmentService.appNameLong;
		const separator = TitlebarPart.TITLE_SEPARATOR;
262
		const titleTemplate = this.configurationService.getValue<string>('window.title');
263

I
isidor 已提交
264
		return template(titleTemplate, {
B
Benjamin Pasero 已提交
265 266 267
			activeEditorShort,
			activeEditorLong,
			activeEditorMedium,
268 269
			rootName,
			rootPath,
270 271
			folderName,
			folderPath,
272 273 274 275 276 277
			dirty,
			appName,
			separator: { label: separator }
		});
	}

B
Benjamin Pasero 已提交
278
	createContentArea(parent: HTMLElement): HTMLElement {
279
		this.titleContainer = parent;
B
Benjamin Pasero 已提交
280

281
		// Draggable region that we can manipulate for #52522
282
		this.dragRegion = append(this.titleContainer, $('div.titlebar-drag-region'));
283

B
Benjamin Pasero 已提交
284
		// App Icon (Windows/Linux)
R
Ryan Adolf 已提交
285
		if (!isMacintosh) {
286
			this.appIcon = append(this.titleContainer, $('div.window-appicon'));
287
		}
S
SteVen Batten 已提交
288

S
SteVen Batten 已提交
289
		// Menubar: the menubar part which is responsible for populating both the custom and native menubars
290 291 292
		this.menubarPart = this.instantiationService.createInstance(MenubarControl);
		this.menubar = append(this.titleContainer, $('div.menubar'));
		this.menubar.setAttribute('role', 'menubar');
S
SteVen Batten 已提交
293

294
		this.menubarPart.create(this.menubar);
S
SteVen Batten 已提交
295 296 297

		if (!isMacintosh) {
			this._register(this.menubarPart.onVisibilityChange(e => this.onMenubarVisibilityChanged(e)));
S
SteVen Batten 已提交
298
			this._register(this.menubarPart.onFocusStateChange(e => this.onMenubarFocusChanged(e)));
S
SteVen Batten 已提交
299 300
		}

B
Benjamin Pasero 已提交
301
		// Title
302
		this.title = append(this.titleContainer, $('div.window-title'));
B
Benjamin Pasero 已提交
303
		if (this.pendingTitle) {
304
			this.title.innerText = this.pendingTitle;
B
Benjamin Pasero 已提交
305 306
		} else {
			this.setTitle(this.getWindowTitle());
B
Benjamin Pasero 已提交
307 308
		}

309
		// Maximize/Restore on doubleclick
S
SteVen Batten 已提交
310
		if (isMacintosh) {
311
			this._register(addDisposableListener(this.titleContainer, EventType.DBLCLICK, e => {
S
SteVen Batten 已提交
312
				EventHelper.stop(e);
313

S
SteVen Batten 已提交
314
				this.onTitleDoubleclick();
315
			}));
S
SteVen Batten 已提交
316
		}
317

B
Benjamin Pasero 已提交
318
		// Context menu on title
319 320 321 322
		[EventType.CONTEXT_MENU, EventType.MOUSE_DOWN].forEach(event => {
			this._register(addDisposableListener(this.title, event, e => {
				if (e.type === EventType.CONTEXT_MENU || e.metaKey) {
					EventHelper.stop(e);
B
Benjamin Pasero 已提交
323

324 325 326
					this.onContextMenu(e);
				}
			}));
B
Benjamin Pasero 已提交
327 328
		});

B
Benjamin Pasero 已提交
329
		// Window Controls (Windows/Linux)
R
Ryan Adolf 已提交
330
		if (!isMacintosh) {
331 332
			this.windowControls = append(this.titleContainer, $('div.window-controls-container'));

S
SteVen Batten 已提交
333

B
Benjamin Pasero 已提交
334
			// Minimize
335 336 337 338 339 340
			const minimizeIconContainer = append(this.windowControls, $('div.window-icon-bg'));
			const minimizeIcon = append(minimizeIconContainer, $('div.window-icon'));
			addClass(minimizeIcon, 'window-minimize');
			this._register(addDisposableListener(minimizeIcon, EventType.CLICK, e => {
				this.windowService.minimizeWindow();
			}));
R
Ryan Adolf 已提交
341

B
Benjamin Pasero 已提交
342
			// Restore
343 344 345 346
			const restoreIconContainer = append(this.windowControls, $('div.window-icon-bg'));
			this.maxRestoreControl = append(restoreIconContainer, $('div.window-icon'));
			addClass(this.maxRestoreControl, 'window-max-restore');
			this._register(addDisposableListener(this.maxRestoreControl, EventType.CLICK, e => {
S
SteVen Batten 已提交
347 348 349 350
				this.windowService.isMaximized().then((maximized) => {
					if (maximized) {
						return this.windowService.unmaximizeWindow();
					}
B
Benjamin Pasero 已提交
351 352

					return this.windowService.maximizeWindow();
353 354
				});
			}));
R
Ryan Adolf 已提交
355

B
Benjamin Pasero 已提交
356
			// Close
357 358 359 360 361 362 363
			const closeIconContainer = append(this.windowControls, $('div.window-icon-bg'));
			addClass(closeIconContainer, 'window-close-bg');
			const closeIcon = append(closeIconContainer, $('div.window-icon'));
			addClass(closeIcon, 'window-close');
			this._register(addDisposableListener(closeIcon, EventType.CLICK, e => {
				this.windowService.closeWindow();
			}));
364

S
SteVen Batten 已提交
365
			// Resizer
366
			this.resizer = append(this.titleContainer, $('div.resizer'));
S
SteVen Batten 已提交
367

B
Benjamin Pasero 已提交
368
			const isMaximized = this.windowService.getConfiguration().maximized ? true : false;
369
			this.onDidChangeMaximized(isMaximized);
370
			this.windowService.onDidChangeMaximize(this.onDidChangeMaximized, this);
371
		}
R
Ryan Adolf 已提交
372

373 374
		// 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.
375 376
		this._register(addDisposableListener(this.titleContainer, EventType.MOUSE_DOWN, e => {
			if (e.target && isAncestor(e.target as HTMLElement, this.menubar)) {
S
SteVen Batten 已提交
377 378 379
				return;
			}

380 381 382 383 384 385
			const active = document.activeElement;
			setTimeout(() => {
				if (active instanceof HTMLElement) {
					active.focus();
				}
			}, 0 /* need a timeout because we are in capture phase */);
386
		}, true /* use capture to know the currently active element properly */));
387

388
		return this.titleContainer;
B
Benjamin Pasero 已提交
389 390
	}

391
	private onDidChangeMaximized(maximized: boolean) {
S
SteVen Batten 已提交
392 393
		if (this.maxRestoreControl) {
			if (maximized) {
394 395
				removeClass(this.maxRestoreControl, 'window-maximize');
				addClass(this.maxRestoreControl, 'window-unmaximize');
S
SteVen Batten 已提交
396
			} else {
397 398
				removeClass(this.maxRestoreControl, 'window-unmaximize');
				addClass(this.maxRestoreControl, 'window-maximize');
S
SteVen Batten 已提交
399
			}
S
SteVen Batten 已提交
400
		}
S
SteVen Batten 已提交
401

S
SteVen Batten 已提交
402 403
		if (this.resizer) {
			if (maximized) {
404
				hide(this.resizer);
S
SteVen Batten 已提交
405
			} else {
406
				show(this.resizer);
S
SteVen Batten 已提交
407
			}
S
SteVen Batten 已提交
408
		}
409 410
	}

B
Benjamin Pasero 已提交
411 412 413 414
	protected updateStyles(): void {
		super.updateStyles();

		// Part container
415
		if (this.titleContainer) {
S
SteVen Batten 已提交
416
			if (this.isInactive) {
417
				addClass(this.titleContainer, 'inactive');
S
SteVen Batten 已提交
418
			} else {
419
				removeClass(this.titleContainer, 'inactive');
S
SteVen Batten 已提交
420 421
			}

B
Benjamin Pasero 已提交
422
			const titleBackground = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_BACKGROUND : TITLE_BAR_ACTIVE_BACKGROUND);
423
			this.titleContainer.style.backgroundColor = titleBackground;
B
Benjamin Pasero 已提交
424
			if (Color.fromHex(titleBackground).isLighter()) {
425
				addClass(this.titleContainer, 'light');
S
SteVen Batten 已提交
426
			} else {
427
				removeClass(this.titleContainer, 'light');
S
SteVen Batten 已提交
428
			}
429

B
Benjamin Pasero 已提交
430
			const titleForeground = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_FOREGROUND : TITLE_BAR_ACTIVE_FOREGROUND);
431
			this.titleContainer.style.color = titleForeground;
B
Benjamin Pasero 已提交
432

433
			const titleBorder = this.getColor(TITLE_BAR_BORDER);
434
			this.titleContainer.style.borderBottom = titleBorder ? `1px solid ${titleBorder}` : null;
435
		}
B
Benjamin Pasero 已提交
436 437
	}

438
	private onTitleDoubleclick(): void {
439
		this.windowService.onWindowTitleDoubleClick().then(null, errors.onUnexpectedError);
440 441
	}

B
Benjamin Pasero 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	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--) {
465 466 467 468 469 470 471 472 473
				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 已提交
474
				let label: string;
475
				if (!isFile) {
I
isidor 已提交
476
					label = getBaseLabel(paths.dirname(path));
B
Benjamin Pasero 已提交
477
				} else {
I
isidor 已提交
478
					label = getBaseLabel(path);
479 480 481
				}

				actions.push(new ShowItemInFolderAction(path, label || paths.sep, this.windowsService));
B
Benjamin Pasero 已提交
482 483 484 485 486 487
			}
		}

		return actions;
	}

B
Benjamin Pasero 已提交
488
	setTitle(title: string): void {
B
Benjamin Pasero 已提交
489 490 491 492 493 494

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

		// Apply if we can
		if (this.title) {
495
			this.title.innerText = title;
B
Benjamin Pasero 已提交
496 497 498 499 500
		} else {
			this.pendingTitle = title;
		}
	}

S
SteVen Batten 已提交
501 502
	private updateLayout(dimension: Dimension) {
		// Store initital title sizing if we need to prevent zooming
503
		if (typeof this.initialSizing.titleFontSize !== 'number') {
504
			this.initialSizing.titleFontSize = parseInt(getComputedStyle(this.title).fontSize, 10);
505 506 507
		}

		if (typeof this.initialSizing.titlebarHeight !== 'number') {
508
			this.initialSizing.titlebarHeight = parseInt(getComputedStyle(this.title).height, 10);
B
Benjamin Pasero 已提交
509
		}
510

S
SteVen Batten 已提交
511 512 513 514
		// Only prevent zooming behavior on macOS or when the menubar is not visible
		if (isMacintosh || this.configurationService.getValue<MenuBarVisibility>('window.menuBarVisibility') === 'hidden') {
			// To prevent zooming we need to adjust the font size with the zoom factor
			const newHeight = this.initialSizing.titlebarHeight / getZoomFactor();
515 516
			this.title.style.fontSize = `${this.initialSizing.titleFontSize / getZoomFactor()}px`;
			this.title.style.lineHeight = `${newHeight}px`;
517

S
SteVen Batten 已提交
518 519 520
			// Windows/Linux specific layout
			if (isWindows || isLinux) {
				if (typeof this.initialSizing.controlsWidth !== 'number') {
521
					this.initialSizing.controlsWidth = parseInt(getComputedStyle(this.windowControls).width, 10);
S
SteVen Batten 已提交
522
				}
523

524
				const appIconComputedStyles = getComputedStyle(this.appIcon);
S
SteVen Batten 已提交
525
				if (typeof this.initialSizing.appIconWidth !== 'number') {
526
					this.initialSizing.appIconWidth = parseInt(appIconComputedStyles.width, 10);
S
SteVen Batten 已提交
527
				}
528

S
SteVen Batten 已提交
529
				if (typeof this.initialSizing.appIconSize !== 'number') {
530
					this.initialSizing.appIconSize = parseInt(appIconComputedStyles.backgroundSize, 10);
S
SteVen Batten 已提交
531
				}
532

533
				const currentAppIconHeight = parseInt(appIconComputedStyles.height, 10);
S
SteVen Batten 已提交
534 535 536 537 538
				const newControlsWidth = this.initialSizing.controlsWidth / getZoomFactor();
				const newAppIconWidth = this.initialSizing.appIconWidth / getZoomFactor();
				const newAppIconSize = this.initialSizing.appIconSize / getZoomFactor();

				// Adjust app icon mimic menubar
539 540 541 542
				this.appIcon.style.width = `${newAppIconWidth}px`;
				this.appIcon.style.backgroundSize = `${newAppIconSize}px`;
				this.appIcon.style.paddingTop = `${(newHeight - currentAppIconHeight) / 2.0}px`;
				this.appIcon.style.paddingBottom = `${(newHeight - currentAppIconHeight) / 2.0}px`;
543

S
SteVen Batten 已提交
544
				// Adjust windows controls
545
				this.windowControls.style.width = `${newControlsWidth}px`;
546
			}
S
SteVen Batten 已提交
547 548
		} else {
			// We need to undo zoom prevention
549 550
			this.title.style.fontSize = null;
			this.title.style.lineHeight = null;
551

552 553 554 555
			this.appIcon.style.width = null;
			this.appIcon.style.backgroundSize = null;
			this.appIcon.style.paddingTop = null;
			this.appIcon.style.paddingBottom = null;
556

557
			this.windowControls.style.width = null;
S
SteVen Batten 已提交
558
		}
559

S
SteVen Batten 已提交
560 561 562
		if (this.menubarPart) {
			const menubarDimension = new Dimension(undefined, dimension.height);
			this.menubarPart.layout(menubarDimension);
563 564 565
		}
	}

B
Benjamin Pasero 已提交
566
	layout(dimension: Dimension): Dimension[] {
S
SteVen Batten 已提交
567
		this.updateLayout(dimension);
B
Benjamin Pasero 已提交
568 569 570

		return super.layout(dimension);
	}
B
Benjamin Pasero 已提交
571 572 573 574
}

class ShowItemInFolderAction extends Action {

575 576
	constructor(private path: string, label: string, private windowsService: IWindowsService) {
		super('showItemInFolder.action.id', label);
B
Benjamin Pasero 已提交
577 578
	}

B
Benjamin Pasero 已提交
579
	run(): TPromise<void> {
B
Benjamin Pasero 已提交
580 581
		return this.windowsService.showItemInFolder(this.path);
	}
R
Ryan Adolf 已提交
582
}
S
SteVen Batten 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
	const titlebarActiveFg = theme.getColor(TITLE_BAR_ACTIVE_FOREGROUND);
	if (titlebarActiveFg) {
		collector.addRule(`
		.monaco-workbench > .part.titlebar > .window-controls-container .window-icon {
			background-color: ${titlebarActiveFg};
		}
		`);
	}

	const titlebarInactiveFg = theme.getColor(TITLE_BAR_INACTIVE_FOREGROUND);
	if (titlebarInactiveFg) {
		collector.addRule(`
		.monaco-workbench > .part.titlebar.inactive > .window-controls-container .window-icon {
				background-color: ${titlebarInactiveFg};
			}
		`);
	}
});