Skip to content

Commit

Permalink
Implement 'more' toolbar item for the explorer
Browse files Browse the repository at this point in the history
Fixes #5951

- implemented `More Actions...` toolbar item for the explorer which is
used to host commands specific to the workspace root whenever there is a
single-root workspace currently opened. The initial implementation adds
only a subset of available commands which make sense for the workspace root.
The following commands have been added:
1. new file
2. new folder
3. open in terminal
4. find in folder

Signed-off-by: Vincent Fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Aug 20, 2019
1 parent d059f9e commit e58e858
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 61 deletions.
47 changes: 45 additions & 2 deletions packages/navigator/src/browser/navigator-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
OpenerService, FrontendApplicationContribution, FrontendApplication, CompositeTreeNode
} from '@theia/core/lib/browser';
import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution';
import { CommandRegistry, MenuModelRegistry, MenuPath, isOSX, Command, DisposableCollection } from '@theia/core/lib/common';
import { CommandRegistry, MenuModelRegistry, MenuPath, isOSX, Command, DisposableCollection, Mutable } from '@theia/core/lib/common';
import { SHELL_TABBAR_CONTEXT_MENU } from '@theia/core/lib/browser';
import { WorkspaceCommands, WorkspaceService, WorkspacePreferences } from '@theia/workspace/lib/browser';
import { FILE_NAVIGATOR_ID, FileNavigatorWidget, EXPLORER_VIEW_CONTAINER_ID } from './navigator-widget';
Expand All @@ -30,7 +30,7 @@ import { NavigatorKeybindingContexts } from './navigator-keybinding-context';
import { FileNavigatorFilter } from './navigator-filter';
import { WorkspaceNode } from './navigator-tree';
import { NavigatorContextKeyService } from './navigator-context-key-service';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { FileSystemCommands } from '@theia/filesystem/lib/browser/filesystem-frontend-contribution';
import { NavigatorDiff, NavigatorDiffCommands } from './navigator-diff';
import { UriSelection } from '@theia/core/lib/common/selection';
Expand Down Expand Up @@ -61,6 +61,14 @@ export namespace FileNavigatorCommands {
};
}

/**
* Navigator `More Actions...` toolbar item groups.
*/
export namespace NavigatorMoreToolbarGroups {
export const NEW_OPEN = '1_navigator_new_open';
export const SEARCH = '2_navigator_search';
}

export const NAVIGATOR_CONTEXT_MENU: MenuPath = ['navigator-context-menu'];

/**
Expand Down Expand Up @@ -95,6 +103,9 @@ export namespace NavigatorContextMenu {
@injectable()
export class FileNavigatorContribution extends AbstractViewContribution<FileNavigatorWidget> implements FrontendApplicationContribution, TabBarToolbarContribution {

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(NavigatorContextKeyService)
protected readonly contextKeyService: NavigatorContextKeyService;

Expand Down Expand Up @@ -340,6 +351,38 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
tooltip: 'Collapse All',
priority: 1,
});

// Register 'more actions' toolbar item (consists of multiple commands for the workspace root).
// Commands are available when there is a single-root workspace currently opened.
const navigatorRegisterItem = (item: Mutable<TabBarToolbarItem>) => {
const commandId = item.command;
const id = 'navigator.tabbar.toolbar.' + commandId;
const command = this.commandRegistry.getCommand(commandId);
this.commandRegistry.registerCommand({ id, iconClass: command && command.iconClass }, {
execute: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.executeCommand(commandId, ...args),
isEnabled: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& this.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbarRegistry.registerItem(item);
};
navigatorRegisterItem({
id: WorkspaceCommands.NEW_FILE_ROOT.id,
command: WorkspaceCommands.NEW_FILE_ROOT.id,
tooltip: 'New File',
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
navigatorRegisterItem({
id: WorkspaceCommands.NEW_FOLDER_ROOT.id,
command: WorkspaceCommands.NEW_FOLDER_ROOT.id,
tooltip: 'New Folder',
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
import { AbstractViewContribution, KeybindingRegistry, LabelProvider, CommonMenus, FrontendApplication, FrontendApplicationContribution } from '@theia/core/lib/browser';
import { SearchInWorkspaceWidget } from './search-in-workspace-widget';
import { injectable, inject, postConstruct } from 'inversify';
import { CommandRegistry, MenuModelRegistry, SelectionService, Command } from '@theia/core';
import { CommandRegistry, MenuModelRegistry, SelectionService, Command, Mutable } from '@theia/core';
import { Widget } from '@theia/core/lib/browser/widgets';
import { NavigatorContextMenu } from '@theia/navigator/lib/browser/navigator-contribution';
import { NavigatorContextMenu, NavigatorMoreToolbarGroups } from '@theia/navigator/lib/browser/navigator-contribution';
import { UriCommandHandler, UriAwareCommandHandler } from '@theia/core/lib/common/uri-command-handler';
import URI from '@theia/core/lib/common/uri';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { FileSystem } from '@theia/filesystem/lib/common';
import { SearchInWorkspaceContextKeyService } from './search-in-workspace-context-key-service';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
import { Range } from 'vscode-languageserver-types';
import { FileNavigatorWidget } from '@theia/navigator/lib/browser/navigator-widget';

export namespace SearchInWorkspaceCommands {
const SEARCH_CATEGORY = 'Search';
Expand All @@ -45,6 +46,9 @@ export namespace SearchInWorkspaceCommands {
category: SEARCH_CATEGORY,
label: 'Find in Folder'
};
export const FIND_IN_FOLDER_ROOT: Command = {
id: 'search-in-workspace.in-folder.root',
};
export const REFRESH_RESULTS: Command = {
id: 'search-in-workspace.refresh',
category: SEARCH_CATEGORY,
Expand All @@ -68,6 +72,7 @@ export namespace SearchInWorkspaceCommands {
@injectable()
export class SearchInWorkspaceFrontendContribution extends AbstractViewContribution<SearchInWorkspaceWidget> implements FrontendApplicationContribution, TabBarToolbarContribution {

@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
@inject(SelectionService) protected readonly selectionService: SelectionService;
@inject(LabelProvider) protected readonly labelProvider: LabelProvider;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
Expand Down Expand Up @@ -110,7 +115,13 @@ export class SearchInWorkspaceFrontendContribution extends AbstractViewContribut
widget.updateSearchTerm(this.getSearchTerm());
}
});

commands.registerCommand(SearchInWorkspaceCommands.FIND_IN_FOLDER_ROOT, {
execute: async () => {
const widget = await this.openView({ activate: true });
widget.clear();
widget.updateSearchTerm(this.getSearchTerm());
}
});
commands.registerCommand(SearchInWorkspaceCommands.FIND_IN_FOLDER, this.newMultiUriAwareCommandHandler({
execute: async uris => {
const resources: string[] = [];
Expand Down Expand Up @@ -222,6 +233,32 @@ export class SearchInWorkspaceFrontendContribution extends AbstractViewContribut
priority: 2,
onDidChange
});
// Register 'more actions' toolbar item (consists of multiple commands for the workspace root).
// Commands are available when there is a single-root workspace currently opened.
const navigatorRegisterItem = (item: Mutable<TabBarToolbarItem>) => {
const commandId = item.command;
const id = 'navigator.tabbar.toolbar.' + commandId;
const command = this.commandRegistry.getCommand(commandId);
this.commandRegistry.registerCommand({ id, iconClass: command && command.iconClass }, {
execute: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.executeCommand(commandId, ...args),
isEnabled: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& !!this.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbarRegistry.registerItem(item);
};
navigatorRegisterItem({
id: SearchInWorkspaceCommands.FIND_IN_FOLDER_ROOT.id,
command: SearchInWorkspaceCommands.FIND_IN_FOLDER_ROOT.id,
tooltip: 'Find in Folder',
group: NavigatorMoreToolbarGroups.SEARCH,
});

}

protected newUriAwareCommandHandler(handler: UriCommandHandler<URI>): UriAwareCommandHandler<URI> {
Expand Down
1 change: 1 addition & 0 deletions packages/terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@theia/core": "^0.9.0",
"@theia/editor": "^0.9.0",
"@theia/filesystem": "^0.9.0",
"@theia/navigator": "^0.9.0",
"@theia/process": "^0.9.0",
"@theia/workspace": "^0.9.0",
"xterm": "3.13.0"
Expand Down
81 changes: 64 additions & 17 deletions packages/terminal/src/browser/terminal-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import {
MenuModelRegistry,
isOSX,
SelectionService,
Emitter, Event
Emitter, Event, Mutable
} from '@theia/core/lib/common';
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import {
ApplicationShell, KeybindingContribution, KeyCode, Key,
KeybindingRegistry, Widget, LabelProvider, WidgetOpenerOptions
} from '@theia/core/lib/browser';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { WidgetManager } from '@theia/core/lib/browser';
import { TERMINAL_WIDGET_FACTORY_ID, TerminalWidgetFactoryOptions } from './terminal-widget-impl';
import { TerminalKeybindingContexts } from './terminal-keybinding-contexts';
Expand All @@ -42,14 +42,16 @@ import URI from '@theia/core/lib/common/uri';
import { MAIN_MENU_BAR } from '@theia/core';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { FileNavigatorWidget } from '@theia/navigator/lib/browser/navigator-widget';
import { NavigatorMoreToolbarGroups, NAVIGATOR_CONTEXT_MENU } from '@theia/navigator/lib/browser/navigator-contribution';

export namespace TerminalMenus {
export const TERMINAL = [...MAIN_MENU_BAR, '7_terminal'];
export const TERMINAL_NEW = [...TERMINAL, '1_terminal'];
export const TERMINAL_TASKS = [...TERMINAL, '2_terminal'];
export const TERMINAL_TASKS_INFO = [...TERMINAL_TASKS, '3_terminal'];
export const TERMINAL_TASKS_CONFIG = [...TERMINAL_TASKS, '4_terminal'];
export const TERMINAL_NAVIGATOR_CONTEXT_MENU = ['navigator-context-menu', 'navigation'];
export const TERMINAL_NAVIGATOR_CONTEXT_MENU = [...NAVIGATOR_CONTEXT_MENU, 'navigation'];
}

export namespace TerminalCommands {
Expand All @@ -74,6 +76,9 @@ export namespace TerminalCommands {
category: TERMINAL_CATEGORY,
label: 'Open in Terminal'
};
export const TERMINAL_CONTEXT_ROOT: Command = {
id: 'terminal:context:root',
};
export const SPLIT: Command = {
id: 'terminal:split',
category: TERMINAL_CATEGORY,
Expand All @@ -99,6 +104,9 @@ export class TerminalFrontendContribution implements TerminalService, CommandCon
@inject(SelectionService) protected readonly selectionService: SelectionService
) { }

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;

Expand Down Expand Up @@ -180,22 +188,16 @@ export class TerminalFrontendContribution implements TerminalService, CommandCon
});

commands.registerCommand(TerminalCommands.TERMINAL_CONTEXT, new UriAwareCommandHandler<URI>(this.selectionService, {
execute: async uri => {
// Determine folder path of URI
const stat = await this.fileSystem.getFileStat(uri.toString());
if (!stat) {
return;
execute: async uri => this.openTerminalAtUri(uri)
}));
commands.registerCommand(TerminalCommands.TERMINAL_CONTEXT_ROOT, {
execute: async () => {
if (this.workspaceService.workspace) {
const uri = new URI(this.workspaceService.workspace.uri);
this.openTerminalAtUri(uri);
}

// Use folder if a file was selected
const cwd = (stat.isDirectory) ? uri.toString() : uri.parent.toString();

// Open terminal
const termWidget = await this.newTerminal({ cwd });
termWidget.start();
this.activateTerminal(termWidget);
}
}));
});
}

registerMenus(menus: MenuModelRegistry): void {
Expand All @@ -222,6 +224,31 @@ export class TerminalFrontendContribution implements TerminalService, CommandCon
text: '$(columns)',
tooltip: TerminalCommands.SPLIT.label
});
// Register 'more actions' toolbar item (consists of multiple commands for the workspace root).
// Commands are available when there is a single-root workspace currently opened.
const navigatorRegisterItem = (item: Mutable<TabBarToolbarItem>) => {
const commandId = item.command;
const id = 'navigator.tabbar.toolbar.' + commandId;
const command = this.commandRegistry.getCommand(commandId);
this.commandRegistry.registerCommand({ id, iconClass: command && command.iconClass }, {
execute: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.executeCommand(commandId, ...args),
isEnabled: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& this.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& !!this.workspaceService.workspace && !this.workspaceService.isMultiRootWorkspaceOpened
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbar.registerItem(item);
};
navigatorRegisterItem({
id: TerminalCommands.TERMINAL_CONTEXT_ROOT.id,
command: TerminalCommands.TERMINAL_CONTEXT_ROOT.id,
tooltip: 'Open in Terminal',
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
}

registerKeybindings(keybindings: KeybindingRegistry): void {
Expand Down Expand Up @@ -356,6 +383,26 @@ export class TerminalFrontendContribution implements TerminalService, CommandCon
}
}

/**
* Open a terminal at the provided uri.
* @param uri {URI} the given uri.
*/
protected async openTerminalAtUri(uri: URI): Promise<void> {
// Determine folder path of URI
const stat = await this.fileSystem.getFileStat(uri.toString());
if (!stat) {
return;
}

// Use folder if a file was selected
const cwd = (stat.isDirectory) ? uri.toString() : uri.parent.toString();

// Open terminal
const termWidget = await this.newTerminal({ cwd });
termWidget.start();
this.activateTerminal(termWidget);
}

protected async selectTerminalCwd(): Promise<string | undefined> {
const roots = this.workspaceService.tryGetRoots();
return this.quickPick.show(roots.map(
Expand Down
Loading

0 comments on commit e58e858

Please sign in to comment.