Skip to content

Commit

Permalink
fix: board comparison in board+port select dialog
Browse files Browse the repository at this point in the history
made "equals" check stricter. if fqbns present, they must be equal:
do not fall back to board name compare.

Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Aug 2, 2023
1 parent a463091 commit e77b924
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as React from '@theia/core/shared/react';
import {
Board,
BoardIdentifier,
BoardsConfig,
BoardWithPackage,
DetectedPort,
findMatchingPortIndex,
Expand All @@ -15,14 +14,15 @@ import {
} from '../../common/protocol/boards-service';
import { Defined } from '../../common/types';
import { NotificationCenter } from '../notification-center';
import { BoardsConfigDialogState } from './boards-config-dialog';
import { EditBoardsConfigActionParams } from './boards-service-provider';

namespace BoardsConfigComponent {
export interface Props {
/**
* This is not the real config, it's only living in the dialog. Users can change it without update and can cancel any modifications.
*/
readonly boardsConfig: BoardsConfig;
readonly boardsConfig: BoardsConfigDialogState;
readonly notificationCenter: NotificationCenter;
readonly appState: FrontendApplicationState;
readonly onFocusNodeSet: (element: HTMLElement | undefined) => void;
Expand Down Expand Up @@ -187,10 +187,7 @@ export class BoardsConfigComponent extends React.Component<
};

private readonly selectBoard = (selectedBoard: BoardWithPackage) => {
this.props.onBoardSelected({
fqbn: selectedBoard.fqbn,
name: selectedBoard.name,
});
this.props.onBoardSelected(selectedBoard);
};

private readonly focusNodeSet = (element: HTMLElement | null) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import React from '@theia/core/shared/react';
import type { ReactNode } from '@theia/core/shared/react/index';
import {
BoardIdentifier,
BoardsConfig,
BoardWithPackage,
DetectedPort,
Expand All @@ -31,8 +30,12 @@ import {
@injectable()
export class BoardsConfigDialogProps extends DialogProps {}

export type BoardsConfigDialogState = Omit<BoardsConfig, 'selectedBoard'> & {
selectedBoard: BoardsConfig['selectedBoard'] | BoardWithPackage;
};

@injectable()
export class BoardsConfigDialog extends ReactDialog<BoardsConfig> {
export class BoardsConfigDialog extends ReactDialog<BoardsConfigDialogState> {
@inject(BoardsServiceProvider)
private readonly boardsServiceProvider: BoardsServiceProvider;
@inject(NotificationCenter)
Expand All @@ -43,7 +46,7 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfig> {
private readonly onFilterTextDidChangeEmitter: Emitter<
Defined<EditBoardsConfigActionParams['query']>
>;
private readonly onBoardSelected = (board: BoardIdentifier): void => {
private readonly onBoardSelected = (board: BoardWithPackage): void => {
this._boardsConfig.selectedBoard = board;
this.update();
};
Expand All @@ -64,7 +67,7 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfig> {
): readonly DetectedPort[] => {
return this.boardsServiceProvider.boardList.ports(predicate);
};
private _boardsConfig: BoardsConfig;
private _boardsConfig: BoardsConfigDialogState;
private focusNode: HTMLElement | undefined;

constructor(
Expand Down Expand Up @@ -181,7 +184,7 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfig> {
return '';
}

get value(): BoardsConfig {
get value(): BoardsConfigDialogState {
return this._boardsConfig;
}
}
56 changes: 33 additions & 23 deletions arduino-ide-extension/src/common/protocol/boards-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,10 @@ export namespace Board {
manuallyInstalled: boolean;
}>;
export function decorateBoards(
selectedBoard: BoardIdentifier | undefined,
selectedBoard: BoardIdentifier | BoardWithPackage | undefined,
boards: Array<BoardWithPackage>
): Array<Detailed> {
let foundSelected = false;
// Board names are not unique. We show the corresponding core name as a detail.
// https://github.com/arduino/arduino-cli/pull/294#issuecomment-513764948
const distinctBoardNames = new Map<string, number>();
Expand All @@ -496,36 +497,47 @@ export namespace Board {
const selectedBoardPackageId = selectedBoard
? createPlatformIdentifier(selectedBoard)
: undefined;
// Due to the non-unique board names, IDE2 has to check the package name.
const selected = (board: BoardWithPackage) => {
if (!!selectedBoard) {
if (
boardIdentifierEquals(
{ name: board.name, fqbn: board.fqbn },
selectedBoard
)
) {
const selectedBoardFqbn = selectedBoard?.fqbn;
// Due to the non-unique board names, IDE2 has to check the package name when boards are not installed and the FQBN is absent.
const isSelected = (board: BoardWithPackage) => {
if (!selectedBoard) {
return false;
}
if (foundSelected) {
return false;
}
let selected = false;
if (board.fqbn && selectedBoardFqbn) {
if (boardIdentifierEquals(board, selectedBoard)) {
selected = true;
}
}
if (!selected) {
if (board.name === selectedBoard.name) {
if (selectedBoardPackageId) {
return platformIdentifierEquals(
selectedBoardPackageId,
board.packageId
);
const boardPackageId = createPlatformIdentifier(board);
if (boardPackageId) {
if (
platformIdentifierEquals(boardPackageId, selectedBoardPackageId)
) {
selected = true;
}
}
}
if (!board.fqbn && board.name === selectedBoard.name) {
return true;
}
return false;
}
}
return false;
if (selected) {
foundSelected = true;
}
return selected;
};
return boards.map((board) => ({
...board,
details:
(distinctBoardNames.get(board.name) || 0) > 1
? ` - ${board.packageName}`
: undefined,
selected: selected(board),
selected: isSelected(board),
missing: !installed(board),
}));
}
Expand Down Expand Up @@ -711,9 +723,7 @@ export function boardIdentifierEquals(
if (left.fqbn && right.fqbn) {
const leftFqbn = options.looseFqbn ? sanitizeFqbn(left.fqbn) : left.fqbn;
const rightFqbn = options.looseFqbn ? sanitizeFqbn(right.fqbn) : right.fqbn;
if (leftFqbn === rightFqbn) {
return true;
}
return leftFqbn === rightFqbn;
}
// No more Genuino hack.
// https://github.com/arduino/arduino-ide/blob/f6a43254f5c416a2e4fa888875358336b42dd4d5/arduino-ide-extension/src/common/protocol/boards-service.ts#L572-L581
Expand Down

0 comments on commit e77b924

Please sign in to comment.