@@ -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 } from '../common/protocol/boards-service' ;
8
+ import { BoardsService , AttachedSerialBoard } 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' ;
@@ -19,7 +19,18 @@ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service
19
19
import { SketchFactory } from './sketch-factory' ;
20
20
import { ArduinoToolbar } from './toolbar/arduino-toolbar' ;
21
21
import { EditorManager , EditorMainMenu } from '@theia/editor/lib/browser' ;
22
- import { ContextMenuRenderer , OpenerService , Widget , StatusBar , ShellLayoutRestorer , StatusBarAlignment , LabelProvider } from '@theia/core/lib/browser' ;
22
+ import {
23
+ ContextMenuRenderer ,
24
+ OpenerService ,
25
+ Widget ,
26
+ StatusBar ,
27
+ ShellLayoutRestorer ,
28
+ StatusBarAlignment ,
29
+ QuickOpenItem ,
30
+ QuickOpenMode ,
31
+ QuickOpenService ,
32
+ LabelProvider
33
+ } from '@theia/core/lib/browser' ;
23
34
import { OpenFileDialogProps , FileDialogService } from '@theia/filesystem/lib/browser/file-dialog' ;
24
35
import { FileSystem , FileStat } from '@theia/filesystem/lib/common' ;
25
36
import { ArduinoToolbarContextMenu } from './arduino-file-menu' ;
@@ -34,6 +45,7 @@ import { MaybePromise } from '@theia/core/lib/common/types';
34
45
import { BoardsConfigDialog } from './boards/boards-config-dialog' ;
35
46
import { BoardsToolBarItem } from './boards/boards-toolbar-item' ;
36
47
import { BoardsConfig } from './boards/boards-config' ;
48
+ import { MonitorService } from '../common/protocol/monitor-service' ;
37
49
38
50
export namespace ArduinoMenus {
39
51
export const SKETCH = [ ...MAIN_MENU_BAR , '3_sketch' ] ;
@@ -56,6 +68,12 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
56
68
@inject ( CoreService )
57
69
protected readonly coreService : CoreService ;
58
70
71
+ @inject ( MonitorService )
72
+ protected readonly monitorService : MonitorService ;
73
+
74
+ // TODO: make this better!
75
+ protected connectionId : string | undefined ;
76
+
59
77
@inject ( WorkspaceServiceExt )
60
78
protected readonly workspaceServiceExt : WorkspaceServiceExt ;
61
79
@@ -115,6 +133,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
115
133
116
134
@inject ( LabelProvider )
117
135
protected readonly labelProvider : LabelProvider ;
136
+
137
+ @inject ( QuickOpenService )
138
+ protected readonly quickOpenService : QuickOpenService ;
118
139
119
140
protected boardsToolbarItem : BoardsToolBarItem | null ;
120
141
protected wsSketchCount : number = 0 ;
@@ -293,14 +314,73 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
293
314
this . boardsServiceClient . boardsConfig = boardsConfig ;
294
315
}
295
316
}
296
- } )
317
+ } ) ;
297
318
registry . registerCommand ( ArduinoCommands . TOGGLE_PRO_MODE , {
298
319
execute : ( ) => {
299
320
const oldModeState = ARDUINO_PRO_MODE ;
300
321
window . localStorage . setItem ( 'arduino-pro-mode' , oldModeState ? 'false' : 'true' ) ;
301
322
registry . executeCommand ( 'reset.layout' ) ;
302
323
} ,
303
324
isToggled : ( ) => ARDUINO_PRO_MODE
325
+ } ) ;
326
+ registry . registerCommand ( ArduinoCommands . CONNECT_TODO , {
327
+ execute : async ( ) => {
328
+ const { boardsConfig } = this . boardsServiceClient ;
329
+ const { selectedBoard, selectedPort } = boardsConfig ;
330
+ if ( ! selectedBoard ) {
331
+ this . messageService . warn ( 'No boards selected.' ) ;
332
+ return ;
333
+ }
334
+ const { name } = selectedBoard ;
335
+ if ( ! selectedPort ) {
336
+ this . messageService . warn ( `No ports selected for board: '${ name } '.` ) ;
337
+ return ;
338
+ }
339
+ const attachedBoards = await this . boardsService . getAttachedBoards ( ) ;
340
+ const connectedBoard = attachedBoards . boards . filter ( AttachedSerialBoard . is ) . find ( board => BoardsConfig . Config . sameAs ( boardsConfig , board ) ) ;
341
+ if ( ! connectedBoard ) {
342
+ this . messageService . warn ( `The selected '${ name } ' board is not connected on ${ selectedPort } .` ) ;
343
+ return ;
344
+ }
345
+ if ( this . connectionId ) {
346
+ console . log ( '>>> Disposing existing monitor connection before establishing a new one...' ) ;
347
+ const result = await this . monitorService . disconnect ( this . connectionId ) ;
348
+ if ( ! result ) {
349
+ // TODO: better!!!
350
+ console . error ( `Could not close connection: ${ this . connectionId } . Check the backend logs.` ) ;
351
+ } else {
352
+ console . log ( `<<< Disposed ${ this . connectionId } connection.` )
353
+ }
354
+ }
355
+ const { connectionId } = await this . monitorService . connect ( { board : selectedBoard , port : selectedPort } ) ;
356
+ this . connectionId = connectionId ;
357
+ }
358
+ } ) ;
359
+ registry . registerCommand ( ArduinoCommands . SEND , {
360
+ isEnabled : ( ) => ! ! this . connectionId ,
361
+ execute : async ( ) => {
362
+ const { monitorService, connectionId } = this ;
363
+ const model = {
364
+ onType ( lookFor : string , acceptor : ( items : QuickOpenItem [ ] ) => void ) : void {
365
+ acceptor ( [
366
+ new QuickOpenItem ( {
367
+ label : "Type your message and press 'Enter' to send it to the board. Escape to cancel." ,
368
+ run : ( mode : QuickOpenMode ) : boolean => {
369
+ if ( mode !== QuickOpenMode . OPEN ) {
370
+ return false ;
371
+ }
372
+ monitorService . send ( connectionId ! , lookFor + '\n' ) ;
373
+ return true ;
374
+ }
375
+ } )
376
+ ] ) ;
377
+ }
378
+ } ;
379
+ const options = {
380
+ placeholder : "Your message. The message will be suffixed with a LF ['\\n']." ,
381
+ } ;
382
+ this . quickOpenService . open ( model , options ) ;
383
+ }
304
384
} )
305
385
}
306
386
0 commit comments