1
1
import * as PQueue from 'p-queue' ;
2
2
import { inject , injectable , postConstruct } from 'inversify' ;
3
- import { MenuPath , CompositeMenuNode } from '@theia/core/lib/common/menu' ;
3
+ import { MenuPath , CompositeMenuNode , SubMenuOptions } from '@theia/core/lib/common/menu' ;
4
4
import { Disposable , DisposableCollection } from '@theia/core/lib/common/disposable' ;
5
5
import { OpenSketch } from './open-sketch' ;
6
6
import { ArduinoMenus , PlaceholderMenuNode } from '../menu/arduino-menus' ;
7
7
import { MainMenuManager } from '../../common/main-menu-manager' ;
8
8
import { BoardsServiceProvider } from '../boards/boards-service-provider' ;
9
- import { ExamplesService , ExampleContainer } from '../../common/protocol/examples-service' ;
9
+ import { ExamplesService } from '../../common/protocol/examples-service' ;
10
10
import { SketchContribution , CommandRegistry , MenuModelRegistry } from './contribution' ;
11
11
import { NotificationCenter } from '../notification-center' ;
12
- import { Board } from '../../common/protocol' ;
12
+ import { Board , Sketch , SketchContainer } from '../../common/protocol' ;
13
13
14
14
@injectable ( )
15
15
export abstract class Examples extends SketchContribution {
@@ -59,18 +59,35 @@ export abstract class Examples extends SketchContribution {
59
59
}
60
60
61
61
registerRecursively (
62
- exampleContainerOrPlaceholder : ExampleContainer | string ,
62
+ sketchContainerOrPlaceholder : SketchContainer | ( Sketch | SketchContainer ) [ ] | string ,
63
63
menuPath : MenuPath ,
64
- pushToDispose : DisposableCollection = new DisposableCollection ( ) ) : void {
64
+ pushToDispose : DisposableCollection = new DisposableCollection ( ) ,
65
+ subMenuOptions ?: SubMenuOptions | undefined ) : void {
65
66
66
- if ( typeof exampleContainerOrPlaceholder === 'string' ) {
67
- const placeholder = new PlaceholderMenuNode ( menuPath , exampleContainerOrPlaceholder ) ;
67
+ if ( typeof sketchContainerOrPlaceholder === 'string' ) {
68
+ const placeholder = new PlaceholderMenuNode ( menuPath , sketchContainerOrPlaceholder ) ;
68
69
this . menuRegistry . registerMenuNode ( menuPath , placeholder ) ;
69
70
pushToDispose . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuNode ( placeholder . id ) ) ) ;
70
71
} else {
71
- const { label, sketches, children } = exampleContainerOrPlaceholder ;
72
- const submenuPath = [ ...menuPath , label ] ;
73
- this . menuRegistry . registerSubmenu ( submenuPath , label ) ;
72
+ const sketches : Sketch [ ] = [ ] ;
73
+ const children : SketchContainer [ ] = [ ] ;
74
+ let submenuPath = menuPath ;
75
+
76
+ if ( SketchContainer . is ( sketchContainerOrPlaceholder ) ) {
77
+ const { label } = sketchContainerOrPlaceholder ;
78
+ submenuPath = [ ...menuPath , label ] ;
79
+ this . menuRegistry . registerSubmenu ( submenuPath , label , subMenuOptions ) ;
80
+ sketches . push ( ...sketchContainerOrPlaceholder . sketches ) ;
81
+ children . push ( ...sketchContainerOrPlaceholder . children ) ;
82
+ } else {
83
+ for ( const sketchOrContainer of sketchContainerOrPlaceholder ) {
84
+ if ( SketchContainer . is ( sketchOrContainer ) ) {
85
+ children . push ( sketchOrContainer ) ;
86
+ } else {
87
+ sketches . push ( sketchOrContainer ) ;
88
+ }
89
+ }
90
+ }
74
91
children . forEach ( child => this . registerRecursively ( child , submenuPath , pushToDispose ) ) ;
75
92
for ( const sketch of sketches ) {
76
93
const { uri } = sketch ;
@@ -98,22 +115,20 @@ export class BuiltInExamples extends Examples {
98
115
this . register ( ) ; // no `await`
99
116
}
100
117
101
- protected async register ( ) {
102
- let exampleContainers : ExampleContainer [ ] | undefined ;
118
+ protected async register ( ) : Promise < void > {
119
+ let sketchContainers : SketchContainer [ ] | undefined ;
103
120
try {
104
- exampleContainers = await this . examplesService . builtIns ( ) ;
121
+ sketchContainers = await this . examplesService . builtIns ( ) ;
105
122
} catch ( e ) {
106
123
console . error ( 'Could not initialize built-in examples.' , e ) ;
107
124
this . messageService . error ( 'Could not initialize built-in examples.' ) ;
108
125
return ;
109
126
}
110
127
this . toDispose . dispose ( ) ;
111
- for ( const container of [ 'Built-in examples' , ...exampleContainers ] ) {
128
+ for ( const container of [ 'Built-in examples' , ...sketchContainers ] ) {
112
129
this . registerRecursively ( container , ArduinoMenus . EXAMPLES__BUILT_IN_GROUP , this . toDispose ) ;
113
130
}
114
131
this . menuManager . update ( ) ;
115
- // TODO: remove
116
- console . log ( typeof this . menuRegistry ) ;
117
132
}
118
133
119
134
}
@@ -136,7 +151,7 @@ export class LibraryExamples extends Examples {
136
151
this . register ( board ) ;
137
152
}
138
153
139
- protected async register ( board : Board | undefined = this . boardsServiceClient . boardsConfig . selectedBoard ) {
154
+ protected async register ( board : Board | undefined = this . boardsServiceClient . boardsConfig . selectedBoard ) : Promise < void > {
140
155
return this . queue . add ( async ( ) => {
141
156
this . toDispose . dispose ( ) ;
142
157
if ( ! board || ! board . fqbn ) {
0 commit comments