1
- import { inject , injectable } from 'inversify' ;
1
+ import { inject , injectable , postConstruct } from 'inversify' ;
2
2
import { Emitter } from '@theia/core/lib/common/event' ;
3
- import { CoreService } from '../../common/protocol' ;
3
+ import { BoardUserField , CoreService } from '../../common/protocol' ;
4
4
import { ArduinoMenus } from '../menu/arduino-menus' ;
5
5
import { ArduinoToolbar } from '../toolbar/arduino-toolbar' ;
6
6
import { BoardsDataStore } from '../boards/boards-data-store' ;
@@ -14,6 +14,7 @@ import {
14
14
KeybindingRegistry ,
15
15
TabBarToolbarRegistry ,
16
16
} from './contribution' ;
17
+ import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog' ;
17
18
18
19
@injectable ( )
19
20
export class UploadSketch extends SketchContribution {
@@ -29,16 +30,79 @@ export class UploadSketch extends SketchContribution {
29
30
@inject ( BoardsServiceProvider )
30
31
protected readonly boardsServiceClientImpl : BoardsServiceProvider ;
31
32
33
+ @inject ( UserFieldsDialog )
34
+ protected readonly userFieldsDialog : UserFieldsDialog ;
35
+
36
+ protected cachedUserFields : Map < string , BoardUserField [ ] > = new Map ( ) ;
37
+
32
38
protected readonly onDidChangeEmitter = new Emitter < Readonly < void > > ( ) ;
33
39
readonly onDidChange = this . onDidChangeEmitter . event ;
34
40
35
41
protected uploadInProgress = false ;
42
+ protected boardRequiresUserFields = false ;
43
+
44
+ @postConstruct ( )
45
+ protected init ( ) : void {
46
+ this . boardsServiceClientImpl . onBoardsConfigChanged ( async ( ) => {
47
+ const userFields = await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
48
+ this . boardRequiresUserFields = userFields . length > 0 ;
49
+ } )
50
+ }
51
+
52
+ private selectedFqbnAddress ( ) : string {
53
+ const { boardsConfig } = this . boardsServiceClientImpl ;
54
+ const fqbn = boardsConfig . selectedBoard ?. fqbn ;
55
+ if ( ! fqbn ) {
56
+ return "" ;
57
+ }
58
+ const address = boardsConfig . selectedBoard ?. port ?. address
59
+ if ( ! address ) {
60
+ return "" ;
61
+ }
62
+ return fqbn + "|" + address ;
63
+ }
36
64
37
65
registerCommands ( registry : CommandRegistry ) : void {
38
66
registry . registerCommand ( UploadSketch . Commands . UPLOAD_SKETCH , {
39
- execute : ( ) => this . uploadSketch ( ) ,
67
+ execute : async ( ) => {
68
+ const key = this . selectedFqbnAddress ( ) ;
69
+ if ( ! key ) {
70
+ return ;
71
+ }
72
+ if ( this . boardRequiresUserFields && ! this . cachedUserFields . has ( key ) ) {
73
+ this . userFieldsDialog . value = await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
74
+ const result = await this . userFieldsDialog . open ( ) ;
75
+ if ( ! result ) {
76
+ return ;
77
+ }
78
+ this . cachedUserFields . set ( key , result ) ;
79
+ }
80
+ this . uploadSketch ( ) ;
81
+ } ,
40
82
isEnabled : ( ) => ! this . uploadInProgress ,
41
83
} ) ;
84
+ registry . registerCommand (
85
+ UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION ,
86
+ {
87
+ execute : async ( ) => {
88
+ const key = this . selectedFqbnAddress ( ) ;
89
+ if ( ! key ) {
90
+ return ;
91
+ }
92
+
93
+ const cached = this . cachedUserFields . get ( key ) ;
94
+ this . userFieldsDialog . value = cached ?? await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
95
+
96
+ const result = await this . userFieldsDialog . open ( )
97
+ if ( ! result ) {
98
+ return ;
99
+ }
100
+ this . cachedUserFields . set ( key , result ) ;
101
+ this . uploadSketch ( ) ;
102
+ } ,
103
+ isEnabled : ( ) => ! this . uploadInProgress && this . boardRequiresUserFields ,
104
+ }
105
+ ) ;
42
106
registry . registerCommand (
43
107
UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER ,
44
108
{
@@ -62,10 +126,15 @@ export class UploadSketch extends SketchContribution {
62
126
label : 'Upload' ,
63
127
order : '1' ,
64
128
} ) ;
129
+ registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
130
+ commandId : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . id ,
131
+ label : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . label ,
132
+ order : '2' ,
133
+ } ) ;
65
134
registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
66
135
commandId : UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER . id ,
67
136
label : 'Upload Using Programmer' ,
68
- order : '2 ' ,
137
+ order : '3 ' ,
69
138
} ) ;
70
139
}
71
140
@@ -131,6 +200,11 @@ export class UploadSketch extends SketchContribution {
131
200
const optimizeForDebug = this . editorMode . compileForDebug ;
132
201
const { selectedPort } = boardsConfig ;
133
202
const port = selectedPort ;
203
+ const userFields = this . cachedUserFields . get ( this . selectedFqbnAddress ( ) ) ;
204
+ if ( ! userFields ) {
205
+ this . messageService . error ( "Can't find user fields for connected board" ) ;
206
+ return ;
207
+ }
134
208
135
209
if ( usingProgrammer ) {
136
210
const programmer = selectedProgrammer ;
@@ -143,6 +217,7 @@ export class UploadSketch extends SketchContribution {
143
217
verbose,
144
218
verify,
145
219
sourceOverride,
220
+ userFields,
146
221
} ;
147
222
} else {
148
223
options = {
@@ -153,6 +228,7 @@ export class UploadSketch extends SketchContribution {
153
228
verbose,
154
229
verify,
155
230
sourceOverride,
231
+ userFields,
156
232
} ;
157
233
}
158
234
this . outputChannelManager . getChannel ( 'Arduino' ) . clear ( ) ;
@@ -196,6 +272,11 @@ export namespace UploadSketch {
196
272
export const UPLOAD_SKETCH : Command = {
197
273
id : 'arduino-upload-sketch' ,
198
274
} ;
275
+ export const UPLOAD_WITH_CONFIGURATION : Command = {
276
+ id : 'arduino-upload-with-configuration-sketch' ,
277
+ label : 'Configure And Upload' ,
278
+ category : 'Arduino' ,
279
+ }
199
280
export const UPLOAD_SKETCH_USING_PROGRAMMER : Command = {
200
281
id : 'arduino-upload-sketch-using-programmer' ,
201
282
} ;
0 commit comments