Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[explorer] implement 'more' toolbar item for the explorer #5953

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 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, 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 @@ -38,8 +41,21 @@ export namespace GitDiffCommands {
};
}

export namespace ScmNavigatorMoreToolbarGroups {
export const SCM = '3_navigator_scm';
}

@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 +84,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 +117,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: ScmNavigatorMoreToolbarGroups.SCM,
});
}

async showWidget(options: Git.Options.Diff): Promise<GitDiffWidget> {
const widget = await this.widget;
await widget.setContent(options);
Expand All @@ -109,8 +134,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);
akosyakov marked this conversation as resolved.
Show resolved Hide resolved

}
58 changes: 56 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,15 @@ 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 NAVIGATOR_CONTEXT_MENU: MenuPath = ['navigator-context-menu'];

/**
Expand Down Expand Up @@ -95,6 +104,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 +355,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
25 changes: 18 additions & 7 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,27 @@ export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<
super(selectionService, handler);
}

protected getUri(): URI | undefined {
const uri = super.getUri();
if (this.workspaceService.isMultiRootWorkspaceEnabled) {
return uri;
}
// tslint:disable-next-line: no-any
public isEnabled(...args: any[]): boolean {
return super.isEnabled(...args) && !!this.workspaceService.tryGetRoots().length;
}

// tslint:disable-next-line: no-any
public isVisible(...args: any[]): boolean {
return super.isVisible(...args) && !!this.workspaceService.tryGetRoots().length;
}

// tslint:disable-next-line: no-any
protected getUri(...args: any[]): URI | undefined {
const uri = super.getUri(...args);
// If the URI is available, return it immediately.
if (uri) {
return uri;
}
const root = this.workspaceService.tryGetRoots()[0];
return root && new URI(root.uri);
// Return the first root if available.
if (!!this.workspaceService.tryGetRoots().length) {
return new URI(this.workspaceService.tryGetRoots()[0].uri);
}
}

}