|
| 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 | +} |
0 commit comments