11import * as PQueue from 'p-queue' ;
22import { inject , injectable } from 'inversify' ;
33import { CommandRegistry } from '@theia/core/lib/common/command' ;
4- import { MenuModelRegistry , MenuNode } from '@theia/core/lib/common/menu' ;
4+ import { MenuModelRegistry } from '@theia/core/lib/common/menu' ;
55import { Disposable , DisposableCollection } from '@theia/core/lib/common/disposable' ;
66import { BoardsServiceProvider } from './boards-service-provider' ;
77import { Board , ConfigOption , Programmer } from '../../common/protocol' ;
88import { FrontendApplicationContribution } from '@theia/core/lib/browser' ;
99import { BoardsDataStore } from './boards-data-store' ;
1010import { MainMenuManager } from '../../common/main-menu-manager' ;
11- import { ArduinoMenus } from '../menu/arduino-menus' ;
11+ import { ArduinoMenus , unregisterSubmenu } from '../menu/arduino-menus' ;
1212
1313@injectable ( )
1414export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
@@ -63,7 +63,7 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
6363 this . menuRegistry . registerSubmenu ( menuPath , label ) ;
6464 this . toDisposeOnBoardChange . pushAll ( [
6565 ...commands . values ( ) ,
66- Disposable . create ( ( ) => this . unregisterSubmenu ( menuPath ) ) , // We cannot dispose submenu entries: https://github.com/eclipse-theia/theia/issues/7299
66+ Disposable . create ( ( ) => unregisterSubmenu ( menuPath , this . menuRegistry ) ) ,
6767 ...Array . from ( commands . keys ( ) ) . map ( ( commandId , i ) => {
6868 const { label } = commands . get ( commandId ) ! ;
6969 this . menuRegistry . registerMenuAction ( menuPath , { commandId, order : `${ i } ` , label } ) ;
@@ -76,7 +76,7 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
7676 const programmersMenuPath = [ ...ArduinoMenus . TOOLS__BOARD_SETTINGS_GROUP , 'z02_programmers' ] ;
7777 const label = selectedProgrammer ? `Programmer: "${ selectedProgrammer . name } "` : 'Programmer'
7878 this . menuRegistry . registerSubmenu ( programmersMenuPath , label ) ;
79- this . toDisposeOnBoardChange . push ( Disposable . create ( ( ) => this . unregisterSubmenu ( programmersMenuPath ) ) ) ;
79+ this . toDisposeOnBoardChange . push ( Disposable . create ( ( ) => unregisterSubmenu ( programmersMenuPath , this . menuRegistry ) ) ) ;
8080 for ( const programmer of programmers ) {
8181 const { id, name } = programmer ;
8282 const command = { id : `${ fqbn } -programmer--${ id } ` } ;
@@ -98,20 +98,4 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
9898 } ) ;
9999 }
100100
101- protected unregisterSubmenu ( menuPath : string [ ] ) : void {
102- if ( menuPath . length < 2 ) {
103- throw new Error ( `Expected at least two item as a menu-path. Got ${ JSON . stringify ( menuPath ) } instead.` ) ;
104- }
105- const toRemove = menuPath [ menuPath . length - 1 ] ;
106- const parentMenuPath = menuPath . slice ( 0 , menuPath . length - 1 ) ;
107- // This is unsafe. Calling `getMenu` with a non-existing menu-path will result in a new menu creation.
108- // https://github.com/eclipse-theia/theia/issues/7300
109- const parent = this . menuRegistry . getMenu ( parentMenuPath ) ;
110- const index = parent . children . findIndex ( ( { id } ) => id === toRemove ) ;
111- if ( index === - 1 ) {
112- throw new Error ( `Could not find menu with menu-path: ${ JSON . stringify ( menuPath ) } .` ) ;
113- }
114- ( parent . children as Array < MenuNode > ) . splice ( index , 1 ) ;
115- }
116-
117101}
0 commit comments