@@ -3,9 +3,9 @@ import { injectable, inject, postConstruct } from 'inversify';
3
3
import URI from '@theia/core/lib/common/uri' ;
4
4
import { EditorWidget } from '@theia/editor/lib/browser/editor-widget' ;
5
5
import { MessageService } from '@theia/core/lib/common/message-service' ;
6
- import { CommandContribution , CommandRegistry } from '@theia/core/lib/common/command' ;
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 } from '../common/protocol/boards-service' ;
8
+ import { BoardsService , Board , AttachedSerialBoard } 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' ;
@@ -15,18 +15,20 @@ import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
15
15
import { BoardsListWidgetFrontendContribution } from './boards/boards-widget-frontend-contribution' ;
16
16
import { BoardsNotificationService } from './boards-notification-service' ;
17
17
import { WorkspaceRootUriAwareCommandHandler , WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands' ;
18
- import { SelectionService } from '@theia/core' ;
18
+ import { SelectionService , MenuModelRegistry } from '@theia/core' ;
19
19
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service' ;
20
20
import { SketchFactory } from './sketch-factory' ;
21
21
import { ArduinoToolbar } from './toolbar/arduino-toolbar' ;
22
22
import { EditorManager } from '@theia/editor/lib/browser' ;
23
23
import { ContextMenuRenderer , OpenerService , Widget } from '@theia/core/lib/browser' ;
24
24
import { OpenFileDialogProps , FileDialogService } from '@theia/filesystem/lib/browser/file-dialog' ;
25
25
import { FileSystem } from '@theia/filesystem/lib/common' ;
26
- import { ArduinoOpenSketchContextMenu } from './arduino-file-menu' ;
26
+ import { ArduinoToolbarContextMenu } from './arduino-file-menu' ;
27
27
import { Sketch , SketchesService } from '../common/protocol/sketches-service' ;
28
28
import { WindowService } from '@theia/core/lib/browser/window/window-service' ;
29
29
import { CommonCommands } from '@theia/core/lib/browser/common-frontend-contribution'
30
+ import { BoardsToolBarItem } from './boards/boards-toolbar-item' ;
31
+ import { SelectBoardDialog } from './boards/select-board-dialog' ;
30
32
31
33
@injectable ( )
32
34
export class ArduinoFrontendContribution implements TabBarToolbarContribution , CommandContribution {
@@ -85,10 +87,58 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
85
87
@inject ( SketchesService )
86
88
protected readonly sketches : SketchesService ;
87
89
90
+ @inject ( SelectBoardDialog )
91
+ protected readonly selectBoardsDialog : SelectBoardDialog ;
92
+
93
+ @inject ( MenuModelRegistry )
94
+ protected readonly menuRegistry : MenuModelRegistry ;
95
+
96
+ @inject ( CommandRegistry )
97
+ protected readonly commands : CommandRegistry ;
98
+
99
+ protected boardsToolbarItem : BoardsToolBarItem | null ;
100
+ protected attachedBoards : Board [ ] ;
101
+ protected selectedBoard : Board ;
102
+
88
103
@postConstruct ( )
89
104
protected async init ( ) : Promise < void > {
90
105
// This is a hack. Otherwise, the backend services won't bind.
91
106
await this . workspaceServiceExt . roots ( ) ;
107
+ const { boards } = await this . boardService . getAttachedBoards ( ) ;
108
+ this . attachedBoards = boards ;
109
+ this . registerConnectedBoardsInMenu ( this . menuRegistry ) ;
110
+ }
111
+
112
+ protected async registerConnectedBoardsInMenu ( registry : MenuModelRegistry ) {
113
+ this . attachedBoards . forEach ( board => {
114
+ const port = this . getPort ( board ) ;
115
+ const command : Command = {
116
+ id : 'selectBoard' + port
117
+ }
118
+ this . commands . registerCommand ( command , {
119
+ execute : ( ) => this . commands . executeCommand ( ArduinoCommands . SELECT_BOARD . id , board ) ,
120
+ isToggled : ( ) => this . isSelectedBoard ( board )
121
+ } ) ;
122
+ registry . registerMenuAction ( ArduinoToolbarContextMenu . CONNECTED_GROUP , {
123
+ commandId : command . id ,
124
+ label : board . name + ' at ' + port
125
+ } ) ;
126
+ } ) ;
127
+ }
128
+
129
+ protected isSelectedBoard ( board : Board ) : boolean {
130
+ return AttachedSerialBoard . is ( board ) &&
131
+ this . selectedBoard &&
132
+ AttachedSerialBoard . is ( this . selectedBoard ) &&
133
+ board . port === this . selectedBoard . port &&
134
+ board . fqbn === this . selectedBoard . fqbn ;
135
+ }
136
+
137
+ protected getPort ( board : Board ) : string {
138
+ if ( AttachedSerialBoard . is ( board ) ) {
139
+ return board . port ;
140
+ }
141
+ return '' ;
92
142
}
93
143
94
144
registerToolbarItems ( registry : TabBarToolbarRegistry ) : void {
@@ -118,15 +168,11 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
118
168
} ) ;
119
169
registry . registerItem ( {
120
170
id : ConnectedBoards . TOOLBAR_ID ,
121
- // render: () => <BoardsToolBarItem
122
- // onNoBoardsInstalled={this.onNoBoardsInstalled.bind(this)}
123
- // onUnknownBoard={this.onUnknownBoard.bind(this)} />,
124
- render : ( ) => < ConnectedBoards
125
- boardsService = { this . boardService }
171
+ render : ( ) => < BoardsToolBarItem
172
+ ref = { ref => this . boardsToolbarItem = ref }
173
+ contextMenuRenderer = { this . contextMenuRenderer }
126
174
boardsNotificationService = { this . boardsNotificationService }
127
- quickPickService = { this . quickPickService }
128
- onNoBoardsInstalled = { this . onNoBoardsInstalled . bind ( this ) }
129
- onUnknownBoard = { this . onUnknownBoard . bind ( this ) } /> ,
175
+ boardService = { this . boardService } /> ,
130
176
isVisible : widget => this . isArduinoToolbar ( widget )
131
177
} )
132
178
}
@@ -180,7 +226,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
180
226
execute : async ( widget : Widget , event : React . MouseEvent < HTMLElement > ) => {
181
227
const el = ( event . target as HTMLElement ) . parentElement ;
182
228
if ( el ) {
183
- this . contextMenuRenderer . render ( ArduinoOpenSketchContextMenu . PATH , {
229
+ this . contextMenuRenderer . render ( ArduinoToolbarContextMenu . OPEN_SKETCH_PATH , {
184
230
x : el . getBoundingClientRect ( ) . left ,
185
231
y : el . getBoundingClientRect ( ) . top + el . offsetHeight
186
232
} ) ;
@@ -221,9 +267,32 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
221
267
registry . registerCommand ( ArduinoCommands . REFRESH_BOARDS , {
222
268
isEnabled : ( ) => true ,
223
269
execute : ( ) => this . boardsNotificationService . notifyBoardsInstalled ( )
270
+ } ) ;
271
+ registry . registerCommand ( ArduinoCommands . SELECT_BOARD , {
272
+ isEnabled : ( ) => true ,
273
+ execute : async ( board : Board ) => {
274
+ this . selectBoard ( board ) ;
275
+ }
276
+ } )
277
+ registry . registerCommand ( ArduinoCommands . OPEN_BOARDS_DIALOG , {
278
+ isEnabled : ( ) => true ,
279
+ execute : async ( ) => {
280
+ const boardAndPort = await this . selectBoardsDialog . open ( ) ;
281
+ if ( boardAndPort && boardAndPort . board ) {
282
+ this . selectBoard ( boardAndPort . board ) ;
283
+ }
284
+ }
224
285
} )
225
286
}
226
287
288
+ protected async selectBoard ( board : Board ) {
289
+ await this . boardService . selectBoard ( board )
290
+ if ( this . boardsToolbarItem ) {
291
+ this . boardsToolbarItem . setSelectedBoard ( board ) ;
292
+ }
293
+ this . selectedBoard = board ;
294
+ }
295
+
227
296
protected async openSketchFilesInNewWindow ( uri : string ) {
228
297
const location = new URL ( window . location . href ) ;
229
298
location . searchParams . set ( 'sketch' , uri ) ;
@@ -278,24 +347,24 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
278
347
return widget ;
279
348
}
280
349
281
- private async onNoBoardsInstalled ( ) {
282
- const action = await this . messageService . info ( "You have no boards installed. Use the boards mangager to install one." , "Open Boards Manager" ) ;
283
- if ( ! action ) {
284
- return ;
285
- }
350
+ // private async onNoBoardsInstalled() {
351
+ // const action = await this.messageService.info("You have no boards installed. Use the boards mangager to install one.", "Open Boards Manager");
352
+ // if (!action) {
353
+ // return;
354
+ // }
286
355
287
- this . boardsListWidgetFrontendContribution . openView ( { reveal : true } ) ;
288
- }
356
+ // this.boardsListWidgetFrontendContribution.openView({ reveal: true });
357
+ // }
289
358
290
- private async onUnknownBoard ( ) {
291
- const action = await this . messageService . warn ( "There's a board connected for which you need to install software." +
292
- " If this were not a PoC we would offer you the right package now." , "Open Boards Manager" ) ;
293
- if ( ! action ) {
294
- return ;
295
- }
359
+ // private async onUnknownBoard() {
360
+ // const action = await this.messageService.warn("There's a board connected for which you need to install software." +
361
+ // " If this were not a PoC we would offer you the right package now.", "Open Boards Manager");
362
+ // if (!action) {
363
+ // return;
364
+ // }
296
365
297
- this . boardsListWidgetFrontendContribution . openView ( { reveal : true } ) ;
298
- }
366
+ // this.boardsListWidgetFrontendContribution.openView({ reveal: true });
367
+ // }
299
368
300
369
private isArduinoToolbar ( maybeToolbarWidget : any ) : boolean {
301
370
if ( maybeToolbarWidget instanceof ArduinoToolbar ) {
0 commit comments