Skip to content

Commit 033ed4e

Browse files
author
Akos Kitta
committed
fix: move UI labels building to to model
1 parent 693815f commit 033ed4e

File tree

3 files changed

+94
-69
lines changed

3 files changed

+94
-69
lines changed

arduino-ide-extension/src/browser/boards/boards-toolbar-item.tsx

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,9 @@ export class BoardsToolBarItem extends React.Component<
283283

284284
override render(): React.ReactNode {
285285
const { coords, boardList } = this.state;
286-
const { selectedBoard, selectedPort } = boardList.boardsConfig;
287-
288-
const boardLabel =
289-
selectedBoard?.name ||
290-
nls.localize('arduino/board/selectBoard', 'Select Board');
291-
const boardFqbn = selectedBoard?.fqbn;
292-
const selectedItem: BoardListItem | undefined =
293-
boardList.items[boardList.selectedIndex];
294-
let tooltip = `${boardLabel}${boardFqbn ? ` (${boardFqbn})` : ''}${
295-
selectedPort ? `\n${selectedPort.address}` : ''
296-
}`;
297-
if (selectedPort && !selectedItem) {
298-
tooltip += ` ${nls.localize(
299-
'arduino/common/notConnected',
300-
'[not connected]'
301-
)}`;
302-
}
303-
304-
const isConnected = boardList.selectedIndex >= 0;
305-
const protocolIcon = isConnected
306-
? iconNameFromProtocol(selectedPort?.protocol || '')
286+
const { boardLabel, selected, portProtocol, tooltip } = boardList.labels;
287+
const protocolIcon = portProtocol
288+
? iconNameFromProtocol(portProtocol)
307289
: null;
308290
const protocolIconClassNames = classNames(
309291
'arduino-boards-toolbar-item--protocol',
@@ -333,7 +315,7 @@ export class BoardsToolBarItem extends React.Component<
333315
'arduino-boards-toolbar-item--label',
334316
'noWrapInfo',
335317
'noselect',
336-
{ 'arduino-boards-toolbar-item--label-connected': isConnected }
318+
{ 'arduino-boards-toolbar-item--label-connected': selected }
337319
)}
338320
>
339321
{boardLabel}

arduino-ide-extension/src/common/protocol/board-list.ts

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { nls } from '@theia/core/lib/common/nls';
12
import type { Mutable } from '@theia/core/lib/common/types';
23
import type { Defined } from '../types';
34
import { 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
*/
250289
export 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
*/
303342
export 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+
516555
function createBoardListItemUI(
517556
detectedPort: DetectedPort,
518557
boardListHistory: BoardListHistory

arduino-ide-extension/src/common/protocol/boards-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,10 @@ export const unconfirmedBoard = nls.localize(
867867
'arduino/board/unconfirmedBoard',
868868
'Unconfirmed board'
869869
);
870+
export const selectBoard = nls.localize(
871+
'arduino/board/selectBoard',
872+
'Select Board'
873+
);
870874

871875
/**
872876
* The returned promise resolves to a `BoardInfo` if available to show in the UI or an info message explaining why showing the board info is not possible.

0 commit comments

Comments
 (0)