From 8bff7225a4370a0e85c9cde5eb10f1540a98d198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=A4der?= Date: Wed, 16 Oct 2024 11:40:26 +0200 Subject: [PATCH] Set menu bar less often at startup (#14295) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #14280 Contributed on behalf of STMicroelectronics Signed-off-by: Thomas Mäder --- .../menu/electron-main-menu-factory.ts | 63 ++++++++++--------- .../menu/electron-menu-contribution.ts | 5 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts b/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts index 4f0b97a51a113..3925b56df82f3 100644 --- a/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts +++ b/packages/core/src/electron-browser/menu/electron-main-menu-factory.ts @@ -74,42 +74,45 @@ export type ElectronMenuItemRole = ('undo' | 'redo' | 'cut' | 'copy' | 'paste' | @injectable() export class ElectronMainMenuFactory extends BrowserMainMenuFactory { - protected _menu?: MenuDto[]; - protected _toggledCommands: Set = new Set(); + protected menu?: MenuDto[]; + protected toggledCommands: Set = new Set(); @inject(PreferenceService) protected preferencesService: PreferenceService; + setMenuBar = debounce(() => this.doSetMenuBar(), 100); + @postConstruct() postConstruct(): void { - this.preferencesService.onPreferenceChanged( - debounce(e => { - if (e.preferenceName === 'window.menuBarVisibility') { - this.setMenuBar(); - } - if (this._menu) { - for (const cmd of this._toggledCommands) { - const menuItem = this.findMenuById(this._menu, cmd); - if (menuItem) { - menuItem.checked = this.commandRegistry.isToggled(cmd); - } - } - window.electronTheiaCore.setMenu(this._menu); - } - }, 10) - ); this.keybindingRegistry.onKeybindingsChanged(() => { this.setMenuBar(); }); this.menuProvider.onDidChange(() => { this.setMenuBar(); }); + this.preferencesService.ready.then(() => { + this.preferencesService.onPreferenceChanged( + debounce(e => { + if (e.preferenceName === 'window.menuBarVisibility') { + this.doSetMenuBar(); + } + if (this.menu) { + for (const cmd of this.toggledCommands) { + const menuItem = this.findMenuById(this.menu, cmd); + if (menuItem && (!!menuItem.checked !== this.commandRegistry.isToggled(cmd))) { + menuItem.checked = !menuItem.checked; + } + } + window.electronTheiaCore.setMenu(this.menu); + } + }, 10) + ); + }); } - async setMenuBar(): Promise { - await this.preferencesService.ready; - const createdMenuBar = this.createElectronMenuBar(); - window.electronTheiaCore.setMenu(createdMenuBar); + doSetMenuBar(): void { + this.menu = this.createElectronMenuBar(); + window.electronTheiaCore.setMenu(this.menu); } createElectronMenuBar(): MenuDto[] | undefined { @@ -117,14 +120,12 @@ export class ElectronMainMenuFactory extends BrowserMainMenuFactory { const maxWidget = document.getElementsByClassName(MAXIMIZED_CLASS); if (preference === 'visible' || (preference === 'classic' && maxWidget.length === 0)) { const menuModel = this.menuProvider.getMenu(MAIN_MENU_BAR); - this._menu = this.fillMenuTemplate([], menuModel, [], { honorDisabled: false, rootMenuPath: MAIN_MENU_BAR }, false); + const menu = this.fillMenuTemplate([], menuModel, [], { honorDisabled: false, rootMenuPath: MAIN_MENU_BAR }, false); if (isOSX) { - this._menu.unshift(this.createOSXMenu()); + menu.unshift(this.createOSXMenu()); } - return this._menu; + return menu; } - this._menu = undefined; - // eslint-disable-next-line no-null/no-null return undefined; } @@ -208,7 +209,7 @@ export class ElectronMainMenuFactory extends BrowserMainMenuFactory { parentItems.push(menuItem); if (this.commandRegistry.getToggledHandler(commandId, ...args)) { - this._toggledCommands.add(commandId); + this.toggledCommands.add(commandId); } } return parentItems; @@ -273,11 +274,11 @@ export class ElectronMainMenuFactory extends BrowserMainMenuFactory { // We need to check if we can execute it. if (this.menuCommandExecutor.isEnabled(menuPath, cmd, ...args)) { await this.menuCommandExecutor.executeCommand(menuPath, cmd, ...args); - if (this._menu && this.menuCommandExecutor.isVisible(menuPath, cmd, ...args)) { - const item = this.findMenuById(this._menu, cmd); + if (this.menu && this.menuCommandExecutor.isVisible(menuPath, cmd, ...args)) { + const item = this.findMenuById(this.menu, cmd); if (item) { item.checked = this.menuCommandExecutor.isToggled(menuPath, cmd, ...args); - window.electronTheiaCore.setMenu(this._menu); + window.electronTheiaCore.setMenu(this.menu); } } } diff --git a/packages/core/src/electron-browser/menu/electron-menu-contribution.ts b/packages/core/src/electron-browser/menu/electron-menu-contribution.ts index 0b77749c37bf3..9023dc2612021 100644 --- a/packages/core/src/electron-browser/menu/electron-menu-contribution.ts +++ b/packages/core/src/electron-browser/menu/electron-menu-contribution.ts @@ -29,7 +29,6 @@ import { WindowService } from '../../browser/window/window-service'; import { WindowTitleService } from '../../browser/window/window-title-service'; import '../../../src/electron-browser/menu/electron-menu-style.css'; -import { MenuDto } from '../../electron-common/electron-api'; import { ThemeService } from '../../browser/theming'; import { ThemeChangeEvent } from '../../common/theme'; @@ -203,7 +202,7 @@ export class ElectronMenuContribution extends BrowserMenuBarContribution impleme } } - protected setMenu(app: FrontendApplication, electronMenu: MenuDto[] | undefined = this.factory.createElectronMenuBar()): void { + protected setMenu(app: FrontendApplication): void { if (!isOSX) { this.hideTopPanel(app); if (this.titleBarStyle === 'custom' && !this.menuBar) { @@ -211,7 +210,7 @@ export class ElectronMenuContribution extends BrowserMenuBarContribution impleme return; } } - window.electronTheiaCore.setMenu(electronMenu); + this.factory.setMenuBar(); } protected createCustomTitleBar(app: FrontendApplication): void {