Skip to content

Commit c2fbccc

Browse files
committed
App doesn't show "open..." anymore if there are no sketches in default sketch folder.
Opens file navigator directly instead. Signed-off-by: jbicker <jan.bicker@typefox.io>
1 parent 6e0a0a1 commit c2fbccc

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

arduino-ide-extension/src/browser/arduino-file-menu.ts

+2-46
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
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'];
@@ -20,55 +17,14 @@ export namespace ArduinoToolbarContextMenu {
2017
@injectable()
2118
export class ArduinoToolbarMenuContribution implements MenuContribution {
2219

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-
3220
constructor(
33-
@inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService,
3421
@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;
6122
}
6223

6324
registerMenus(registry: MenuModelRegistry) {
6425
registry.registerMenuAction([...CommonMenus.FILE, '0_new_sletch'], {
6526
commandId: ArduinoCommands.NEW_SKETCH.id
66-
});
67-
68-
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, {
69-
commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id,
70-
label: 'Open...'
71-
});
27+
})
7228

7329
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_BOARDS_DIALOG_GROUP, {
7430
commandId: ArduinoCommands.OPEN_BOARDS_DIALOG.id,

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

+47-9
Original file line numberDiff line numberDiff line change
@@ -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

@@ -108,6 +105,15 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
108105
protected boardsToolbarItem: BoardsToolBarItem | null;
109106
protected attachedBoards: Board[];
110107
protected selectedBoard: Board;
108+
protected wsSketchCount: number = 0;
109+
110+
constructor(@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService) {
111+
this.workspaceService.onWorkspaceChanged(() => {
112+
if (this.workspaceService.workspace) {
113+
this.registerSketchesInMenu(this.menuRegistry);
114+
}
115+
})
116+
}
111117

112118
@postConstruct()
113119
protected async init(): Promise<void> {
@@ -233,12 +239,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
233239
isVisible: widget => this.isArduinoToolbar(widget),
234240
isEnabled: widget => this.isArduinoToolbar(widget),
235241
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-
});
242+
if (this.wsSketchCount) {
243+
const el = (target as HTMLElement).parentElement;
244+
if (el) {
245+
this.contextMenuRenderer.render(ArduinoToolbarContextMenu.OPEN_SKETCH_PATH, {
246+
x: el.getBoundingClientRect().left,
247+
y: el.getBoundingClientRect().top + el.offsetHeight
248+
});
249+
}
250+
} else {
251+
this.commands.executeCommand(ArduinoCommands.OPEN_FILE_NAVIGATOR.id);
242252
}
243253
}
244254
});
@@ -332,6 +342,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
332342
label: 'Upload',
333343
order: '2'
334344
});
345+
registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, {
346+
commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id,
347+
label: 'Open...'
348+
});
335349

336350
registry.registerSubmenu(ArduinoMenus.TOOLS, 'Tools');
337351
}
@@ -342,6 +356,30 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
342356
return menuId;
343357
}
344358

359+
protected registerSketchesInMenu(registry: MenuModelRegistry) {
360+
this.getWorkspaceSketches().then(sketches => {
361+
this.wsSketchCount = sketches.length;
362+
sketches.forEach(sketch => {
363+
const command: Command = {
364+
id: 'openSketch' + sketch.name
365+
}
366+
this.commands.registerCommand(command, {
367+
execute: () => this.commands.executeCommand(ArduinoCommands.OPEN_SKETCH.id, sketch)
368+
});
369+
370+
registry.registerMenuAction(ArduinoToolbarContextMenu.WS_SKETCHES_GROUP, {
371+
commandId: command.id,
372+
label: sketch.name
373+
});
374+
})
375+
})
376+
}
377+
378+
protected async getWorkspaceSketches(): Promise<Sketch[]> {
379+
const sketches = this.sketches.getSketches(this.workspaceService.workspace);
380+
return sketches;
381+
}
382+
345383
protected async openSketchFilesInNewWindow(uri: string) {
346384
const location = new URL(window.location.href);
347385
location.searchParams.set('sketch', uri);

0 commit comments

Comments
 (0)