Skip to content

Commit 269f08b

Browse files
authored
Merge pull request #37 from bcmi-labs/board-selector-toolbar-item
Board selector toolbar item
2 parents 6e0a0a1 + 4d2bd87 commit 269f08b

File tree

5 files changed

+276
-147
lines changed

5 files changed

+276
-147
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,25 @@
11
import { injectable, inject } from "inversify";
2-
import { MenuContribution, MenuModelRegistry, MenuPath, CommandRegistry, Command } from "@theia/core";
2+
import { MenuContribution, MenuModelRegistry, MenuPath } from "@theia/core";
33
import { CommonMenus } from "@theia/core/lib/browser";
44
import { ArduinoCommands } from "./arduino-commands";
5-
import { SketchesService, Sketch } from "../common/protocol/sketches-service";
6-
import { AWorkspaceService } from "./arduino-workspace-service";
7-
import { BoardsService } from "../common/protocol/boards-service";
85

96
export namespace ArduinoToolbarContextMenu {
107
export const OPEN_SKETCH_PATH: MenuPath = ['arduino-open-sketch-context-menu'];
118
export const OPEN_GROUP: MenuPath = [...OPEN_SKETCH_PATH, '1_open'];
129
export const WS_SKETCHES_GROUP: MenuPath = [...OPEN_SKETCH_PATH, '2_sketches'];
1310
export const EXAMPLE_SKETCHES_GROUP: MenuPath = [...OPEN_SKETCH_PATH, '3_examples'];
14-
15-
export const SELECT_BOARDS_PATH: MenuPath = ['arduino-select-boards-context-menu'];
16-
export const CONNECTED_GROUP: MenuPath = [...SELECT_BOARDS_PATH, '1_connected'];
17-
export const OPEN_BOARDS_DIALOG_GROUP: MenuPath = [...SELECT_BOARDS_PATH, '2_open_boards_dialog'];
1811
}
1912

2013
@injectable()
2114
export class ArduinoToolbarMenuContribution implements MenuContribution {
2215

23-
@inject(CommandRegistry)
24-
protected readonly commands: CommandRegistry;
25-
26-
@inject(SketchesService)
27-
protected readonly sketches: SketchesService;
28-
29-
@inject(BoardsService)
30-
protected readonly boardsService: BoardsService;
31-
3216
constructor(
33-
@inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService,
3417
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry) {
35-
workspaceService.onWorkspaceChanged(() => {
36-
if (this.workspaceService.workspace) {
37-
this.registerSketchesInMenu(menuRegistry);
38-
}
39-
})
40-
}
41-
42-
protected async registerSketchesInMenu(registry: MenuModelRegistry) {
43-
const sketches = await this.getWorkspaceSketches();
44-
sketches.forEach(sketch => {
45-
const command: Command = {
46-
id: 'openSketch' + sketch.name
47-
}
48-
this.commands.registerCommand(command, {
49-
execute: () => this.commands.executeCommand(ArduinoCommands.OPEN_SKETCH.id, sketch)
50-
});
51-
registry.registerMenuAction(ArduinoToolbarContextMenu.WS_SKETCHES_GROUP, {
52-
commandId: command.id,
53-
label: sketch.name
54-
});
55-
})
56-
}
57-
58-
protected async getWorkspaceSketches(): Promise<Sketch[]> {
59-
const sketches = this.sketches.getSketches(this.workspaceService.workspace);
60-
return sketches;
6118
}
6219

6320
registerMenus(registry: MenuModelRegistry) {
6421
registry.registerMenuAction([...CommonMenus.FILE, '0_new_sletch'], {
6522
commandId: ArduinoCommands.NEW_SKETCH.id
66-
});
67-
68-
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, {
69-
commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id,
70-
label: 'Open...'
71-
});
72-
73-
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_BOARDS_DIALOG_GROUP, {
74-
commandId: ArduinoCommands.OPEN_BOARDS_DIALOG.id,
75-
label: 'Select Other Board & Port'
76-
});
23+
})
7724
}
7825
}

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+51-49
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
55
import { MessageService } from '@theia/core/lib/common/message-service';
66
import { CommandContribution, CommandRegistry, Command } from '@theia/core/lib/common/command';
77
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
8-
import { BoardsService, Board, AttachedSerialBoard } from '../common/protocol/boards-service';
8+
import { BoardsService, Board } from '../common/protocol/boards-service';
99
import { ArduinoCommands } from './arduino-commands';
1010
import { ConnectedBoards } from './components/connected-boards';
1111
import { CoreService } from '../common/protocol/core-service';
@@ -66,9 +66,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
6666
@inject(BoardsNotificationService)
6767
protected readonly boardsNotificationService: BoardsNotificationService;
6868

69-
@inject(WorkspaceService)
70-
protected readonly workspaceService: WorkspaceService;
71-
7269
@inject(SelectionService)
7370
protected readonly selectionService: SelectionService;
7471

@@ -106,48 +103,20 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
106103
protected readonly commands: CommandRegistry;
107104

108105
protected boardsToolbarItem: BoardsToolBarItem | null;
109-
protected attachedBoards: Board[];
110-
protected selectedBoard: Board;
106+
protected wsSketchCount: number = 0;
107+
108+
constructor(@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService) {
109+
this.workspaceService.onWorkspaceChanged(() => {
110+
if (this.workspaceService.workspace) {
111+
this.registerSketchesInMenu(this.menuRegistry);
112+
}
113+
})
114+
}
111115

112116
@postConstruct()
113117
protected async init(): Promise<void> {
114118
// This is a hack. Otherwise, the backend services won't bind.
115119
await this.workspaceServiceExt.roots();
116-
const { boards } = await this.boardService.getAttachedBoards();
117-
this.attachedBoards = boards;
118-
this.registerConnectedBoardsInMenu(this.menuRegistry);
119-
}
120-
121-
protected async registerConnectedBoardsInMenu(registry: MenuModelRegistry) {
122-
this.attachedBoards.forEach(board => {
123-
const port = this.getPort(board);
124-
const command: Command = {
125-
id: 'selectBoard' + port
126-
}
127-
this.commands.registerCommand(command, {
128-
execute: () => this.commands.executeCommand(ArduinoCommands.SELECT_BOARD.id, board),
129-
isToggled: () => this.isSelectedBoard(board)
130-
});
131-
registry.registerMenuAction(ArduinoToolbarContextMenu.CONNECTED_GROUP, {
132-
commandId: command.id,
133-
label: board.name + ' at ' + port
134-
});
135-
});
136-
}
137-
138-
protected isSelectedBoard(board: Board): boolean {
139-
return AttachedSerialBoard.is(board) &&
140-
this.selectedBoard &&
141-
AttachedSerialBoard.is(this.selectedBoard) &&
142-
board.port === this.selectedBoard.port &&
143-
board.fqbn === this.selectedBoard.fqbn;
144-
}
145-
146-
protected getPort(board: Board): string {
147-
if (AttachedSerialBoard.is(board)) {
148-
return board.port;
149-
}
150-
return '';
151120
}
152121

153122
registerToolbarItems(registry: TabBarToolbarRegistry): void {
@@ -178,7 +147,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
178147
registry.registerItem({
179148
id: ConnectedBoards.TOOLBAR_ID,
180149
render: () => <BoardsToolBarItem
150+
key='boardsToolbarItem'
181151
ref={ref => this.boardsToolbarItem = ref}
152+
commands={this.commands}
182153
contextMenuRenderer={this.contextMenuRenderer}
183154
boardsNotificationService={this.boardsNotificationService}
184155
boardService={this.boardService} />,
@@ -233,12 +204,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
233204
isVisible: widget => this.isArduinoToolbar(widget),
234205
isEnabled: widget => this.isArduinoToolbar(widget),
235206
execute: async (widget: Widget, target: EventTarget) => {
236-
const el = (target as HTMLElement).parentElement;
237-
if (el) {
238-
this.contextMenuRenderer.render(ArduinoToolbarContextMenu.OPEN_SKETCH_PATH, {
239-
x: el.getBoundingClientRect().left,
240-
y: el.getBoundingClientRect().top + el.offsetHeight
241-
});
207+
if (this.wsSketchCount) {
208+
const el = (target as HTMLElement).parentElement;
209+
if (el) {
210+
this.contextMenuRenderer.render(ArduinoToolbarContextMenu.OPEN_SKETCH_PATH, {
211+
x: el.getBoundingClientRect().left,
212+
y: el.getBoundingClientRect().top + el.offsetHeight
213+
});
214+
}
215+
} else {
216+
this.commands.executeCommand(ArduinoCommands.OPEN_FILE_NAVIGATOR.id);
242217
}
243218
}
244219
});
@@ -295,11 +270,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
295270
}
296271

297272
protected async selectBoard(board: Board) {
298-
await this.boardService.selectBoard(board)
273+
await this.boardService.selectBoard(board);
299274
if (this.boardsToolbarItem) {
300275
this.boardsToolbarItem.setSelectedBoard(board);
301276
}
302-
this.selectedBoard = board;
303277
}
304278

305279
registerMenus(registry: MenuModelRegistry) {
@@ -332,6 +306,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
332306
label: 'Upload',
333307
order: '2'
334308
});
309+
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, {
310+
commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id,
311+
label: 'Open...'
312+
});
335313

336314
registry.registerSubmenu(ArduinoMenus.TOOLS, 'Tools');
337315
}
@@ -342,6 +320,30 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
342320
return menuId;
343321
}
344322

323+
protected registerSketchesInMenu(registry: MenuModelRegistry) {
324+
this.getWorkspaceSketches().then(sketches => {
325+
this.wsSketchCount = sketches.length;
326+
sketches.forEach(sketch => {
327+
const command: Command = {
328+
id: 'openSketch' + sketch.name
329+
}
330+
this.commands.registerCommand(command, {
331+
execute: () => this.commands.executeCommand(ArduinoCommands.OPEN_SKETCH.id, sketch)
332+
});
333+
334+
registry.registerMenuAction(ArduinoToolbarContextMenu.WS_SKETCHES_GROUP, {
335+
commandId: command.id,
336+
label: sketch.name
337+
});
338+
})
339+
})
340+
}
341+
342+
protected async getWorkspaceSketches(): Promise<Sketch[]> {
343+
const sketches = this.sketches.getSketches(this.workspaceService.workspace);
344+
return sketches;
345+
}
346+
345347
protected async openSketchFilesInNewWindow(uri: string) {
346348
const location = new URL(window.location.href);
347349
location.searchParams.set('sketch', uri);

0 commit comments

Comments
 (0)