1+ import { nls } from '@theia/core/lib/common/nls' ;
12import type { Mutable } from '@theia/core/lib/common/types' ;
23import type { Defined } from '../types' ;
34import { naturalCompare } from '../utils' ;
@@ -16,6 +17,7 @@ import {
1617 PortIdentifier ,
1718 portIdentifierEquals ,
1819 portProtocolComparator ,
20+ selectBoard ,
1921 unconfirmedBoard ,
2022 unknownBoard ,
2123} from './boards-service' ;
@@ -245,7 +247,44 @@ function boardListItemComparator(
245247}
246248
247249/**
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.
249288 */
250289export interface BoardListItemLabels {
251290 readonly boardLabel : string ;
@@ -301,6 +340,7 @@ function createBoardListItemLabels(item: BoardListItem): BoardListItemLabels {
301340 * and makes a `1..1` association between a board identifier and the port it belongs to.
302341 */
303342export interface BoardList {
343+ readonly labels : BoardListLabels ;
304344 /**
305345 * All detected ports with zero to many boards and optional inferred information based on historical selection/usage.
306346 */
@@ -375,46 +415,10 @@ export function createBoardList(
375415 items . push ( item ) ;
376416 }
377417 items . sort ( boardListItemComparator ) ;
418+ const selectedIndex = findSelectedIndex ( boardsConfig , items ) ;
419+ const labels = createBoardListLabels ( boardsConfig , items [ selectedIndex ] ) ;
378420
379421 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 ;
418422 let _allPorts : DetectedPort [ ] | undefined ;
419423 const ports = (
420424 predicate : ( detectedPort : DetectedPort ) => boolean = ( ) => true
@@ -442,19 +446,14 @@ export function createBoardList(
442446 ) ;
443447 return Object . assign ( ports , { matchingIndex } ) ;
444448 } ;
445- const selectedIndexMemoized = ( ) => {
446- if ( typeof _selectedIndex !== 'number' ) {
447- _selectedIndex = findSelectedIndex ( ) ;
448- }
449- return _selectedIndex ;
450- } ;
451449
452450 let _boards : ( BoardListItemWithBoard | InferredBoardListItem ) [ ] | undefined ;
453451 return {
452+ labels,
454453 items,
455454 boardsConfig,
456455 get selectedIndex ( ) {
457- return selectedIndexMemoized ( ) ;
456+ return selectedIndex ;
458457 } ,
459458 get boards ( ) {
460459 if ( ! _boards ) {
@@ -497,7 +496,6 @@ export function createBoardList(
497496 return result ;
498497 } ,
499498 toString ( ) {
500- const selectedIndex = selectedIndexMemoized ( ) ;
501499 return JSON . stringify (
502500 {
503501 detectedPorts,
@@ -513,6 +511,47 @@ export function createBoardList(
513511 } ;
514512}
515513
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+
516555function createBoardListItemUI (
517556 detectedPort : DetectedPort ,
518557 boardListHistory : BoardListHistory
0 commit comments