@@ -12,6 +12,7 @@ import { ILogger } from '@theia/core/lib/common/logger';
12
12
import { deepClone , deepFreeze } from '@theia/core/lib/common/objects' ;
13
13
import type { Mutable } from '@theia/core/lib/common/types' ;
14
14
import { inject , injectable , named } from '@theia/core/shared/inversify' ;
15
+ import { FQBN } from 'fqbn' ;
15
16
import {
16
17
BoardDetails ,
17
18
BoardsService ,
@@ -29,6 +30,14 @@ import type {
29
30
import { NotificationCenter } from '../notification-center' ;
30
31
import { BoardsServiceProvider } from './boards-service-provider' ;
31
32
33
+ export interface SelectConfigOptionParams {
34
+ readonly fqbn : string ;
35
+ readonly optionsToUpdate : readonly Readonly < {
36
+ option : string ;
37
+ selectedValue : string ;
38
+ } > [ ] ;
39
+ }
40
+
32
41
@injectable ( )
33
42
export class BoardsDataStore
34
43
implements
@@ -64,7 +73,12 @@ export class BoardsDataStore
64
73
this . toDispose . pushAll ( [
65
74
this . boardsServiceProvider . onBoardsConfigDidChange ( ( event ) => {
66
75
if ( isBoardIdentifierChangeEvent ( event ) ) {
67
- this . updateSelectedBoardData ( event . selectedBoard ?. fqbn ) ;
76
+ this . updateSelectedBoardData (
77
+ event . selectedBoard ?. fqbn ,
78
+ // If the change event comes from toolbar and the FQBN contains custom board options, change the currently selected options
79
+ // https://github.com/arduino/arduino-ide/issues/1588
80
+ event . reason === 'toolbar'
81
+ ) ;
68
82
}
69
83
} ) ,
70
84
this . notificationCenter . onPlatformDidInstall ( async ( { item } ) => {
@@ -125,9 +139,22 @@ export class BoardsDataStore
125
139
}
126
140
127
141
private async updateSelectedBoardData (
128
- fqbn : string | undefined
142
+ fqbn : string | undefined ,
143
+ updateConfigOptions = false
129
144
) : Promise < void > {
130
145
this . _selectedBoardData = await this . getSelectedBoardData ( fqbn ) ;
146
+ if ( fqbn && updateConfigOptions ) {
147
+ const { options } = new FQBN ( fqbn ) ;
148
+ if ( options ) {
149
+ const optionsToUpdate = Object . entries ( options ) . map ( ( [ key , value ] ) => ( {
150
+ option : key ,
151
+ selectedValue : value ,
152
+ } ) ) ;
153
+ const params = { fqbn, optionsToUpdate } ;
154
+ await this . selectConfigOption ( params ) ;
155
+ this . _selectedBoardData = await this . getSelectedBoardData ( fqbn ) ; // reload the updated data
156
+ }
157
+ }
131
158
}
132
159
133
160
onStop ( ) : void {
@@ -168,7 +195,7 @@ export class BoardsDataStore
168
195
return undefined ;
169
196
}
170
197
const { configOptions } = await this . getData ( fqbn ) ;
171
- return ConfigOption . decorate ( fqbn , configOptions ) ;
198
+ return new FQBN ( fqbn ) . withConfigOptions ( ... configOptions ) . toString ( ) ;
172
199
}
173
200
174
201
async getData ( fqbn : string | undefined ) : Promise < BoardsDataStore . Data > {
@@ -213,36 +240,41 @@ export class BoardsDataStore
213
240
return true ;
214
241
}
215
242
216
- async selectConfigOption ( {
217
- fqbn,
218
- option,
219
- selectedValue,
220
- } : {
221
- fqbn : string ;
222
- option : string ;
223
- selectedValue : string ;
224
- } ) : Promise < boolean > {
225
- const data = deepClone ( await this . getData ( fqbn ) ) ;
226
- const { configOptions } = data ;
227
- const configOption = configOptions . find ( ( c ) => c . option === option ) ;
228
- if ( ! configOption ) {
243
+ async selectConfigOption ( params : SelectConfigOptionParams ) : Promise < boolean > {
244
+ const { fqbn, optionsToUpdate } = params ;
245
+ if ( ! optionsToUpdate . length ) {
229
246
return false ;
230
247
}
231
- let updated = false ;
232
- for ( const value of configOption . values ) {
233
- const mutable : Mutable < ConfigValue > = value ;
234
- if ( mutable . value === selectedValue ) {
235
- mutable . selected = true ;
236
- updated = true ;
237
- } else {
238
- mutable . selected = false ;
248
+
249
+ const mutableData = deepClone ( await this . getData ( fqbn ) ) ;
250
+ let didChange = false ;
251
+
252
+ for ( const { option, selectedValue } of optionsToUpdate ) {
253
+ const { configOptions } = mutableData ;
254
+ const configOption = configOptions . find ( ( c ) => c . option === option ) ;
255
+ if ( configOption ) {
256
+ let updated = false ;
257
+ for ( const value of configOption . values ) {
258
+ const mutable : Mutable < ConfigValue > = value ;
259
+ if ( mutable . value === selectedValue ) {
260
+ mutable . selected = true ;
261
+ updated = true ;
262
+ } else {
263
+ mutable . selected = false ;
264
+ }
265
+ }
266
+ if ( updated ) {
267
+ didChange = true ;
268
+ }
239
269
}
240
270
}
241
- if ( ! updated ) {
271
+
272
+ if ( ! didChange ) {
242
273
return false ;
243
274
}
244
- await this . setData ( { fqbn, data } ) ;
245
- this . fireChanged ( { fqbn, data } ) ;
275
+
276
+ await this . setData ( { fqbn, data : mutableData } ) ;
277
+ this . fireChanged ( { fqbn, data : mutableData } ) ;
246
278
return true ;
247
279
}
248
280
0 commit comments