Skip to content

Commit

Permalink
Set menu bar less often at startup (#14295)
Browse files Browse the repository at this point in the history
Fixes #14280

Contributed on behalf of STMicroelectronics

Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder authored Oct 16, 2024
1 parent de441e2 commit 8bff722
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,57 +74,58 @@ export type ElectronMenuItemRole = ('undo' | 'redo' | 'cut' | 'copy' | 'paste' |
@injectable()
export class ElectronMainMenuFactory extends BrowserMainMenuFactory {

protected _menu?: MenuDto[];
protected _toggledCommands: Set<string> = new Set();
protected menu?: MenuDto[];
protected toggledCommands: Set<string> = 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<void> {
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 {
const preference = this.preferencesService.get<string>('window.menuBarVisibility') || 'classic';
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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -203,15 +202,15 @@ 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) {
this.createCustomTitleBar(app);
return;
}
}
window.electronTheiaCore.setMenu(electronMenu);
this.factory.setMenuBar();
}

protected createCustomTitleBar(app: FrontendApplication): void {
Expand Down

0 comments on commit 8bff722

Please sign in to comment.