Skip to content

Commit c6b1250

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
ATL-814: Show boards and ports under Tools menu.
Signed-off-by: Akos Kitta <[email protected]>
1 parent f6b5dd2 commit c6b1250

File tree

7 files changed

+234
-21
lines changed

7 files changed

+234
-21
lines changed

Diff for: arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import { DebugSessionManager as TheiaDebugSessionManager } from '@theia/debug/li
133133
import { Sketchbook } from './contributions/sketchbook';
134134
import { DebugFrontendApplicationContribution } from './theia/debug/debug-frontend-application-contribution';
135135
import { DebugFrontendApplicationContribution as TheiaDebugFrontendApplicationContribution } from '@theia/debug/lib/browser/debug-frontend-application-contribution';
136+
import { BoardSelection } from './contributions/board-selection';
136137

137138
const ElementQueries = require('css-element-queries/src/ElementQueries');
138139

@@ -335,6 +336,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
335336
Contribution.configure(bind, About);
336337
Contribution.configure(bind, Debug);
337338
Contribution.configure(bind, Sketchbook);
339+
Contribution.configure(bind, BoardSelection);
338340

339341
bind(OutputServiceImpl).toSelf().inSingletonScope().onActivation(({ container }, outputService) => {
340342
WebSocketConnectionProvider.createProxy(container, OutputServicePath, outputService);
@@ -343,6 +345,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
343345
bind(OutputService).toService(OutputServiceImpl);
344346

345347
bind(NotificationCenter).toSelf().inSingletonScope();
348+
bind(FrontendApplicationContribution).toService(NotificationCenter);
346349
bind(NotificationServiceServer).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, NotificationServicePath)).inSingletonScope();
347350

348351
// Enable the dirty indicator on uncloseable widgets.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { injectable } from 'inversify';
2-
import { MenuModelRegistry } from '@theia/core';
32
import { BoardsListWidget } from './boards-list-widget';
43
import { BoardsPackage } from '../../common/protocol/boards-service';
54
import { ListWidgetFrontendContribution } from '../widgets/component-list/list-widget-frontend-contribution';
6-
import { ArduinoMenus } from '../menu/arduino-menus';
75

86
@injectable()
97
export class BoardsListWidgetFrontendContribution extends ListWidgetFrontendContribution<BoardsPackage> {
108

11-
static readonly OPEN_MANAGER = `${BoardsListWidget.WIDGET_ID}:toggle`;
12-
139
constructor() {
1410
super({
1511
widgetId: BoardsListWidget.WIDGET_ID,
@@ -18,7 +14,7 @@ export class BoardsListWidgetFrontendContribution extends ListWidgetFrontendCont
1814
area: 'left',
1915
rank: 600
2016
},
21-
toggleCommandId: BoardsListWidgetFrontendContribution.OPEN_MANAGER,
17+
toggleCommandId: `${BoardsListWidget.WIDGET_ID}:toggle`,
2218
toggleKeybinding: 'CtrlCmd+Shift+B'
2319
});
2420
}
@@ -27,14 +23,4 @@ export class BoardsListWidgetFrontendContribution extends ListWidgetFrontendCont
2723
this.openView();
2824
}
2925

30-
registerMenus(menus: MenuModelRegistry): void {
31-
if (this.toggleCommand) {
32-
menus.registerMenuAction(ArduinoMenus.TOOLS__MAIN_GROUP, {
33-
commandId: this.toggleCommand.id,
34-
label: 'Boards Manager...',
35-
order: '4'
36-
});
37-
}
38-
}
39-
4026
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import { inject, injectable } from 'inversify';
2+
import { remote } from 'electron';
3+
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
4+
import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable';
5+
import { BoardsConfig } from '../boards/boards-config';
6+
import { MainMenuManager } from '../../common/main-menu-manager';
7+
import { BoardsListWidget } from '../boards/boards-list-widget';
8+
import { NotificationCenter } from '../notification-center';
9+
import { BoardsServiceProvider } from '../boards/boards-service-provider';
10+
import { ArduinoMenus, unregisterSubmenu } from '../menu/arduino-menus';
11+
import { BoardsService, InstalledBoardWithPackage, AvailablePorts, Port } from '../../common/protocol';
12+
import { SketchContribution, Command, CommandRegistry } from './contribution';
13+
14+
@injectable()
15+
export class BoardSelection extends SketchContribution {
16+
17+
@inject(CommandRegistry)
18+
protected readonly commandRegistry: CommandRegistry;
19+
20+
@inject(MainMenuManager)
21+
protected readonly mainMenuManager: MainMenuManager;
22+
23+
@inject(MenuModelRegistry)
24+
protected readonly menuModelRegistry: MenuModelRegistry;
25+
26+
@inject(NotificationCenter)
27+
protected readonly notificationCenter: NotificationCenter;
28+
29+
@inject(BoardsService)
30+
protected readonly boardsService: BoardsService;
31+
32+
@inject(BoardsServiceProvider)
33+
protected readonly boardsServiceProvider: BoardsServiceProvider;
34+
35+
protected readonly toDisposeBeforeMenuRebuild = new DisposableCollection();
36+
37+
registerCommands(registry: CommandRegistry): void {
38+
registry.registerCommand(BoardSelection.Commands.GET_BOARD_INFO, {
39+
execute: async () => {
40+
const { selectedBoard, selectedPort } = this.boardsServiceProvider.boardsConfig;
41+
if (!selectedBoard) {
42+
this.messageService.info('Please select a board to obtain board info.');
43+
return;
44+
}
45+
if (!selectedBoard.fqbn) {
46+
this.messageService.info(`The platform for the selected '${selectedBoard.name}' board is not installed.`);
47+
return;
48+
}
49+
if (!selectedPort) {
50+
this.messageService.info('Please select a port to obtain board info.');
51+
return;
52+
}
53+
const boardDetails = await this.boardsService.getBoardDetails({ fqbn: selectedBoard.fqbn });
54+
if (boardDetails) {
55+
const { VID, PID } = boardDetails;
56+
const detail = `BN: ${selectedBoard.name}
57+
VID: ${VID}
58+
PID: ${PID}`;
59+
await remote.dialog.showMessageBox(remote.getCurrentWindow(), {
60+
message: 'Board Info',
61+
title: 'Board Info',
62+
type: 'info',
63+
detail,
64+
buttons: ['OK']
65+
});
66+
}
67+
}
68+
});
69+
}
70+
71+
onStart(): void {
72+
this.updateMenus();
73+
this.notificationCenter.onPlatformInstalled(this.updateMenus.bind(this));
74+
this.notificationCenter.onPlatformUninstalled(this.updateMenus.bind(this));
75+
this.boardsServiceProvider.onBoardsConfigChanged(this.updateMenus.bind(this));
76+
this.boardsServiceProvider.onAvailableBoardsChanged(this.updateMenus.bind(this));
77+
}
78+
79+
protected async updateMenus(): Promise<void> {
80+
const [installedBoards, availablePorts, config] = await Promise.all([
81+
this.installedBoards(),
82+
this.boardsService.getState(),
83+
this.boardsServiceProvider.boardsConfig
84+
]);
85+
this.rebuildMenus(installedBoards, availablePorts, config);
86+
}
87+
88+
protected rebuildMenus(installedBoards: InstalledBoardWithPackage[], availablePorts: AvailablePorts, config: BoardsConfig.Config): void {
89+
this.toDisposeBeforeMenuRebuild.dispose();
90+
91+
// Boards submenu
92+
const boardsSubmenuPath = [...ArduinoMenus.TOOLS__BOARD_SELECTION_GROUP, '1_boards'];
93+
const boardsSubmenuLabel = config.selectedBoard?.name;
94+
// Note: The submenu order starts from `100` because `Auto Format`, `Serial Monitor`, etc starts from `0` index.
95+
// The board specific items, and the rest, have order with `z`. We needed something between `0` and `z` with natural-order.
96+
this.menuModelRegistry.registerSubmenu(boardsSubmenuPath, `Board${!!boardsSubmenuLabel ? `: "${boardsSubmenuLabel}"` : ''}`, { order: '100' });
97+
this.toDisposeBeforeMenuRebuild.push(Disposable.create(() => unregisterSubmenu(boardsSubmenuPath, this.menuModelRegistry)));
98+
99+
// Ports submenu
100+
const portsSubmenuPath = [...ArduinoMenus.TOOLS__BOARD_SELECTION_GROUP, '2_ports'];
101+
const portsSubmenuLabel = config.selectedPort?.address;
102+
this.menuModelRegistry.registerSubmenu(portsSubmenuPath, `Port${!!portsSubmenuLabel ? `: "${portsSubmenuLabel}"` : ''}`, { order: '101' });
103+
this.toDisposeBeforeMenuRebuild.push(Disposable.create(() => unregisterSubmenu(portsSubmenuPath, this.menuModelRegistry)));
104+
105+
const getBoardInfo = { commandId: BoardSelection.Commands.GET_BOARD_INFO.id, label: 'Get Board Info', order: '103' };
106+
this.menuModelRegistry.registerMenuAction(ArduinoMenus.TOOLS__BOARD_SELECTION_GROUP, getBoardInfo);
107+
this.toDisposeBeforeMenuRebuild.push(Disposable.create(() => this.menuModelRegistry.unregisterMenuAction(getBoardInfo)));
108+
109+
const boardsManagerGroup = [...boardsSubmenuPath, '0_manager'];
110+
const boardsPackagesGroup = [...boardsSubmenuPath, '1_packages'];
111+
112+
this.menuModelRegistry.registerMenuAction(boardsManagerGroup, {
113+
commandId: `${BoardsListWidget.WIDGET_ID}:toggle`,
114+
label: 'Boards Manager...'
115+
});
116+
117+
// Installed boards
118+
for (const board of installedBoards) {
119+
const { packageId, packageName, fqbn, name } = board;
120+
121+
// Platform submenu
122+
const platformMenuPath = [...boardsPackagesGroup, packageId];
123+
// Note: Registering the same submenu twice is a noop. No need to group the boards per platform.
124+
this.menuModelRegistry.registerSubmenu(platformMenuPath, packageName);
125+
126+
const id = `arduino-select-board--${fqbn}`;
127+
const command = { id };
128+
const handler = {
129+
execute: () => {
130+
if (fqbn !== this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn) {
131+
this.boardsServiceProvider.boardsConfig = {
132+
selectedBoard: {
133+
name,
134+
fqbn,
135+
port: this.boardsServiceProvider.boardsConfig.selectedBoard?.port // TODO: verify!
136+
},
137+
selectedPort: this.boardsServiceProvider.boardsConfig.selectedPort
138+
}
139+
}
140+
},
141+
isToggled: () => fqbn === this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn
142+
};
143+
144+
// Board menu
145+
const menuAction = { commandId: id, label: name };
146+
this.commandRegistry.registerCommand(command, handler);
147+
this.toDisposeBeforeMenuRebuild.push(Disposable.create(() => this.commandRegistry.unregisterCommand(command)));
148+
this.menuModelRegistry.registerMenuAction(platformMenuPath, menuAction);
149+
// Note: we do not dispose the menu actions individually. Calling `unregisterSubmenu` on the parent will wipe the children menu nodes recursively.
150+
}
151+
152+
// Installed ports
153+
for (const address of Object.keys(availablePorts)) {
154+
if (!!availablePorts[address]) {
155+
const [port, boards] = availablePorts[address];
156+
if (!boards.length) {
157+
boards.push({
158+
name: ''
159+
});
160+
}
161+
for (const { name, fqbn } of boards) {
162+
const id = `arduino-select-port--${address}${fqbn ? `--${fqbn}` : ''}`;
163+
const command = { id };
164+
const handler = {
165+
execute: () => {
166+
if (!Port.equals(port, this.boardsServiceProvider.boardsConfig.selectedPort)) {
167+
this.boardsServiceProvider.boardsConfig = {
168+
selectedBoard: this.boardsServiceProvider.boardsConfig.selectedBoard,
169+
selectedPort: port
170+
}
171+
}
172+
},
173+
isToggled: () => Port.equals(port, this.boardsServiceProvider.boardsConfig.selectedPort)
174+
};
175+
const menuAction = {
176+
commandId: id,
177+
label: `${address}${name ? ` (${name})` : ''}`
178+
};
179+
this.commandRegistry.registerCommand(command, handler);
180+
this.toDisposeBeforeMenuRebuild.push(Disposable.create(() => this.commandRegistry.unregisterCommand(command)));
181+
this.menuModelRegistry.registerMenuAction(portsSubmenuPath, menuAction);
182+
}
183+
}
184+
}
185+
186+
this.mainMenuManager.update();
187+
}
188+
189+
protected async installedBoards(): Promise<InstalledBoardWithPackage[]> {
190+
const allBoards = await this.boardsService.allBoards({});
191+
return allBoards.filter(InstalledBoardWithPackage.is);
192+
}
193+
194+
}
195+
export namespace BoardSelection {
196+
export namespace Commands {
197+
export const GET_BOARD_INFO: Command = { id: 'arduino-get-board-info' };
198+
}
199+
}

Diff for: arduino-ide-extension/src/browser/contributions/examples.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as PQueue from 'p-queue';
22
import { inject, injectable, postConstruct } from 'inversify';
3-
import { MenuPath, SubMenuOptions, CompositeMenuNode } from '@theia/core/lib/common/menu';
3+
import { MenuPath, CompositeMenuNode } from '@theia/core/lib/common/menu';
44
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
55
import { OpenSketch } from './open-sketch';
66
import { ArduinoMenus } from '../menu/arduino-menus';
@@ -60,12 +60,11 @@ export abstract class Examples extends SketchContribution {
6060
registerRecursively(
6161
exampleContainer: ExampleContainer,
6262
menuPath: MenuPath,
63-
pushToDispose: DisposableCollection = new DisposableCollection(),
64-
options?: SubMenuOptions): void {
63+
pushToDispose: DisposableCollection = new DisposableCollection()): void {
6564

6665
const { label, sketches, children } = exampleContainer;
6766
const submenuPath = [...menuPath, label];
68-
this.menuRegistry.registerSubmenu(submenuPath, label, options);
67+
this.menuRegistry.registerSubmenu(submenuPath, label);
6968
children.forEach(child => this.registerRecursively(child, submenuPath, pushToDispose));
7069
for (const sketch of sketches) {
7170
const { uri } = sketch;

Diff for: arduino-ide-extension/src/browser/menu/arduino-menus.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export namespace ArduinoMenus {
4040
export const TOOLS = [...MAIN_MENU_BAR, '4_tools'];
4141
// `Auto Format`, `Library Manager...`, `Boards Manager...`
4242
export const TOOLS__MAIN_GROUP = [...TOOLS, '0_main'];
43+
// `Board`, `Port`, and `Get Board Info`.
44+
export const TOOLS__BOARD_SELECTION_GROUP = [...TOOLS, '2_board_selection'];
4345
// Core settings, such as `Processor` and `Programmers` for the board and `Burn Bootloader`
44-
export const TOOLS__BOARD_SETTINGS_GROUP = [...TOOLS, '1_board_settings'];
46+
export const TOOLS__BOARD_SETTINGS_GROUP = [...TOOLS, '3_board_settings'];
4547

4648
// -- Help
4749
// `About` group

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

+13
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,25 @@ export namespace BoardWithPackage {
267267

268268
}
269269

270+
export interface InstalledBoardWithPackage extends BoardWithPackage {
271+
readonly fqbn: string;
272+
}
273+
export namespace InstalledBoardWithPackage {
274+
275+
export function is(boardWithPackage: BoardWithPackage): boardWithPackage is InstalledBoardWithPackage {
276+
return !!boardWithPackage.fqbn;
277+
}
278+
279+
}
280+
270281
export interface BoardDetails {
271282
readonly fqbn: string;
272283
readonly requiredTools: Tool[];
273284
readonly configOptions: ConfigOption[];
274285
readonly programmers: Programmer[];
275286
readonly debuggingSupported: boolean;
287+
readonly VID: string;
288+
readonly PID: string;
276289
}
277290

278291
export interface Tool {

Diff for: arduino-ide-extension/src/node/boards-service-impl.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { injectable, inject, named } from 'inversify';
22
import { ILogger } from '@theia/core/lib/common/logger';
3+
import { notEmpty } from '@theia/core/lib/common/objects';
34
import {
45
BoardsService,
56
Installable,
@@ -128,12 +129,22 @@ export class BoardsServiceImpl implements BoardsService {
128129
platform: p.getPlatform()
129130
});
130131

132+
let VID = 'N/A';
133+
let PID = 'N/A';
134+
const usbId = detailsResp.getIdentificationPrefList().map(item => item.getUsbid()).find(notEmpty);
135+
if (usbId) {
136+
VID = usbId.getVid();
137+
PID = usbId.getPid();
138+
}
139+
131140
return {
132141
fqbn,
133142
requiredTools,
134143
configOptions,
135144
programmers,
136-
debuggingSupported
145+
debuggingSupported,
146+
VID,
147+
PID
137148
};
138149
}
139150

0 commit comments

Comments
 (0)