|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (C) 2021 EclipseSource and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | + * with the GNU Classpath Exception which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + ********************************************************************************/ |
| 16 | + |
| 17 | +import { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core'; |
| 18 | +import { CommonCommands } from '@theia/core/lib/browser'; |
| 19 | +import { WidgetManager } from '@theia/core/lib/browser/widget-manager'; |
| 20 | +import * as electron from '@theia/core/shared/electron'; |
| 21 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 22 | +import { FileStatNode } from '@theia/filesystem/lib/browser'; |
| 23 | +import { FileNavigatorWidget, FILE_NAVIGATOR_ID } from '../browser'; |
| 24 | +import { NavigatorContextMenu } from '../browser/navigator-contribution'; |
| 25 | + |
| 26 | +export const OPEN_CONTAINING_FOLDER = Command.toDefaultLocalizedCommand({ |
| 27 | + id: 'navigator.openContainingFolder', |
| 28 | + category: CommonCommands.FILE_CATEGORY, |
| 29 | + label: 'Open Containing Folder' |
| 30 | +}); |
| 31 | + |
| 32 | +@injectable() |
| 33 | +export class ElectronNavigatorMenuContribution implements MenuContribution, CommandContribution { |
| 34 | + @inject(WidgetManager) |
| 35 | + protected readonly widgetManager: WidgetManager; |
| 36 | + |
| 37 | + registerCommands(commands: CommandRegistry): void { |
| 38 | + commands.registerCommand(OPEN_CONTAINING_FOLDER, { |
| 39 | + isEnabled: () => this.getSelectedFileStatNodes().length > 0, |
| 40 | + isVisible: () => this.getSelectedFileStatNodes().length > 0, |
| 41 | + execute: () => { |
| 42 | + this.getSelectedFileStatNodes().forEach(async node => { |
| 43 | + electron.shell.showItemInFolder(node.uri.path.toString()); |
| 44 | + }); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + registerMenus(menus: MenuModelRegistry): void { |
| 49 | + menus.registerMenuAction(NavigatorContextMenu.NAVIGATION, { |
| 50 | + commandId: OPEN_CONTAINING_FOLDER.id, |
| 51 | + label: OPEN_CONTAINING_FOLDER.label |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + protected getSelectedFileStatNodes(): FileStatNode[] { |
| 56 | + return this.tryGetNavigatorWidget()?.model.selectedNodes.filter(FileStatNode.is) || []; |
| 57 | + } |
| 58 | + |
| 59 | + tryGetNavigatorWidget(): FileNavigatorWidget | undefined { |
| 60 | + return this.widgetManager.tryGetWidget(FILE_NAVIGATOR_ID); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments