-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command for opening the containing folder of a file
A new 'OPEN_CONTAINING_FOLDER' command was added for the electron application. It opens the native file explorer at the path where the selected resource (or resources) from the file navigator is located. Signed-off-by: Alexandra Buzila <[email protected]>
- Loading branch information
1 parent
e0359d4
commit 26a5017
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/navigator/src/electron-browser/electron-navigator-menu-contribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core'; | ||
import { CommonCommands, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser'; | ||
import { WidgetManager } from '@theia/core/lib/browser/widget-manager'; | ||
import * as electron from '@theia/core/shared/electron'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { FileStatNode } from '@theia/filesystem/lib/browser'; | ||
import { FileNavigatorWidget, FILE_NAVIGATOR_ID } from '../browser'; | ||
import { NavigatorContextMenu } from '../browser/navigator-contribution'; | ||
import { isWindows, isOSX } from '@theia/core/lib/common/os'; | ||
|
||
export const OPEN_CONTAINING_FOLDER = Command.toDefaultLocalizedCommand({ | ||
id: 'revealFileInOS', | ||
category: CommonCommands.FILE_CATEGORY, | ||
label: isWindows ? 'Reveal in File Explorer' : | ||
isOSX ? 'Reveal in Finder' : | ||
/* linux */ 'Open Containing Folder' | ||
}); | ||
|
||
@injectable() | ||
export class ElectronNavigatorMenuContribution implements MenuContribution, CommandContribution, KeybindingContribution { | ||
|
||
@inject(WidgetManager) | ||
protected readonly widgetManager: WidgetManager; | ||
|
||
registerCommands(commands: CommandRegistry): void { | ||
commands.registerCommand(OPEN_CONTAINING_FOLDER, { | ||
isEnabled: () => this.getSelectedFileStatNodes().length > 0, | ||
isVisible: () => this.getSelectedFileStatNodes().length > 0, | ||
execute: () => { | ||
this.getSelectedFileStatNodes().forEach(node => { | ||
electron.shell.showItemInFolder(node.uri['codeUri'].fsPath); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
registerMenus(menus: MenuModelRegistry): void { | ||
menus.registerMenuAction(NavigatorContextMenu.NAVIGATION, { | ||
commandId: OPEN_CONTAINING_FOLDER.id, | ||
label: OPEN_CONTAINING_FOLDER.label | ||
}); | ||
} | ||
|
||
registerKeybindings(keybindings: KeybindingRegistry): void { | ||
keybindings.registerKeybinding({ | ||
command: OPEN_CONTAINING_FOLDER.id, | ||
keybinding: 'ctrlcmd+alt+p', | ||
when: 'filesExplorerFocus' | ||
}); | ||
} | ||
|
||
protected getSelectedFileStatNodes(): FileStatNode[] { | ||
const navigator = this.tryGetNavigatorWidget(); | ||
return navigator ? navigator.model.selectedNodes.filter(FileStatNode.is) : []; | ||
} | ||
|
||
tryGetNavigatorWidget(): FileNavigatorWidget | undefined { | ||
return this.widgetManager.tryGetWidget(FILE_NAVIGATOR_ID); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
packages/navigator/src/electron-browser/electron-navigator-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { CommandContribution, MenuContribution } from '@theia/core'; | ||
import { ContainerModule } from '@theia/core/shared/inversify'; | ||
import { KeybindingContribution } from '@theia/core/lib/browser'; | ||
import { ElectronNavigatorMenuContribution } from './electron-navigator-menu-contribution'; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(MenuContribution).to(ElectronNavigatorMenuContribution).inSingletonScope(); | ||
bind(CommandContribution).to(ElectronNavigatorMenuContribution).inSingletonScope(); | ||
bind(KeybindingContribution).to(ElectronNavigatorMenuContribution).inSingletonScope(); | ||
}); |