From 4949df73959511e14587dcb7676818c9ed3c87f9 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 24 Sep 2019 16:01:19 +0200 Subject: [PATCH] Fixed missing `New Sketch` menu in electron. Signed-off-by: Akos Kitta --- .../electron-arduino-menu-contribution.ts | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/arduino-ide-extension/src/electron-browser/electron-arduino-menu-contribution.ts b/arduino-ide-extension/src/electron-browser/electron-arduino-menu-contribution.ts index c35fcc8f..293cf13a 100644 --- a/arduino-ide-extension/src/electron-browser/electron-arduino-menu-contribution.ts +++ b/arduino-ide-extension/src/electron-browser/electron-arduino-menu-contribution.ts @@ -1,31 +1,49 @@ import * as electron from 'electron'; -import { injectable, inject } from "inversify"; -import { ElectronMenuContribution } from "@theia/core/lib/electron-browser/menu/electron-menu-contribution"; -import { FrontendApplication } from "@theia/core/lib/browser"; -import { isOSX } from '@theia/core'; -import { WorkspaceService } from '@theia/workspace/lib/browser'; +import { injectable, inject } from 'inversify'; +import { isOSX } from '@theia/core/lib/common/os'; +import { Disposable } from '@theia/core/lib/common/disposable'; +import { FrontendApplication } from '@theia/core/lib/browser/frontend-application'; +import { ElectronMenuContribution } from '@theia/core/lib/electron-browser/menu/electron-menu-contribution'; +import { FrontendApplicationStateService, FrontendApplicationState } from '@theia/core/lib/browser/frontend-application-state'; +// Code was copied from https://github.com/eclipse-theia/theia/pull/5140/commits/be873411eff1f48822a65261305bbe3549ac903d @injectable() export class ElectronArduinoMenuContribution extends ElectronMenuContribution { - @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService; + @inject(FrontendApplicationStateService) + protected readonly stateService: FrontendApplicationStateService; onStart(app: FrontendApplication): void { - this.workspaceService.onWorkspaceChanged(() => { - const createdMenuBar = this.factory.createMenuBar(); - const currentWindow = electron.remote.getCurrentWindow(); - 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()) - ); - } else { - // Unix/Windows: Set the per-window menus - currentWindow.setMenu(createdMenuBar); + this.setMenu(); + if (isOSX) { + // 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); } -} \ No newline at end of file + + 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 + electronWindow.setMenu(menu); + } + } + +}