@@ -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 , AttachedSerialBoard } from '../common/protocol/boards-service' ;
8
+ import { BoardsService } from '../common/protocol/boards-service' ;
9
9
import { ArduinoCommands } from './arduino-commands' ;
10
10
import { CoreService } from '../common/protocol/core-service' ;
11
11
import { WorkspaceServiceExt } from './workspace-service-ext' ;
@@ -26,8 +26,6 @@ import {
26
26
StatusBar ,
27
27
ShellLayoutRestorer ,
28
28
StatusBarAlignment ,
29
- QuickOpenItem ,
30
- QuickOpenMode ,
31
29
QuickOpenService ,
32
30
LabelProvider
33
31
} from '@theia/core/lib/browser' ;
@@ -47,6 +45,8 @@ import { BoardsToolBarItem } from './boards/boards-toolbar-item';
47
45
import { BoardsConfig } from './boards/boards-config' ;
48
46
import { MonitorService } from '../common/protocol/monitor-service' ;
49
47
import { ConfigService } from '../common/protocol/config-service' ;
48
+ import { MonitorConnection } from './monitor/monitor-connection' ;
49
+ import { MonitorViewContribution } from './monitor/monitor-view-contribution' ;
50
50
51
51
export namespace ArduinoMenus {
52
52
export const SKETCH = [ ...MAIN_MENU_BAR , '3_sketch' ] ;
@@ -72,9 +72,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
72
72
@inject ( MonitorService )
73
73
protected readonly monitorService : MonitorService ;
74
74
75
- // TODO: make this better!
76
- protected connectionId : string | undefined ;
77
-
78
75
@inject ( WorkspaceServiceExt )
79
76
protected readonly workspaceServiceExt : WorkspaceServiceExt ;
80
77
@@ -143,6 +140,8 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
143
140
144
141
@inject ( ConfigService )
145
142
protected readonly configService : ConfigService ;
143
+ @inject ( MonitorConnection )
144
+ protected readonly monitorConnection : MonitorConnection ;
146
145
147
146
protected boardsToolbarItem : BoardsToolBarItem | null ;
148
147
protected wsSketchCount : number = 0 ;
@@ -197,13 +196,19 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
197
196
commands = { this . commands }
198
197
boardsServiceClient = { this . boardsServiceClient }
199
198
boardService = { this . boardsService } /> ,
200
- isVisible : widget => this . isArduinoToolbar ( widget )
199
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left'
200
+ } ) ;
201
+ registry . registerItem ( {
202
+ id : 'toggle-serial-monitor' ,
203
+ command : MonitorViewContribution . OPEN_SERIAL_MONITOR ,
204
+ tooltip : 'Toggle Serial Monitor' ,
205
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'right'
201
206
} )
202
207
}
203
208
204
209
registerCommands ( registry : CommandRegistry ) : void {
205
210
registry . registerCommand ( ArduinoCommands . VERIFY , {
206
- isVisible : widget => this . isArduinoToolbar ( widget ) ,
211
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
207
212
isEnabled : widget => true ,
208
213
execute : async ( ) => {
209
214
const widget = this . getCurrentWidget ( ) ;
@@ -231,7 +236,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
231
236
}
232
237
} ) ;
233
238
registry . registerCommand ( ArduinoCommands . UPLOAD , {
234
- isVisible : widget => this . isArduinoToolbar ( widget ) ,
239
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
235
240
isEnabled : widget => true ,
236
241
execute : async ( ) => {
237
242
const widget = this . getCurrentWidget ( ) ;
@@ -244,6 +249,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
244
249
return ;
245
250
}
246
251
252
+ const connectionConfig = this . monitorConnection . connectionConfig ;
253
+ await this . monitorConnection . disconnect ( ) ;
254
+
247
255
try {
248
256
const { boardsConfig } = this . boardsServiceClient ;
249
257
if ( ! boardsConfig || ! boardsConfig . selectedBoard ) {
@@ -256,12 +264,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
256
264
await this . coreService . upload ( { uri : uri . toString ( ) , board : boardsConfig . selectedBoard , port : selectedPort } ) ;
257
265
} catch ( e ) {
258
266
await this . messageService . error ( e . toString ( ) ) ;
267
+ } finally {
268
+ if ( connectionConfig ) {
269
+ await this . monitorConnection . connect ( connectionConfig ) ;
270
+ }
259
271
}
260
272
}
261
273
} ) ;
262
274
registry . registerCommand ( ArduinoCommands . SHOW_OPEN_CONTEXT_MENU , {
263
- isVisible : widget => this . isArduinoToolbar ( widget ) ,
264
- isEnabled : widget => this . isArduinoToolbar ( widget ) ,
275
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
276
+ isEnabled : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
265
277
execute : async ( widget : Widget , target : EventTarget ) => {
266
278
if ( this . wsSketchCount ) {
267
279
const el = ( target as HTMLElement ) . parentElement ;
@@ -287,8 +299,8 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
287
299
}
288
300
} )
289
301
registry . registerCommand ( ArduinoCommands . SAVE_SKETCH , {
290
- isEnabled : widget => this . isArduinoToolbar ( widget ) ,
291
- isVisible : widget => this . isArduinoToolbar ( widget ) ,
302
+ isEnabled : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
303
+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
292
304
execute : async ( sketch : Sketch ) => {
293
305
registry . executeCommand ( CommonCommands . SAVE_ALL . id ) ;
294
306
}
@@ -324,65 +336,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
324
336
} ,
325
337
isToggled : ( ) => ARDUINO_PRO_MODE
326
338
} ) ;
327
- registry . registerCommand ( ArduinoCommands . CONNECT_TODO , {
328
- execute : async ( ) => {
329
- const { boardsConfig } = this . boardsServiceClient ;
330
- const { selectedBoard, selectedPort } = boardsConfig ;
331
- if ( ! selectedBoard ) {
332
- this . messageService . warn ( 'No boards selected.' ) ;
333
- return ;
334
- }
335
- const { name } = selectedBoard ;
336
- if ( ! selectedPort ) {
337
- this . messageService . warn ( `No ports selected for board: '${ name } '.` ) ;
338
- return ;
339
- }
340
- const attachedBoards = await this . boardsService . getAttachedBoards ( ) ;
341
- const connectedBoard = attachedBoards . boards . filter ( AttachedSerialBoard . is ) . find ( board => BoardsConfig . Config . sameAs ( boardsConfig , board ) ) ;
342
- if ( ! connectedBoard ) {
343
- this . messageService . warn ( `The selected '${ name } ' board is not connected on ${ selectedPort } .` ) ;
344
- return ;
345
- }
346
- if ( this . connectionId ) {
347
- console . log ( '>>> Disposing existing monitor connection before establishing a new one...' ) ;
348
- const result = await this . monitorService . disconnect ( this . connectionId ) ;
349
- if ( ! result ) {
350
- // TODO: better!!!
351
- console . error ( `Could not close connection: ${ this . connectionId } . Check the backend logs.` ) ;
352
- } else {
353
- console . log ( `<<< Disposed ${ this . connectionId } connection.` )
354
- }
355
- }
356
- const { connectionId } = await this . monitorService . connect ( { board : selectedBoard , port : selectedPort } ) ;
357
- this . connectionId = connectionId ;
358
- }
359
- } ) ;
360
- registry . registerCommand ( ArduinoCommands . SEND , {
361
- isEnabled : ( ) => ! ! this . connectionId ,
362
- execute : async ( ) => {
363
- const { monitorService, connectionId } = this ;
364
- const model = {
365
- onType ( lookFor : string , acceptor : ( items : QuickOpenItem [ ] ) => void ) : void {
366
- acceptor ( [
367
- new QuickOpenItem ( {
368
- label : "Type your message and press 'Enter' to send it to the board. Escape to cancel." ,
369
- run : ( mode : QuickOpenMode ) : boolean => {
370
- if ( mode !== QuickOpenMode . OPEN ) {
371
- return false ;
372
- }
373
- monitorService . send ( connectionId ! , lookFor + '\n' ) ;
374
- return true ;
375
- }
376
- } )
377
- ] ) ;
378
- }
379
- } ;
380
- const options = {
381
- placeholder : "Your message. The message will be suffixed with a LF ['\\n']." ,
382
- } ;
383
- this . quickOpenService . open ( model , options ) ;
384
- }
385
- } )
386
339
}
387
340
388
341
registerMenus ( registry : MenuModelRegistry ) {
@@ -555,13 +508,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
555
508
return undefined ;
556
509
}
557
510
558
- private isArduinoToolbar ( maybeToolbarWidget : any ) : boolean {
559
- if ( maybeToolbarWidget instanceof ArduinoToolbar ) {
560
- return true ;
561
- }
562
- return false ;
563
- }
564
-
565
511
private toUri ( arg : any ) : URI | undefined {
566
512
if ( arg instanceof URI ) {
567
513
return arg ;
0 commit comments