1
+ import { nls } from '@theia/core/lib/common/nls' ;
1
2
import type { Mutable } from '@theia/core/lib/common/types' ;
2
3
import type { Defined } from '../types' ;
3
4
import { naturalCompare } from '../utils' ;
@@ -16,6 +17,7 @@ import {
16
17
PortIdentifier ,
17
18
portIdentifierEquals ,
18
19
portProtocolComparator ,
20
+ selectBoard ,
19
21
unconfirmedBoard ,
20
22
unknownBoard ,
21
23
} from './boards-service' ;
@@ -245,7 +247,44 @@ function boardListItemComparator(
245
247
}
246
248
247
249
/**
248
- * What is show in the UI with all the refinements, fallbacks, and tooltips.
250
+ * What is shown in the UI for the entire board list.
251
+ */
252
+ export interface BoardListLabels {
253
+ readonly boardLabel : string ;
254
+ readonly portProtocol : string | undefined ;
255
+ readonly tooltip : string ;
256
+ /**
257
+ * The client's board+port selection matches with one of the board list items.
258
+ */
259
+ readonly selected : boolean ;
260
+ }
261
+
262
+ function createBoardListLabels (
263
+ boardsConfig : BoardsConfig ,
264
+ selectedItem : BoardListItem | undefined
265
+ ) : BoardListLabels {
266
+ const { selectedBoard, selectedPort } = boardsConfig ;
267
+ const boardLabel = selectedBoard ?. name || selectBoard ;
268
+ const boardFqbn = selectedBoard ?. fqbn ;
269
+ let tooltip = `${ boardLabel } ${ boardFqbn ? ` (${ boardFqbn } )` : '' } ${
270
+ selectedPort ? `\n${ selectedPort . address } ` : ''
271
+ } `;
272
+ if ( selectedPort && ! selectedItem ) {
273
+ tooltip += ` ${ nls . localize (
274
+ 'arduino/common/notConnected' ,
275
+ '[not connected]'
276
+ ) } `;
277
+ }
278
+ return {
279
+ boardLabel,
280
+ portProtocol : selectedPort ?. protocol ,
281
+ tooltip,
282
+ selected : Boolean ( selectedItem ) ,
283
+ } ;
284
+ }
285
+
286
+ /**
287
+ * What is show in the UI for a particular board with all its refinements, fallbacks, and tooltips.
249
288
*/
250
289
export interface BoardListItemLabels {
251
290
readonly boardLabel : string ;
@@ -301,6 +340,7 @@ function createBoardListItemLabels(item: BoardListItem): BoardListItemLabels {
301
340
* and makes a `1..1` association between a board identifier and the port it belongs to.
302
341
*/
303
342
export interface BoardList {
343
+ readonly labels : BoardListLabels ;
304
344
/**
305
345
* All detected ports with zero to many boards and optional inferred information based on historical selection/usage.
306
346
*/
@@ -375,46 +415,10 @@ export function createBoardList(
375
415
items . push ( item ) ;
376
416
}
377
417
items . sort ( boardListItemComparator ) ;
418
+ const selectedIndex = findSelectedIndex ( boardsConfig , items ) ;
419
+ const labels = createBoardListLabels ( boardsConfig , items [ selectedIndex ] ) ;
378
420
379
421
const length = items . length ;
380
- const findSelectedIndex = ( ) : number => {
381
- if ( ! isDefinedBoardsConfig ( boardsConfig ) ) {
382
- return - 1 ;
383
- }
384
- const { selectedPort, selectedBoard } = boardsConfig ;
385
- const portKey = Port . keyOf ( selectedPort ) ;
386
- // find the exact match of the board and port combination
387
- for ( let index = 0 ; index < length ; index ++ ) {
388
- const item = items [ index ] ;
389
- const { board, port } = item ;
390
- if ( ! board ) {
391
- continue ;
392
- }
393
- if (
394
- Port . keyOf ( port ) === portKey &&
395
- boardIdentifierEquals ( board , selectedBoard )
396
- ) {
397
- return index ;
398
- }
399
- }
400
- // find match from inferred board
401
- for ( let index = 0 ; index < length ; index ++ ) {
402
- const item = items [ index ] ;
403
- if ( ! isInferredBoardListItem ( item ) ) {
404
- continue ;
405
- }
406
- const { inferredBoard, port } = item ;
407
- if (
408
- Port . keyOf ( port ) === portKey &&
409
- boardIdentifierEquals ( inferredBoard , boardsConfig . selectedBoard )
410
- ) {
411
- return index ;
412
- }
413
- }
414
- return - 1 ;
415
- } ;
416
-
417
- let _selectedIndex : number | undefined ;
418
422
let _allPorts : DetectedPort [ ] | undefined ;
419
423
const ports = (
420
424
predicate : ( detectedPort : DetectedPort ) => boolean = ( ) => true
@@ -442,19 +446,14 @@ export function createBoardList(
442
446
) ;
443
447
return Object . assign ( ports , { matchingIndex } ) ;
444
448
} ;
445
- const selectedIndexMemoized = ( ) => {
446
- if ( typeof _selectedIndex !== 'number' ) {
447
- _selectedIndex = findSelectedIndex ( ) ;
448
- }
449
- return _selectedIndex ;
450
- } ;
451
449
452
450
let _boards : ( BoardListItemWithBoard | InferredBoardListItem ) [ ] | undefined ;
453
451
return {
452
+ labels,
454
453
items,
455
454
boardsConfig,
456
455
get selectedIndex ( ) {
457
- return selectedIndexMemoized ( ) ;
456
+ return selectedIndex ;
458
457
} ,
459
458
get boards ( ) {
460
459
if ( ! _boards ) {
@@ -497,7 +496,6 @@ export function createBoardList(
497
496
return result ;
498
497
} ,
499
498
toString ( ) {
500
- const selectedIndex = selectedIndexMemoized ( ) ;
501
499
return JSON . stringify (
502
500
{
503
501
detectedPorts,
@@ -513,6 +511,47 @@ export function createBoardList(
513
511
} ;
514
512
}
515
513
514
+ function findSelectedIndex (
515
+ boardsConfig : BoardsConfig ,
516
+ items : readonly BoardListItem [ ]
517
+ ) : number {
518
+ if ( ! isDefinedBoardsConfig ( boardsConfig ) ) {
519
+ return - 1 ;
520
+ }
521
+ const length = items . length ;
522
+ const { selectedPort, selectedBoard } = boardsConfig ;
523
+ const portKey = Port . keyOf ( selectedPort ) ;
524
+ // find the exact match of the board and port combination
525
+ for ( let index = 0 ; index < length ; index ++ ) {
526
+ const item = items [ index ] ;
527
+ const { board, port } = item ;
528
+ if ( ! board ) {
529
+ continue ;
530
+ }
531
+ if (
532
+ Port . keyOf ( port ) === portKey &&
533
+ boardIdentifierEquals ( board , selectedBoard )
534
+ ) {
535
+ return index ;
536
+ }
537
+ }
538
+ // find match from inferred board
539
+ for ( let index = 0 ; index < length ; index ++ ) {
540
+ const item = items [ index ] ;
541
+ if ( ! isInferredBoardListItem ( item ) ) {
542
+ continue ;
543
+ }
544
+ const { inferredBoard, port } = item ;
545
+ if (
546
+ Port . keyOf ( port ) === portKey &&
547
+ boardIdentifierEquals ( inferredBoard , boardsConfig . selectedBoard )
548
+ ) {
549
+ return index ;
550
+ }
551
+ }
552
+ return - 1 ;
553
+ }
554
+
516
555
function createBoardListItemUI (
517
556
detectedPort : DetectedPort ,
518
557
boardListHistory : BoardListHistory
0 commit comments