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
Fixes #6010

- Added a new toolbar item for the `explorer` which consists of multiple commands.
- Adjusted the `WorkspaceRootUriAwareCommandHandler` to better handle commands which
can be used by the workspace root in both a single and multi-root workspace.
- Added the command `New File` to the toolbar item.
- Added the command `New Folder` to the toolbar item.
- Added the command `Add Folder to Workspace...` to the toolbar item.
- Added the command `Compare With...` to the toolbar item.

Signed-off-by: Vincent Fugnitto <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Aug 23, 2019
1 parent 566ebda commit 24585fe
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 13 deletions.
34 changes: 27 additions & 7 deletions packages/git/src/browser/diff/git-diff-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ import { WidgetManager } from '@theia/core/lib/browser/widget-manager';
import { injectable, inject } from 'inversify';
import { GitDiffWidget, GIT_DIFF } from './git-diff-widget';
import { open, OpenerService } from '@theia/core/lib/browser';
import { NavigatorContextMenu } from '@theia/navigator/lib/browser/navigator-contribution';
import { UriCommandHandler, UriAwareCommandHandler } from '@theia/core/lib/common/uri-command-handler';
import { NavigatorContextMenu, NavigatorMoreToolbarGroups, FileNavigatorContribution } from '@theia/navigator/lib/browser/navigator-contribution';
import { UriCommandHandler } from '@theia/core/lib/common/uri-command-handler';
import { GitQuickOpenService } from '../git-quick-open-service';
import { FileSystem } from '@theia/filesystem/lib/common';
import { DiffUris } from '@theia/core/lib/browser/diff-uris';
import URI from '@theia/core/lib/common/uri';
import { GIT_RESOURCE_SCHEME } from '../git-resource';
import { Git } from '../../common';
import { GitRepositoryProvider } from '../git-repository-provider';
import { WorkspaceRootUriAwareCommandHandler } from '@theia/workspace/lib/browser/workspace-commands';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';

export namespace GitDiffCommands {
export const OPEN_FILE_DIFF: Command = {
Expand All @@ -39,7 +42,16 @@ export namespace GitDiffCommands {
}

@injectable()
export class GitDiffContribution extends AbstractViewContribution<GitDiffWidget> {
export class GitDiffContribution extends AbstractViewContribution<GitDiffWidget> implements TabBarToolbarContribution {

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(FileNavigatorContribution)
protected readonly fileNavigatorContribution: FileNavigatorContribution;

@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;

constructor(
@inject(SelectionService) protected readonly selectionService: SelectionService,
Expand Down Expand Up @@ -68,7 +80,7 @@ export class GitDiffContribution extends AbstractViewContribution<GitDiffWidget>
}

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(GitDiffCommands.OPEN_FILE_DIFF, this.newUriAwareCommandHandler({
commands.registerCommand(GitDiffCommands.OPEN_FILE_DIFF, this.newWorkspaceRootUriAwareCommandHandler({
isVisible: uri => !!this.repositoryProvider.findRepository(uri),
isEnabled: uri => !!this.repositoryProvider.findRepository(uri),
execute: async fileUri => {
Expand Down Expand Up @@ -101,6 +113,15 @@ export class GitDiffContribution extends AbstractViewContribution<GitDiffWidget>
}));
}

registerToolbarItems(registry: TabBarToolbarRegistry): void {
this.fileNavigatorContribution.registerMoreToolbarItem({
id: GitDiffCommands.OPEN_FILE_DIFF.id,
command: GitDiffCommands.OPEN_FILE_DIFF.id,
tooltip: GitDiffCommands.OPEN_FILE_DIFF.label,
group: NavigatorMoreToolbarGroups.SCM,
});
}

async showWidget(options: Git.Options.Diff): Promise<GitDiffWidget> {
const widget = await this.widget;
await widget.setContent(options);
Expand All @@ -109,8 +130,7 @@ export class GitDiffContribution extends AbstractViewContribution<GitDiffWidget>
});
}

protected newUriAwareCommandHandler(handler: UriCommandHandler<URI>): UriAwareCommandHandler<URI> {
return new UriAwareCommandHandler(this.selectionService, handler);
protected newWorkspaceRootUriAwareCommandHandler(handler: UriCommandHandler<URI>): WorkspaceRootUriAwareCommandHandler {
return new WorkspaceRootUriAwareCommandHandler(this.workspaceService, this.selectionService, handler);
}

}
2 changes: 2 additions & 0 deletions packages/git/src/browser/diff/git-diff-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { interfaces } from 'inversify';
import { GitDiffContribution } from './git-diff-contribution';
import { WidgetFactory, bindViewContribution } from '@theia/core/lib/browser';
import { GitDiffWidget, GIT_DIFF } from './git-diff-widget';
import { TabBarToolbarContribution } from '@theia/core/lib/browser/shell/tab-bar-toolbar';

import '../../../src/browser/style/diff.css';

Expand All @@ -30,5 +31,6 @@ export function bindGitDiffModule(bind: interfaces.Bind): void {
}));

bindViewContribution(bind, GitDiffContribution);
bind(TabBarToolbarContribution).toService(GitDiffContribution);

}
59 changes: 57 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,16 @@ export namespace FileNavigatorCommands {
};
}

/**
* Navigator `More Actions...` toolbar item groups.
* Used in order to group items present in the toolbar.
*/
export namespace NavigatorMoreToolbarGroups {
export const NEW_OPEN = '1_navigator_new_open';
export const WORKSPACE = '2_navigator_workspace';
export const SCM = '3_navigator_scm';
}

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

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

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(TabBarToolbarRegistry)
protected readonly tabbarToolbarRegistry: TabBarToolbarRegistry;

@inject(NavigatorContextKeyService)
protected readonly contextKeyService: NavigatorContextKeyService;

Expand Down Expand Up @@ -340,6 +356,45 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
tooltip: 'Collapse All',
priority: 1,
});
this.registerMoreToolbarItem({
id: WorkspaceCommands.NEW_FILE.id,
command: WorkspaceCommands.NEW_FILE.id,
tooltip: WorkspaceCommands.NEW_FILE.label,
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
this.registerMoreToolbarItem({
id: WorkspaceCommands.NEW_FOLDER.id,
command: WorkspaceCommands.NEW_FOLDER.id,
tooltip: WorkspaceCommands.NEW_FOLDER.label,
group: NavigatorMoreToolbarGroups.NEW_OPEN,
});
this.registerMoreToolbarItem({
id: WorkspaceCommands.ADD_FOLDER.id,
command: WorkspaceCommands.ADD_FOLDER.id,
tooltip: WorkspaceCommands.ADD_FOLDER.label,
group: NavigatorMoreToolbarGroups.WORKSPACE,
});
}

/**
* Register commands to the `More Actions...` navigator toolbar item.
*/
public registerMoreToolbarItem = (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),
isToggled: (w, ...args) => w instanceof FileNavigatorWidget
&& this.commandRegistry.isToggled(commandId, ...args),
});
item.command = id;
this.tabbarToolbarRegistry.registerItem(item);
}

/**
Expand Down
22 changes: 18 additions & 4 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,29 @@ export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<
super(selectionService, handler);
}

isVisible(): boolean {
return super.isVisible();
}

isEnabled(): boolean {
return super.isEnabled();
}

protected getUri(): URI | undefined {
const uri = super.getUri();
if (this.workspaceService.isMultiRootWorkspaceEnabled) {
return uri;
}
// If the URI is available, return it immediately.
if (uri) {
return uri;
}
const root = this.workspaceService.tryGetRoots()[0];
// If a multi-root workspace is currently opened with at least one root,
// return the URI of the first available root.
if (this.workspaceService.isMultiRootWorkspaceOpened &&
!!this.workspaceService.tryGetRoots().length) {
const firstRoot = this.workspaceService.tryGetRoots()[0];
return new URI(firstRoot.uri);
}
// Return the workspace root URI if available.
const root = this.workspaceService.workspace;
return root && new URI(root.uri);
}

Expand Down

0 comments on commit 24585fe

Please sign in to comment.