From ce373e22e18014dee0d10447e76dd45d5f1b5d34 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 14 May 2019 10:14:34 +0200 Subject: [PATCH] GH-5100: Update the menu if the frontend is ready. Closes #5100. Signed-off-by: Akos Kitta --- .../menu/electron-menu-contribution.ts | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) 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 aa8142a05570e..cd7d99472f912 100644 --- a/packages/core/src/electron-browser/menu/electron-menu-contribution.ts +++ b/packages/core/src/electron-browser/menu/electron-menu-contribution.ts @@ -18,11 +18,12 @@ import * as electron from 'electron'; import { inject, injectable } from 'inversify'; import { Command, CommandContribution, CommandRegistry, - isOSX, MenuModelRegistry, MenuContribution + isOSX, MenuModelRegistry, MenuContribution, Disposable } from '../../common'; import { KeybindingContribution, KeybindingRegistry } from '../../browser'; import { FrontendApplication, FrontendApplicationContribution, CommonMenus } from '../../browser'; import { ElectronMainMenuFactory } from './electron-main-menu-factory'; +import { FrontendApplicationStateService, FrontendApplicationState } from '../../browser/frontend-application-state'; export namespace ElectronCommands { export const TOGGLE_DEVELOPER_TOOLS: Command = { @@ -67,6 +68,9 @@ export namespace ElectronMenus { @injectable() export class ElectronMenuContribution implements FrontendApplicationContribution, CommandContribution, MenuContribution, KeybindingContribution { + @inject(FrontendApplicationStateService) + protected readonly stateService: FrontendApplicationStateService; + constructor( @inject(ElectronMainMenuFactory) protected readonly factory: ElectronMainMenuFactory ) { } @@ -85,21 +89,35 @@ export class ElectronMenuContribution implements FrontendApplicationContribution } } - const currentWindow = electron.remote.getCurrentWindow(); - const createdMenuBar = this.factory.createMenuBar(); - + this.setMenu(); if (isOSX) { - electron.remote.Menu.setApplicationMenu(createdMenuBar); - currentWindow.on('focus', () => - // OSX: Recreate the menus when changing windows. - // OSX only has one menu bar for all windows, so we need to swap - // between them as the user switch windows. - electron.remote.Menu.setApplicationMenu(this.factory.createMenuBar()) - ); + // OSX: Recreate the menus when changing windows. + // OSX only has one menu bar for all windows, so we need to swap + // between them as the user switches windows. + electron.remote.getCurrentWindow().on('focus', () => this.setMenu()); + } + // Make sure the application menu is complete, once the frontend application is ready. + // https://github.com/theia-ide/theia/issues/5100 + let onStateChange: Disposable | undefined = undefined; + const stateServiceListener = (state: FrontendApplicationState) => { + if (state === 'ready') { + this.setMenu(); + } + if (state === 'closing_window') { + if (!!onStateChange) { + onStateChange.dispose(); + } + } + }; + onStateChange = this.stateService.onStateChanged(stateServiceListener); + } + private setMenu(menu: electron.Menu = this.factory.createMenuBar(), electronWindow: electron.BrowserWindow = electron.remote.getCurrentWindow()): void { + if (isOSX) { + electron.remote.Menu.setApplicationMenu(menu); } else { // Unix/Windows: Set the per-window menus - currentWindow.setMenu(createdMenuBar); + electronWindow.setMenu(menu); } }