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

- implement the 'more' toolbar item for the explorer which
triggers a context menu which displays helpful commands.
- mainly used to limit the impact of recent developments which
removes the root node from the tree and thus the ability to
perform actions directly on the root.

Signed-off-by: Vincent Fugnitto <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Aug 16, 2019
1 parent e8a29ca commit c171f29
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 7 deletions.
35 changes: 32 additions & 3 deletions packages/git/src/browser/history/git-history-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MenuModelRegistry, CommandRegistry, Command, SelectionService } from '@theia/core';
import { MenuModelRegistry, CommandRegistry, Command, SelectionService, Mutable } from '@theia/core';
import { AbstractViewContribution, OpenViewArguments } from '@theia/core/lib/browser';
import { injectable, inject, postConstruct } from 'inversify';
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 { GitHistoryWidget } from './git-history-widget';
import { Git } from '../../common';
import { GitRepositoryTracker } from '../git-repository-tracker';
import { GitRepositoryProvider } from '../git-repository-provider';
import { EDITOR_CONTEXT_MENU_GIT } from '../git-contribution';
import { TabBarToolbarContribution, TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { FileNavigatorWidget } from '@theia/navigator/lib/browser/navigator-widget';

export const GIT_HISTORY_ID = 'git-history';
export const GIT_HISTORY_LABEL = 'Git History';
Expand All @@ -46,8 +48,10 @@ export interface GitHistoryOpenViewArguments extends OpenViewArguments {
}

@injectable()
export class GitHistoryContribution extends AbstractViewContribution<GitHistoryWidget> {
export class GitHistoryContribution extends AbstractViewContribution<GitHistoryWidget> implements TabBarToolbarContribution {

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;
@inject(SelectionService)
protected readonly selectionService: SelectionService;
@inject(GitRepositoryTracker)
Expand Down Expand Up @@ -115,6 +119,31 @@ export class GitHistoryContribution extends AbstractViewContribution<GitHistoryW
super.registerCommands(commands);
}

async registerToolbarItems(toolbarRegistry: TabBarToolbarRegistry): Promise<void> {
// Register to the navigator's `more` toolbar item.
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.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbarRegistry.registerItem(item);
};
navigatorRegisterItem({
id: GitHistoryCommands.OPEN_BRANCH_HISTORY.id,
command: GitHistoryCommands.OPEN_BRANCH_HISTORY.id,
tooltip: GitHistoryCommands.OPEN_BRANCH_HISTORY.label,
group: NavigatorMoreToolbarGroups.SEARCH,
});
}

protected async refreshWidget(uri: string | undefined): Promise<void> {
const widget = this.tryGetWidget();
if (!widget) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { GIT_COMMIT_DETAIL, GitCommitDetailWidget, GitCommitDetails, GitCommitDe
import '../../../src/browser/style/history.css';
import '../../../src/browser/style/git-icons.css';
import { GitCommitDetailOpenHandler } from './git-commit-detail-open-handler';
import { TabBarToolbarContribution } from '@theia/core/lib/browser/shell/tab-bar-toolbar';

export function bindGitHistoryModule(bind: interfaces.Bind): void {

Expand All @@ -48,4 +49,5 @@ export function bindGitHistoryModule(bind: interfaces.Bind): void {

bindViewContribution(bind, GitHistoryContribution);

bind(TabBarToolbarContribution).toService(GitHistoryContribution);
}
72 changes: 70 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,18 @@ export namespace FileNavigatorCommands {
};
}

/**
* Navigator `More Actions...` toolbar item groups.
*/
export namespace NavigatorMoreToolbarGroups {
export const NEW_OPEN = '1_new_open';
export const WORKSPACE = '2_workspace';
export const COMPARE = '3_compare';
export const SEARCH = '4_search';
export const EDIT_ACTIONS = '5_edit_actions';
export const UPLOAD_DOWNLOAD = '6_upload_download';
}

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

/**
Expand Down Expand Up @@ -95,6 +107,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 +355,59 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
tooltip: 'Collapse All',
priority: 1,
});

// Register 'more' toolbar item (consists of multiple commands)
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.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbarRegistry.registerItem(item);
};
navigatorRegisterItem({
id: WorkspaceCommands.NEW_FILE.id,
command: WorkspaceCommands.NEW_FILE.id,
tooltip: WorkspaceCommands.NEW_FILE.label,
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
navigatorRegisterItem({
id: WorkspaceCommands.NEW_FOLDER.id,
command: WorkspaceCommands.NEW_FOLDER.id,
tooltip: WorkspaceCommands.NEW_FOLDER.label,
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
navigatorRegisterItem({
id: WorkspaceCommands.ADD_FOLDER.id,
command: WorkspaceCommands.ADD_FOLDER.id,
tooltip: WorkspaceCommands.ADD_FOLDER.label,
group: NavigatorMoreToolbarGroups.WORKSPACE,
});
navigatorRegisterItem({
id: FileSystemCommands.UPLOAD.id,
command: FileSystemCommands.UPLOAD.id,
tooltip: FileSystemCommands.UPLOAD.label,
group: NavigatorMoreToolbarGroups.UPLOAD_DOWNLOAD,
});
navigatorRegisterItem({
id: FileDownloadCommands.DOWNLOAD.id,
command: FileDownloadCommands.DOWNLOAD.id,
tooltip: FileDownloadCommands.DOWNLOAD.label,
group: NavigatorMoreToolbarGroups.UPLOAD_DOWNLOAD,
});
navigatorRegisterItem({
id: FileDownloadCommands.COPY_DOWNLOAD_LINK.id,
command: FileDownloadCommands.COPY_DOWNLOAD_LINK.id,
tooltip: FileDownloadCommands.COPY_DOWNLOAD_LINK.label,
group: NavigatorMoreToolbarGroups.UPLOAD_DOWNLOAD,
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import { SearchInWorkspaceWidget } from './search-in-workspace-widget';
import { injectable, inject, postConstruct } from 'inversify';
import { CommandRegistry, MenuModelRegistry, SelectionService, Command } 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 { Mutable } from '@theia/core/lib/common/types';
import { FileNavigatorWidget } from '@theia/navigator/lib/browser/navigator-widget';

export namespace SearchInWorkspaceCommands {
const SEARCH_CATEGORY = 'Search';
Expand Down Expand Up @@ -68,6 +70,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 @@ -222,6 +225,29 @@ export class SearchInWorkspaceFrontendContribution extends AbstractViewContribut
priority: 2,
onDidChange
});

// Register to the navigator's `more` toolbar item.
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.commandRegistry.isEnabled(commandId, ...args),
isVisible: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.isVisible(commandId, ...args),
});
item.command = id;
toolbarRegistry.registerItem(item);
};
navigatorRegisterItem({
id: SearchInWorkspaceCommands.FIND_IN_FOLDER.id,
command: SearchInWorkspaceCommands.FIND_IN_FOLDER.id,
tooltip: SearchInWorkspaceCommands.FIND_IN_FOLDER.label,
group: NavigatorMoreToolbarGroups.SEARCH,
});
}

protected newUriAwareCommandHandler(handler: UriCommandHandler<URI>): UriAwareCommandHandler<URI> {
Expand Down
8 changes: 8 additions & 0 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<
super(selectionService, handler);
}

isEnabled(): boolean {
return true;
}

isVisible(): boolean {
return true;
}

protected getUri(): URI | undefined {
const uri = super.getUri();
if (this.workspaceService.isMultiRootWorkspaceEnabled) {
Expand Down

0 comments on commit c171f29

Please sign in to comment.