menubar.ts 32.2 KB
Newer Older
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 * as nls from 'vs/nls';
7
import { isMacintosh, language } from 'vs/base/common/platform';
8
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
9
import { app, shell, Menu, MenuItem, BrowserWindow, MenuItemConstructorOptions, WebContents, Event, KeyboardEvent } from 'electron';
10 11
import { getTitleBarStyle, IDesktopRunActionInWindowRequest, IDesktopRunKeybindingInWindowRequest, IWindowOpenable } from 'vs/platform/windows/common/windows';
import { OpenContext } from 'vs/platform/windows/node/window';
12
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
13 14
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IUpdateService, StateType } from 'vs/platform/update/common/update';
15
import product from 'vs/platform/product/common/product';
16
import { RunOnceScheduler } from 'vs/base/common/async';
17
import { ILogService } from 'vs/platform/log/common/log';
18
import { mnemonicMenuLabel } from 'vs/base/common/labels';
19
import { IWindowsMainService, IWindowsCountChangedEvent } from 'vs/platform/windows/electron-main/windows';
20
import { IWorkspacesHistoryMainService } from 'vs/platform/workspaces/electron-main/workspacesHistoryMainService';
B
Benjamin Pasero 已提交
21
import { IMenubarData, IMenubarKeybinding, MenubarMenuItem, isMenubarMenuItemSeparator, isMenubarMenuItemSubmenu, isMenubarMenuItemAction, IMenubarMenu, isMenubarMenuItemUriAction } from 'vs/platform/menubar/common/menubar';
22
import { URI } from 'vs/base/common/uri';
23
import { IStateService } from 'vs/platform/state/node/state';
24
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
25
import { WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification } from 'vs/base/common/actions';
26
import { IElectronMainService } from 'vs/platform/electron/electron-main/electronMainService';
27
import { INativeEnvironmentService } from 'vs/platform/environment/node/environmentService';
28 29 30

const telemetryFrom = 'menu';

31
interface IMenuItemClickHandler {
32
	inDevTools: (contents: WebContents) => void;
33 34 35
	inNoWindow: () => void;
}

36 37 38 39 40 41 42 43 44
type IMenuItemInvocation = (
	{ type: 'commandId'; commandId: string; }
	| { type: 'keybinding'; userSettingsLabel: string; }
);

interface IMenuItemWithKeybinding {
	userSettingsLabel?: string;
}

45 46
export class Menubar {

47
	private static readonly lastKnownMenubarStorageKey = 'lastKnownMenubarData';
48

49 50
	private willShutdown: boolean | undefined;
	private appMenuInstalled: boolean | undefined;
51
	private closedLastWindow: boolean;
S
SteVen Batten 已提交
52
	private noActiveWindow: boolean;
53 54

	private menuUpdater: RunOnceScheduler;
55 56 57 58 59
	private menuGC: RunOnceScheduler;

	// Array to keep menus around so that GC doesn't cause crash as explained in #55347
	// TODO@sbatten Remove this when fixed upstream by Electron
	private oldMenus: Menu[];
60

61
	private menubarMenus: { [id: string]: IMenubarMenu };
62

S
SteVen Batten 已提交
63
	private keybindings: { [commandId: string]: IMenubarKeybinding };
S
SteVen Batten 已提交
64

65
	private readonly fallbackMenuHandlers: { [id: string]: (menuItem: MenuItem, browserWindow: BrowserWindow | undefined, event: Event) => void } = Object.create(null);
66

67
	constructor(
68 69 70
		@IUpdateService private readonly updateService: IUpdateService,
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
71
		@IEnvironmentService private readonly environmentService: INativeEnvironmentService,
72
		@ITelemetryService private readonly telemetryService: ITelemetryService,
73
		@IWorkspacesHistoryMainService private readonly workspacesHistoryMainService: IWorkspacesHistoryMainService,
74
		@IStateService private readonly stateService: IStateService,
75
		@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
76 77
		@ILogService private readonly logService: ILogService,
		@IElectronMainService private readonly electronMainService: IElectronMainService
78 79
	) {
		this.menuUpdater = new RunOnceScheduler(() => this.doUpdateMenu(), 0);
S
SteVen Batten 已提交
80

81 82
		this.menuGC = new RunOnceScheduler(() => { this.oldMenus = []; }, 10000);

83 84
		this.menubarMenus = Object.create(null);
		this.keybindings = Object.create(null);
85

B
Benjamin Pasero 已提交
86
		if (isMacintosh || getTitleBarStyle(this.configurationService, this.environmentService) === 'native') {
87 88
			this.restoreCachedMenubarData();
		}
89 90

		this.addFallbackHandlers();
91

92
		this.closedLastWindow = false;
S
SteVen Batten 已提交
93
		this.noActiveWindow = false;
94

95 96
		this.oldMenus = [];

97 98 99 100 101
		this.install();

		this.registerListeners();
	}

102 103 104 105 106 107 108 109 110 111 112 113 114
	private restoreCachedMenubarData() {
		const menubarData = this.stateService.getItem<IMenubarData>(Menubar.lastKnownMenubarStorageKey);
		if (menubarData) {
			if (menubarData.menus) {
				this.menubarMenus = menubarData.menus;
			}

			if (menubarData.keybindings) {
				this.keybindings = menubarData.keybindings;
			}
		}
	}

115
	private addFallbackHandlers(): void {
116

117
		// File Menu Items
S
SteVen Batten 已提交
118 119
		this.fallbackMenuHandlers['workbench.action.files.newUntitledFile'] = (menuItem, win, event) => this.windowsMainService.openEmptyWindow({ context: OpenContext.MENU, contextWindowId: win?.id });
		this.fallbackMenuHandlers['workbench.action.newWindow'] = (menuItem, win, event) => this.windowsMainService.openEmptyWindow({ context: OpenContext.MENU, contextWindowId: win?.id });
120 121
		this.fallbackMenuHandlers['workbench.action.files.openFileFolder'] = (menuItem, win, event) => this.electronMainService.pickFileFolderAndOpen(undefined, { forceNewWindow: this.isOptionClick(event), telemetryExtraData: { from: telemetryFrom } });
		this.fallbackMenuHandlers['workbench.action.openWorkspace'] = (menuItem, win, event) => this.electronMainService.pickWorkspaceAndOpen(undefined, { forceNewWindow: this.isOptionClick(event), telemetryExtraData: { from: telemetryFrom } });
122 123

		// Recent Menu Items
124
		this.fallbackMenuHandlers['workbench.action.clearRecentFiles'] = () => this.workspacesHistoryMainService.clearRecentlyOpened();
125 126

		// Help Menu Items
127 128 129
		const twitterUrl = product.twitterUrl;
		if (twitterUrl) {
			this.fallbackMenuHandlers['workbench.action.openTwitterUrl'] = () => this.openUrl(twitterUrl, 'openTwitterUrl');
130 131
		}

132 133 134
		const requestFeatureUrl = product.requestFeatureUrl;
		if (requestFeatureUrl) {
			this.fallbackMenuHandlers['workbench.action.openRequestFeatureUrl'] = () => this.openUrl(requestFeatureUrl, 'openUserVoiceUrl');
135 136
		}

137 138 139
		const reportIssueUrl = product.reportIssueUrl;
		if (reportIssueUrl) {
			this.fallbackMenuHandlers['workbench.action.openIssueReporter'] = () => this.openUrl(reportIssueUrl, 'openReportIssues');
140 141
		}

142 143
		const licenseUrl = product.licenseUrl;
		if (licenseUrl) {
S
SteVen Batten 已提交
144
			this.fallbackMenuHandlers['workbench.action.openLicenseUrl'] = () => {
145
				if (language) {
146 147
					const queryArgChar = licenseUrl.indexOf('?') > 0 ? '&' : '?';
					this.openUrl(`${licenseUrl}${queryArgChar}lang=${language}`, 'openLicenseUrl');
148
				} else {
149
					this.openUrl(licenseUrl, 'openLicenseUrl');
150 151 152 153
				}
			};
		}

154 155
		const privacyStatementUrl = product.privacyStatementUrl;
		if (privacyStatementUrl && licenseUrl) {
156 157
			this.fallbackMenuHandlers['workbench.action.openPrivacyStatementUrl'] = () => {
				if (language) {
158 159
					const queryArgChar = licenseUrl.indexOf('?') > 0 ? '&' : '?';
					this.openUrl(`${privacyStatementUrl}${queryArgChar}lang=${language}`, 'openPrivacyStatement');
160
				} else {
161
					this.openUrl(privacyStatementUrl, 'openPrivacyStatement');
162 163 164 165 166
				}
			};
		}
	}

167 168
	private registerListeners(): void {
		// Keep flag when app quits
169
		this.lifecycleMainService.onWillShutdown(() => this.willShutdown = true);
170 171

		// // Listen to some events from window service to update menu
172
		this.windowsMainService.onWindowsCountChanged(e => this.onWindowsCountChanged(e));
S
SteVen Batten 已提交
173 174
		this.electronMainService.onWindowBlur(() => this.onWindowFocusChange());
		this.electronMainService.onWindowFocus(() => this.onWindowFocusChange());
175 176 177 178 179 180 181
	}

	private get currentEnableMenuBarMnemonics(): boolean {
		let enableMenuBarMnemonics = this.configurationService.getValue<boolean>('window.enableMenuBarMnemonics');
		if (typeof enableMenuBarMnemonics !== 'boolean') {
			enableMenuBarMnemonics = true;
		}
182

183 184 185 186
		return enableMenuBarMnemonics;
	}

	private get currentEnableNativeTabs(): boolean {
S
SteVen Batten 已提交
187 188 189 190
		if (!isMacintosh) {
			return false;
		}

191 192 193 194 195 196 197
		let enableNativeTabs = this.configurationService.getValue<boolean>('window.nativeTabs');
		if (typeof enableNativeTabs !== 'boolean') {
			enableNativeTabs = false;
		}
		return enableNativeTabs;
	}

198 199 200
	updateMenu(menubarData: IMenubarData, windowId: number) {
		this.menubarMenus = menubarData.menus;
		this.keybindings = menubarData.keybindings;
S
SteVen Batten 已提交
201

202
		// Save off new menu and keybindings
203
		this.stateService.setItem(Menubar.lastKnownMenubarStorageKey, menubarData);
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		this.scheduleUpdateMenu();
	}


	private scheduleUpdateMenu(): void {
		this.menuUpdater.schedule(); // buffer multiple attempts to update the menu
	}

	private doUpdateMenu(): void {

		// Due to limitations in Electron, it is not possible to update menu items dynamically. The suggested
		// workaround from Electron is to set the application menu again.
		// See also https://github.com/electron/electron/issues/846
		//
		// Run delayed to prevent updating menu while it is open
B
Benjamin Pasero 已提交
220
		if (!this.willShutdown) {
221
			setTimeout(() => {
B
Benjamin Pasero 已提交
222
				if (!this.willShutdown) {
223 224 225 226 227 228
					this.install();
				}
			}, 10 /* delay this because there is an issue with updating a menu when it is open */);
		}
	}

229 230 231 232 233 234 235
	private onWindowsCountChanged(e: IWindowsCountChangedEvent): void {
		if (!isMacintosh) {
			return;
		}

		// Update menu if window count goes from N > 0 or 0 > N to update menu item enablement
		if ((e.oldCount === 0 && e.newCount > 0) || (e.oldCount > 0 && e.newCount === 0)) {
236
			this.closedLastWindow = e.newCount === 0;
237 238 239
			this.scheduleUpdateMenu();
		}
	}
240

S
SteVen Batten 已提交
241 242 243 244 245 246 247 248 249
	private onWindowFocusChange(): void {
		if (!isMacintosh) {
			return;
		}

		this.noActiveWindow = !BrowserWindow.getFocusedWindow();
		this.scheduleUpdateMenu();
	}

250
	private install(): void {
251 252 253 254 255 256
		// Store old menu in our array to avoid GC to collect the menu and crash. See #55347
		// TODO@sbatten Remove this when fixed upstream by Electron
		const oldMenu = Menu.getApplicationMenu();
		if (oldMenu) {
			this.oldMenus.push(oldMenu);
		}
257

258 259 260 261 262 263 264
		// If we don't have a menu yet, set it to null to avoid the electron menu.
		// This should only happen on the first launch ever
		if (Object.keys(this.menubarMenus).length === 0) {
			Menu.setApplicationMenu(isMacintosh ? new Menu() : null);
			return;
		}

265 266 267 268
		// Menus
		const menubar = new Menu();

		// Mac: Application
269
		let macApplicationMenuItem: MenuItem;
270 271 272 273
		if (isMacintosh) {
			const applicationMenu = new Menu();
			macApplicationMenuItem = new MenuItem({ label: product.nameShort, submenu: applicationMenu });
			this.setMacApplicationMenu(applicationMenu);
274
			menubar.append(macApplicationMenuItem);
275 276
		}

277
		// Mac: Dock
278 279 280 281
		if (isMacintosh && !this.appMenuInstalled) {
			this.appMenuInstalled = true;

			const dockMenu = new Menu();
282
			dockMenu.append(new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'miNewWindow', comment: ['&& denotes a mnemonic'] }, "New &&Window")), click: () => this.windowsMainService.openEmptyWindow({ context: OpenContext.DOCK }) }));
283 284 285 286

			app.dock.setMenu(dockMenu);
		}

287 288 289 290
		// File
		const fileMenu = new Menu();
		const fileMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mFile', comment: ['&& denotes a mnemonic'] }, "&&File")), submenu: fileMenu });

291 292
		this.setMenuById(fileMenu, 'File');
		menubar.append(fileMenuItem);
293

294 295 296 297
		// Edit
		const editMenu = new Menu();
		const editMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mEdit', comment: ['&& denotes a mnemonic'] }, "&&Edit")), submenu: editMenu });

298 299
		this.setMenuById(editMenu, 'Edit');
		menubar.append(editMenuItem);
300 301 302 303

		// Selection
		const selectionMenu = new Menu();
		const selectionMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mSelection', comment: ['&& denotes a mnemonic'] }, "&&Selection")), submenu: selectionMenu });
304

305 306
		this.setMenuById(selectionMenu, 'Selection');
		menubar.append(selectionMenuItem);
307 308

		// View
309 310 311
		const viewMenu = new Menu();
		const viewMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mView', comment: ['&& denotes a mnemonic'] }, "&&View")), submenu: viewMenu });

312 313
		this.setMenuById(viewMenu, 'View');
		menubar.append(viewMenuItem);
314

315
		// Go
316 317
		const gotoMenu = new Menu();
		const gotoMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mGoto', comment: ['&& denotes a mnemonic'] }, "&&Go")), submenu: gotoMenu });
318

319 320
		this.setMenuById(gotoMenu, 'Go');
		menubar.append(gotoMenuItem);
321

322 323
		// Debug
		const debugMenu = new Menu();
324
		const debugMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mRun', comment: ['&& denotes a mnemonic'] }, "&&Run")), submenu: debugMenu });
325

326
		this.setMenuById(debugMenu, 'Run');
327
		menubar.append(debugMenuItem);
328

329 330 331
		// Terminal
		const terminalMenu = new Menu();
		const terminalMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mTerminal', comment: ['&& denotes a mnemonic'] }, "&&Terminal")), submenu: terminalMenu });
332

333 334
		this.setMenuById(terminalMenu, 'Terminal');
		menubar.append(terminalMenuItem);
335

336
		// Mac: Window
337
		let macWindowMenuItem: MenuItem | undefined;
S
SteVen Batten 已提交
338
		if (this.shouldDrawMenu('Window')) {
339 340 341 342
			const windowMenu = new Menu();
			macWindowMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize('mWindow', "Window")), submenu: windowMenu, role: 'window' });
			this.setMacWindowMenu(windowMenu);
		}
343 344 345 346 347

		if (macWindowMenuItem) {
			menubar.append(macWindowMenuItem);
		}

348 349 350 351
		// Help
		const helpMenu = new Menu();
		const helpMenuItem = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'mHelp', comment: ['&& denotes a mnemonic'] }, "&&Help")), submenu: helpMenu, role: 'help' });

352 353
		this.setMenuById(helpMenu, 'Help');
		menubar.append(helpMenuItem);
354

S
SteVen Batten 已提交
355 356 357 358 359
		if (menubar.items && menubar.items.length > 0) {
			Menu.setApplicationMenu(menubar);
		} else {
			Menu.setApplicationMenu(null);
		}
360 361 362

		// Dispose of older menus after some time
		this.menuGC.schedule();
363 364
	}

365
	private setMacApplicationMenu(macApplicationMenu: Menu): void {
366
		const about = this.createMenuItem(nls.localize('mAbout', "About {0}", product.nameLong), 'workbench.action.showAboutDialog');
367
		const checkForUpdates = this.getUpdateMenuItems();
S
SteVen Batten 已提交
368 369 370 371 372 373 374 375

		let preferences;
		if (this.shouldDrawMenu('Preferences')) {
			const preferencesMenu = new Menu();
			this.setMenuById(preferencesMenu, 'Preferences');
			preferences = new MenuItem({ label: this.mnemonicLabel(nls.localize({ key: 'miPreferences', comment: ['&& denotes a mnemonic'] }, "&&Preferences")), submenu: preferencesMenu });
		}

376 377 378
		const servicesMenu = new Menu();
		const services = new MenuItem({ label: nls.localize('mServices', "Services"), role: 'services', submenu: servicesMenu });
		const hide = new MenuItem({ label: nls.localize('mHide', "Hide {0}", product.nameLong), role: 'hide', accelerator: 'Command+H' });
379
		const hideOthers = new MenuItem({ label: nls.localize('mHideOthers', "Hide Others"), role: 'hideOthers', accelerator: 'Command+Alt+H' });
380 381 382
		const showAll = new MenuItem({ label: nls.localize('mShowAll', "Show All"), role: 'unhide' });
		const quit = new MenuItem(this.likeAction('workbench.action.quit', {
			label: nls.localize('miQuit', "Quit {0}", product.nameLong), click: () => {
383
				const lastActiveWindow = this.windowsMainService.getLastActiveWindow();
B
Benjamin Pasero 已提交
384
				if (
B
Benjamin Pasero 已提交
385 386 387
					this.windowsMainService.getWindowCount() === 0 || 	// allow to quit when no more windows are open
					!!BrowserWindow.getFocusedWindow() ||				// allow to quit when window has focus (fix for https://github.com/Microsoft/vscode/issues/39191)
					lastActiveWindow?.isMinimized()						// allow to quit when window has no focus but is minimized (https://github.com/Microsoft/vscode/issues/63000)
B
Benjamin Pasero 已提交
388
				) {
389
					this.electronMainService.quit(undefined);
390 391 392 393 394 395
				}
			}
		}));

		const actions = [about];
		actions.push(...checkForUpdates);
S
SteVen Batten 已提交
396 397 398 399 400 401 402 403

		if (preferences) {
			actions.push(...[
				__separator__(),
				preferences
			]);
		}

404 405 406 407 408 409 410 411 412 413 414 415 416 417
		actions.push(...[
			__separator__(),
			services,
			__separator__(),
			hide,
			hideOthers,
			showAll,
			__separator__(),
			quit
		]);

		actions.forEach(i => macApplicationMenu.append(i));
	}

418
	private shouldDrawMenu(menuId: string): boolean {
S
SteVen Batten 已提交
419
		// We need to draw an empty menu to override the electron default
B
Benjamin Pasero 已提交
420
		if (!isMacintosh && getTitleBarStyle(this.configurationService, this.environmentService) === 'custom') {
S
SteVen Batten 已提交
421 422 423
			return false;
		}

424 425 426
		switch (menuId) {
			case 'File':
			case 'Help':
S
SteVen Batten 已提交
427
				if (isMacintosh) {
S
SteVen Batten 已提交
428
					return (this.windowsMainService.getWindowCount() === 0 && this.closedLastWindow) || (this.windowsMainService.getWindowCount() > 0 && this.noActiveWindow) || (!!this.menubarMenus && !!this.menubarMenus[menuId]);
429
				}
S
SteVen Batten 已提交
430

431 432
			case 'Window':
				if (isMacintosh) {
S
SteVen Batten 已提交
433
					return (this.windowsMainService.getWindowCount() === 0 && this.closedLastWindow) || (this.windowsMainService.getWindowCount() > 0 && this.noActiveWindow) || !!this.menubarMenus;
S
SteVen Batten 已提交
434
				}
S
SteVen Batten 已提交
435

436
			default:
437
				return this.windowsMainService.getWindowCount() > 0 && (!!this.menubarMenus && !!this.menubarMenus[menuId]);
438 439 440 441
		}
	}


442
	private setMenu(menu: Menu, items: Array<MenubarMenuItem>) {
443 444
		items.forEach((item: MenubarMenuItem) => {
			if (isMenubarMenuItemSeparator(item)) {
445
				menu.append(__separator__());
446 447
			} else if (isMenubarMenuItemSubmenu(item)) {
				const submenu = new Menu();
448
				const submenuItem = new MenuItem({ label: this.mnemonicLabel(item.label), submenu });
449 450
				this.setMenu(submenu, item.submenu.items);
				menu.append(submenuItem);
451
			} else if (isMenubarMenuItemUriAction(item)) {
452
				menu.append(this.createOpenRecentMenuItem(item.uri, item.label, item.id));
453
			} else if (isMenubarMenuItemAction(item)) {
454
				if (item.id === 'workbench.action.showAboutDialog') {
S
SteVen Batten 已提交
455
					this.insertCheckForUpdatesItems(menu);
456 457
				}

458
				if (isMacintosh) {
S
SteVen Batten 已提交
459 460
					if ((this.windowsMainService.getWindowCount() === 0 && this.closedLastWindow) ||
						(this.windowsMainService.getWindowCount() > 0 && this.noActiveWindow)) {
461 462 463 464 465 466 467
						// In the fallback scenario, we are either disabled or using a fallback handler
						if (this.fallbackMenuHandlers[item.id]) {
							menu.append(new MenuItem(this.likeAction(item.id, { label: this.mnemonicLabel(item.label), click: this.fallbackMenuHandlers[item.id] })));
						} else {
							menu.append(this.createMenuItem(item.label, item.id, false, item.checked));
						}
					} else {
468
						menu.append(this.createMenuItem(item.label, item.id, item.enabled === false ? false : true, !!item.checked));
469 470
					}
				} else {
471
					menu.append(this.createMenuItem(item.label, item.id, item.enabled === false ? false : true, !!item.checked));
472
				}
473
			}
474 475 476
		});
	}

477
	private setMenuById(menu: Menu, menuId: string): void {
478
		if (this.menubarMenus && this.menubarMenus[menuId]) {
S
SteVen Batten 已提交
479 480
			this.setMenu(menu, this.menubarMenus[menuId].items);
		}
481 482
	}

483
	private insertCheckForUpdatesItems(menu: Menu) {
S
SteVen Batten 已提交
484 485 486 487 488 489 490
		const updateItems = this.getUpdateMenuItems();
		if (updateItems.length) {
			updateItems.forEach(i => menu.append(i));
			menu.append(__separator__());
		}
	}

491
	private createOpenRecentMenuItem(uri: URI, label: string, commandId: string): MenuItem {
492
		const revivedUri = URI.revive(uri);
493
		const openable: IWindowOpenable =
M
Martin Aeschlimann 已提交
494 495
			(commandId === 'openRecentFile') ? { fileUri: revivedUri } :
				(commandId === 'openRecentWorkspace') ? { workspaceUri: revivedUri } : { folderUri: revivedUri };
496 497 498 499 500 501 502 503

		return new MenuItem(this.likeAction(commandId, {
			label,
			click: (menuItem, win, event) => {
				const openInNewWindow = this.isOptionClick(event);
				const success = this.windowsMainService.open({
					context: OpenContext.MENU,
					cli: this.environmentService.args,
504
					urisToOpen: [openable],
505 506
					forceNewWindow: openInNewWindow,
					gotoLineMode: false
507 508 509
				}).length > 0;

				if (!success) {
B
Benjamin Pasero 已提交
510
					this.workspacesHistoryMainService.removeRecentlyOpened([revivedUri]);
511 512 513 514 515
				}
			}
		}, false));
	}

516
	private isOptionClick(event: KeyboardEvent): boolean {
M
Matt Bierner 已提交
517
		return !!(event && ((!isMacintosh && (event.ctrlKey || event.shiftKey)) || (isMacintosh && (event.metaKey || event.altKey))));
518 519
	}

520 521
	private createRoleMenuItem(label: string, commandId: string, role: any): MenuItem {
		const options: MenuItemConstructorOptions = {
522 523 524 525 526 527 528 529
			label: this.mnemonicLabel(label),
			role,
			enabled: true
		};

		return new MenuItem(this.withKeybinding(commandId, options));
	}

530
	private setMacWindowMenu(macWindowMenu: Menu): void {
531 532 533 534 535
		const minimize = new MenuItem({ label: nls.localize('mMinimize', "Minimize"), role: 'minimize', accelerator: 'Command+M', enabled: this.windowsMainService.getWindowCount() > 0 });
		const zoom = new MenuItem({ label: nls.localize('mZoom', "Zoom"), role: 'zoom', enabled: this.windowsMainService.getWindowCount() > 0 });
		const bringAllToFront = new MenuItem({ label: nls.localize('mBringToFront', "Bring All to Front"), role: 'front', enabled: this.windowsMainService.getWindowCount() > 0 });
		const switchWindow = this.createMenuItem(nls.localize({ key: 'miSwitchWindow', comment: ['&& denotes a mnemonic'] }, "Switch &&Window..."), 'workbench.action.switchWindow');

536
		const nativeTabMenuItems: MenuItem[] = [];
537
		if (this.currentEnableNativeTabs) {
538 539
			nativeTabMenuItems.push(__separator__());

B
Benjamin Pasero 已提交
540
			nativeTabMenuItems.push(this.createMenuItem(nls.localize('mNewTab', "New Tab"), 'workbench.action.newWindowTab'));
541

B
Benjamin Pasero 已提交
542 543 544 545
			nativeTabMenuItems.push(this.createRoleMenuItem(nls.localize('mShowPreviousTab', "Show Previous Tab"), 'workbench.action.showPreviousWindowTab', 'selectPreviousTab'));
			nativeTabMenuItems.push(this.createRoleMenuItem(nls.localize('mShowNextTab', "Show Next Tab"), 'workbench.action.showNextWindowTab', 'selectNextTab'));
			nativeTabMenuItems.push(this.createRoleMenuItem(nls.localize('mMoveTabToNewWindow', "Move Tab to New Window"), 'workbench.action.moveWindowTabToNewWindow', 'moveTabToNewWindow'));
			nativeTabMenuItems.push(this.createRoleMenuItem(nls.localize('mMergeAllWindows', "Merge All Windows"), 'workbench.action.mergeAllWindowTabs', 'mergeAllWindows'));
546 547 548 549 550
		}

		[
			minimize,
			zoom,
551
			__separator__(),
552 553 554 555 556 557 558
			switchWindow,
			...nativeTabMenuItems,
			__separator__(),
			bringAllToFront
		].forEach(item => macWindowMenu.append(item));
	}

559
	private getUpdateMenuItems(): MenuItem[] {
560 561 562 563 564 565 566 567
		const state = this.updateService.state;

		switch (state.type) {
			case StateType.Uninitialized:
				return [];

			case StateType.Idle:
				return [new MenuItem({
S
SteVen Batten 已提交
568
					label: this.mnemonicLabel(nls.localize('miCheckForUpdates', "Check for &&Updates...")), click: () => setTimeout(() => {
569 570
						this.reportMenuActionTelemetry('CheckForUpdate');

J
Joao Moreno 已提交
571
						const window = this.windowsMainService.getLastActiveWindow();
J
Joao Moreno 已提交
572
						const context = window && `window:${window.id}`; // sessionId
573 574 575 576 577
						this.updateService.checkForUpdates(context);
					}, 0)
				})];

			case StateType.CheckingForUpdates:
578
				return [new MenuItem({ label: nls.localize('miCheckingForUpdates', "Checking for Updates..."), enabled: false })];
579 580 581

			case StateType.AvailableForDownload:
				return [new MenuItem({
S
SteVen Batten 已提交
582
					label: this.mnemonicLabel(nls.localize('miDownloadUpdate', "D&&ownload Available Update")), click: () => {
583 584 585 586 587 588 589 590 591
						this.updateService.downloadUpdate();
					}
				})];

			case StateType.Downloading:
				return [new MenuItem({ label: nls.localize('miDownloadingUpdate', "Downloading Update..."), enabled: false })];

			case StateType.Downloaded:
				return [new MenuItem({
S
SteVen Batten 已提交
592
					label: this.mnemonicLabel(nls.localize('miInstallUpdate', "Install &&Update...")), click: () => {
593 594 595 596 597 598 599 600 601 602
						this.reportMenuActionTelemetry('InstallUpdate');
						this.updateService.applyUpdate();
					}
				})];

			case StateType.Updating:
				return [new MenuItem({ label: nls.localize('miInstallingUpdate', "Installing Update..."), enabled: false })];

			case StateType.Ready:
				return [new MenuItem({
J
Joao Moreno 已提交
603
					label: this.mnemonicLabel(nls.localize('miRestartToUpdate', "Restart to &&Update")), click: () => {
604 605 606 607 608 609 610
						this.reportMenuActionTelemetry('RestartToUpdate');
						this.updateService.quitAndInstall();
					}
				})];
		}
	}

611
	private static _menuItemIsTriggeredViaKeybinding(event: KeyboardEvent, userSettingsLabel: string): boolean {
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		// The event coming in from Electron will inform us only about the modifier keys pressed.
		// The strategy here is to check if the modifier keys match those of the keybinding,
		// since it is highly unlikely to use modifier keys when clicking with the mouse
		if (!userSettingsLabel) {
			// There is no keybinding
			return false;
		}

		let ctrlRequired = /ctrl/.test(userSettingsLabel);
		let shiftRequired = /shift/.test(userSettingsLabel);
		let altRequired = /alt/.test(userSettingsLabel);
		let metaRequired = /cmd/.test(userSettingsLabel) || /super/.test(userSettingsLabel);

		if (!ctrlRequired && !shiftRequired && !altRequired && !metaRequired) {
			// This keybinding does not use any modifier keys, so we cannot use this heuristic
			return false;
		}

		return (
			ctrlRequired === event.ctrlKey
			&& shiftRequired === event.shiftKey
			&& altRequired === event.altKey
			&& metaRequired === event.metaKey
		);
	}

638 639 640
	private createMenuItem(label: string, commandId: string | string[], enabled?: boolean, checked?: boolean): MenuItem;
	private createMenuItem(label: string, click: () => void, enabled?: boolean, checked?: boolean): MenuItem;
	private createMenuItem(arg1: string, arg2: any, arg3?: boolean, arg4?: boolean): MenuItem {
641
		const label = this.mnemonicLabel(arg1);
642
		const click: () => void = (typeof arg2 === 'function') ? arg2 : (menuItem: MenuItem & IMenuItemWithKeybinding, win: BrowserWindow, event: Event) => {
A
Alex Dima 已提交
643
			const userSettingsLabel = menuItem ? menuItem.userSettingsLabel : null;
644 645 646 647 648
			let commandId = arg2;
			if (Array.isArray(arg2)) {
				commandId = this.isOptionClick(event) ? arg2[1] : arg2[0]; // support alternative action if we got multiple action Ids and the option key was pressed while invoking
			}

649
			if (userSettingsLabel && Menubar._menuItemIsTriggeredViaKeybinding(event, userSettingsLabel)) {
650 651 652 653
				this.runActionInRenderer({ type: 'keybinding', userSettingsLabel });
			} else {
				this.runActionInRenderer({ type: 'commandId', commandId });
			}
654 655 656 657
		};
		const enabled = typeof arg3 === 'boolean' ? arg3 : this.windowsMainService.getWindowCount() > 0;
		const checked = typeof arg4 === 'boolean' ? arg4 : false;

658
		const options: MenuItemConstructorOptions = {
659 660 661 662 663 664
			label,
			click,
			enabled
		};

		if (checked) {
665 666
			options.type = 'checkbox';
			options.checked = checked;
667 668
		}

M
Matt Bierner 已提交
669
		let commandId: string | undefined;
670 671 672 673 674 675
		if (typeof arg2 === 'string') {
			commandId = arg2;
		} else if (Array.isArray(arg2)) {
			commandId = arg2[0];
		}

S
SteVen Batten 已提交
676
		if (isMacintosh) {
677

678
			// Add role for special case menu items
S
SteVen Batten 已提交
679
			if (commandId === 'editor.action.clipboardCutAction') {
680
				options.role = 'cut';
S
SteVen Batten 已提交
681
			} else if (commandId === 'editor.action.clipboardCopyAction') {
682
				options.role = 'copy';
S
SteVen Batten 已提交
683
			} else if (commandId === 'editor.action.clipboardPasteAction') {
684
				options.role = 'paste';
S
SteVen Batten 已提交
685
			}
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

			// Add context aware click handlers for special case menu items
			if (commandId === 'undo') {
				options.click = this.makeContextAwareClickHandler(click, {
					inDevTools: devTools => devTools.undo(),
					inNoWindow: () => Menu.sendActionToFirstResponder('undo:')
				});
			} else if (commandId === 'redo') {
				options.click = this.makeContextAwareClickHandler(click, {
					inDevTools: devTools => devTools.redo(),
					inNoWindow: () => Menu.sendActionToFirstResponder('redo:')
				});
			} else if (commandId === 'editor.action.selectAll') {
				options.click = this.makeContextAwareClickHandler(click, {
					inDevTools: devTools => devTools.selectAll(),
					inNoWindow: () => Menu.sendActionToFirstResponder('selectAll:')
				});
			}
S
SteVen Batten 已提交
704 705
		}

706 707 708
		return new MenuItem(this.withKeybinding(commandId, options));
	}

709 710
	private makeContextAwareClickHandler(click: () => void, contextSpecificHandlers: IMenuItemClickHandler): () => void {
		return () => {
711

712
			// No Active Window
713
			const activeWindow = BrowserWindow.getFocusedWindow();
714 715 716 717 718
			if (!activeWindow) {
				return contextSpecificHandlers.inNoWindow();
			}

			// DevTools focused
R
Robo 已提交
719 720
			if (activeWindow.webContents.isDevToolsFocused() &&
				activeWindow.webContents.devToolsWebContents) {
721
				return contextSpecificHandlers.inDevTools(activeWindow.webContents.devToolsWebContents);
722 723 724 725 726 727 728
			}

			// Finally execute command in Window
			click();
		};
	}

729
	private runActionInRenderer(invocation: IMenuItemInvocation): void {
730 731 732
		// We make sure to not run actions when the window has no focus, this helps
		// for https://github.com/Microsoft/vscode/issues/25907 and specifically for
		// https://github.com/Microsoft/vscode/issues/11928
B
Benjamin Pasero 已提交
733 734
		// Still allow to run when the last active window is minimized though for
		// https://github.com/Microsoft/vscode/issues/63000
735 736
		let activeBrowserWindow = BrowserWindow.getFocusedWindow();
		if (!activeBrowserWindow) {
B
Benjamin Pasero 已提交
737
			const lastActiveWindow = this.windowsMainService.getLastActiveWindow();
B
Benjamin Pasero 已提交
738
			if (lastActiveWindow?.isMinimized()) {
739
				activeBrowserWindow = lastActiveWindow.win;
B
Benjamin Pasero 已提交
740 741 742
			}
		}

743
		const activeWindow = activeBrowserWindow ? this.windowsMainService.getWindowById(activeBrowserWindow.id) : undefined;
744
		if (activeWindow) {
745 746
			this.logService.trace('menubar#runActionInRenderer', invocation);

B
Benjamin Pasero 已提交
747 748
			if (isMacintosh && !this.environmentService.isBuilt && !activeWindow.isReady) {
				if ((invocation.type === 'commandId' && invocation.commandId === 'workbench.action.toggleDevTools') || (invocation.type !== 'commandId' && invocation.userSettingsLabel === 'alt+cmd+i')) {
749 750 751 752 753
					// prevent this action from running twice on macOS (https://github.com/Microsoft/vscode/issues/62719)
					// we already register a keybinding in bootstrap-window.js for opening developer tools in case something
					// goes wrong and that keybinding is only removed when the application has loaded (= window ready).
					return;
				}
B
Benjamin Pasero 已提交
754
			}
B
Benjamin Pasero 已提交
755

B
Benjamin Pasero 已提交
756
			if (invocation.type === 'commandId') {
757 758
				const runActionPayload: IDesktopRunActionInWindowRequest = { id: invocation.commandId, from: 'menu' };
				activeWindow.sendWhenReady('vscode:runAction', runActionPayload);
B
Benjamin Pasero 已提交
759
			} else {
760 761
				const runKeybindingPayload: IDesktopRunKeybindingInWindowRequest = { userSettingsLabel: invocation.userSettingsLabel };
				activeWindow.sendWhenReady('vscode:runKeybinding', runKeybindingPayload);
B
Benjamin Pasero 已提交
762
			}
763 764
		} else {
			this.logService.trace('menubar#runActionInRenderer: no active window found', invocation);
765 766 767
		}
	}

768
	private withKeybinding(commandId: string | undefined, options: MenuItemConstructorOptions & IMenuItemWithKeybinding): MenuItemConstructorOptions {
M
Matt Bierner 已提交
769
		const binding = typeof commandId === 'string' ? this.keybindings[commandId] : undefined;
770 771

		// Apply binding if there is one
B
Benjamin Pasero 已提交
772
		if (binding?.label) {
773 774

			// if the binding is native, we can just apply it
775
			if (binding.isNative !== false) {
776
				options.accelerator = binding.label;
777
				options.userSettingsLabel = binding.userSettingsLabel;
778 779 780 781
			}

			// the keybinding is not native so we cannot show it as part of the accelerator of
			// the menu item. we fallback to a different strategy so that we always display it
M
Matt Bierner 已提交
782
			else if (typeof options.label === 'string') {
783 784 785 786 787 788 789 790 791 792 793
				const bindingIndex = options.label.indexOf('[');
				if (bindingIndex >= 0) {
					options.label = `${options.label.substr(0, bindingIndex)} [${binding.label}]`;
				} else {
					options.label = `${options.label} [${binding.label}]`;
				}
			}
		}

		// Unset bindings if there is none
		else {
R
Rob Lourens 已提交
794
			options.accelerator = undefined;
795 796 797 798 799
		}

		return options;
	}

800
	private likeAction(commandId: string, options: MenuItemConstructorOptions, setAccelerator = !options.accelerator): MenuItemConstructorOptions {
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
		if (setAccelerator) {
			options = this.withKeybinding(commandId, options);
		}

		const originalClick = options.click;
		options.click = (item, window, event) => {
			this.reportMenuActionTelemetry(commandId);
			if (originalClick) {
				originalClick(item, window, event);
			}
		};

		return options;
	}

	private openUrl(url: string, id: string): void {
		shell.openExternal(url);
		this.reportMenuActionTelemetry(id);
	}

	private reportMenuActionTelemetry(id: string): void {
822
		this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id, from: telemetryFrom });
823 824 825
	}

	private mnemonicLabel(label: string): string {
826
		return mnemonicMenuLabel(label, !this.currentEnableMenuBarMnemonics);
827 828 829
	}
}

830
function __separator__(): MenuItem {
831 832
	return new MenuItem({ type: 'separator' });
}