diff --git a/arduino-ide-extension/src/browser/arduino-file-menu.ts b/arduino-ide-extension/src/browser/arduino-file-menu.ts index 2a0d1663..74a6c292 100644 --- a/arduino-ide-extension/src/browser/arduino-file-menu.ts +++ b/arduino-ide-extension/src/browser/arduino-file-menu.ts @@ -1,10 +1,7 @@ import { injectable, inject } from "inversify"; -import { MenuContribution, MenuModelRegistry, MenuPath, CommandRegistry, Command } from "@theia/core"; +import { MenuContribution, MenuModelRegistry, MenuPath } from "@theia/core"; import { CommonMenus } from "@theia/core/lib/browser"; import { ArduinoCommands } from "./arduino-commands"; -import { SketchesService, Sketch } from "../common/protocol/sketches-service"; -import { AWorkspaceService } from "./arduino-workspace-service"; -import { BoardsService } from "../common/protocol/boards-service"; export namespace ArduinoToolbarContextMenu { export const OPEN_SKETCH_PATH: MenuPath = ['arduino-open-sketch-context-menu']; @@ -20,55 +17,14 @@ export namespace ArduinoToolbarContextMenu { @injectable() export class ArduinoToolbarMenuContribution implements MenuContribution { - @inject(CommandRegistry) - protected readonly commands: CommandRegistry; - - @inject(SketchesService) - protected readonly sketches: SketchesService; - - @inject(BoardsService) - protected readonly boardsService: BoardsService; - constructor( - @inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService, @inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry) { - workspaceService.onWorkspaceChanged(() => { - if (this.workspaceService.workspace) { - this.registerSketchesInMenu(menuRegistry); - } - }) - } - - protected async registerSketchesInMenu(registry: MenuModelRegistry) { - const sketches = await this.getWorkspaceSketches(); - sketches.forEach(sketch => { - const command: Command = { - id: 'openSketch' + sketch.name - } - this.commands.registerCommand(command, { - execute: () => this.commands.executeCommand(ArduinoCommands.OPEN_SKETCH.id, sketch) - }); - registry.registerMenuAction(ArduinoToolbarContextMenu.WS_SKETCHES_GROUP, { - commandId: command.id, - label: sketch.name - }); - }) - } - - protected async getWorkspaceSketches(): Promise { - const sketches = this.sketches.getSketches(this.workspaceService.workspace); - return sketches; } registerMenus(registry: MenuModelRegistry) { registry.registerMenuAction([...CommonMenus.FILE, '0_new_sletch'], { commandId: ArduinoCommands.NEW_SKETCH.id - }); - - registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, { - commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id, - label: 'Open...' - }); + }) registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_BOARDS_DIALOG_GROUP, { commandId: ArduinoCommands.OPEN_BOARDS_DIALOG.id, diff --git a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx index 51f29225..e7d4efb6 100644 --- a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx +++ b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx @@ -66,9 +66,6 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C @inject(BoardsNotificationService) protected readonly boardsNotificationService: BoardsNotificationService; - @inject(WorkspaceService) - protected readonly workspaceService: WorkspaceService; - @inject(SelectionService) protected readonly selectionService: SelectionService; @@ -108,6 +105,15 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C protected boardsToolbarItem: BoardsToolBarItem | null; protected attachedBoards: Board[]; protected selectedBoard: Board; + protected wsSketchCount: number = 0; + + constructor(@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService) { + this.workspaceService.onWorkspaceChanged(() => { + if (this.workspaceService.workspace) { + this.registerSketchesInMenu(this.menuRegistry); + } + }) + } @postConstruct() protected async init(): Promise { @@ -233,12 +239,16 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C isVisible: widget => this.isArduinoToolbar(widget), isEnabled: widget => this.isArduinoToolbar(widget), execute: async (widget: Widget, target: EventTarget) => { - const el = (target as HTMLElement).parentElement; - if (el) { - this.contextMenuRenderer.render(ArduinoToolbarContextMenu.OPEN_SKETCH_PATH, { - x: el.getBoundingClientRect().left, - y: el.getBoundingClientRect().top + el.offsetHeight - }); + if (this.wsSketchCount) { + const el = (target as HTMLElement).parentElement; + if (el) { + this.contextMenuRenderer.render(ArduinoToolbarContextMenu.OPEN_SKETCH_PATH, { + x: el.getBoundingClientRect().left, + y: el.getBoundingClientRect().top + el.offsetHeight + }); + } + } else { + this.commands.executeCommand(ArduinoCommands.OPEN_FILE_NAVIGATOR.id); } } }); @@ -332,6 +342,10 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C label: 'Upload', order: '2' }); + registry.registerMenuAction(ArduinoToolbarContextMenu.OPEN_GROUP, { + commandId: ArduinoCommands.OPEN_FILE_NAVIGATOR.id, + label: 'Open...' + }); registry.registerSubmenu(ArduinoMenus.TOOLS, 'Tools'); } @@ -342,6 +356,30 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C return menuId; } + protected registerSketchesInMenu(registry: MenuModelRegistry) { + this.getWorkspaceSketches().then(sketches => { + this.wsSketchCount = sketches.length; + sketches.forEach(sketch => { + const command: Command = { + id: 'openSketch' + sketch.name + } + this.commands.registerCommand(command, { + execute: () => this.commands.executeCommand(ArduinoCommands.OPEN_SKETCH.id, sketch) + }); + + registry.registerMenuAction(ArduinoToolbarContextMenu.WS_SKETCHES_GROUP, { + commandId: command.id, + label: sketch.name + }); + }) + }) + } + + protected async getWorkspaceSketches(): Promise { + const sketches = this.sketches.getSketches(this.workspaceService.workspace); + return sketches; + } + protected async openSketchFilesInNewWindow(uri: string) { const location = new URL(window.location.href); location.searchParams.set('sketch', uri);