Path: blob/main/src/vs/workbench/browser/parts/titlebar/menubarControl.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import './media/menubarControl.css';6import { localize, localize2 } from '../../../../nls.js';7import { IMenuService, MenuId, IMenu, SubmenuItemAction, registerAction2, Action2, MenuItemAction, MenuRegistry } from '../../../../platform/actions/common/actions.js';8import { MenuBarVisibility, IWindowOpenable, getMenuBarVisibility, MenuSettings, hasNativeMenu } from '../../../../platform/window/common/window.js';9import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';10import { IAction, Action, SubmenuAction, Separator, IActionRunner, ActionRunner, WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification, toAction } from '../../../../base/common/actions.js';11import { addDisposableListener, Dimension, EventType } from '../../../../base/browser/dom.js';12import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';13import { isMacintosh, isWeb, isIOS, isNative } from '../../../../base/common/platform.js';14import { IConfigurationService, IConfigurationChangeEvent } from '../../../../platform/configuration/common/configuration.js';15import { Event, Emitter } from '../../../../base/common/event.js';16import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';17import { IRecentlyOpened, isRecentFolder, IRecent, isRecentWorkspace, IWorkspacesService } from '../../../../platform/workspaces/common/workspaces.js';18import { RunOnceScheduler } from '../../../../base/common/async.js';19import { URI } from '../../../../base/common/uri.js';20import { ILabelService, Verbosity } from '../../../../platform/label/common/label.js';21import { IUpdateService, StateType } from '../../../../platform/update/common/update.js';22import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';23import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';24import { IPreferencesService } from '../../../services/preferences/common/preferences.js';25import { IWorkbenchEnvironmentService } from '../../../services/environment/common/environmentService.js';26import { MenuBar, IMenuBarOptions } from '../../../../base/browser/ui/menu/menubar.js';27import { HorizontalDirection, IMenuDirection, VerticalDirection } from '../../../../base/browser/ui/menu/menu.js';28import { mnemonicMenuLabel, unmnemonicLabel } from '../../../../base/common/labels.js';29import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.js';30import { isFullscreen, onDidChangeFullscreen } from '../../../../base/browser/browser.js';31import { IHostService } from '../../../services/host/browser/host.js';32import { BrowserFeatures } from '../../../../base/browser/canIUse.js';33import { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';34import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';35import { IsMacNativeContext, IsWebContext } from '../../../../platform/contextkey/common/contextkeys.js';36import { ICommandService } from '../../../../platform/commands/common/commands.js';37import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.js';38import { OpenRecentAction } from '../../actions/windowActions.js';39import { isICommandActionToggleInfo } from '../../../../platform/action/common/action.js';40import { getFlatContextMenuActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';41import { defaultMenuStyles } from '../../../../platform/theme/browser/defaultStyles.js';42import { mainWindow } from '../../../../base/browser/window.js';43import { ActivityBarPosition } from '../../../services/layout/browser/layoutService.js';4445export type IOpenRecentAction = IAction & { uri: URI; remoteAuthority?: string };4647MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {48submenu: MenuId.MenubarFileMenu,49title: {50value: 'File',51original: 'File',52mnemonicTitle: localize({ key: 'mFile', comment: ['&& denotes a mnemonic'] }, "&&File"),53},54order: 155});5657MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {58submenu: MenuId.MenubarEditMenu,59title: {60value: 'Edit',61original: 'Edit',62mnemonicTitle: localize({ key: 'mEdit', comment: ['&& denotes a mnemonic'] }, "&&Edit")63},64order: 265});6667MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {68submenu: MenuId.MenubarSelectionMenu,69title: {70value: 'Selection',71original: 'Selection',72mnemonicTitle: localize({ key: 'mSelection', comment: ['&& denotes a mnemonic'] }, "&&Selection")73},74order: 375});7677MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {78submenu: MenuId.MenubarViewMenu,79title: {80value: 'View',81original: 'View',82mnemonicTitle: localize({ key: 'mView', comment: ['&& denotes a mnemonic'] }, "&&View")83},84order: 485});8687MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {88submenu: MenuId.MenubarGoMenu,89title: {90value: 'Go',91original: 'Go',92mnemonicTitle: localize({ key: 'mGoto', comment: ['&& denotes a mnemonic'] }, "&&Go")93},94order: 595});9697MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {98submenu: MenuId.MenubarTerminalMenu,99title: {100value: 'Terminal',101original: 'Terminal',102mnemonicTitle: localize({ key: 'mTerminal', comment: ['&& denotes a mnemonic'] }, "&&Terminal")103},104order: 7105});106107MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {108submenu: MenuId.MenubarHelpMenu,109title: {110value: 'Help',111original: 'Help',112mnemonicTitle: localize({ key: 'mHelp', comment: ['&& denotes a mnemonic'] }, "&&Help")113},114order: 8115});116117MenuRegistry.appendMenuItem(MenuId.MenubarMainMenu, {118submenu: MenuId.MenubarPreferencesMenu,119title: {120value: 'Preferences',121original: 'Preferences',122mnemonicTitle: localize({ key: 'mPreferences', comment: ['&& denotes a mnemonic'] }, "Preferences")123},124when: IsMacNativeContext,125order: 9126});127128export abstract class MenubarControl extends Disposable {129130protected keys = [131MenuSettings.MenuBarVisibility,132'window.enableMenuBarMnemonics',133'window.customMenuBarAltFocus',134'workbench.sideBar.location',135'window.nativeTabs'136];137138protected mainMenu: IMenu;139protected menus: {140[index: string]: IMenu | undefined;141} = {};142143protected topLevelTitles: { [menu: string]: string } = {};144145protected readonly mainMenuDisposables: DisposableStore;146147protected recentlyOpened: IRecentlyOpened = { files: [], workspaces: [] };148149protected menuUpdater: RunOnceScheduler;150151protected static readonly MAX_MENU_RECENT_ENTRIES = 10;152153constructor(154protected readonly menuService: IMenuService,155protected readonly workspacesService: IWorkspacesService,156protected readonly contextKeyService: IContextKeyService,157protected readonly keybindingService: IKeybindingService,158protected readonly configurationService: IConfigurationService,159protected readonly labelService: ILabelService,160protected readonly updateService: IUpdateService,161protected readonly storageService: IStorageService,162protected readonly notificationService: INotificationService,163protected readonly preferencesService: IPreferencesService,164protected readonly environmentService: IWorkbenchEnvironmentService,165protected readonly accessibilityService: IAccessibilityService,166protected readonly hostService: IHostService,167protected readonly commandService: ICommandService168) {169170super();171172this.mainMenu = this._register(this.menuService.createMenu(MenuId.MenubarMainMenu, this.contextKeyService));173this.mainMenuDisposables = this._register(new DisposableStore());174175this.setupMainMenu();176177this.menuUpdater = this._register(new RunOnceScheduler(() => this.doUpdateMenubar(false), 200));178179this.notifyUserOfCustomMenubarAccessibility();180}181182protected abstract doUpdateMenubar(firstTime: boolean): void;183184protected registerListeners(): void {185// Listen for window focus changes186this._register(this.hostService.onDidChangeFocus(e => this.onDidChangeWindowFocus(e)));187188// Update when config changes189this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationUpdated(e)));190191// Listen to update service192this._register(this.updateService.onStateChange(() => this.onUpdateStateChange()));193194// Listen for changes in recently opened menu195this._register(this.workspacesService.onDidChangeRecentlyOpened(() => { this.onDidChangeRecentlyOpened(); }));196197// Listen to keybindings change198this._register(this.keybindingService.onDidUpdateKeybindings(() => this.updateMenubar()));199200// Update recent menu items on formatter registration201this._register(this.labelService.onDidChangeFormatters(() => { this.onDidChangeRecentlyOpened(); }));202203// Listen for changes on the main menu204this._register(this.mainMenu.onDidChange(() => { this.setupMainMenu(); this.doUpdateMenubar(true); }));205}206207protected setupMainMenu(): void {208this.mainMenuDisposables.clear();209this.menus = {};210this.topLevelTitles = {};211212const [, mainMenuActions] = this.mainMenu.getActions()[0];213for (const mainMenuAction of mainMenuActions) {214if (mainMenuAction instanceof SubmenuItemAction && typeof mainMenuAction.item.title !== 'string') {215this.menus[mainMenuAction.item.title.original] = this.mainMenuDisposables.add(this.menuService.createMenu(mainMenuAction.item.submenu, this.contextKeyService, { emitEventsForSubmenuChanges: true }));216this.topLevelTitles[mainMenuAction.item.title.original] = mainMenuAction.item.title.mnemonicTitle ?? mainMenuAction.item.title.value;217}218}219}220221protected updateMenubar(): void {222this.menuUpdater.schedule();223}224225protected calculateActionLabel(action: { id: string; label: string }): string {226const label = action.label;227switch (action.id) {228default:229break;230}231232return label;233}234235protected onUpdateStateChange(): void {236this.updateMenubar();237}238239protected onUpdateKeybindings(): void {240this.updateMenubar();241}242243protected getOpenRecentActions(): (Separator | IOpenRecentAction)[] {244if (!this.recentlyOpened) {245return [];246}247248const { workspaces, files } = this.recentlyOpened;249250const result = [];251252if (workspaces.length > 0) {253for (let i = 0; i < MenubarControl.MAX_MENU_RECENT_ENTRIES && i < workspaces.length; i++) {254result.push(this.createOpenRecentMenuAction(workspaces[i]));255}256257result.push(new Separator());258}259260if (files.length > 0) {261for (let i = 0; i < MenubarControl.MAX_MENU_RECENT_ENTRIES && i < files.length; i++) {262result.push(this.createOpenRecentMenuAction(files[i]));263}264265result.push(new Separator());266}267268return result;269}270271protected onDidChangeWindowFocus(hasFocus: boolean): void {272// When we regain focus, update the recent menu items273if (hasFocus) {274this.onDidChangeRecentlyOpened();275}276}277278private onConfigurationUpdated(event: IConfigurationChangeEvent): void {279if (this.keys.some(key => event.affectsConfiguration(key))) {280this.updateMenubar();281}282283if (event.affectsConfiguration('editor.accessibilitySupport')) {284this.notifyUserOfCustomMenubarAccessibility();285}286287// Since we try not update when hidden, we should288// try to update the recently opened list on visibility changes289if (event.affectsConfiguration(MenuSettings.MenuBarVisibility)) {290this.onDidChangeRecentlyOpened();291}292}293294private get menubarHidden(): boolean {295return isMacintosh && isNative ? false : getMenuBarVisibility(this.configurationService) === 'hidden';296}297298protected onDidChangeRecentlyOpened(): void {299300// Do not update recently opened when the menubar is hidden #108712301if (!this.menubarHidden) {302this.workspacesService.getRecentlyOpened().then(recentlyOpened => {303this.recentlyOpened = recentlyOpened;304this.updateMenubar();305});306}307}308309private createOpenRecentMenuAction(recent: IRecent): IOpenRecentAction {310311let label: string;312let uri: URI;313let commandId: string;314let openable: IWindowOpenable;315const remoteAuthority = recent.remoteAuthority;316317if (isRecentFolder(recent)) {318uri = recent.folderUri;319label = recent.label || this.labelService.getWorkspaceLabel(uri, { verbose: Verbosity.LONG });320commandId = 'openRecentFolder';321openable = { folderUri: uri };322} else if (isRecentWorkspace(recent)) {323uri = recent.workspace.configPath;324label = recent.label || this.labelService.getWorkspaceLabel(recent.workspace, { verbose: Verbosity.LONG });325commandId = 'openRecentWorkspace';326openable = { workspaceUri: uri };327} else {328uri = recent.fileUri;329label = recent.label || this.labelService.getUriLabel(uri, { appendWorkspaceSuffix: true });330commandId = 'openRecentFile';331openable = { fileUri: uri };332}333334const ret = toAction({335id: commandId, label: unmnemonicLabel(label), run: (browserEvent: KeyboardEvent) => {336const openInNewWindow = browserEvent && ((!isMacintosh && (browserEvent.ctrlKey || browserEvent.shiftKey)) || (isMacintosh && (browserEvent.metaKey || browserEvent.altKey)));337338return this.hostService.openWindow([openable], {339forceNewWindow: !!openInNewWindow,340remoteAuthority: remoteAuthority || null // local window if remoteAuthority is not set or can not be deducted from the openable341});342}343});344345return Object.assign(ret, { uri, remoteAuthority });346}347348private notifyUserOfCustomMenubarAccessibility(): void {349if (isWeb || isMacintosh) {350return;351}352353const hasBeenNotified = this.storageService.getBoolean('menubar/accessibleMenubarNotified', StorageScope.APPLICATION, false);354const usingCustomMenubar = !hasNativeMenu(this.configurationService);355356if (hasBeenNotified || usingCustomMenubar || !this.accessibilityService.isScreenReaderOptimized()) {357return;358}359360const message = localize('menubar.customTitlebarAccessibilityNotification', "Accessibility support is enabled for you. For the most accessible experience, we recommend the custom menu style.");361this.notificationService.prompt(Severity.Info, message, [362{363label: localize('goToSetting', "Open Settings"),364run: () => {365return this.preferencesService.openUserSettings({ query: MenuSettings.MenuStyle });366}367}368]);369370this.storageService.store('menubar/accessibleMenubarNotified', true, StorageScope.APPLICATION, StorageTarget.USER);371}372}373374// This is a bit complex due to the issue https://github.com/microsoft/vscode/issues/205836375let focusMenuBarEmitter: Emitter<void> | undefined = undefined;376function enableFocusMenuBarAction(): Emitter<void> {377if (!focusMenuBarEmitter) {378focusMenuBarEmitter = new Emitter<void>();379380registerAction2(class extends Action2 {381constructor() {382super({383id: `workbench.actions.menubar.focus`,384title: localize2('focusMenu', 'Focus Application Menu'),385keybinding: {386primary: KeyMod.Alt | KeyCode.F10,387weight: KeybindingWeight.WorkbenchContrib,388when: IsWebContext389},390f1: true391});392}393394async run(): Promise<void> {395focusMenuBarEmitter?.fire();396}397});398}399400return focusMenuBarEmitter;401}402403export class CustomMenubarControl extends MenubarControl {404private menubar: MenuBar | undefined;405private container: HTMLElement | undefined;406private alwaysOnMnemonics: boolean = false;407private focusInsideMenubar: boolean = false;408private pendingFirstTimeUpdate: boolean = false;409private visible: boolean = true;410private actionRunner: IActionRunner;411private readonly webNavigationMenu = this._register(this.menuService.createMenu(MenuId.MenubarHomeMenu, this.contextKeyService));412413private readonly _onVisibilityChange: Emitter<boolean>;414private readonly _onFocusStateChange: Emitter<boolean>;415416constructor(417@IMenuService menuService: IMenuService,418@IWorkspacesService workspacesService: IWorkspacesService,419@IContextKeyService contextKeyService: IContextKeyService,420@IKeybindingService keybindingService: IKeybindingService,421@IConfigurationService configurationService: IConfigurationService,422@ILabelService labelService: ILabelService,423@IUpdateService updateService: IUpdateService,424@IStorageService storageService: IStorageService,425@INotificationService notificationService: INotificationService,426@IPreferencesService preferencesService: IPreferencesService,427@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,428@IAccessibilityService accessibilityService: IAccessibilityService,429@ITelemetryService private readonly telemetryService: ITelemetryService,430@IHostService hostService: IHostService,431@ICommandService commandService: ICommandService432) {433super(menuService, workspacesService, contextKeyService, keybindingService, configurationService, labelService, updateService, storageService, notificationService, preferencesService, environmentService, accessibilityService, hostService, commandService);434435this._onVisibilityChange = this._register(new Emitter<boolean>());436this._onFocusStateChange = this._register(new Emitter<boolean>());437438this.actionRunner = this._register(new ActionRunner());439this.actionRunner.onDidRun(e => {440this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id: e.action.id, from: 'menu' });441});442443this.workspacesService.getRecentlyOpened().then((recentlyOpened) => {444this.recentlyOpened = recentlyOpened;445});446447this.registerListeners();448}449450protected doUpdateMenubar(firstTime: boolean): void {451if (!this.focusInsideMenubar) {452this.setupCustomMenubar(firstTime);453}454455if (firstTime) {456this.pendingFirstTimeUpdate = true;457}458}459460private getUpdateAction(): IAction | null {461const state = this.updateService.state;462463switch (state.type) {464case StateType.Idle:465return new Action('update.check', localize({ key: 'checkForUpdates', comment: ['&& denotes a mnemonic'] }, "Check for &&Updates..."), undefined, true, () =>466this.updateService.checkForUpdates(true));467468case StateType.CheckingForUpdates:469return new Action('update.checking', localize('checkingForUpdates', "Checking for Updates..."), undefined, false);470471case StateType.AvailableForDownload:472return new Action('update.downloadNow', localize({ key: 'download now', comment: ['&& denotes a mnemonic'] }, "D&&ownload Update"), undefined, true, () =>473this.updateService.downloadUpdate());474475case StateType.Downloading:476return new Action('update.downloading', localize('DownloadingUpdate', "Downloading Update..."), undefined, false);477478case StateType.Downloaded:479return isMacintosh ? null : new Action('update.install', localize({ key: 'installUpdate...', comment: ['&& denotes a mnemonic'] }, "Install &&Update..."), undefined, true, () =>480this.updateService.applyUpdate());481482case StateType.Updating:483return new Action('update.updating', localize('installingUpdate', "Installing Update..."), undefined, false);484485case StateType.Ready:486return new Action('update.restart', localize({ key: 'restartToUpdate', comment: ['&& denotes a mnemonic'] }, "Restart to &&Update"), undefined, true, () =>487this.updateService.quitAndInstall());488489default:490return null;491}492}493494private get currentMenubarVisibility(): MenuBarVisibility {495return getMenuBarVisibility(this.configurationService);496}497498private get currentDisableMenuBarAltFocus(): boolean {499const settingValue = this.configurationService.getValue<boolean>('window.customMenuBarAltFocus');500501let disableMenuBarAltBehavior = false;502if (typeof settingValue === 'boolean') {503disableMenuBarAltBehavior = !settingValue;504}505506return disableMenuBarAltBehavior;507}508509private insertActionsBefore(nextAction: IAction, target: IAction[]): void {510switch (nextAction.id) {511case OpenRecentAction.ID:512target.push(...this.getOpenRecentActions());513break;514515case 'workbench.action.showAboutDialog':516if (!isMacintosh && !isWeb) {517const updateAction = this.getUpdateAction();518if (updateAction) {519updateAction.label = mnemonicMenuLabel(updateAction.label);520target.push(updateAction);521target.push(new Separator());522}523}524525break;526527default:528break;529}530}531532private get currentEnableMenuBarMnemonics(): boolean {533let enableMenuBarMnemonics = this.configurationService.getValue<boolean>('window.enableMenuBarMnemonics');534if (typeof enableMenuBarMnemonics !== 'boolean') {535enableMenuBarMnemonics = true;536}537538return enableMenuBarMnemonics && (!isWeb || isFullscreen(mainWindow));539}540541private get currentCompactMenuMode(): IMenuDirection | undefined {542if (this.currentMenubarVisibility !== 'compact') {543return undefined;544}545546// Menu bar lives in activity bar and should flow based on its location547const currentSidebarLocation = this.configurationService.getValue<string>('workbench.sideBar.location');548const horizontalDirection = currentSidebarLocation === 'right' ? HorizontalDirection.Left : HorizontalDirection.Right;549550const activityBarLocation = this.configurationService.getValue<string>('workbench.activityBar.location');551const verticalDirection = activityBarLocation === ActivityBarPosition.BOTTOM ? VerticalDirection.Above : VerticalDirection.Below;552553return { horizontal: horizontalDirection, vertical: verticalDirection };554}555556private onDidVisibilityChange(visible: boolean): void {557this.visible = visible;558this.onDidChangeRecentlyOpened();559this._onVisibilityChange.fire(visible);560}561562private toActionsArray(menu: IMenu): IAction[] {563return getFlatContextMenuActions(menu.getActions({ shouldForwardArgs: true }));564}565566private readonly reinstallDisposables = this._register(new DisposableStore());567private readonly updateActionsDisposables = this._register(new DisposableStore());568private setupCustomMenubar(firstTime: boolean): void {569// If there is no container, we cannot setup the menubar570if (!this.container) {571return;572}573574if (firstTime) {575// Reset and create new menubar576if (this.menubar) {577this.reinstallDisposables.clear();578}579580this.menubar = this.reinstallDisposables.add(new MenuBar(this.container, this.getMenuBarOptions(), defaultMenuStyles));581582this.accessibilityService.alwaysUnderlineAccessKeys().then(val => {583this.alwaysOnMnemonics = val;584this.menubar?.update(this.getMenuBarOptions());585});586587this.reinstallDisposables.add(this.menubar.onFocusStateChange(focused => {588this._onFocusStateChange.fire(focused);589590// When the menubar loses focus, update it to clear any pending updates591if (!focused) {592if (this.pendingFirstTimeUpdate) {593this.setupCustomMenubar(true);594this.pendingFirstTimeUpdate = false;595} else {596this.updateMenubar();597}598599this.focusInsideMenubar = false;600}601}));602603this.reinstallDisposables.add(this.menubar.onVisibilityChange(e => this.onDidVisibilityChange(e)));604605// Before we focus the menubar, stop updates to it so that focus-related context keys will work606this.reinstallDisposables.add(addDisposableListener(this.container, EventType.FOCUS_IN, () => {607this.focusInsideMenubar = true;608}));609610this.reinstallDisposables.add(addDisposableListener(this.container, EventType.FOCUS_OUT, () => {611this.focusInsideMenubar = false;612}));613614// Fire visibility change for the first install if menu is shown615if (this.menubar.isVisible) {616this.onDidVisibilityChange(true);617}618} else {619this.menubar?.update(this.getMenuBarOptions());620}621622// Update the menu actions623const updateActions = (menuActions: readonly IAction[], target: IAction[], topLevelTitle: string, store: DisposableStore) => {624target.splice(0);625626for (const menuItem of menuActions) {627this.insertActionsBefore(menuItem, target);628629if (menuItem instanceof Separator) {630target.push(menuItem);631} else if (menuItem instanceof SubmenuItemAction || menuItem instanceof MenuItemAction) {632// use mnemonicTitle whenever possible633let title = typeof menuItem.item.title === 'string'634? menuItem.item.title635: menuItem.item.title.mnemonicTitle ?? menuItem.item.title.value;636637if (menuItem instanceof SubmenuItemAction) {638const submenuActions: SubmenuAction[] = [];639updateActions(menuItem.actions, submenuActions, topLevelTitle, store);640641if (submenuActions.length > 0) {642target.push(new SubmenuAction(menuItem.id, mnemonicMenuLabel(title), submenuActions));643}644} else {645if (isICommandActionToggleInfo(menuItem.item.toggled)) {646title = menuItem.item.toggled.mnemonicTitle ?? menuItem.item.toggled.title ?? title;647}648649const newAction = store.add(new Action(menuItem.id, mnemonicMenuLabel(title), menuItem.class, menuItem.enabled, () => this.commandService.executeCommand(menuItem.id)));650newAction.tooltip = menuItem.tooltip;651newAction.checked = menuItem.checked;652target.push(newAction);653}654}655656}657658// Append web navigation menu items to the file menu when not compact659if (topLevelTitle === 'File' && this.currentCompactMenuMode === undefined) {660const webActions = this.getWebNavigationActions();661if (webActions.length) {662target.push(...webActions);663}664}665};666667for (const title of Object.keys(this.topLevelTitles)) {668const menu = this.menus[title];669if (firstTime && menu) {670const menuChangedDisposable = this.reinstallDisposables.add(new DisposableStore());671this.reinstallDisposables.add(menu.onDidChange(() => {672if (!this.focusInsideMenubar) {673const actions: IAction[] = [];674menuChangedDisposable.clear();675updateActions(this.toActionsArray(menu), actions, title, menuChangedDisposable);676this.menubar?.updateMenu({ actions, label: mnemonicMenuLabel(this.topLevelTitles[title]) });677}678}));679680// For the file menu, we need to update if the web nav menu updates as well681if (menu === this.menus.File) {682const webMenuChangedDisposable = this.reinstallDisposables.add(new DisposableStore());683this.reinstallDisposables.add(this.webNavigationMenu.onDidChange(() => {684if (!this.focusInsideMenubar) {685const actions: IAction[] = [];686webMenuChangedDisposable.clear();687updateActions(this.toActionsArray(menu), actions, title, webMenuChangedDisposable);688this.menubar?.updateMenu({ actions, label: mnemonicMenuLabel(this.topLevelTitles[title]) });689}690}));691}692}693694const actions: IAction[] = [];695if (menu) {696this.updateActionsDisposables.clear();697updateActions(this.toActionsArray(menu), actions, title, this.updateActionsDisposables);698}699700if (this.menubar) {701if (!firstTime) {702this.menubar.updateMenu({ actions, label: mnemonicMenuLabel(this.topLevelTitles[title]) });703} else {704this.menubar.push({ actions, label: mnemonicMenuLabel(this.topLevelTitles[title]) });705}706}707}708}709710private getWebNavigationActions(): IAction[] {711if (!isWeb) {712return []; // only for web713}714715const webNavigationActions = [];716for (const groups of this.webNavigationMenu.getActions()) {717const [, actions] = groups;718for (const action of actions) {719if (action instanceof MenuItemAction) {720const title = typeof action.item.title === 'string'721? action.item.title722: action.item.title.mnemonicTitle ?? action.item.title.value;723webNavigationActions.push(new Action(action.id, mnemonicMenuLabel(title), action.class, action.enabled, async (event?: any) => {724this.commandService.executeCommand(action.id, event);725}));726}727}728729webNavigationActions.push(new Separator());730}731732if (webNavigationActions.length) {733webNavigationActions.pop();734}735736return webNavigationActions;737}738739private getMenuBarOptions(): IMenuBarOptions {740return {741enableMnemonics: this.currentEnableMenuBarMnemonics,742disableAltFocus: this.currentDisableMenuBarAltFocus,743visibility: this.currentMenubarVisibility,744actionRunner: this.actionRunner,745getKeybinding: (action) => this.keybindingService.lookupKeybinding(action.id),746alwaysOnMnemonics: this.alwaysOnMnemonics,747compactMode: this.currentCompactMenuMode,748getCompactMenuActions: () => {749if (!isWeb) {750return []; // only for web751}752753return this.getWebNavigationActions();754}755};756}757758protected override onDidChangeWindowFocus(hasFocus: boolean): void {759if (!this.visible) {760return;761}762763super.onDidChangeWindowFocus(hasFocus);764765if (this.container) {766if (hasFocus) {767this.container.classList.remove('inactive');768} else {769this.container.classList.add('inactive');770this.menubar?.blur();771}772}773}774775protected override onUpdateStateChange(): void {776if (!this.visible) {777return;778}779780super.onUpdateStateChange();781}782783protected override onDidChangeRecentlyOpened(): void {784if (!this.visible) {785return;786}787788super.onDidChangeRecentlyOpened();789}790791protected override onUpdateKeybindings(): void {792if (!this.visible) {793return;794}795796super.onUpdateKeybindings();797}798799protected override registerListeners(): void {800super.registerListeners();801802this._register(addDisposableListener(mainWindow, EventType.RESIZE, () => {803if (this.menubar && !(isIOS && BrowserFeatures.pointerEvents)) {804this.menubar.blur();805}806}));807808// Mnemonics require fullscreen in web809if (isWeb) {810this._register(onDidChangeFullscreen(windowId => {811if (windowId === mainWindow.vscodeWindowId) {812this.updateMenubar();813}814}));815this._register(this.webNavigationMenu.onDidChange(() => this.updateMenubar()));816this._register(enableFocusMenuBarAction().event(() => this.menubar?.toggleFocus()));817}818}819820get onVisibilityChange(): Event<boolean> {821return this._onVisibilityChange.event;822}823824get onFocusStateChange(): Event<boolean> {825return this._onFocusStateChange.event;826}827828getMenubarItemsDimensions(): Dimension {829if (this.menubar) {830return new Dimension(this.menubar.getWidth(), this.menubar.getHeight());831}832833return new Dimension(0, 0);834}835836create(parent: HTMLElement): HTMLElement {837this.container = parent;838839// Build the menubar840if (this.container) {841this.doUpdateMenubar(true);842}843844return this.container;845}846847layout(dimension: Dimension) {848this.menubar?.update(this.getMenuBarOptions());849}850851toggleFocus() {852this.menubar?.toggleFocus();853}854}855856857