@@ -5,7 +5,7 @@ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
55import { MessageService } from '@theia/core/lib/common/message-service' ;
66import { CommandContribution , CommandRegistry , Command } from '@theia/core/lib/common/command' ;
77import { 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' ;
99import { ArduinoCommands } from './arduino-commands' ;
1010import { CoreService } from '../common/protocol/core-service' ;
1111import { WorkspaceServiceExt } from './workspace-service-ext' ;
@@ -26,8 +26,6 @@ import {
2626 StatusBar ,
2727 ShellLayoutRestorer ,
2828 StatusBarAlignment ,
29- QuickOpenItem ,
30- QuickOpenMode ,
3129 QuickOpenService ,
3230 LabelProvider
3331} from '@theia/core/lib/browser' ;
@@ -47,6 +45,8 @@ import { BoardsToolBarItem } from './boards/boards-toolbar-item';
4745import { BoardsConfig } from './boards/boards-config' ;
4846import { MonitorService } from '../common/protocol/monitor-service' ;
4947import { ConfigService } from '../common/protocol/config-service' ;
48+ import { MonitorConnection } from './monitor/monitor-connection' ;
49+ import { MonitorViewContribution } from './monitor/monitor-view-contribution' ;
5050
5151export namespace ArduinoMenus {
5252 export const SKETCH = [ ...MAIN_MENU_BAR , '3_sketch' ] ;
@@ -72,9 +72,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
7272 @inject ( MonitorService )
7373 protected readonly monitorService : MonitorService ;
7474
75- // TODO: make this better!
76- protected connectionId : string | undefined ;
77-
7875 @inject ( WorkspaceServiceExt )
7976 protected readonly workspaceServiceExt : WorkspaceServiceExt ;
8077
@@ -143,6 +140,8 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
143140
144141 @inject ( ConfigService )
145142 protected readonly configService : ConfigService ;
143+ @inject ( MonitorConnection )
144+ protected readonly monitorConnection : MonitorConnection ;
146145
147146 protected boardsToolbarItem : BoardsToolBarItem | null ;
148147 protected wsSketchCount : number = 0 ;
@@ -197,13 +196,19 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
197196 commands = { this . commands }
198197 boardsServiceClient = { this . boardsServiceClient }
199198 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'
201206 } )
202207 }
203208
204209 registerCommands ( registry : CommandRegistry ) : void {
205210 registry . registerCommand ( ArduinoCommands . VERIFY , {
206- isVisible : widget => this . isArduinoToolbar ( widget ) ,
211+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
207212 isEnabled : widget => true ,
208213 execute : async ( ) => {
209214 const widget = this . getCurrentWidget ( ) ;
@@ -231,7 +236,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
231236 }
232237 } ) ;
233238 registry . registerCommand ( ArduinoCommands . UPLOAD , {
234- isVisible : widget => this . isArduinoToolbar ( widget ) ,
239+ isVisible : widget => ArduinoToolbar . is ( widget ) && widget . side === 'left' ,
235240 isEnabled : widget => true ,
236241 execute : async ( ) => {
237242 const widget = this . getCurrentWidget ( ) ;
@@ -244,6 +249,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
244249 return ;
245250 }
246251
252+ const connectionConfig = this . monitorConnection . connectionConfig ;
253+ await this . monitorConnection . disconnect ( ) ;
254+
247255 try {
248256 const { boardsConfig } = this . boardsServiceClient ;
249257 if ( ! boardsConfig || ! boardsConfig . selectedBoard ) {
@@ -256,12 +264,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
256264 await this . coreService . upload ( { uri : uri . toString ( ) , board : boardsConfig . selectedBoard , port : selectedPort } ) ;
257265 } catch ( e ) {
258266 await this . messageService . error ( e . toString ( ) ) ;
267+ } finally {
268+ if ( connectionConfig ) {
269+ await this . monitorConnection . connect ( connectionConfig ) ;
270+ }
259271 }
260272 }
261273 } ) ;
262274 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' ,
265277 execute : async ( widget : Widget , target : EventTarget ) => {
266278 if ( this . wsSketchCount ) {
267279 const el = ( target as HTMLElement ) . parentElement ;
@@ -287,8 +299,8 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
287299 }
288300 } )
289301 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' ,
292304 execute : async ( sketch : Sketch ) => {
293305 registry . executeCommand ( CommonCommands . SAVE_ALL . id ) ;
294306 }
@@ -324,65 +336,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
324336 } ,
325337 isToggled : ( ) => ARDUINO_PRO_MODE
326338 } ) ;
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- } )
386339 }
387340
388341 registerMenus ( registry : MenuModelRegistry ) {
@@ -555,13 +508,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
555508 return undefined ;
556509 }
557510
558- private isArduinoToolbar ( maybeToolbarWidget : any ) : boolean {
559- if ( maybeToolbarWidget instanceof ArduinoToolbar ) {
560- return true ;
561- }
562- return false ;
563- }
564-
565511 private toUri ( arg : any ) : URI | undefined {
566512 if ( arg instanceof URI ) {
567513 return arg ;
0 commit comments