@@ -5,7 +5,7 @@ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
5
5
import { MessageService } from '@theia/core/lib/common/message-service' ;
6
6
import { CommandContribution , CommandRegistry , Command } from '@theia/core/lib/common/command' ;
7
7
import { TabBarToolbarContribution , TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar' ;
8
- import { BoardsService , Board , AttachedSerialBoard } from '../common/protocol/boards-service' ;
8
+ import { BoardsService , Board } from '../common/protocol/boards-service' ;
9
9
import { ArduinoCommands } from './arduino-commands' ;
10
10
import { ConnectedBoards } from './components/connected-boards' ;
11
11
import { CoreService } from '../common/protocol/core-service' ;
@@ -66,9 +66,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
66
66
@inject ( BoardsNotificationService )
67
67
protected readonly boardsNotificationService : BoardsNotificationService ;
68
68
69
- @inject ( WorkspaceService )
70
- protected readonly workspaceService : WorkspaceService ;
71
-
72
69
@inject ( SelectionService )
73
70
protected readonly selectionService : SelectionService ;
74
71
@@ -106,48 +103,20 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
106
103
protected readonly commands : CommandRegistry ;
107
104
108
105
protected boardsToolbarItem : BoardsToolBarItem | null ;
109
- protected attachedBoards : Board [ ] ;
110
- protected selectedBoard : Board ;
106
+ protected wsSketchCount : number = 0 ;
107
+
108
+ constructor ( @inject ( WorkspaceService ) protected readonly workspaceService : WorkspaceService ) {
109
+ this . workspaceService . onWorkspaceChanged ( ( ) => {
110
+ if ( this . workspaceService . workspace ) {
111
+ this . registerSketchesInMenu ( this . menuRegistry ) ;
112
+ }
113
+ } )
114
+ }
111
115
112
116
@postConstruct ( )
113
117
protected async init ( ) : Promise < void > {
114
118
// This is a hack. Otherwise, the backend services won't bind.
115
119
await this . workspaceServiceExt . roots ( ) ;
116
- const { boards } = await this . boardService . getAttachedBoards ( ) ;
117
- this . attachedBoards = boards ;
118
- this . registerConnectedBoardsInMenu ( this . menuRegistry ) ;
119
- }
120
-
121
- protected async registerConnectedBoardsInMenu ( registry : MenuModelRegistry ) {
122
- this . attachedBoards . forEach ( board => {
123
- const port = this . getPort ( board ) ;
124
- const command : Command = {
125
- id : 'selectBoard' + port
126
- }
127
- this . commands . registerCommand ( command , {
128
- execute : ( ) => this . commands . executeCommand ( ArduinoCommands . SELECT_BOARD . id , board ) ,
129
- isToggled : ( ) => this . isSelectedBoard ( board )
130
- } ) ;
131
- registry . registerMenuAction ( ArduinoToolbarContextMenu . CONNECTED_GROUP , {
132
- commandId : command . id ,
133
- label : board . name + ' at ' + port
134
- } ) ;
135
- } ) ;
136
- }
137
-
138
- protected isSelectedBoard ( board : Board ) : boolean {
139
- return AttachedSerialBoard . is ( board ) &&
140
- this . selectedBoard &&
141
- AttachedSerialBoard . is ( this . selectedBoard ) &&
142
- board . port === this . selectedBoard . port &&
143
- board . fqbn === this . selectedBoard . fqbn ;
144
- }
145
-
146
- protected getPort ( board : Board ) : string {
147
- if ( AttachedSerialBoard . is ( board ) ) {
148
- return board . port ;
149
- }
150
- return '' ;
151
120
}
152
121
153
122
registerToolbarItems ( registry : TabBarToolbarRegistry ) : void {
@@ -178,7 +147,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
178
147
registry . registerItem ( {
179
148
id : ConnectedBoards . TOOLBAR_ID ,
180
149
render : ( ) => < BoardsToolBarItem
150
+ key = 'boardsToolbarItem'
181
151
ref = { ref => this . boardsToolbarItem = ref }
152
+ commands = { this . commands }
182
153
contextMenuRenderer = { this . contextMenuRenderer }
183
154
boardsNotificationService = { this . boardsNotificationService }
184
155
boardService = { this . boardService } /> ,
@@ -233,12 +204,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
233
204
isVisible : widget => this . isArduinoToolbar ( widget ) ,
234
205
isEnabled : widget => this . isArduinoToolbar ( widget ) ,
235
206
execute : async ( widget : Widget , target : EventTarget ) => {
236
- const el = ( target as HTMLElement ) . parentElement ;
237
- if ( el ) {
238
- this . contextMenuRenderer . render ( ArduinoToolbarContextMenu . OPEN_SKETCH_PATH , {
239
- x : el . getBoundingClientRect ( ) . left ,
240
- y : el . getBoundingClientRect ( ) . top + el . offsetHeight
241
- } ) ;
207
+ if ( this . wsSketchCount ) {
208
+ const el = ( target as HTMLElement ) . parentElement ;
209
+ if ( el ) {
210
+ this . contextMenuRenderer . render ( ArduinoToolbarContextMenu . OPEN_SKETCH_PATH , {
211
+ x : el . getBoundingClientRect ( ) . left ,
212
+ y : el . getBoundingClientRect ( ) . top + el . offsetHeight
213
+ } ) ;
214
+ }
215
+ } else {
216
+ this . commands . executeCommand ( ArduinoCommands . OPEN_FILE_NAVIGATOR . id ) ;
242
217
}
243
218
}
244
219
} ) ;
@@ -295,11 +270,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
295
270
}
296
271
297
272
protected async selectBoard ( board : Board ) {
298
- await this . boardService . selectBoard ( board )
273
+ await this . boardService . selectBoard ( board ) ;
299
274
if ( this . boardsToolbarItem ) {
300
275
this . boardsToolbarItem . setSelectedBoard ( board ) ;
301
276
}
302
- this . selectedBoard = board ;
303
277
}
304
278
305
279
registerMenus ( registry : MenuModelRegistry ) {
@@ -332,6 +306,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
332
306
label : 'Upload' ,
333
307
order : '2'
334
308
} ) ;
309
+ registry . registerMenuAction ( ArduinoToolbarContextMenu . OPEN_GROUP , {
310
+ commandId : ArduinoCommands . OPEN_FILE_NAVIGATOR . id ,
311
+ label : 'Open...'
312
+ } ) ;
335
313
336
314
registry . registerSubmenu ( ArduinoMenus . TOOLS , 'Tools' ) ;
337
315
}
@@ -342,6 +320,30 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
342
320
return menuId ;
343
321
}
344
322
323
+ protected registerSketchesInMenu ( registry : MenuModelRegistry ) {
324
+ this . getWorkspaceSketches ( ) . then ( sketches => {
325
+ this . wsSketchCount = sketches . length ;
326
+ sketches . forEach ( sketch => {
327
+ const command : Command = {
328
+ id : 'openSketch' + sketch . name
329
+ }
330
+ this . commands . registerCommand ( command , {
331
+ execute : ( ) => this . commands . executeCommand ( ArduinoCommands . OPEN_SKETCH . id , sketch )
332
+ } ) ;
333
+
334
+ registry . registerMenuAction ( ArduinoToolbarContextMenu . WS_SKETCHES_GROUP , {
335
+ commandId : command . id ,
336
+ label : sketch . name
337
+ } ) ;
338
+ } )
339
+ } )
340
+ }
341
+
342
+ protected async getWorkspaceSketches ( ) : Promise < Sketch [ ] > {
343
+ const sketches = this . sketches . getSketches ( this . workspaceService . workspace ) ;
344
+ return sketches ;
345
+ }
346
+
345
347
protected async openSketchFilesInNewWindow ( uri : string ) {
346
348
const location = new URL ( window . location . href ) ;
347
349
location . searchParams . set ( 'sketch' , uri ) ;
0 commit comments