@@ -3,12 +3,13 @@ import { inject, injectable, postConstruct } from 'inversify';
3
3
import { MenuPath , CompositeMenuNode } 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
- import { ArduinoMenus } from '../menu/arduino-menus' ;
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
9
import { ExamplesService , ExampleContainer } 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
13
13
14
@injectable ( )
14
15
export abstract class Examples extends SketchContribution {
@@ -32,10 +33,10 @@ export abstract class Examples extends SketchContribution {
32
33
33
34
@postConstruct ( )
34
35
init ( ) : void {
35
- this . boardsServiceClient . onBoardsConfigChanged ( ( { selectedBoard } ) => this . handleBoardChanged ( selectedBoard ?. fqbn ) ) ;
36
+ this . boardsServiceClient . onBoardsConfigChanged ( ( { selectedBoard } ) => this . handleBoardChanged ( selectedBoard ) ) ;
36
37
}
37
38
38
- protected handleBoardChanged ( fqbn : string | undefined ) : void {
39
+ protected handleBoardChanged ( board : Board | undefined ) : void {
39
40
// NOOP
40
41
}
41
42
@@ -58,27 +59,33 @@ export abstract class Examples extends SketchContribution {
58
59
}
59
60
60
61
registerRecursively (
61
- exampleContainer : ExampleContainer ,
62
+ exampleContainerOrPlaceholder : ExampleContainer | string ,
62
63
menuPath : MenuPath ,
63
64
pushToDispose : DisposableCollection = new DisposableCollection ( ) ) : void {
64
65
65
- const { label, sketches, children } = exampleContainer ;
66
- const submenuPath = [ ...menuPath , label ] ;
67
- this . menuRegistry . registerSubmenu ( submenuPath , label ) ;
68
- children . forEach ( child => this . registerRecursively ( child , submenuPath , pushToDispose ) ) ;
69
- for ( const sketch of sketches ) {
70
- const { uri } = sketch ;
71
- const commandId = `arduino-open-example-${ submenuPath . join ( ':' ) } --${ uri } ` ;
72
- const command = { id : commandId } ;
73
- const handler = {
74
- execute : async ( ) => {
75
- const sketch = await this . sketchService . cloneExample ( uri ) ;
76
- this . commandService . executeCommand ( OpenSketch . Commands . OPEN_SKETCH . id , sketch ) ;
77
- }
78
- } ;
79
- pushToDispose . push ( this . commandRegistry . registerCommand ( command , handler ) ) ;
80
- this . menuRegistry . registerMenuAction ( submenuPath , { commandId, label : sketch . name } ) ;
81
- pushToDispose . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuAction ( command ) ) ) ;
66
+ if ( typeof exampleContainerOrPlaceholder === 'string' ) {
67
+ const placeholder = new PlaceholderMenuNode ( menuPath , exampleContainerOrPlaceholder ) ;
68
+ this . menuRegistry . registerMenuNode ( menuPath , placeholder ) ;
69
+ pushToDispose . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuNode ( placeholder . id ) ) ) ;
70
+ } else {
71
+ const { label, sketches, children } = exampleContainerOrPlaceholder ;
72
+ const submenuPath = [ ...menuPath , label ] ;
73
+ this . menuRegistry . registerSubmenu ( submenuPath , label ) ;
74
+ children . forEach ( child => this . registerRecursively ( child , submenuPath , pushToDispose ) ) ;
75
+ for ( const sketch of sketches ) {
76
+ const { uri } = sketch ;
77
+ const commandId = `arduino-open-example-${ submenuPath . join ( ':' ) } --${ uri } ` ;
78
+ const command = { id : commandId } ;
79
+ const handler = {
80
+ execute : async ( ) => {
81
+ const sketch = await this . sketchService . cloneExample ( uri ) ;
82
+ this . commandService . executeCommand ( OpenSketch . Commands . OPEN_SKETCH . id , sketch ) ;
83
+ }
84
+ } ;
85
+ pushToDispose . push ( this . commandRegistry . registerCommand ( command , handler ) ) ;
86
+ this . menuRegistry . registerMenuAction ( submenuPath , { commandId, label : sketch . name } ) ;
87
+ pushToDispose . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuAction ( command ) ) ) ;
88
+ }
82
89
}
83
90
}
84
91
@@ -101,10 +108,12 @@ export class BuiltInExamples extends Examples {
101
108
return ;
102
109
}
103
110
this . toDispose . dispose ( ) ;
104
- for ( const container of exampleContainers ) {
111
+ for ( const container of [ 'Built-in examples' , ... exampleContainers ] ) {
105
112
this . registerRecursively ( container , ArduinoMenus . EXAMPLES__BUILT_IN_GROUP , this . toDispose ) ;
106
113
}
107
114
this . menuManager . update ( ) ;
115
+ // TODO: remove
116
+ console . log ( typeof this . menuRegistry ) ;
108
117
}
109
118
110
119
}
@@ -123,17 +132,27 @@ export class LibraryExamples extends Examples {
123
132
this . notificationCenter . onLibraryUninstalled ( ( ) => this . register ( ) ) ;
124
133
}
125
134
126
- protected handleBoardChanged ( fqbn : string | undefined ) : void {
127
- this . register ( fqbn ) ;
135
+ protected handleBoardChanged ( board : Board | undefined ) : void {
136
+ this . register ( board ) ;
128
137
}
129
138
130
- protected async register ( fqbn : string | undefined = this . boardsServiceClient . boardsConfig . selectedBoard ?. fqbn ) {
139
+ protected async register ( board : Board | undefined = this . boardsServiceClient . boardsConfig . selectedBoard ) {
131
140
return this . queue . add ( async ( ) => {
132
141
this . toDispose . dispose ( ) ;
133
- if ( ! fqbn ) {
142
+ if ( ! board || ! board . fqbn ) {
134
143
return ;
135
144
}
145
+ const { fqbn, name } = board ;
136
146
const { user, current, any } = await this . examplesService . installed ( { fqbn } ) ;
147
+ if ( user . length ) {
148
+ ( user as any ) . unshift ( 'Examples from Custom Libraries' ) ;
149
+ }
150
+ if ( current . length ) {
151
+ ( current as any ) . unshift ( `Examples for ${ name } ` ) ;
152
+ }
153
+ if ( any . length ) {
154
+ ( any as any ) . unshift ( 'Examples for any board' ) ;
155
+ }
137
156
for ( const container of user ) {
138
157
this . registerRecursively ( container , ArduinoMenus . EXAMPLES__USER_LIBS_GROUP , this . toDispose ) ;
139
158
}
0 commit comments