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

Don't forward menu action args automatically #23340

Merged
merged 1 commit into from
Mar 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/base/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,6 @@ export class ActionRunner extends EventEmitter implements IActionRunner {
}

protected runAction(action: IAction, context?: any): TPromise<any> {
return TPromise.as(action.run(context));
return TPromise.as(context ? action.run(context) : action.run());
}
}
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/contextmenu/browser/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ContextMenuController implements IEditorContribution {
const result: IAction[] = [];

let contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._contextKeyService);
const groups = contextMenu.getActions(this._editor.getModel().uri);
const groups = contextMenu.getActions({ arg: this._editor.getModel().uri });
contextMenu.dispose();

for (let group of groups) {
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/actions/browser/menuItemActionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { localize } from 'vs/nls';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IMenu, MenuItemAction } from 'vs/platform/actions/common/actions';
import { IMenu, MenuItemAction, IMenuActionOptions } from 'vs/platform/actions/common/actions';
import { IMessageService } from 'vs/platform/message/common/message';
import Severity from 'vs/base/common/severity';
import { IAction } from 'vs/base/common/actions';
Expand All @@ -17,8 +17,8 @@ import { domEvent } from 'vs/base/browser/event';
import { Emitter } from 'vs/base/common/event';


export function fillInActions(menu: IMenu, context: any, target: IAction[] | { primary: IAction[]; secondary: IAction[]; }, isPrimaryGroup: (group: string) => boolean = group => group === 'navigation'): void {
const groups = menu.getActions(context);
export function fillInActions(menu: IMenu, options: IMenuActionOptions, target: IAction[] | { primary: IAction[]; secondary: IAction[]; }, isPrimaryGroup: (group: string) => boolean = group => group === 'navigation'): void {
const groups = menu.getActions(options);
if (groups.length === 0) {
return;
}
Expand Down
29 changes: 20 additions & 9 deletions src/vs/platform/actions/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ export class MenuId {
}
}

export interface IMenuActionOptions {
arg?: any;
shouldForwardArgs?: boolean;
}

export interface IMenu extends IDisposable {
onDidChange: Event<IMenu>;
getActions(arg?: any): [string, MenuItemAction[]][];
getActions(options?: IMenuActionOptions): [string, MenuItemAction[]][];
}

export const IMenuService = createDecorator<IMenuService>('menuService');
Expand Down Expand Up @@ -158,32 +163,38 @@ export class ExecuteCommandAction extends Action {

export class MenuItemAction extends ExecuteCommandAction {

private _arg: any;
private _options: IMenuActionOptions;

readonly item: ICommandAction;
readonly alt: MenuItemAction;

constructor(
item: ICommandAction,
alt: ICommandAction,
arg: any,
options: IMenuActionOptions,
@ICommandService commandService: ICommandService
) {
typeof item.title === 'string' ? super(item.id, item.title, commandService) : super(item.id, item.title.value, commandService);
this._cssClass = item.iconClass;
this._enabled = true;
this._arg = arg;
this._options = options;

this.item = item;
this.alt = alt ? new MenuItemAction(alt, undefined, arg, commandService) : undefined;
this.alt = alt ? new MenuItemAction(alt, undefined, this._options, commandService) : undefined;
}

run(...args: any[]): TPromise<any> {
if (this._arg) {
return super.run(this._arg, ...args);
} else {
return super.run(...args);
let runArgs = [];

if (this._options.arg) {
runArgs = [...runArgs, this._options.arg];
}

if (this._options.shouldForwardArgs) {
runArgs = [...runArgs, ...args];
}

return super.run(...runArgs);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/actions/common/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Event, { Emitter } from 'vs/base/common/event';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { MenuId, MenuRegistry, MenuItemAction, IMenu, IMenuItem } from 'vs/platform/actions/common/actions';
import { MenuId, MenuRegistry, MenuItemAction, IMenu, IMenuItem, IMenuActionOptions } from 'vs/platform/actions/common/actions';
import { ICommandService } from 'vs/platform/commands/common/commands';

type MenuItemGroup = [string, IMenuItem[]];
Expand Down Expand Up @@ -69,14 +69,14 @@ export class Menu implements IMenu {
return this._onDidChange.event;
}

getActions(arg?: any): [string, MenuItemAction[]][] {
getActions(options: IMenuActionOptions): [string, MenuItemAction[]][] {
const result: [string, MenuItemAction[]][] = [];
for (let group of this._menuGroups) {
const [id, items] = group;
const activeActions: MenuItemAction[] = [];
for (const item of items) {
if (this._contextKeyService.contextMatchesRules(item.when)) {
const action = new MenuItemAction(item.command, item.alt, arg, this._commandService);
const action = new MenuItemAction(item.command, item.alt, options, this._commandService);
action.order = item.order; //TODO@Ben order is menu item property, not an action property
activeActions.push(action);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/browser/parts/editor/titleControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export abstract class TitleControl extends Themable implements ITitleAreaControl
const titleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, scopedContextKeyService);
this.disposeOnEditorActions.push(titleBarMenu, titleBarMenu.onDidChange(_ => this.update()));

fillInActions(titleBarMenu, this.resourceContext.get(), { primary, secondary });
fillInActions(titleBarMenu, { arg: this.resourceContext.get() }, { primary, secondary });
}

return { primary, secondary };
Expand Down Expand Up @@ -475,7 +475,7 @@ export abstract class TitleControl extends Themable implements ITitleAreaControl
}

// Fill in contributed actions
fillInActions(this.contextMenu, this.resourceContext.get(), actions);
fillInActions(this.contextMenu, { arg: this.resourceContext.get() }, actions);

return actions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class BaseDebugController extends DefaultController {
this.contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => this.actionProvider.getSecondaryActions(tree, element).then(actions => {
fillInActions(this.contributedContextMenu, this.getContext(element), actions);
fillInActions(this.contributedContextMenu, { arg: this.getContext(element) }, actions);
return actions;
}),
onHide: (wasCancelled?: boolean) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export class FileController extends DefaultController {
getAnchor: () => anchor,
getActions: () => {
return this.state.actionProvider.getSecondaryActions(tree, stat).then(actions => {
fillInActions(this.contributedContextMenu, stat.resource, actions);
fillInActions(this.contributedContextMenu, { arg: stat.resource }, actions);
return actions;
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/parts/scm/electron-browser/scmMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class SCMMenus implements IDisposable {
const primary = [];
const secondary = [];
const result = { primary, secondary };
fillInActions(menu, resource.uri, result, g => g === 'inline');
fillInActions(menu, { arg: resource.uri, shouldForwardArgs: true }, result, g => g === 'inline');

menu.dispose();
contextKeyService.dispose();
Expand Down