titlebarPart.ts 22.4 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';
S
SteVen Batten 已提交
32
import { EventType, EventHelper, Dimension, isAncestor, hide, show, removeClass, addClass, append, $, addDisposableListener, getComputedStyle } 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
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChanged(e)));
		this._register(this.editorService.onDidActiveEditorChange(() => this.onActiveEditorChange()));
B
Benjamin Pasero 已提交
100 101 102 103
		this._register(this.contextService.onDidChangeWorkspaceFolders(() => this.doUpdateTitle()));
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.doUpdateTitle()));
		this._register(this.contextService.onDidChangeWorkspaceName(() => this.doUpdateTitle()));
		this._register(this.labelService.onDidRegisterFormatter(() => this.doUpdateTitle()));
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')) {
B
Benjamin Pasero 已提交
118
			this.doUpdateTitle();
119 120 121
		}
	}

S
SteVen Batten 已提交
122 123 124 125 126
	private onMenubarVisibilityChanged(visible: boolean) {
		if (isWindows || isLinux) {
			// Hide title when toggling menu bar
			if (this.configurationService.getValue<MenuBarVisibility>('window.menuBarVisibility') === 'toggle' && visible) {
				// Hack to fix issue #52522 with layered webkit-app-region elements appearing under cursor
127 128
				hide(this.dragRegion);
				setTimeout(() => show(this.dragRegion), 50);
S
SteVen Batten 已提交
129
			}
S
SteVen Batten 已提交
130 131

			this.adjustTitleMarginToCenter();
S
SteVen Batten 已提交
132
		}
133 134
	}

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

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

B
Benjamin Pasero 已提交
149
	private onActiveEditorChange(): void {
150 151 152 153 154 155

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

		// Calculate New Window Title
B
Benjamin Pasero 已提交
156
		this.doUpdateTitle();
157 158

		// Apply listener for dirty and label changes
B
Benjamin Pasero 已提交
159 160
		const activeEditor = this.editorService.activeEditor;
		if (activeEditor instanceof EditorInput) {
B
Benjamin Pasero 已提交
161 162
			this.activeEditorListeners.push(activeEditor.onDidChangeDirty(() => this.doUpdateTitle()));
			this.activeEditorListeners.push(activeEditor.onDidChangeLabel(() => this.doUpdateTitle()));
163
		}
B
Benjamin Pasero 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177

		// 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;
178 179
	}

B
Benjamin Pasero 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	private doUpdateTitle(): void {
		const title = this.getWindowTitle();

		// Always set the native window title to identify us properly to the OS
		let nativeTitle = title;
		if (!trim(nativeTitle)) {
			nativeTitle = this.environmentService.appNameLong;
		}
		window.document.title = nativeTitle;

		// Apply custom title if we can
		if (this.title) {
			this.title.innerText = title;
		} else {
			this.pendingTitle = title;
		}
S
SteVen Batten 已提交
196

197 198 199
		if (isWindows || isLinux) {
			this.adjustTitleMarginToCenter();
		}
B
Benjamin Pasero 已提交
200 201
	}

202 203 204
	private getWindowTitle(): string {
		let title = this.doGetWindowTitle();

205
		if (this.properties.isAdmin) {
B
Benjamin Pasero 已提交
206
			title = `${title || this.environmentService.appNameLong} ${TitlebarPart.NLS_USER_IS_ADMIN}`;
207 208 209
		}

		if (!this.properties.isPure) {
B
Benjamin Pasero 已提交
210
			title = `${title || this.environmentService.appNameLong} ${TitlebarPart.NLS_UNSUPPORTED}`;
211 212 213
		}

		if (this.environmentService.isExtensionDevelopment) {
B
Benjamin Pasero 已提交
214
			title = `${TitlebarPart.NLS_EXTENSION_HOST} - ${title || this.environmentService.appNameLong}`;
215 216 217 218 219
		}

		return title;
	}

B
Benjamin Pasero 已提交
220
	updateProperties(properties: ITitleProperties): void {
221 222 223 224 225 226 227
		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;

B
Benjamin Pasero 已提交
228
			this.doUpdateTitle();
229 230 231
		}
	}

232 233 234
	/**
	 * Possible template values:
	 *
B
Benjamin Pasero 已提交
235 236 237
	 * {activeEditorLong}: e.g. /Users/Development/myProject/myFolder/myFile.txt
	 * {activeEditorMedium}: e.g. myFolder/myFile.txt
	 * {activeEditorShort}: e.g. myFile.txt
238
	 * {rootName}: e.g. myFolder1, myFolder2, myFolder3
239
	 * {rootPath}: e.g. /Users/Development/myProject
240 241
	 * {folderName}: e.g. myFolder
	 * {folderPath}: e.g. /Users/Development/myFolder
242 243 244 245 246
	 * {appName}: e.g. VS Code
	 * {dirty}: indiactor
	 * {separator}: conditional separator
	 */
	private doGetWindowTitle(): string {
B
Benjamin Pasero 已提交
247
		const editor = this.editorService.activeEditor;
B
Benjamin Pasero 已提交
248
		const workspace = this.contextService.getWorkspace();
249

250
		let root: URI;
251
		if (workspace.configuration) {
252
			root = workspace.configuration;
253 254
		} else if (workspace.folders.length) {
			root = workspace.folders[0].uri;
255 256 257 258
		}

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

262
		// Variables
B
Benjamin Pasero 已提交
263 264 265
		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 已提交
266
		const rootName = this.labelService.getWorkspaceLabel(workspace);
I
isidor 已提交
267
		const rootPath = root ? this.labelService.getUriLabel(root) : '';
B
Benjamin Pasero 已提交
268
		const folderName = folder ? folder.name : '';
I
isidor 已提交
269
		const folderPath = folder ? this.labelService.getUriLabel(folder.uri) : '';
B
Benjamin Pasero 已提交
270
		const dirty = editor && editor.isDirty() ? TitlebarPart.TITLE_DIRTY : '';
271 272
		const appName = this.environmentService.appNameLong;
		const separator = TitlebarPart.TITLE_SEPARATOR;
273
		const titleTemplate = this.configurationService.getValue<string>('window.title');
274

I
isidor 已提交
275
		return template(titleTemplate, {
B
Benjamin Pasero 已提交
276 277 278
			activeEditorShort,
			activeEditorLong,
			activeEditorMedium,
279 280
			rootName,
			rootPath,
281 282
			folderName,
			folderPath,
283 284 285 286 287 288
			dirty,
			appName,
			separator: { label: separator }
		});
	}

B
Benjamin Pasero 已提交
289
	createContentArea(parent: HTMLElement): HTMLElement {
290
		this.titleContainer = parent;
B
Benjamin Pasero 已提交
291

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

B
Benjamin Pasero 已提交
295
		// App Icon (Windows/Linux)
R
Ryan Adolf 已提交
296
		if (!isMacintosh) {
297
			this.appIcon = append(this.titleContainer, $('div.window-appicon'));
298
		}
S
SteVen Batten 已提交
299

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

305
		this.menubarPart.create(this.menubar);
S
SteVen Batten 已提交
306 307 308

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

B
Benjamin Pasero 已提交
312
		// Title
313
		this.title = append(this.titleContainer, $('div.window-title'));
B
Benjamin Pasero 已提交
314
		if (this.pendingTitle) {
315
			this.title.innerText = this.pendingTitle;
B
Benjamin Pasero 已提交
316
		} else {
B
Benjamin Pasero 已提交
317
			this.doUpdateTitle();
B
Benjamin Pasero 已提交
318 319
		}

320
		// Maximize/Restore on doubleclick
S
SteVen Batten 已提交
321
		if (isMacintosh) {
322
			this._register(addDisposableListener(this.titleContainer, EventType.DBLCLICK, e => {
S
SteVen Batten 已提交
323
				EventHelper.stop(e);
324

S
SteVen Batten 已提交
325
				this.onTitleDoubleclick();
326
			}));
S
SteVen Batten 已提交
327
		}
328

B
Benjamin Pasero 已提交
329
		// Context menu on title
330 331 332 333
		[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 已提交
334

335 336 337
					this.onContextMenu(e);
				}
			}));
B
Benjamin Pasero 已提交
338 339
		});

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

S
SteVen Batten 已提交
344

B
Benjamin Pasero 已提交
345
			// Minimize
346 347 348 349 350 351
			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 已提交
352

B
Benjamin Pasero 已提交
353
			// Restore
354 355 356 357
			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 已提交
358 359 360 361
				this.windowService.isMaximized().then((maximized) => {
					if (maximized) {
						return this.windowService.unmaximizeWindow();
					}
B
Benjamin Pasero 已提交
362 363

					return this.windowService.maximizeWindow();
364 365
				});
			}));
R
Ryan Adolf 已提交
366

B
Benjamin Pasero 已提交
367
			// Close
368 369 370 371 372 373 374
			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();
			}));
375

S
SteVen Batten 已提交
376
			// Resizer
377
			this.resizer = append(this.titleContainer, $('div.resizer'));
S
SteVen Batten 已提交
378

B
Benjamin Pasero 已提交
379
			const isMaximized = this.windowService.getConfiguration().maximized ? true : false;
380
			this.onDidChangeMaximized(isMaximized);
381
			this.windowService.onDidChangeMaximize(this.onDidChangeMaximized, this);
382
		}
R
Ryan Adolf 已提交
383

384 385
		// 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.
386 387
		this._register(addDisposableListener(this.titleContainer, EventType.MOUSE_DOWN, e => {
			if (e.target && isAncestor(e.target as HTMLElement, this.menubar)) {
S
SteVen Batten 已提交
388 389 390
				return;
			}

391 392 393 394 395 396
			const active = document.activeElement;
			setTimeout(() => {
				if (active instanceof HTMLElement) {
					active.focus();
				}
			}, 0 /* need a timeout because we are in capture phase */);
397
		}, true /* use capture to know the currently active element properly */));
398

399
		return this.titleContainer;
B
Benjamin Pasero 已提交
400 401
	}

402
	private onDidChangeMaximized(maximized: boolean) {
S
SteVen Batten 已提交
403 404
		if (this.maxRestoreControl) {
			if (maximized) {
405 406
				removeClass(this.maxRestoreControl, 'window-maximize');
				addClass(this.maxRestoreControl, 'window-unmaximize');
S
SteVen Batten 已提交
407
			} else {
408 409
				removeClass(this.maxRestoreControl, 'window-unmaximize');
				addClass(this.maxRestoreControl, 'window-maximize');
S
SteVen Batten 已提交
410
			}
S
SteVen Batten 已提交
411
		}
S
SteVen Batten 已提交
412

S
SteVen Batten 已提交
413 414
		if (this.resizer) {
			if (maximized) {
415
				hide(this.resizer);
S
SteVen Batten 已提交
416
			} else {
417
				show(this.resizer);
S
SteVen Batten 已提交
418
			}
S
SteVen Batten 已提交
419
		}
S
SteVen Batten 已提交
420 421

		this.adjustTitleMarginToCenter();
422 423
	}

B
Benjamin Pasero 已提交
424 425 426 427
	protected updateStyles(): void {
		super.updateStyles();

		// Part container
428
		if (this.titleContainer) {
S
SteVen Batten 已提交
429
			if (this.isInactive) {
430
				addClass(this.titleContainer, 'inactive');
S
SteVen Batten 已提交
431
			} else {
432
				removeClass(this.titleContainer, 'inactive');
S
SteVen Batten 已提交
433 434
			}

B
Benjamin Pasero 已提交
435
			const titleBackground = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_BACKGROUND : TITLE_BAR_ACTIVE_BACKGROUND);
436
			this.titleContainer.style.backgroundColor = titleBackground;
B
Benjamin Pasero 已提交
437
			if (Color.fromHex(titleBackground).isLighter()) {
438
				addClass(this.titleContainer, 'light');
S
SteVen Batten 已提交
439
			} else {
440
				removeClass(this.titleContainer, 'light');
S
SteVen Batten 已提交
441
			}
442

B
Benjamin Pasero 已提交
443
			const titleForeground = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_FOREGROUND : TITLE_BAR_ACTIVE_FOREGROUND);
444
			this.titleContainer.style.color = titleForeground;
B
Benjamin Pasero 已提交
445

446
			const titleBorder = this.getColor(TITLE_BAR_BORDER);
447
			this.titleContainer.style.borderBottom = titleBorder ? `1px solid ${titleBorder}` : null;
448
		}
B
Benjamin Pasero 已提交
449 450
	}

451
	private onTitleDoubleclick(): void {
452
		this.windowService.onWindowTitleDoubleClick().then(null, errors.onUnexpectedError);
453 454
	}

B
Benjamin Pasero 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	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--) {
478 479 480 481 482 483 484 485 486
				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 已提交
487
				let label: string;
488
				if (!isFile) {
I
isidor 已提交
489
					label = getBaseLabel(paths.dirname(path));
B
Benjamin Pasero 已提交
490
				} else {
I
isidor 已提交
491
					label = getBaseLabel(path);
492 493 494
				}

				actions.push(new ShowItemInFolderAction(path, label || paths.sep, this.windowsService));
B
Benjamin Pasero 已提交
495 496 497 498 499 500
			}
		}

		return actions;
	}

S
SteVen Batten 已提交
501 502 503
	private adjustTitleMarginToCenter(): void {
		setTimeout(() => {
			// Center the title in the window
M
Matt Bierner 已提交
504
			const currentAppIconWidth = this.appIcon ? parseInt(getComputedStyle(this.appIcon).width, 10) : 0;
S
SteVen Batten 已提交
505 506 507 508
			let currentMenubarWidth = parseInt(getComputedStyle(this.menubar).width, 10);
			currentMenubarWidth = isNaN(currentMenubarWidth) ? 0 : currentMenubarWidth;
			const currentTotalWidth = parseInt(getComputedStyle(document.body).width, 10);
			const currentTitleWidth = parseInt(getComputedStyle(this.title).width, 10);
M
Matt Bierner 已提交
509
			const currentWindowControlsWidth = this.windowControls ? parseInt(getComputedStyle(this.windowControls).width, 10) : 0;
S
SteVen Batten 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522

			let leftMargin = (currentTotalWidth / 2) - (currentTitleWidth / 2) - (currentMenubarWidth + currentAppIconWidth);
			let rightMargin = currentTotalWidth - (currentAppIconWidth + currentMenubarWidth + leftMargin + currentTitleWidth + currentWindowControlsWidth);

			// Center if we can, leaving some space on both sides
			if (leftMargin >= 20 && rightMargin >= 20) {
				this.title.style.marginLeft = `${leftMargin}px`;
			} else {
				this.title.style.marginLeft = null;
			}
		}, 0); // delay so that we can get accurate information about the widths
	}

S
SteVen Batten 已提交
523 524
	private updateLayout(dimension: Dimension) {
		// Store initital title sizing if we need to prevent zooming
525
		if (typeof this.initialSizing.titleFontSize !== 'number') {
526
			this.initialSizing.titleFontSize = parseInt(getComputedStyle(this.title).fontSize, 10);
527 528 529
		}

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

S
SteVen Batten 已提交
533 534 535 536
		// 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();
537 538
			this.title.style.fontSize = `${this.initialSizing.titleFontSize / getZoomFactor()}px`;
			this.title.style.lineHeight = `${newHeight}px`;
539

S
SteVen Batten 已提交
540 541 542
			// Windows/Linux specific layout
			if (isWindows || isLinux) {
				if (typeof this.initialSizing.controlsWidth !== 'number') {
543
					this.initialSizing.controlsWidth = parseInt(getComputedStyle(this.windowControls).width, 10);
S
SteVen Batten 已提交
544
				}
545

546
				const appIconComputedStyles = getComputedStyle(this.appIcon);
S
SteVen Batten 已提交
547
				if (typeof this.initialSizing.appIconWidth !== 'number') {
548
					this.initialSizing.appIconWidth = parseInt(appIconComputedStyles.width, 10);
S
SteVen Batten 已提交
549
				}
550

S
SteVen Batten 已提交
551
				if (typeof this.initialSizing.appIconSize !== 'number') {
552
					this.initialSizing.appIconSize = parseInt(appIconComputedStyles.backgroundSize, 10);
S
SteVen Batten 已提交
553
				}
554

555
				const currentAppIconHeight = parseInt(appIconComputedStyles.height, 10);
S
SteVen Batten 已提交
556 557 558 559 560
				const newControlsWidth = this.initialSizing.controlsWidth / getZoomFactor();
				const newAppIconWidth = this.initialSizing.appIconWidth / getZoomFactor();
				const newAppIconSize = this.initialSizing.appIconSize / getZoomFactor();

				// Adjust app icon mimic menubar
561 562 563 564
				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`;
565

S
SteVen Batten 已提交
566
				// Adjust windows controls
567
				this.windowControls.style.width = `${newControlsWidth}px`;
568
			}
S
SteVen Batten 已提交
569 570
		} else {
			// We need to undo zoom prevention
571 572
			this.title.style.fontSize = null;
			this.title.style.lineHeight = null;
573

574 575 576 577
			this.appIcon.style.width = null;
			this.appIcon.style.backgroundSize = null;
			this.appIcon.style.paddingTop = null;
			this.appIcon.style.paddingBottom = null;
578

579
			this.windowControls.style.width = null;
S
SteVen Batten 已提交
580
		}
581

S
SteVen Batten 已提交
582 583 584
		if (this.menubarPart) {
			const menubarDimension = new Dimension(undefined, dimension.height);
			this.menubarPart.layout(menubarDimension);
585 586 587
		}
	}

B
Benjamin Pasero 已提交
588
	layout(dimension: Dimension): Dimension[] {
S
SteVen Batten 已提交
589
		this.updateLayout(dimension);
B
Benjamin Pasero 已提交
590 591 592

		return super.layout(dimension);
	}
B
Benjamin Pasero 已提交
593 594 595 596
}

class ShowItemInFolderAction extends Action {

597 598
	constructor(private path: string, label: string, private windowsService: IWindowsService) {
		super('showItemInFolder.action.id', label);
B
Benjamin Pasero 已提交
599 600
	}

B
Benjamin Pasero 已提交
601
	run(): TPromise<void> {
B
Benjamin Pasero 已提交
602 603
		return this.windowsService.showItemInFolder(this.path);
	}
R
Ryan Adolf 已提交
604
}
S
SteVen Batten 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

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};
			}
		`);
	}
});