titlebarPart.ts 20.5 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

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

export class TitlebarPart extends Part implements ITitleService {

B
Benjamin Pasero 已提交
38
	_serviceBrand: any;
B
Benjamin Pasero 已提交
39

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

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

B
Benjamin Pasero 已提交
56
	private pendingTitle: string;
B
Benjamin Pasero 已提交
57
	private representedFileName: string;
58

B
Benjamin Pasero 已提交
59 60
	private isInactive: boolean;

61
	private properties: ITitleProperties;
62 63
	private activeEditorListeners: IDisposable[];

64 65
	constructor(
		id: string,
66 67 68 69 70 71 72 73
		@IContextMenuService private readonly contextMenuService: IContextMenuService,
		@IWindowService private readonly windowService: IWindowService,
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IWindowsService private readonly windowsService: IWindowsService,
		@IEditorService private readonly editorService: IEditorService,
		@IEnvironmentService private readonly environmentService: IEnvironmentService,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
I
isidor 已提交
74
		@IThemeService themeService: IThemeService,
75
		@ILabelService private readonly labelService: ILabelService,
76
		@IStorageService storageService: IStorageService
77
	) {
78
		super(id, { hasTitle: false }, themeService, storageService);
79

80
		this.properties = { isPure: true, isAdmin: false };
81 82
		this.activeEditorListeners = [];

83 84 85 86
		this.registerListeners();
	}

	private registerListeners(): void {
87
		this._register(this.windowService.onDidChangeFocus(focused => focused ? this.onFocus() : this.onBlur()));
B
Benjamin Pasero 已提交
88 89
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChanged(e)));
		this._register(this.editorService.onDidActiveEditorChange(() => this.onActiveEditorChange()));
B
Benjamin Pasero 已提交
90 91 92
		this._register(this.contextService.onDidChangeWorkspaceFolders(() => this.doUpdateTitle()));
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.doUpdateTitle()));
		this._register(this.contextService.onDidChangeWorkspaceName(() => this.doUpdateTitle()));
93
		this._register(this.labelService.onDidChangeFormatters(() => this.doUpdateTitle()));
94 95
	}

B
Benjamin Pasero 已提交
96 97 98 99 100 101 102 103 104 105
	private onBlur(): void {
		this.isInactive = true;
		this.updateStyles();
	}

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

106 107
	private onConfigurationChanged(event: IConfigurationChangeEvent): void {
		if (event.affectsConfiguration('window.title')) {
B
Benjamin Pasero 已提交
108
			this.doUpdateTitle();
109
		}
S
SteVen Batten 已提交
110 111 112 113 114 115

		if (event.affectsConfiguration('window.doubleClickIconToClose')) {
			if (this.appIcon) {
				this.onUpdateAppIconDragBehavior();
			}
		}
116 117
	}

S
SteVen Batten 已提交
118 119 120 121 122
	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
123 124
				hide(this.dragRegion);
				setTimeout(() => show(this.dragRegion), 50);
S
SteVen Batten 已提交
125
			}
S
SteVen Batten 已提交
126 127

			this.adjustTitleMarginToCenter();
S
SteVen Batten 已提交
128
		}
129 130
	}

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

141 142 143 144
	onMenubarVisibilityChange(): Event<boolean> {
		return this.menubarPart.onVisibilityChange;
	}

B
Benjamin Pasero 已提交
145
	private onActiveEditorChange(): void {
146 147 148 149 150 151

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

		// Calculate New Window Title
B
Benjamin Pasero 已提交
152
		this.doUpdateTitle();
153 154

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

		// 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;
174 175
	}

B
Benjamin Pasero 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	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 已提交
192

193
		if ((isWindows || isLinux) && this.title) {
194 195
			this.adjustTitleMarginToCenter();
		}
B
Benjamin Pasero 已提交
196 197
	}

198 199 200
	private getWindowTitle(): string {
		let title = this.doGetWindowTitle();

201
		if (this.properties.isAdmin) {
B
Benjamin Pasero 已提交
202
			title = `${title || this.environmentService.appNameLong} ${TitlebarPart.NLS_USER_IS_ADMIN}`;
203 204 205
		}

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

		if (this.environmentService.isExtensionDevelopment) {
B
Benjamin Pasero 已提交
210
			title = `${TitlebarPart.NLS_EXTENSION_HOST} - ${title || this.environmentService.appNameLong}`;
211 212 213 214 215
		}

		return title;
	}

B
Benjamin Pasero 已提交
216
	updateProperties(properties: ITitleProperties): void {
217 218 219 220 221 222 223
		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 已提交
224
			this.doUpdateTitle();
225 226 227
		}
	}

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

246
		let root: URI;
247
		if (workspace.configuration) {
248
			root = workspace.configuration;
249 250
		} else if (workspace.folders.length) {
			root = workspace.folders[0].uri;
251 252 253 254
		}

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

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

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

B
Benjamin Pasero 已提交
285
	createContentArea(parent: HTMLElement): HTMLElement {
286
		this.titleContainer = parent;
B
Benjamin Pasero 已提交
287

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

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

			this._register(addDisposableListener(this.appIcon, EventType.DBLCLICK, (e => {
				this.windowService.closeWindow();
			})));
299
		}
S
SteVen Batten 已提交
300

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

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

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

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

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

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

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

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

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

S
SteVen Batten 已提交
345

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

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

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

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

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

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

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

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

S
SteVen Batten 已提交
400 401
		this.updateStyles();

402
		return this.titleContainer;
B
Benjamin Pasero 已提交
403 404
	}

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

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

		this.adjustTitleMarginToCenter();
425 426
	}

B
Benjamin Pasero 已提交
427 428 429 430
	protected updateStyles(): void {
		super.updateStyles();

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

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

B
Benjamin Pasero 已提交
446
			const titleForeground = this.getColor(this.isInactive ? TITLE_BAR_INACTIVE_FOREGROUND : TITLE_BAR_ACTIVE_FOREGROUND);
447
			this.titleContainer.style.color = titleForeground;
B
Benjamin Pasero 已提交
448

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

454
	private onTitleDoubleclick(): void {
455
		this.windowService.onWindowTitleDoubleClick();
456 457
	}

S
SteVen Batten 已提交
458 459 460 461 462 463 464 465 466 467
	private onUpdateAppIconDragBehavior() {
		const setting = this.configurationService.getValue('window.doubleClickIconToClose');
		if (setting) {
			this.appIcon.style['-webkit-app-region'] = 'no-drag';
		}
		else {
			this.appIcon.style['-webkit-app-region'] = 'drag';
		}
	}

B
Benjamin Pasero 已提交
468 469 470 471 472 473 474 475 476 477 478
	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,
479
				getActions: () => actions,
B
Benjamin Pasero 已提交
480 481 482 483 484 485 486 487 488 489 490
				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--) {
491 492 493 494 495 496 497 498 499
				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 已提交
500
				let label: string;
501
				if (!isFile) {
I
isidor 已提交
502
					label = getBaseLabel(paths.dirname(path));
B
Benjamin Pasero 已提交
503
				} else {
I
isidor 已提交
504
					label = getBaseLabel(path);
505 506 507
				}

				actions.push(new ShowItemInFolderAction(path, label || paths.sep, this.windowsService));
B
Benjamin Pasero 已提交
508 509 510 511 512 513
			}
		}

		return actions;
	}

S
SteVen Batten 已提交
514
	private adjustTitleMarginToCenter(): void {
S
SteVen Batten 已提交
515 516 517 518 519 520 521 522 523 524 525
		if (!isMacintosh &&
			(this.appIcon.clientWidth + this.menubar.clientWidth + 10 > (this.titleContainer.clientWidth - this.title.clientWidth) / 2 ||
				this.titleContainer.clientWidth - this.windowControls.clientWidth - 10 < (this.titleContainer.clientWidth + this.title.clientWidth) / 2)) {
			this.title.style.position = null;
			this.title.style.left = null;
			this.title.style.transform = null;
		} else {
			this.title.style.position = 'absolute';
			this.title.style.left = '50%';
			this.title.style.transform = 'translate(-50%, 0)';
		}
S
SteVen Batten 已提交
526 527
	}

528
	layout(dimension: Dimension): Dimension[] {
B
Benjamin Pasero 已提交
529
		if (getTitleBarStyle(this.configurationService, this.environmentService) === 'custom') {
530 531
			// Only prevent zooming behavior on macOS or when the menubar is not visible
			if (isMacintosh || this.configurationService.getValue<MenuBarVisibility>('window.menuBarVisibility') === 'hidden') {
532
				this.title.style.zoom = `${1 / getZoomFactor()}`;
533
				if (isWindows || isLinux) {
534 535
					this.appIcon.style.zoom = `${1 / getZoomFactor()}`;
					this.windowControls.style.zoom = `${1 / getZoomFactor()}`;
S
SteVen Batten 已提交
536
				}
537 538 539 540 541
			} else {
				this.title.style.zoom = null;
				if (isWindows || isLinux) {
					this.appIcon.style.zoom = null;
					this.windowControls.style.zoom = null;
S
SteVen Batten 已提交
542
				}
543 544
			}

S
SteVen Batten 已提交
545
			runAtThisOrScheduleAtNextAnimationFrame(() => this.adjustTitleMarginToCenter());
546

547 548 549 550
			if (this.menubarPart) {
				const menubarDimension = new Dimension(undefined, dimension.height);
				this.menubarPart.layout(menubarDimension);
			}
551
		}
B
Benjamin Pasero 已提交
552 553 554

		return super.layout(dimension);
	}
B
Benjamin Pasero 已提交
555 556 557 558
}

class ShowItemInFolderAction extends Action {

559 560
	constructor(private path: string, label: string, private windowsService: IWindowsService) {
		super('showItemInFolder.action.id', label);
B
Benjamin Pasero 已提交
561 562
	}

J
Johannes Rieken 已提交
563
	run(): Promise<void> {
B
Benjamin Pasero 已提交
564 565
		return this.windowsService.showItemInFolder(this.path);
	}
R
Ryan Adolf 已提交
566
}
S
SteVen Batten 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

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