Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set menu bar less often at startup #14295

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading